blob: a2e9816486962e538482e0b180d92c79175942a8 [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};
Eric V. Smith235a6f02015-09-19 14:51:32 -0400288static PyTypeObject *FormattedValue_type;
289_Py_IDENTIFIER(conversion);
290_Py_IDENTIFIER(format_spec);
291static char *FormattedValue_fields[]={
292 "value",
293 "conversion",
294 "format_spec",
295};
296static PyTypeObject *JoinedStr_type;
297static char *JoinedStr_fields[]={
298 "values",
299};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000300static PyTypeObject *Bytes_type;
301static char *Bytes_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200302 "s",
Thomas Wouters00e41de2007-02-23 19:56:57 +0000303};
Benjamin Peterson442f2092012-12-06 17:41:04 -0500304static PyTypeObject *NameConstant_type;
305static char *NameConstant_fields[]={
Victor Stinneree4b59c2013-07-27 00:01:35 +0200306 "value",
Benjamin Peterson442f2092012-12-06 17:41:04 -0500307};
Georg Brandl52318d62006-09-06 07:06:08 +0000308static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000309static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200310_Py_IDENTIFIER(attr);
311_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000312static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200313 "value",
314 "attr",
315 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000316};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000317static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200318_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000319static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200320 "value",
321 "slice",
322 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000323};
Guido van Rossum0368b722007-05-11 16:50:42 +0000324static PyTypeObject *Starred_type;
325static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200326 "value",
327 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000328};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000329static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200330_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000331static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200332 "id",
333 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000335static PyTypeObject *List_type;
336static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200337 "elts",
338 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000339};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000340static PyTypeObject *Tuple_type;
341static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200342 "elts",
343 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000344};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000345static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
347*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
348static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static PyTypeObject *Load_type;
350static PyTypeObject *Store_type;
351static PyTypeObject *Del_type;
352static PyTypeObject *AugLoad_type;
353static PyTypeObject *AugStore_type;
354static PyTypeObject *Param_type;
355static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000356static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000357static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200358_Py_IDENTIFIER(lower);
359_Py_IDENTIFIER(upper);
360_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000361static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200362 "lower",
363 "upper",
364 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200367_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000368static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200369 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000370};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000371static PyTypeObject *Index_type;
372static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200373 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000375static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000376static PyObject *And_singleton, *Or_singleton;
377static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000378static PyTypeObject *And_type;
379static PyTypeObject *Or_type;
380static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000381static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400382*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
383*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
384*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000385static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000386static PyTypeObject *Add_type;
387static PyTypeObject *Sub_type;
388static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400389static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000390static PyTypeObject *Div_type;
391static PyTypeObject *Mod_type;
392static PyTypeObject *Pow_type;
393static PyTypeObject *LShift_type;
394static PyTypeObject *RShift_type;
395static PyTypeObject *BitOr_type;
396static PyTypeObject *BitXor_type;
397static PyTypeObject *BitAnd_type;
398static PyTypeObject *FloorDiv_type;
399static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000400static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
401*USub_singleton;
402static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000403static PyTypeObject *Invert_type;
404static PyTypeObject *Not_type;
405static PyTypeObject *UAdd_type;
406static PyTypeObject *USub_type;
407static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000408static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
409*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
410*NotIn_singleton;
411static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000412static PyTypeObject *Eq_type;
413static PyTypeObject *NotEq_type;
414static PyTypeObject *Lt_type;
415static PyTypeObject *LtE_type;
416static PyTypeObject *Gt_type;
417static PyTypeObject *GtE_type;
418static PyTypeObject *Is_type;
419static PyTypeObject *IsNot_type;
420static PyTypeObject *In_type;
421static PyTypeObject *NotIn_type;
422static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000423static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200424_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000425static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200426 "target",
427 "iter",
428 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000429};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000430static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000431static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200432 "lineno",
433 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000434};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000435static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000436static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200437_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000438static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200439 "type",
440 "name",
441 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000442};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000443static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000444static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200445_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200446_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200447_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700448_Py_IDENTIFIER(kwarg);
449_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000450static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200451 "args",
452 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200453 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200454 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200455 "kwarg",
456 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000457};
Neal Norwitzc1505362006-12-28 06:47:50 +0000458static PyTypeObject *arg_type;
459static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700460static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200461 "lineno",
462 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700463};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200464_Py_IDENTIFIER(arg);
465_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000466static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200467 "arg",
468 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000469};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000470static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000471static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000472static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200473 "arg",
474 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000475};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000476static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000477static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200478_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000479static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200480 "name",
481 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000482};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500483static PyTypeObject *withitem_type;
484static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200485_Py_IDENTIFIER(context_expr);
486_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500487static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200488 "context_expr",
489 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500490};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000491
492
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700493typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100494 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700495 PyObject *dict;
496} AST_object;
497
Benjamin Peterson1767e022012-03-14 21:50:29 -0500498static void
499ast_dealloc(AST_object *self)
500{
501 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200502 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500503}
504
Neal Norwitz207c9f32008-03-31 04:42:11 +0000505static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700506ast_traverse(AST_object *self, visitproc visit, void *arg)
507{
508 Py_VISIT(self->dict);
509 return 0;
510}
511
512static void
513ast_clear(AST_object *self)
514{
515 Py_CLEAR(self->dict);
516}
517
518static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000519ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
520{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200521 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000522 Py_ssize_t i, numfields = 0;
523 int res = -1;
524 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200525 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000526 if (!fields)
527 PyErr_Clear();
528 if (fields) {
529 numfields = PySequence_Size(fields);
530 if (numfields == -1)
531 goto cleanup;
532 }
533 res = 0; /* if no error occurs, this stays 0 to the end */
534 if (PyTuple_GET_SIZE(args) > 0) {
535 if (numfields != PyTuple_GET_SIZE(args)) {
536 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000537 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000538 Py_TYPE(self)->tp_name,
539 numfields == 0 ? "" : "either 0 or ",
540 numfields, numfields == 1 ? "" : "s");
541 res = -1;
542 goto cleanup;
543 }
544 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
545 /* cannot be reached when fields is NULL */
546 PyObject *name = PySequence_GetItem(fields, i);
547 if (!name) {
548 res = -1;
549 goto cleanup;
550 }
551 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
552 Py_DECREF(name);
553 if (res < 0)
554 goto cleanup;
555 }
556 }
557 if (kw) {
558 i = 0; /* needed by PyDict_Next */
559 while (PyDict_Next(kw, &i, &key, &value)) {
560 res = PyObject_SetAttr(self, key, value);
561 if (res < 0)
562 goto cleanup;
563 }
564 }
565 cleanup:
566 Py_XDECREF(fields);
567 return res;
568}
569
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000570/* Pickling support */
571static PyObject *
572ast_type_reduce(PyObject *self, PyObject *unused)
573{
574 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200575 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200576 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000577 if (dict == NULL) {
578 if (PyErr_ExceptionMatches(PyExc_AttributeError))
579 PyErr_Clear();
580 else
581 return NULL;
582 }
583 if (dict) {
584 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
585 Py_DECREF(dict);
586 return res;
587 }
588 return Py_BuildValue("O()", Py_TYPE(self));
589}
590
591static PyMethodDef ast_type_methods[] = {
592 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
593 {NULL}
594};
595
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700596static PyGetSetDef ast_type_getsets[] = {
597 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
598 {NULL}
599};
600
Neal Norwitz207c9f32008-03-31 04:42:11 +0000601static PyTypeObject AST_type = {
602 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000603 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700604 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000605 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500606 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000607 0, /* tp_print */
608 0, /* tp_getattr */
609 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000610 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000611 0, /* tp_repr */
612 0, /* tp_as_number */
613 0, /* tp_as_sequence */
614 0, /* tp_as_mapping */
615 0, /* tp_hash */
616 0, /* tp_call */
617 0, /* tp_str */
618 PyObject_GenericGetAttr, /* tp_getattro */
619 PyObject_GenericSetAttr, /* tp_setattro */
620 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000622 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700623 (traverseproc)ast_traverse, /* tp_traverse */
624 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000625 0, /* tp_richcompare */
626 0, /* tp_weaklistoffset */
627 0, /* tp_iter */
628 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000629 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000630 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700631 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000632 0, /* tp_base */
633 0, /* tp_dict */
634 0, /* tp_descr_get */
635 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700636 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000637 (initproc)ast_type_init, /* tp_init */
638 PyType_GenericAlloc, /* tp_alloc */
639 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700640 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641};
642
643
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
645{
646 PyObject *fnames, *result;
647 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000648 fnames = PyTuple_New(num_fields);
649 if (!fnames) return NULL;
650 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000651 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 if (!field) {
653 Py_DECREF(fnames);
654 return NULL;
655 }
656 PyTuple_SET_ITEM(fnames, i, field);
657 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000658 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660 Py_DECREF(fnames);
661 return (PyTypeObject*)result;
662}
663
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
665{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000666 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200667 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000668 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000669 if (!l)
670 return 0;
671 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000672 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!s) {
674 Py_DECREF(l);
675 return 0;
676 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000677 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200679 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 Py_DECREF(l);
681 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682}
683
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000684/* Conversion AST -> Python */
685
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
687{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700688 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000689 PyObject *result = PyList_New(n);
690 PyObject *value;
691 if (!result)
692 return NULL;
693 for (i = 0; i < n; i++) {
694 value = func(asdl_seq_GET(seq, i));
695 if (!value) {
696 Py_DECREF(result);
697 return NULL;
698 }
699 PyList_SET_ITEM(result, i, value);
700 }
701 return result;
702}
703
704static PyObject* ast2obj_object(void *o)
705{
706 if (!o)
707 o = Py_None;
708 Py_INCREF((PyObject*)o);
709 return (PyObject*)o;
710}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500711#define ast2obj_singleton ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712#define ast2obj_identifier ast2obj_object
713#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500714#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000716static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717{
Christian Heimes217cfd12007-12-02 14:31:20 +0000718 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719}
720
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000721/* Conversion Python -> AST */
722
Benjamin Peterson442f2092012-12-06 17:41:04 -0500723static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
724{
725 if (obj != Py_None && obj != Py_True && obj != Py_False) {
726 PyErr_SetString(PyExc_ValueError,
727 "AST singleton must be True, False, or None");
728 return 1;
729 }
730 *out = obj;
731 return 0;
732}
733
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000734static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
735{
736 if (obj == Py_None)
737 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200738 if (obj) {
739 if (PyArena_AddPyObject(arena, obj) < 0) {
740 *out = NULL;
741 return -1;
742 }
743 Py_INCREF(obj);
744 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000745 *out = obj;
746 return 0;
747}
748
Benjamin Peterson180e6352011-07-22 11:09:07 -0500749static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500750{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500751 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
752 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500753 return 1;
754 }
755 return obj2ast_object(obj, out, arena);
756}
757
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500758static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
759{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400760 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500761 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
762 return 1;
763 }
764 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500765}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000766
Benjamin Petersone2498412011-08-09 16:08:39 -0500767static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
768{
769 if (!PyBytes_CheckExact(obj)) {
770 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
771 return 1;
772 }
773 return obj2ast_object(obj, out, arena);
774}
775
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000776static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
777{
778 int i;
779 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100780 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000781 return 1;
782 }
783
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300784 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000785 if (i == -1 && PyErr_Occurred())
786 return 1;
787 *out = i;
788 return 0;
789}
790
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000791static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000792{
793 PyObject *empty_tuple, *d;
794 if (PyType_Ready(&AST_type) < 0)
795 return -1;
796 d = AST_type.tp_dict;
797 empty_tuple = PyTuple_New(0);
798 if (!empty_tuple ||
799 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
800 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
801 Py_XDECREF(empty_tuple);
802 return -1;
803 }
804 Py_DECREF(empty_tuple);
805 return 0;
806}
807
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700808static int exists_not_none(PyObject *obj, _Py_Identifier *id)
809{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700810 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700811 PyObject *attr = _PyObject_GetAttrId(obj, id);
812 if (!attr) {
813 PyErr_Clear();
814 return 0;
815 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700816 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700817 Py_DECREF(attr);
818 return !isnone;
819}
820
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000821
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000822static int init_types(void)
823{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200824 static int initialized;
825 if (initialized) return 1;
826 if (add_ast_fields() < 0) return 0;
827 mod_type = make_type("mod", &AST_type, NULL, 0);
828 if (!mod_type) return 0;
829 if (!add_attributes(mod_type, NULL, 0)) return 0;
830 Module_type = make_type("Module", mod_type, Module_fields, 1);
831 if (!Module_type) return 0;
832 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
833 1);
834 if (!Interactive_type) return 0;
835 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
836 if (!Expression_type) return 0;
837 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
838 if (!Suite_type) return 0;
839 stmt_type = make_type("stmt", &AST_type, NULL, 0);
840 if (!stmt_type) return 0;
841 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
842 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
843 5);
844 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400845 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
846 AsyncFunctionDef_fields, 5);
847 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400848 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200849 if (!ClassDef_type) return 0;
850 Return_type = make_type("Return", stmt_type, Return_fields, 1);
851 if (!Return_type) return 0;
852 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
853 if (!Delete_type) return 0;
854 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
855 if (!Assign_type) return 0;
856 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
857 if (!AugAssign_type) return 0;
858 For_type = make_type("For", stmt_type, For_fields, 4);
859 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400860 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
861 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200862 While_type = make_type("While", stmt_type, While_fields, 3);
863 if (!While_type) return 0;
864 If_type = make_type("If", stmt_type, If_fields, 3);
865 if (!If_type) return 0;
866 With_type = make_type("With", stmt_type, With_fields, 2);
867 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400868 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
869 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200870 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
871 if (!Raise_type) return 0;
872 Try_type = make_type("Try", stmt_type, Try_fields, 4);
873 if (!Try_type) return 0;
874 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
875 if (!Assert_type) return 0;
876 Import_type = make_type("Import", stmt_type, Import_fields, 1);
877 if (!Import_type) return 0;
878 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
879 if (!ImportFrom_type) return 0;
880 Global_type = make_type("Global", stmt_type, Global_fields, 1);
881 if (!Global_type) return 0;
882 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
883 if (!Nonlocal_type) return 0;
884 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
885 if (!Expr_type) return 0;
886 Pass_type = make_type("Pass", stmt_type, NULL, 0);
887 if (!Pass_type) return 0;
888 Break_type = make_type("Break", stmt_type, NULL, 0);
889 if (!Break_type) return 0;
890 Continue_type = make_type("Continue", stmt_type, NULL, 0);
891 if (!Continue_type) return 0;
892 expr_type = make_type("expr", &AST_type, NULL, 0);
893 if (!expr_type) return 0;
894 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
895 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
896 if (!BoolOp_type) return 0;
897 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
898 if (!BinOp_type) return 0;
899 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
900 if (!UnaryOp_type) return 0;
901 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
902 if (!Lambda_type) return 0;
903 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
904 if (!IfExp_type) return 0;
905 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
906 if (!Dict_type) return 0;
907 Set_type = make_type("Set", expr_type, Set_fields, 1);
908 if (!Set_type) return 0;
909 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
910 if (!ListComp_type) return 0;
911 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
912 if (!SetComp_type) return 0;
913 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
914 if (!DictComp_type) return 0;
915 GeneratorExp_type = make_type("GeneratorExp", expr_type,
916 GeneratorExp_fields, 2);
917 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400918 Await_type = make_type("Await", expr_type, Await_fields, 1);
919 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200920 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
921 if (!Yield_type) return 0;
922 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
923 if (!YieldFrom_type) return 0;
924 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
925 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400926 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200927 if (!Call_type) return 0;
928 Num_type = make_type("Num", expr_type, Num_fields, 1);
929 if (!Num_type) return 0;
930 Str_type = make_type("Str", expr_type, Str_fields, 1);
931 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400932 FormattedValue_type = make_type("FormattedValue", expr_type,
933 FormattedValue_fields, 3);
934 if (!FormattedValue_type) return 0;
935 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
936 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200937 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
938 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200939 NameConstant_type = make_type("NameConstant", expr_type,
940 NameConstant_fields, 1);
941 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200942 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
943 if (!Ellipsis_type) return 0;
944 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
945 if (!Attribute_type) return 0;
946 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
947 if (!Subscript_type) return 0;
948 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
949 if (!Starred_type) return 0;
950 Name_type = make_type("Name", expr_type, Name_fields, 2);
951 if (!Name_type) return 0;
952 List_type = make_type("List", expr_type, List_fields, 2);
953 if (!List_type) return 0;
954 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
955 if (!Tuple_type) return 0;
956 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
957 if (!expr_context_type) return 0;
958 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
959 Load_type = make_type("Load", expr_context_type, NULL, 0);
960 if (!Load_type) return 0;
961 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
962 if (!Load_singleton) return 0;
963 Store_type = make_type("Store", expr_context_type, NULL, 0);
964 if (!Store_type) return 0;
965 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
966 if (!Store_singleton) return 0;
967 Del_type = make_type("Del", expr_context_type, NULL, 0);
968 if (!Del_type) return 0;
969 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
970 if (!Del_singleton) return 0;
971 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
972 if (!AugLoad_type) return 0;
973 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
974 if (!AugLoad_singleton) return 0;
975 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
976 if (!AugStore_type) return 0;
977 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
978 if (!AugStore_singleton) return 0;
979 Param_type = make_type("Param", expr_context_type, NULL, 0);
980 if (!Param_type) return 0;
981 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
982 if (!Param_singleton) return 0;
983 slice_type = make_type("slice", &AST_type, NULL, 0);
984 if (!slice_type) return 0;
985 if (!add_attributes(slice_type, NULL, 0)) return 0;
986 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
987 if (!Slice_type) return 0;
988 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
989 if (!ExtSlice_type) return 0;
990 Index_type = make_type("Index", slice_type, Index_fields, 1);
991 if (!Index_type) return 0;
992 boolop_type = make_type("boolop", &AST_type, NULL, 0);
993 if (!boolop_type) return 0;
994 if (!add_attributes(boolop_type, NULL, 0)) return 0;
995 And_type = make_type("And", boolop_type, NULL, 0);
996 if (!And_type) return 0;
997 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
998 if (!And_singleton) return 0;
999 Or_type = make_type("Or", boolop_type, NULL, 0);
1000 if (!Or_type) return 0;
1001 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1002 if (!Or_singleton) return 0;
1003 operator_type = make_type("operator", &AST_type, NULL, 0);
1004 if (!operator_type) return 0;
1005 if (!add_attributes(operator_type, NULL, 0)) return 0;
1006 Add_type = make_type("Add", operator_type, NULL, 0);
1007 if (!Add_type) return 0;
1008 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1009 if (!Add_singleton) return 0;
1010 Sub_type = make_type("Sub", operator_type, NULL, 0);
1011 if (!Sub_type) return 0;
1012 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1013 if (!Sub_singleton) return 0;
1014 Mult_type = make_type("Mult", operator_type, NULL, 0);
1015 if (!Mult_type) return 0;
1016 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1017 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001018 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1019 if (!MatMult_type) return 0;
1020 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1021 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001022 Div_type = make_type("Div", operator_type, NULL, 0);
1023 if (!Div_type) return 0;
1024 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1025 if (!Div_singleton) return 0;
1026 Mod_type = make_type("Mod", operator_type, NULL, 0);
1027 if (!Mod_type) return 0;
1028 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1029 if (!Mod_singleton) return 0;
1030 Pow_type = make_type("Pow", operator_type, NULL, 0);
1031 if (!Pow_type) return 0;
1032 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1033 if (!Pow_singleton) return 0;
1034 LShift_type = make_type("LShift", operator_type, NULL, 0);
1035 if (!LShift_type) return 0;
1036 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1037 if (!LShift_singleton) return 0;
1038 RShift_type = make_type("RShift", operator_type, NULL, 0);
1039 if (!RShift_type) return 0;
1040 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1041 if (!RShift_singleton) return 0;
1042 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1043 if (!BitOr_type) return 0;
1044 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1045 if (!BitOr_singleton) return 0;
1046 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1047 if (!BitXor_type) return 0;
1048 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1049 if (!BitXor_singleton) return 0;
1050 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1051 if (!BitAnd_type) return 0;
1052 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1053 if (!BitAnd_singleton) return 0;
1054 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1055 if (!FloorDiv_type) return 0;
1056 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1057 if (!FloorDiv_singleton) return 0;
1058 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1059 if (!unaryop_type) return 0;
1060 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1061 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1062 if (!Invert_type) return 0;
1063 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1064 if (!Invert_singleton) return 0;
1065 Not_type = make_type("Not", unaryop_type, NULL, 0);
1066 if (!Not_type) return 0;
1067 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1068 if (!Not_singleton) return 0;
1069 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1070 if (!UAdd_type) return 0;
1071 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1072 if (!UAdd_singleton) return 0;
1073 USub_type = make_type("USub", unaryop_type, NULL, 0);
1074 if (!USub_type) return 0;
1075 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1076 if (!USub_singleton) return 0;
1077 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1078 if (!cmpop_type) return 0;
1079 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1080 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1081 if (!Eq_type) return 0;
1082 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1083 if (!Eq_singleton) return 0;
1084 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1085 if (!NotEq_type) return 0;
1086 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1087 if (!NotEq_singleton) return 0;
1088 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1089 if (!Lt_type) return 0;
1090 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1091 if (!Lt_singleton) return 0;
1092 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1093 if (!LtE_type) return 0;
1094 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1095 if (!LtE_singleton) return 0;
1096 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1097 if (!Gt_type) return 0;
1098 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1099 if (!Gt_singleton) return 0;
1100 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1101 if (!GtE_type) return 0;
1102 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1103 if (!GtE_singleton) return 0;
1104 Is_type = make_type("Is", cmpop_type, NULL, 0);
1105 if (!Is_type) return 0;
1106 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1107 if (!Is_singleton) return 0;
1108 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1109 if (!IsNot_type) return 0;
1110 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1111 if (!IsNot_singleton) return 0;
1112 In_type = make_type("In", cmpop_type, NULL, 0);
1113 if (!In_type) return 0;
1114 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1115 if (!In_singleton) return 0;
1116 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1117 if (!NotIn_type) return 0;
1118 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1119 if (!NotIn_singleton) return 0;
1120 comprehension_type = make_type("comprehension", &AST_type,
1121 comprehension_fields, 3);
1122 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001123 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001124 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1125 if (!excepthandler_type) return 0;
1126 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1127 return 0;
1128 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1129 ExceptHandler_fields, 3);
1130 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001131 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001132 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001133 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001134 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1135 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001136 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001137 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1138 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001139 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001140 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1141 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001142 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001143 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1144 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001145 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001146 initialized = 1;
1147 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001148}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001149
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001150static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1151static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1152static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1153static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1154 arena);
1155static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1156static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1157static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1158static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1159static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1160static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1161 arena);
1162static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1163 arena);
1164static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1165static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1166static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1167static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001168static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001169
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001173 mod_ty p;
1174 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1175 if (!p)
1176 return NULL;
1177 p->kind = Module_kind;
1178 p->v.Module.body = body;
1179 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180}
1181
1182mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001183Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001185 mod_ty p;
1186 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1187 if (!p)
1188 return NULL;
1189 p->kind = Interactive_kind;
1190 p->v.Interactive.body = body;
1191 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192}
1193
1194mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001195Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001197 mod_ty p;
1198 if (!body) {
1199 PyErr_SetString(PyExc_ValueError,
1200 "field body is required for Expression");
1201 return NULL;
1202 }
1203 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1204 if (!p)
1205 return NULL;
1206 p->kind = Expression_kind;
1207 p->v.Expression.body = body;
1208 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209}
1210
1211mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001212Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001214 mod_ty p;
1215 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1216 if (!p)
1217 return NULL;
1218 p->kind = Suite_kind;
1219 p->v.Suite.body = body;
1220 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221}
1222
1223stmt_ty
1224FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001225 decorator_list, expr_ty returns, int lineno, int col_offset,
1226 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001228 stmt_ty p;
1229 if (!name) {
1230 PyErr_SetString(PyExc_ValueError,
1231 "field name is required for FunctionDef");
1232 return NULL;
1233 }
1234 if (!args) {
1235 PyErr_SetString(PyExc_ValueError,
1236 "field args is required for FunctionDef");
1237 return NULL;
1238 }
1239 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1240 if (!p)
1241 return NULL;
1242 p->kind = FunctionDef_kind;
1243 p->v.FunctionDef.name = name;
1244 p->v.FunctionDef.args = args;
1245 p->v.FunctionDef.body = body;
1246 p->v.FunctionDef.decorator_list = decorator_list;
1247 p->v.FunctionDef.returns = returns;
1248 p->lineno = lineno;
1249 p->col_offset = col_offset;
1250 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001254AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1255 * decorator_list, expr_ty returns, int lineno, int col_offset,
1256 PyArena *arena)
1257{
1258 stmt_ty p;
1259 if (!name) {
1260 PyErr_SetString(PyExc_ValueError,
1261 "field name is required for AsyncFunctionDef");
1262 return NULL;
1263 }
1264 if (!args) {
1265 PyErr_SetString(PyExc_ValueError,
1266 "field args is required for AsyncFunctionDef");
1267 return NULL;
1268 }
1269 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1270 if (!p)
1271 return NULL;
1272 p->kind = AsyncFunctionDef_kind;
1273 p->v.AsyncFunctionDef.name = name;
1274 p->v.AsyncFunctionDef.args = args;
1275 p->v.AsyncFunctionDef.body = body;
1276 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1277 p->v.AsyncFunctionDef.returns = returns;
1278 p->lineno = lineno;
1279 p->col_offset = col_offset;
1280 return p;
1281}
1282
1283stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001284ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1285 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1286 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001288 stmt_ty p;
1289 if (!name) {
1290 PyErr_SetString(PyExc_ValueError,
1291 "field name is required for ClassDef");
1292 return NULL;
1293 }
1294 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1295 if (!p)
1296 return NULL;
1297 p->kind = ClassDef_kind;
1298 p->v.ClassDef.name = name;
1299 p->v.ClassDef.bases = bases;
1300 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001301 p->v.ClassDef.body = body;
1302 p->v.ClassDef.decorator_list = decorator_list;
1303 p->lineno = lineno;
1304 p->col_offset = col_offset;
1305 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306}
1307
1308stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001309Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001311 stmt_ty p;
1312 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1313 if (!p)
1314 return NULL;
1315 p->kind = Return_kind;
1316 p->v.Return.value = value;
1317 p->lineno = lineno;
1318 p->col_offset = col_offset;
1319 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001323Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001325 stmt_ty p;
1326 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1327 if (!p)
1328 return NULL;
1329 p->kind = Delete_kind;
1330 p->v.Delete.targets = targets;
1331 p->lineno = lineno;
1332 p->col_offset = col_offset;
1333 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334}
1335
1336stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001337Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1338 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001340 stmt_ty p;
1341 if (!value) {
1342 PyErr_SetString(PyExc_ValueError,
1343 "field value is required for Assign");
1344 return NULL;
1345 }
1346 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1347 if (!p)
1348 return NULL;
1349 p->kind = Assign_kind;
1350 p->v.Assign.targets = targets;
1351 p->v.Assign.value = value;
1352 p->lineno = lineno;
1353 p->col_offset = col_offset;
1354 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355}
1356
1357stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001358AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1359 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001361 stmt_ty p;
1362 if (!target) {
1363 PyErr_SetString(PyExc_ValueError,
1364 "field target is required for AugAssign");
1365 return NULL;
1366 }
1367 if (!op) {
1368 PyErr_SetString(PyExc_ValueError,
1369 "field op is required for AugAssign");
1370 return NULL;
1371 }
1372 if (!value) {
1373 PyErr_SetString(PyExc_ValueError,
1374 "field value is required for AugAssign");
1375 return NULL;
1376 }
1377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1378 if (!p)
1379 return NULL;
1380 p->kind = AugAssign_kind;
1381 p->v.AugAssign.target = target;
1382 p->v.AugAssign.op = op;
1383 p->v.AugAssign.value = value;
1384 p->lineno = lineno;
1385 p->col_offset = col_offset;
1386 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387}
1388
1389stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001391 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001393 stmt_ty p;
1394 if (!target) {
1395 PyErr_SetString(PyExc_ValueError,
1396 "field target is required for For");
1397 return NULL;
1398 }
1399 if (!iter) {
1400 PyErr_SetString(PyExc_ValueError,
1401 "field iter is required for For");
1402 return NULL;
1403 }
1404 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1405 if (!p)
1406 return NULL;
1407 p->kind = For_kind;
1408 p->v.For.target = target;
1409 p->v.For.iter = iter;
1410 p->v.For.body = body;
1411 p->v.For.orelse = orelse;
1412 p->lineno = lineno;
1413 p->col_offset = col_offset;
1414 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415}
1416
1417stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001418AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1419 lineno, int col_offset, PyArena *arena)
1420{
1421 stmt_ty p;
1422 if (!target) {
1423 PyErr_SetString(PyExc_ValueError,
1424 "field target is required for AsyncFor");
1425 return NULL;
1426 }
1427 if (!iter) {
1428 PyErr_SetString(PyExc_ValueError,
1429 "field iter is required for AsyncFor");
1430 return NULL;
1431 }
1432 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1433 if (!p)
1434 return NULL;
1435 p->kind = AsyncFor_kind;
1436 p->v.AsyncFor.target = target;
1437 p->v.AsyncFor.iter = iter;
1438 p->v.AsyncFor.body = body;
1439 p->v.AsyncFor.orelse = orelse;
1440 p->lineno = lineno;
1441 p->col_offset = col_offset;
1442 return p;
1443}
1444
1445stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001446While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1447 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001449 stmt_ty p;
1450 if (!test) {
1451 PyErr_SetString(PyExc_ValueError,
1452 "field test is required for While");
1453 return NULL;
1454 }
1455 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1456 if (!p)
1457 return NULL;
1458 p->kind = While_kind;
1459 p->v.While.test = test;
1460 p->v.While.body = body;
1461 p->v.While.orelse = orelse;
1462 p->lineno = lineno;
1463 p->col_offset = col_offset;
1464 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465}
1466
1467stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1469 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001471 stmt_ty p;
1472 if (!test) {
1473 PyErr_SetString(PyExc_ValueError,
1474 "field test is required for If");
1475 return NULL;
1476 }
1477 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1478 if (!p)
1479 return NULL;
1480 p->kind = If_kind;
1481 p->v.If.test = test;
1482 p->v.If.body = body;
1483 p->v.If.orelse = orelse;
1484 p->lineno = lineno;
1485 p->col_offset = col_offset;
1486 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487}
1488
1489stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001490With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1491 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001492{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001493 stmt_ty p;
1494 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1495 if (!p)
1496 return NULL;
1497 p->kind = With_kind;
1498 p->v.With.items = items;
1499 p->v.With.body = body;
1500 p->lineno = lineno;
1501 p->col_offset = col_offset;
1502 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001503}
1504
1505stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001506AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1507 PyArena *arena)
1508{
1509 stmt_ty p;
1510 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1511 if (!p)
1512 return NULL;
1513 p->kind = AsyncWith_kind;
1514 p->v.AsyncWith.items = items;
1515 p->v.AsyncWith.body = body;
1516 p->lineno = lineno;
1517 p->col_offset = col_offset;
1518 return p;
1519}
1520
1521stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001522Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001524 stmt_ty p;
1525 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1526 if (!p)
1527 return NULL;
1528 p->kind = Raise_kind;
1529 p->v.Raise.exc = exc;
1530 p->v.Raise.cause = cause;
1531 p->lineno = lineno;
1532 p->col_offset = col_offset;
1533 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
1536stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001537Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1538 finalbody, 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 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1542 if (!p)
1543 return NULL;
1544 p->kind = Try_kind;
1545 p->v.Try.body = body;
1546 p->v.Try.handlers = handlers;
1547 p->v.Try.orelse = orelse;
1548 p->v.Try.finalbody = finalbody;
1549 p->lineno = lineno;
1550 p->col_offset = col_offset;
1551 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001552}
1553
1554stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001555Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001557 stmt_ty p;
1558 if (!test) {
1559 PyErr_SetString(PyExc_ValueError,
1560 "field test is required for Assert");
1561 return NULL;
1562 }
1563 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1564 if (!p)
1565 return NULL;
1566 p->kind = Assert_kind;
1567 p->v.Assert.test = test;
1568 p->v.Assert.msg = msg;
1569 p->lineno = lineno;
1570 p->col_offset = col_offset;
1571 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572}
1573
1574stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001575Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001577 stmt_ty p;
1578 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1579 if (!p)
1580 return NULL;
1581 p->kind = Import_kind;
1582 p->v.Import.names = names;
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 +00001589ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1590 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001592 stmt_ty p;
1593 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1594 if (!p)
1595 return NULL;
1596 p->kind = ImportFrom_kind;
1597 p->v.ImportFrom.module = module;
1598 p->v.ImportFrom.names = names;
1599 p->v.ImportFrom.level = level;
1600 p->lineno = lineno;
1601 p->col_offset = col_offset;
1602 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603}
1604
1605stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001606Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001608 stmt_ty p;
1609 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1610 if (!p)
1611 return NULL;
1612 p->kind = Global_kind;
1613 p->v.Global.names = names;
1614 p->lineno = lineno;
1615 p->col_offset = col_offset;
1616 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617}
1618
1619stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001620Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1621{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001622 stmt_ty p;
1623 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1624 if (!p)
1625 return NULL;
1626 p->kind = Nonlocal_kind;
1627 p->v.Nonlocal.names = names;
1628 p->lineno = lineno;
1629 p->col_offset = col_offset;
1630 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001631}
1632
1633stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001634Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001636 stmt_ty p;
1637 if (!value) {
1638 PyErr_SetString(PyExc_ValueError,
1639 "field value is required for Expr");
1640 return NULL;
1641 }
1642 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1643 if (!p)
1644 return NULL;
1645 p->kind = Expr_kind;
1646 p->v.Expr.value = value;
1647 p->lineno = lineno;
1648 p->col_offset = col_offset;
1649 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001653Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001655 stmt_ty p;
1656 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1657 if (!p)
1658 return NULL;
1659 p->kind = Pass_kind;
1660 p->lineno = lineno;
1661 p->col_offset = col_offset;
1662 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663}
1664
1665stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001668 stmt_ty p;
1669 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1670 if (!p)
1671 return NULL;
1672 p->kind = Break_kind;
1673 p->lineno = lineno;
1674 p->col_offset = col_offset;
1675 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
1677
1678stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001679Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001681 stmt_ty p;
1682 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1683 if (!p)
1684 return NULL;
1685 p->kind = Continue_kind;
1686 p->lineno = lineno;
1687 p->col_offset = col_offset;
1688 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689}
1690
1691expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001692BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1693 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001695 expr_ty p;
1696 if (!op) {
1697 PyErr_SetString(PyExc_ValueError,
1698 "field op is required for BoolOp");
1699 return NULL;
1700 }
1701 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1702 if (!p)
1703 return NULL;
1704 p->kind = BoolOp_kind;
1705 p->v.BoolOp.op = op;
1706 p->v.BoolOp.values = values;
1707 p->lineno = lineno;
1708 p->col_offset = col_offset;
1709 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710}
1711
1712expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1714 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001716 expr_ty p;
1717 if (!left) {
1718 PyErr_SetString(PyExc_ValueError,
1719 "field left is required for BinOp");
1720 return NULL;
1721 }
1722 if (!op) {
1723 PyErr_SetString(PyExc_ValueError,
1724 "field op is required for BinOp");
1725 return NULL;
1726 }
1727 if (!right) {
1728 PyErr_SetString(PyExc_ValueError,
1729 "field right is required for BinOp");
1730 return NULL;
1731 }
1732 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1733 if (!p)
1734 return NULL;
1735 p->kind = BinOp_kind;
1736 p->v.BinOp.left = left;
1737 p->v.BinOp.op = op;
1738 p->v.BinOp.right = right;
1739 p->lineno = lineno;
1740 p->col_offset = col_offset;
1741 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742}
1743
1744expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001745UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1746 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001748 expr_ty p;
1749 if (!op) {
1750 PyErr_SetString(PyExc_ValueError,
1751 "field op is required for UnaryOp");
1752 return NULL;
1753 }
1754 if (!operand) {
1755 PyErr_SetString(PyExc_ValueError,
1756 "field operand is required for UnaryOp");
1757 return NULL;
1758 }
1759 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1760 if (!p)
1761 return NULL;
1762 p->kind = UnaryOp_kind;
1763 p->v.UnaryOp.op = op;
1764 p->v.UnaryOp.operand = operand;
1765 p->lineno = lineno;
1766 p->col_offset = col_offset;
1767 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768}
1769
1770expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001771Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1772 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001774 expr_ty p;
1775 if (!args) {
1776 PyErr_SetString(PyExc_ValueError,
1777 "field args is required for Lambda");
1778 return NULL;
1779 }
1780 if (!body) {
1781 PyErr_SetString(PyExc_ValueError,
1782 "field body is required for Lambda");
1783 return NULL;
1784 }
1785 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1786 if (!p)
1787 return NULL;
1788 p->kind = Lambda_kind;
1789 p->v.Lambda.args = args;
1790 p->v.Lambda.body = body;
1791 p->lineno = lineno;
1792 p->col_offset = col_offset;
1793 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794}
1795
1796expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001797IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1798 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001799{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001800 expr_ty p;
1801 if (!test) {
1802 PyErr_SetString(PyExc_ValueError,
1803 "field test is required for IfExp");
1804 return NULL;
1805 }
1806 if (!body) {
1807 PyErr_SetString(PyExc_ValueError,
1808 "field body is required for IfExp");
1809 return NULL;
1810 }
1811 if (!orelse) {
1812 PyErr_SetString(PyExc_ValueError,
1813 "field orelse is required for IfExp");
1814 return NULL;
1815 }
1816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1817 if (!p)
1818 return NULL;
1819 p->kind = IfExp_kind;
1820 p->v.IfExp.test = test;
1821 p->v.IfExp.body = body;
1822 p->v.IfExp.orelse = orelse;
1823 p->lineno = lineno;
1824 p->col_offset = col_offset;
1825 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001826}
1827
1828expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001829Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1830 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001832 expr_ty p;
1833 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1834 if (!p)
1835 return NULL;
1836 p->kind = Dict_kind;
1837 p->v.Dict.keys = keys;
1838 p->v.Dict.values = values;
1839 p->lineno = lineno;
1840 p->col_offset = col_offset;
1841 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842}
1843
1844expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001845Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1846{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001847 expr_ty p;
1848 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1849 if (!p)
1850 return NULL;
1851 p->kind = Set_kind;
1852 p->v.Set.elts = elts;
1853 p->lineno = lineno;
1854 p->col_offset = col_offset;
1855 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001856}
1857
1858expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001859ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1860 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001862 expr_ty p;
1863 if (!elt) {
1864 PyErr_SetString(PyExc_ValueError,
1865 "field elt is required for ListComp");
1866 return NULL;
1867 }
1868 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1869 if (!p)
1870 return NULL;
1871 p->kind = ListComp_kind;
1872 p->v.ListComp.elt = elt;
1873 p->v.ListComp.generators = generators;
1874 p->lineno = lineno;
1875 p->col_offset = col_offset;
1876 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877}
1878
1879expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001880SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1881 *arena)
1882{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001883 expr_ty p;
1884 if (!elt) {
1885 PyErr_SetString(PyExc_ValueError,
1886 "field elt is required for SetComp");
1887 return NULL;
1888 }
1889 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1890 if (!p)
1891 return NULL;
1892 p->kind = SetComp_kind;
1893 p->v.SetComp.elt = elt;
1894 p->v.SetComp.generators = generators;
1895 p->lineno = lineno;
1896 p->col_offset = col_offset;
1897 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001898}
1899
1900expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001901DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1902 col_offset, PyArena *arena)
1903{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001904 expr_ty p;
1905 if (!key) {
1906 PyErr_SetString(PyExc_ValueError,
1907 "field key is required for DictComp");
1908 return NULL;
1909 }
1910 if (!value) {
1911 PyErr_SetString(PyExc_ValueError,
1912 "field value is required for DictComp");
1913 return NULL;
1914 }
1915 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1916 if (!p)
1917 return NULL;
1918 p->kind = DictComp_kind;
1919 p->v.DictComp.key = key;
1920 p->v.DictComp.value = value;
1921 p->v.DictComp.generators = generators;
1922 p->lineno = lineno;
1923 p->col_offset = col_offset;
1924 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001925}
1926
1927expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001928GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1929 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001931 expr_ty p;
1932 if (!elt) {
1933 PyErr_SetString(PyExc_ValueError,
1934 "field elt is required for GeneratorExp");
1935 return NULL;
1936 }
1937 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1938 if (!p)
1939 return NULL;
1940 p->kind = GeneratorExp_kind;
1941 p->v.GeneratorExp.elt = elt;
1942 p->v.GeneratorExp.generators = generators;
1943 p->lineno = lineno;
1944 p->col_offset = col_offset;
1945 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946}
1947
1948expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001949Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
1950{
1951 expr_ty p;
1952 if (!value) {
1953 PyErr_SetString(PyExc_ValueError,
1954 "field value is required for Await");
1955 return NULL;
1956 }
1957 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1958 if (!p)
1959 return NULL;
1960 p->kind = Await_kind;
1961 p->v.Await.value = value;
1962 p->lineno = lineno;
1963 p->col_offset = col_offset;
1964 return p;
1965}
1966
1967expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001968Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001970 expr_ty p;
1971 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1972 if (!p)
1973 return NULL;
1974 p->kind = Yield_kind;
1975 p->v.Yield.value = value;
1976 p->lineno = lineno;
1977 p->col_offset = col_offset;
1978 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979}
1980
1981expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001982YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
1983{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001984 expr_ty p;
1985 if (!value) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "field value is required for YieldFrom");
1988 return NULL;
1989 }
1990 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1991 if (!p)
1992 return NULL;
1993 p->kind = YieldFrom_kind;
1994 p->v.YieldFrom.value = value;
1995 p->lineno = lineno;
1996 p->col_offset = col_offset;
1997 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05001998}
1999
2000expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002001Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2002 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002004 expr_ty p;
2005 if (!left) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field left is required for Compare");
2008 return NULL;
2009 }
2010 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2011 if (!p)
2012 return NULL;
2013 p->kind = Compare_kind;
2014 p->v.Compare.left = left;
2015 p->v.Compare.ops = ops;
2016 p->v.Compare.comparators = comparators;
2017 p->lineno = lineno;
2018 p->col_offset = col_offset;
2019 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020}
2021
2022expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002023Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2024 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002026 expr_ty p;
2027 if (!func) {
2028 PyErr_SetString(PyExc_ValueError,
2029 "field func is required for Call");
2030 return NULL;
2031 }
2032 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2033 if (!p)
2034 return NULL;
2035 p->kind = Call_kind;
2036 p->v.Call.func = func;
2037 p->v.Call.args = args;
2038 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002039 p->lineno = lineno;
2040 p->col_offset = col_offset;
2041 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042}
2043
2044expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002045Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002047 expr_ty p;
2048 if (!n) {
2049 PyErr_SetString(PyExc_ValueError,
2050 "field n is required for Num");
2051 return NULL;
2052 }
2053 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2054 if (!p)
2055 return NULL;
2056 p->kind = Num_kind;
2057 p->v.Num.n = n;
2058 p->lineno = lineno;
2059 p->col_offset = col_offset;
2060 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061}
2062
2063expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002064Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002066 expr_ty p;
2067 if (!s) {
2068 PyErr_SetString(PyExc_ValueError,
2069 "field s is required for Str");
2070 return NULL;
2071 }
2072 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2073 if (!p)
2074 return NULL;
2075 p->kind = Str_kind;
2076 p->v.Str.s = s;
2077 p->lineno = lineno;
2078 p->col_offset = col_offset;
2079 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080}
2081
2082expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002083FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2084 int col_offset, PyArena *arena)
2085{
2086 expr_ty p;
2087 if (!value) {
2088 PyErr_SetString(PyExc_ValueError,
2089 "field value is required for FormattedValue");
2090 return NULL;
2091 }
2092 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2093 if (!p)
2094 return NULL;
2095 p->kind = FormattedValue_kind;
2096 p->v.FormattedValue.value = value;
2097 p->v.FormattedValue.conversion = conversion;
2098 p->v.FormattedValue.format_spec = format_spec;
2099 p->lineno = lineno;
2100 p->col_offset = col_offset;
2101 return p;
2102}
2103
2104expr_ty
2105JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2106{
2107 expr_ty p;
2108 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2109 if (!p)
2110 return NULL;
2111 p->kind = JoinedStr_kind;
2112 p->v.JoinedStr.values = values;
2113 p->lineno = lineno;
2114 p->col_offset = col_offset;
2115 return p;
2116}
2117
2118expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002119Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002120{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002121 expr_ty p;
2122 if (!s) {
2123 PyErr_SetString(PyExc_ValueError,
2124 "field s is required for Bytes");
2125 return NULL;
2126 }
2127 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2128 if (!p)
2129 return NULL;
2130 p->kind = Bytes_kind;
2131 p->v.Bytes.s = s;
2132 p->lineno = lineno;
2133 p->col_offset = col_offset;
2134 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002135}
2136
2137expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002138NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2139{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002140 expr_ty p;
2141 if (!value) {
2142 PyErr_SetString(PyExc_ValueError,
2143 "field value is required for NameConstant");
2144 return NULL;
2145 }
2146 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2147 if (!p)
2148 return NULL;
2149 p->kind = NameConstant_kind;
2150 p->v.NameConstant.value = value;
2151 p->lineno = lineno;
2152 p->col_offset = col_offset;
2153 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002154}
2155
2156expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002157Ellipsis(int lineno, int col_offset, PyArena *arena)
2158{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002159 expr_ty p;
2160 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2161 if (!p)
2162 return NULL;
2163 p->kind = Ellipsis_kind;
2164 p->lineno = lineno;
2165 p->col_offset = col_offset;
2166 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002167}
2168
2169expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002170Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2171 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002173 expr_ty p;
2174 if (!value) {
2175 PyErr_SetString(PyExc_ValueError,
2176 "field value is required for Attribute");
2177 return NULL;
2178 }
2179 if (!attr) {
2180 PyErr_SetString(PyExc_ValueError,
2181 "field attr is required for Attribute");
2182 return NULL;
2183 }
2184 if (!ctx) {
2185 PyErr_SetString(PyExc_ValueError,
2186 "field ctx is required for Attribute");
2187 return NULL;
2188 }
2189 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2190 if (!p)
2191 return NULL;
2192 p->kind = Attribute_kind;
2193 p->v.Attribute.value = value;
2194 p->v.Attribute.attr = attr;
2195 p->v.Attribute.ctx = ctx;
2196 p->lineno = lineno;
2197 p->col_offset = col_offset;
2198 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199}
2200
2201expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002202Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2203 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002205 expr_ty p;
2206 if (!value) {
2207 PyErr_SetString(PyExc_ValueError,
2208 "field value is required for Subscript");
2209 return NULL;
2210 }
2211 if (!slice) {
2212 PyErr_SetString(PyExc_ValueError,
2213 "field slice is required for Subscript");
2214 return NULL;
2215 }
2216 if (!ctx) {
2217 PyErr_SetString(PyExc_ValueError,
2218 "field ctx is required for Subscript");
2219 return NULL;
2220 }
2221 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2222 if (!p)
2223 return NULL;
2224 p->kind = Subscript_kind;
2225 p->v.Subscript.value = value;
2226 p->v.Subscript.slice = slice;
2227 p->v.Subscript.ctx = ctx;
2228 p->lineno = lineno;
2229 p->col_offset = col_offset;
2230 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231}
2232
2233expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002234Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2235 *arena)
2236{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002237 expr_ty p;
2238 if (!value) {
2239 PyErr_SetString(PyExc_ValueError,
2240 "field value is required for Starred");
2241 return NULL;
2242 }
2243 if (!ctx) {
2244 PyErr_SetString(PyExc_ValueError,
2245 "field ctx is required for Starred");
2246 return NULL;
2247 }
2248 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2249 if (!p)
2250 return NULL;
2251 p->kind = Starred_kind;
2252 p->v.Starred.value = value;
2253 p->v.Starred.ctx = ctx;
2254 p->lineno = lineno;
2255 p->col_offset = col_offset;
2256 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002257}
2258
2259expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002260Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2261 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002263 expr_ty p;
2264 if (!id) {
2265 PyErr_SetString(PyExc_ValueError,
2266 "field id is required for Name");
2267 return NULL;
2268 }
2269 if (!ctx) {
2270 PyErr_SetString(PyExc_ValueError,
2271 "field ctx is required for Name");
2272 return NULL;
2273 }
2274 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2275 if (!p)
2276 return NULL;
2277 p->kind = Name_kind;
2278 p->v.Name.id = id;
2279 p->v.Name.ctx = ctx;
2280 p->lineno = lineno;
2281 p->col_offset = col_offset;
2282 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283}
2284
2285expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002286List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2287 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002289 expr_ty p;
2290 if (!ctx) {
2291 PyErr_SetString(PyExc_ValueError,
2292 "field ctx is required for List");
2293 return NULL;
2294 }
2295 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2296 if (!p)
2297 return NULL;
2298 p->kind = List_kind;
2299 p->v.List.elts = elts;
2300 p->v.List.ctx = ctx;
2301 p->lineno = lineno;
2302 p->col_offset = col_offset;
2303 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304}
2305
2306expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002307Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2308 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002310 expr_ty p;
2311 if (!ctx) {
2312 PyErr_SetString(PyExc_ValueError,
2313 "field ctx is required for Tuple");
2314 return NULL;
2315 }
2316 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2317 if (!p)
2318 return NULL;
2319 p->kind = Tuple_kind;
2320 p->v.Tuple.elts = elts;
2321 p->v.Tuple.ctx = ctx;
2322 p->lineno = lineno;
2323 p->col_offset = col_offset;
2324 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325}
2326
2327slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002328Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002330 slice_ty p;
2331 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2332 if (!p)
2333 return NULL;
2334 p->kind = Slice_kind;
2335 p->v.Slice.lower = lower;
2336 p->v.Slice.upper = upper;
2337 p->v.Slice.step = step;
2338 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339}
2340
2341slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002342ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002344 slice_ty p;
2345 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2346 if (!p)
2347 return NULL;
2348 p->kind = ExtSlice_kind;
2349 p->v.ExtSlice.dims = dims;
2350 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351}
2352
2353slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002354Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002356 slice_ty p;
2357 if (!value) {
2358 PyErr_SetString(PyExc_ValueError,
2359 "field value is required for Index");
2360 return NULL;
2361 }
2362 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2363 if (!p)
2364 return NULL;
2365 p->kind = Index_kind;
2366 p->v.Index.value = value;
2367 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368}
2369
2370comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002371comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002373 comprehension_ty p;
2374 if (!target) {
2375 PyErr_SetString(PyExc_ValueError,
2376 "field target is required for comprehension");
2377 return NULL;
2378 }
2379 if (!iter) {
2380 PyErr_SetString(PyExc_ValueError,
2381 "field iter is required for comprehension");
2382 return NULL;
2383 }
2384 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2385 if (!p)
2386 return NULL;
2387 p->target = target;
2388 p->iter = iter;
2389 p->ifs = ifs;
2390 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391}
2392
2393excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002394ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002395 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002397 excepthandler_ty p;
2398 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2399 if (!p)
2400 return NULL;
2401 p->kind = ExceptHandler_kind;
2402 p->v.ExceptHandler.type = type;
2403 p->v.ExceptHandler.name = name;
2404 p->v.ExceptHandler.body = body;
2405 p->lineno = lineno;
2406 p->col_offset = col_offset;
2407 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408}
2409
2410arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002411arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2412 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002414 arguments_ty p;
2415 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2416 if (!p)
2417 return NULL;
2418 p->args = args;
2419 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002420 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002421 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002422 p->kwarg = kwarg;
2423 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002424 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002425}
2426
Neal Norwitzc1505362006-12-28 06:47:50 +00002427arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002428arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002429{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002430 arg_ty p;
2431 if (!arg) {
2432 PyErr_SetString(PyExc_ValueError,
2433 "field arg is required for arg");
2434 return NULL;
2435 }
2436 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2437 if (!p)
2438 return NULL;
2439 p->arg = arg;
2440 p->annotation = annotation;
2441 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002442}
2443
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002445keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002447 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002448 if (!value) {
2449 PyErr_SetString(PyExc_ValueError,
2450 "field value is required for keyword");
2451 return NULL;
2452 }
2453 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2454 if (!p)
2455 return NULL;
2456 p->arg = arg;
2457 p->value = value;
2458 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459}
2460
2461alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002462alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002464 alias_ty p;
2465 if (!name) {
2466 PyErr_SetString(PyExc_ValueError,
2467 "field name is required for alias");
2468 return NULL;
2469 }
2470 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2471 if (!p)
2472 return NULL;
2473 p->name = name;
2474 p->asname = asname;
2475 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476}
2477
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002478withitem_ty
2479withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2480{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002481 withitem_ty p;
2482 if (!context_expr) {
2483 PyErr_SetString(PyExc_ValueError,
2484 "field context_expr is required for withitem");
2485 return NULL;
2486 }
2487 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2488 if (!p)
2489 return NULL;
2490 p->context_expr = context_expr;
2491 p->optional_vars = optional_vars;
2492 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002493}
2494
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002495
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002496PyObject*
2497ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002498{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002499 mod_ty o = (mod_ty)_o;
2500 PyObject *result = NULL, *value = NULL;
2501 if (!o) {
2502 Py_INCREF(Py_None);
2503 return Py_None;
2504 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002505
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002506 switch (o->kind) {
2507 case Module_kind:
2508 result = PyType_GenericNew(Module_type, NULL, NULL);
2509 if (!result) goto failed;
2510 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2511 if (!value) goto failed;
2512 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2513 goto failed;
2514 Py_DECREF(value);
2515 break;
2516 case Interactive_kind:
2517 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2518 if (!result) goto failed;
2519 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2520 if (!value) goto failed;
2521 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2522 goto failed;
2523 Py_DECREF(value);
2524 break;
2525 case Expression_kind:
2526 result = PyType_GenericNew(Expression_type, NULL, NULL);
2527 if (!result) goto failed;
2528 value = ast2obj_expr(o->v.Expression.body);
2529 if (!value) goto failed;
2530 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2531 goto failed;
2532 Py_DECREF(value);
2533 break;
2534 case Suite_kind:
2535 result = PyType_GenericNew(Suite_type, NULL, NULL);
2536 if (!result) goto failed;
2537 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2538 if (!value) goto failed;
2539 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2540 goto failed;
2541 Py_DECREF(value);
2542 break;
2543 }
2544 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002545failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002546 Py_XDECREF(value);
2547 Py_XDECREF(result);
2548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549}
2550
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002551PyObject*
2552ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002554 stmt_ty o = (stmt_ty)_o;
2555 PyObject *result = NULL, *value = NULL;
2556 if (!o) {
2557 Py_INCREF(Py_None);
2558 return Py_None;
2559 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002560
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002561 switch (o->kind) {
2562 case FunctionDef_kind:
2563 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2564 if (!result) goto failed;
2565 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002566 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002567 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2568 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002569 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002570 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002571 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002572 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2573 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002574 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002575 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2576 if (!value) goto failed;
2577 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2578 goto failed;
2579 Py_DECREF(value);
2580 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2581 if (!value) goto failed;
2582 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
2585 value = ast2obj_expr(o->v.FunctionDef.returns);
2586 if (!value) goto failed;
2587 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2588 goto failed;
2589 Py_DECREF(value);
2590 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002591 case AsyncFunctionDef_kind:
2592 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2593 if (!result) goto failed;
2594 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2595 if (!value) goto failed;
2596 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2600 if (!value) goto failed;
2601 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
2604 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2605 if (!value) goto failed;
2606 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2607 goto failed;
2608 Py_DECREF(value);
2609 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2610 ast2obj_expr);
2611 if (!value) goto failed;
2612 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
2615 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2616 if (!value) goto failed;
2617 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
2620 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002621 case ClassDef_kind:
2622 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2623 if (!result) goto failed;
2624 value = ast2obj_identifier(o->v.ClassDef.name);
2625 if (!value) goto failed;
2626 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
2629 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2630 if (!value) goto failed;
2631 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
2634 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2635 if (!value) goto failed;
2636 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2637 goto failed;
2638 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002639 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2640 if (!value) goto failed;
2641 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2642 goto failed;
2643 Py_DECREF(value);
2644 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2645 if (!value) goto failed;
2646 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2647 goto failed;
2648 Py_DECREF(value);
2649 break;
2650 case Return_kind:
2651 result = PyType_GenericNew(Return_type, NULL, NULL);
2652 if (!result) goto failed;
2653 value = ast2obj_expr(o->v.Return.value);
2654 if (!value) goto failed;
2655 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2656 goto failed;
2657 Py_DECREF(value);
2658 break;
2659 case Delete_kind:
2660 result = PyType_GenericNew(Delete_type, NULL, NULL);
2661 if (!result) goto failed;
2662 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2663 if (!value) goto failed;
2664 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 break;
2668 case Assign_kind:
2669 result = PyType_GenericNew(Assign_type, NULL, NULL);
2670 if (!result) goto failed;
2671 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2672 if (!value) goto failed;
2673 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
2676 value = ast2obj_expr(o->v.Assign.value);
2677 if (!value) goto failed;
2678 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 break;
2682 case AugAssign_kind:
2683 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_expr(o->v.AugAssign.target);
2686 if (!value) goto failed;
2687 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 value = ast2obj_operator(o->v.AugAssign.op);
2691 if (!value) goto failed;
2692 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2693 goto failed;
2694 Py_DECREF(value);
2695 value = ast2obj_expr(o->v.AugAssign.value);
2696 if (!value) goto failed;
2697 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 break;
2701 case For_kind:
2702 result = PyType_GenericNew(For_type, NULL, NULL);
2703 if (!result) goto failed;
2704 value = ast2obj_expr(o->v.For.target);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 value = ast2obj_expr(o->v.For.iter);
2710 if (!value) goto failed;
2711 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2715 if (!value) goto failed;
2716 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
2719 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2720 if (!value) goto failed;
2721 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2722 goto failed;
2723 Py_DECREF(value);
2724 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002725 case AsyncFor_kind:
2726 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2727 if (!result) goto failed;
2728 value = ast2obj_expr(o->v.AsyncFor.target);
2729 if (!value) goto failed;
2730 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
2733 value = ast2obj_expr(o->v.AsyncFor.iter);
2734 if (!value) goto failed;
2735 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2736 goto failed;
2737 Py_DECREF(value);
2738 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2739 if (!value) goto failed;
2740 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2741 goto failed;
2742 Py_DECREF(value);
2743 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2744 if (!value) goto failed;
2745 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2746 goto failed;
2747 Py_DECREF(value);
2748 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002749 case While_kind:
2750 result = PyType_GenericNew(While_type, NULL, NULL);
2751 if (!result) goto failed;
2752 value = ast2obj_expr(o->v.While.test);
2753 if (!value) goto failed;
2754 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2755 goto failed;
2756 Py_DECREF(value);
2757 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2758 if (!value) goto failed;
2759 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2760 goto failed;
2761 Py_DECREF(value);
2762 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2763 if (!value) goto failed;
2764 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2765 goto failed;
2766 Py_DECREF(value);
2767 break;
2768 case If_kind:
2769 result = PyType_GenericNew(If_type, NULL, NULL);
2770 if (!result) goto failed;
2771 value = ast2obj_expr(o->v.If.test);
2772 if (!value) goto failed;
2773 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2774 goto failed;
2775 Py_DECREF(value);
2776 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2777 if (!value) goto failed;
2778 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2779 goto failed;
2780 Py_DECREF(value);
2781 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2782 if (!value) goto failed;
2783 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2784 goto failed;
2785 Py_DECREF(value);
2786 break;
2787 case With_kind:
2788 result = PyType_GenericNew(With_type, NULL, NULL);
2789 if (!result) goto failed;
2790 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2791 if (!value) goto failed;
2792 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2793 goto failed;
2794 Py_DECREF(value);
2795 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2796 if (!value) goto failed;
2797 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2798 goto failed;
2799 Py_DECREF(value);
2800 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002801 case AsyncWith_kind:
2802 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2803 if (!result) goto failed;
2804 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2805 if (!value) goto failed;
2806 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2807 goto failed;
2808 Py_DECREF(value);
2809 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2810 if (!value) goto failed;
2811 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2812 goto failed;
2813 Py_DECREF(value);
2814 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002815 case Raise_kind:
2816 result = PyType_GenericNew(Raise_type, NULL, NULL);
2817 if (!result) goto failed;
2818 value = ast2obj_expr(o->v.Raise.exc);
2819 if (!value) goto failed;
2820 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2821 goto failed;
2822 Py_DECREF(value);
2823 value = ast2obj_expr(o->v.Raise.cause);
2824 if (!value) goto failed;
2825 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2826 goto failed;
2827 Py_DECREF(value);
2828 break;
2829 case Try_kind:
2830 result = PyType_GenericNew(Try_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2833 if (!value) goto failed;
2834 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2838 if (!value) goto failed;
2839 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2843 if (!value) goto failed;
2844 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2848 if (!value) goto failed;
2849 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
2852 break;
2853 case Assert_kind:
2854 result = PyType_GenericNew(Assert_type, NULL, NULL);
2855 if (!result) goto failed;
2856 value = ast2obj_expr(o->v.Assert.test);
2857 if (!value) goto failed;
2858 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_expr(o->v.Assert.msg);
2862 if (!value) goto failed;
2863 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 break;
2867 case Import_kind:
2868 result = PyType_GenericNew(Import_type, NULL, NULL);
2869 if (!result) goto failed;
2870 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2871 if (!value) goto failed;
2872 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2873 goto failed;
2874 Py_DECREF(value);
2875 break;
2876 case ImportFrom_kind:
2877 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2878 if (!result) goto failed;
2879 value = ast2obj_identifier(o->v.ImportFrom.module);
2880 if (!value) goto failed;
2881 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
2884 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_int(o->v.ImportFrom.level);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 break;
2895 case Global_kind:
2896 result = PyType_GenericNew(Global_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 break;
2904 case Nonlocal_kind:
2905 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2906 if (!result) goto failed;
2907 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2908 if (!value) goto failed;
2909 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2910 goto failed;
2911 Py_DECREF(value);
2912 break;
2913 case Expr_kind:
2914 result = PyType_GenericNew(Expr_type, NULL, NULL);
2915 if (!result) goto failed;
2916 value = ast2obj_expr(o->v.Expr.value);
2917 if (!value) goto failed;
2918 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
2921 break;
2922 case Pass_kind:
2923 result = PyType_GenericNew(Pass_type, NULL, NULL);
2924 if (!result) goto failed;
2925 break;
2926 case Break_kind:
2927 result = PyType_GenericNew(Break_type, NULL, NULL);
2928 if (!result) goto failed;
2929 break;
2930 case Continue_kind:
2931 result = PyType_GenericNew(Continue_type, NULL, NULL);
2932 if (!result) goto failed;
2933 break;
2934 }
2935 value = ast2obj_int(o->lineno);
2936 if (!value) goto failed;
2937 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2938 goto failed;
2939 Py_DECREF(value);
2940 value = ast2obj_int(o->col_offset);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2943 goto failed;
2944 Py_DECREF(value);
2945 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002946failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002947 Py_XDECREF(value);
2948 Py_XDECREF(result);
2949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950}
2951
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002952PyObject*
2953ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002955 expr_ty o = (expr_ty)_o;
2956 PyObject *result = NULL, *value = NULL;
2957 if (!o) {
2958 Py_INCREF(Py_None);
2959 return Py_None;
2960 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002961
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002962 switch (o->kind) {
2963 case BoolOp_kind:
2964 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2965 if (!result) goto failed;
2966 value = ast2obj_boolop(o->v.BoolOp.op);
2967 if (!value) goto failed;
2968 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2969 goto failed;
2970 Py_DECREF(value);
2971 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2972 if (!value) goto failed;
2973 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2974 goto failed;
2975 Py_DECREF(value);
2976 break;
2977 case BinOp_kind:
2978 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2979 if (!result) goto failed;
2980 value = ast2obj_expr(o->v.BinOp.left);
2981 if (!value) goto failed;
2982 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2983 goto failed;
2984 Py_DECREF(value);
2985 value = ast2obj_operator(o->v.BinOp.op);
2986 if (!value) goto failed;
2987 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2988 goto failed;
2989 Py_DECREF(value);
2990 value = ast2obj_expr(o->v.BinOp.right);
2991 if (!value) goto failed;
2992 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2993 goto failed;
2994 Py_DECREF(value);
2995 break;
2996 case UnaryOp_kind:
2997 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2998 if (!result) goto failed;
2999 value = ast2obj_unaryop(o->v.UnaryOp.op);
3000 if (!value) goto failed;
3001 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3002 goto failed;
3003 Py_DECREF(value);
3004 value = ast2obj_expr(o->v.UnaryOp.operand);
3005 if (!value) goto failed;
3006 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3007 goto failed;
3008 Py_DECREF(value);
3009 break;
3010 case Lambda_kind:
3011 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3012 if (!result) goto failed;
3013 value = ast2obj_arguments(o->v.Lambda.args);
3014 if (!value) goto failed;
3015 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3016 goto failed;
3017 Py_DECREF(value);
3018 value = ast2obj_expr(o->v.Lambda.body);
3019 if (!value) goto failed;
3020 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3021 goto failed;
3022 Py_DECREF(value);
3023 break;
3024 case IfExp_kind:
3025 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3026 if (!result) goto failed;
3027 value = ast2obj_expr(o->v.IfExp.test);
3028 if (!value) goto failed;
3029 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3030 goto failed;
3031 Py_DECREF(value);
3032 value = ast2obj_expr(o->v.IfExp.body);
3033 if (!value) goto failed;
3034 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3035 goto failed;
3036 Py_DECREF(value);
3037 value = ast2obj_expr(o->v.IfExp.orelse);
3038 if (!value) goto failed;
3039 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3040 goto failed;
3041 Py_DECREF(value);
3042 break;
3043 case Dict_kind:
3044 result = PyType_GenericNew(Dict_type, NULL, NULL);
3045 if (!result) goto failed;
3046 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3047 if (!value) goto failed;
3048 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3049 goto failed;
3050 Py_DECREF(value);
3051 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3052 if (!value) goto failed;
3053 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3054 goto failed;
3055 Py_DECREF(value);
3056 break;
3057 case Set_kind:
3058 result = PyType_GenericNew(Set_type, NULL, NULL);
3059 if (!result) goto failed;
3060 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3061 if (!value) goto failed;
3062 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3063 goto failed;
3064 Py_DECREF(value);
3065 break;
3066 case ListComp_kind:
3067 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3068 if (!result) goto failed;
3069 value = ast2obj_expr(o->v.ListComp.elt);
3070 if (!value) goto failed;
3071 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3072 goto failed;
3073 Py_DECREF(value);
3074 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3075 if (!value) goto failed;
3076 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3077 goto failed;
3078 Py_DECREF(value);
3079 break;
3080 case SetComp_kind:
3081 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3082 if (!result) goto failed;
3083 value = ast2obj_expr(o->v.SetComp.elt);
3084 if (!value) goto failed;
3085 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3086 goto failed;
3087 Py_DECREF(value);
3088 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3089 if (!value) goto failed;
3090 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 break;
3094 case DictComp_kind:
3095 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3096 if (!result) goto failed;
3097 value = ast2obj_expr(o->v.DictComp.key);
3098 if (!value) goto failed;
3099 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3100 goto failed;
3101 Py_DECREF(value);
3102 value = ast2obj_expr(o->v.DictComp.value);
3103 if (!value) goto failed;
3104 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3105 goto failed;
3106 Py_DECREF(value);
3107 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3108 if (!value) goto failed;
3109 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3110 goto failed;
3111 Py_DECREF(value);
3112 break;
3113 case GeneratorExp_kind:
3114 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3115 if (!result) goto failed;
3116 value = ast2obj_expr(o->v.GeneratorExp.elt);
3117 if (!value) goto failed;
3118 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3119 goto failed;
3120 Py_DECREF(value);
3121 value = ast2obj_list(o->v.GeneratorExp.generators,
3122 ast2obj_comprehension);
3123 if (!value) goto failed;
3124 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3125 goto failed;
3126 Py_DECREF(value);
3127 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003128 case Await_kind:
3129 result = PyType_GenericNew(Await_type, NULL, NULL);
3130 if (!result) goto failed;
3131 value = ast2obj_expr(o->v.Await.value);
3132 if (!value) goto failed;
3133 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3134 goto failed;
3135 Py_DECREF(value);
3136 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003137 case Yield_kind:
3138 result = PyType_GenericNew(Yield_type, NULL, NULL);
3139 if (!result) goto failed;
3140 value = ast2obj_expr(o->v.Yield.value);
3141 if (!value) goto failed;
3142 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3143 goto failed;
3144 Py_DECREF(value);
3145 break;
3146 case YieldFrom_kind:
3147 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3148 if (!result) goto failed;
3149 value = ast2obj_expr(o->v.YieldFrom.value);
3150 if (!value) goto failed;
3151 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3152 goto failed;
3153 Py_DECREF(value);
3154 break;
3155 case Compare_kind:
3156 result = PyType_GenericNew(Compare_type, NULL, NULL);
3157 if (!result) goto failed;
3158 value = ast2obj_expr(o->v.Compare.left);
3159 if (!value) goto failed;
3160 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3161 goto failed;
3162 Py_DECREF(value);
3163 {
3164 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3165 value = PyList_New(n);
3166 if (!value) goto failed;
3167 for(i = 0; i < n; i++)
3168 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003170 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003171 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3172 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003173 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003174 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003175 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003176 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3177 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003178 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003179 break;
3180 case Call_kind:
3181 result = PyType_GenericNew(Call_type, NULL, NULL);
3182 if (!result) goto failed;
3183 value = ast2obj_expr(o->v.Call.func);
3184 if (!value) goto failed;
3185 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3186 goto failed;
3187 Py_DECREF(value);
3188 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3189 if (!value) goto failed;
3190 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3191 goto failed;
3192 Py_DECREF(value);
3193 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3194 if (!value) goto failed;
3195 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3196 goto failed;
3197 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003198 break;
3199 case Num_kind:
3200 result = PyType_GenericNew(Num_type, NULL, NULL);
3201 if (!result) goto failed;
3202 value = ast2obj_object(o->v.Num.n);
3203 if (!value) goto failed;
3204 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3205 goto failed;
3206 Py_DECREF(value);
3207 break;
3208 case Str_kind:
3209 result = PyType_GenericNew(Str_type, NULL, NULL);
3210 if (!result) goto failed;
3211 value = ast2obj_string(o->v.Str.s);
3212 if (!value) goto failed;
3213 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3214 goto failed;
3215 Py_DECREF(value);
3216 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003217 case FormattedValue_kind:
3218 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3219 if (!result) goto failed;
3220 value = ast2obj_expr(o->v.FormattedValue.value);
3221 if (!value) goto failed;
3222 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3223 goto failed;
3224 Py_DECREF(value);
3225 value = ast2obj_int(o->v.FormattedValue.conversion);
3226 if (!value) goto failed;
3227 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3228 goto failed;
3229 Py_DECREF(value);
3230 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 break;
3236 case JoinedStr_kind:
3237 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3238 if (!result) goto failed;
3239 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3240 if (!value) goto failed;
3241 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3242 goto failed;
3243 Py_DECREF(value);
3244 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003245 case Bytes_kind:
3246 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3247 if (!result) goto failed;
3248 value = ast2obj_bytes(o->v.Bytes.s);
3249 if (!value) goto failed;
3250 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
3253 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003254 case NameConstant_kind:
3255 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3256 if (!result) goto failed;
3257 value = ast2obj_singleton(o->v.NameConstant.value);
3258 if (!value) goto failed;
3259 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3260 goto failed;
3261 Py_DECREF(value);
3262 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003263 case Ellipsis_kind:
3264 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3265 if (!result) goto failed;
3266 break;
3267 case Attribute_kind:
3268 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3269 if (!result) goto failed;
3270 value = ast2obj_expr(o->v.Attribute.value);
3271 if (!value) goto failed;
3272 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3273 goto failed;
3274 Py_DECREF(value);
3275 value = ast2obj_identifier(o->v.Attribute.attr);
3276 if (!value) goto failed;
3277 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3278 goto failed;
3279 Py_DECREF(value);
3280 value = ast2obj_expr_context(o->v.Attribute.ctx);
3281 if (!value) goto failed;
3282 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3283 goto failed;
3284 Py_DECREF(value);
3285 break;
3286 case Subscript_kind:
3287 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3288 if (!result) goto failed;
3289 value = ast2obj_expr(o->v.Subscript.value);
3290 if (!value) goto failed;
3291 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3292 goto failed;
3293 Py_DECREF(value);
3294 value = ast2obj_slice(o->v.Subscript.slice);
3295 if (!value) goto failed;
3296 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3297 goto failed;
3298 Py_DECREF(value);
3299 value = ast2obj_expr_context(o->v.Subscript.ctx);
3300 if (!value) goto failed;
3301 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3302 goto failed;
3303 Py_DECREF(value);
3304 break;
3305 case Starred_kind:
3306 result = PyType_GenericNew(Starred_type, NULL, NULL);
3307 if (!result) goto failed;
3308 value = ast2obj_expr(o->v.Starred.value);
3309 if (!value) goto failed;
3310 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3311 goto failed;
3312 Py_DECREF(value);
3313 value = ast2obj_expr_context(o->v.Starred.ctx);
3314 if (!value) goto failed;
3315 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3316 goto failed;
3317 Py_DECREF(value);
3318 break;
3319 case Name_kind:
3320 result = PyType_GenericNew(Name_type, NULL, NULL);
3321 if (!result) goto failed;
3322 value = ast2obj_identifier(o->v.Name.id);
3323 if (!value) goto failed;
3324 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3325 goto failed;
3326 Py_DECREF(value);
3327 value = ast2obj_expr_context(o->v.Name.ctx);
3328 if (!value) goto failed;
3329 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3330 goto failed;
3331 Py_DECREF(value);
3332 break;
3333 case List_kind:
3334 result = PyType_GenericNew(List_type, NULL, NULL);
3335 if (!result) goto failed;
3336 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3337 if (!value) goto failed;
3338 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3339 goto failed;
3340 Py_DECREF(value);
3341 value = ast2obj_expr_context(o->v.List.ctx);
3342 if (!value) goto failed;
3343 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3344 goto failed;
3345 Py_DECREF(value);
3346 break;
3347 case Tuple_kind:
3348 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3349 if (!result) goto failed;
3350 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3351 if (!value) goto failed;
3352 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3353 goto failed;
3354 Py_DECREF(value);
3355 value = ast2obj_expr_context(o->v.Tuple.ctx);
3356 if (!value) goto failed;
3357 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3358 goto failed;
3359 Py_DECREF(value);
3360 break;
3361 }
3362 value = ast2obj_int(o->lineno);
3363 if (!value) goto failed;
3364 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3365 goto failed;
3366 Py_DECREF(value);
3367 value = ast2obj_int(o->col_offset);
3368 if (!value) goto failed;
3369 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3370 goto failed;
3371 Py_DECREF(value);
3372 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003373failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003374 Py_XDECREF(value);
3375 Py_XDECREF(result);
3376 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377}
3378
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003379PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003381 switch(o) {
3382 case Load:
3383 Py_INCREF(Load_singleton);
3384 return Load_singleton;
3385 case Store:
3386 Py_INCREF(Store_singleton);
3387 return Store_singleton;
3388 case Del:
3389 Py_INCREF(Del_singleton);
3390 return Del_singleton;
3391 case AugLoad:
3392 Py_INCREF(AugLoad_singleton);
3393 return AugLoad_singleton;
3394 case AugStore:
3395 Py_INCREF(AugStore_singleton);
3396 return AugStore_singleton;
3397 case Param:
3398 Py_INCREF(Param_singleton);
3399 return Param_singleton;
3400 default:
3401 /* should never happen, but just in case ... */
3402 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3403 return NULL;
3404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003406PyObject*
3407ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003409 slice_ty o = (slice_ty)_o;
3410 PyObject *result = NULL, *value = NULL;
3411 if (!o) {
3412 Py_INCREF(Py_None);
3413 return Py_None;
3414 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003415
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003416 switch (o->kind) {
3417 case Slice_kind:
3418 result = PyType_GenericNew(Slice_type, NULL, NULL);
3419 if (!result) goto failed;
3420 value = ast2obj_expr(o->v.Slice.lower);
3421 if (!value) goto failed;
3422 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3423 goto failed;
3424 Py_DECREF(value);
3425 value = ast2obj_expr(o->v.Slice.upper);
3426 if (!value) goto failed;
3427 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3428 goto failed;
3429 Py_DECREF(value);
3430 value = ast2obj_expr(o->v.Slice.step);
3431 if (!value) goto failed;
3432 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3433 goto failed;
3434 Py_DECREF(value);
3435 break;
3436 case ExtSlice_kind:
3437 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3438 if (!result) goto failed;
3439 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3440 if (!value) goto failed;
3441 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3442 goto failed;
3443 Py_DECREF(value);
3444 break;
3445 case Index_kind:
3446 result = PyType_GenericNew(Index_type, NULL, NULL);
3447 if (!result) goto failed;
3448 value = ast2obj_expr(o->v.Index.value);
3449 if (!value) goto failed;
3450 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3451 goto failed;
3452 Py_DECREF(value);
3453 break;
3454 }
3455 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003456failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003457 Py_XDECREF(value);
3458 Py_XDECREF(result);
3459 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460}
3461
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003462PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003464 switch(o) {
3465 case And:
3466 Py_INCREF(And_singleton);
3467 return And_singleton;
3468 case Or:
3469 Py_INCREF(Or_singleton);
3470 return Or_singleton;
3471 default:
3472 /* should never happen, but just in case ... */
3473 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3474 return NULL;
3475 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003476}
3477PyObject* ast2obj_operator(operator_ty o)
3478{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003479 switch(o) {
3480 case Add:
3481 Py_INCREF(Add_singleton);
3482 return Add_singleton;
3483 case Sub:
3484 Py_INCREF(Sub_singleton);
3485 return Sub_singleton;
3486 case Mult:
3487 Py_INCREF(Mult_singleton);
3488 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003489 case MatMult:
3490 Py_INCREF(MatMult_singleton);
3491 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003492 case Div:
3493 Py_INCREF(Div_singleton);
3494 return Div_singleton;
3495 case Mod:
3496 Py_INCREF(Mod_singleton);
3497 return Mod_singleton;
3498 case Pow:
3499 Py_INCREF(Pow_singleton);
3500 return Pow_singleton;
3501 case LShift:
3502 Py_INCREF(LShift_singleton);
3503 return LShift_singleton;
3504 case RShift:
3505 Py_INCREF(RShift_singleton);
3506 return RShift_singleton;
3507 case BitOr:
3508 Py_INCREF(BitOr_singleton);
3509 return BitOr_singleton;
3510 case BitXor:
3511 Py_INCREF(BitXor_singleton);
3512 return BitXor_singleton;
3513 case BitAnd:
3514 Py_INCREF(BitAnd_singleton);
3515 return BitAnd_singleton;
3516 case FloorDiv:
3517 Py_INCREF(FloorDiv_singleton);
3518 return FloorDiv_singleton;
3519 default:
3520 /* should never happen, but just in case ... */
3521 PyErr_Format(PyExc_SystemError, "unknown operator found");
3522 return NULL;
3523 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003524}
3525PyObject* ast2obj_unaryop(unaryop_ty o)
3526{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003527 switch(o) {
3528 case Invert:
3529 Py_INCREF(Invert_singleton);
3530 return Invert_singleton;
3531 case Not:
3532 Py_INCREF(Not_singleton);
3533 return Not_singleton;
3534 case UAdd:
3535 Py_INCREF(UAdd_singleton);
3536 return UAdd_singleton;
3537 case USub:
3538 Py_INCREF(USub_singleton);
3539 return USub_singleton;
3540 default:
3541 /* should never happen, but just in case ... */
3542 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3543 return NULL;
3544 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003545}
3546PyObject* ast2obj_cmpop(cmpop_ty o)
3547{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003548 switch(o) {
3549 case Eq:
3550 Py_INCREF(Eq_singleton);
3551 return Eq_singleton;
3552 case NotEq:
3553 Py_INCREF(NotEq_singleton);
3554 return NotEq_singleton;
3555 case Lt:
3556 Py_INCREF(Lt_singleton);
3557 return Lt_singleton;
3558 case LtE:
3559 Py_INCREF(LtE_singleton);
3560 return LtE_singleton;
3561 case Gt:
3562 Py_INCREF(Gt_singleton);
3563 return Gt_singleton;
3564 case GtE:
3565 Py_INCREF(GtE_singleton);
3566 return GtE_singleton;
3567 case Is:
3568 Py_INCREF(Is_singleton);
3569 return Is_singleton;
3570 case IsNot:
3571 Py_INCREF(IsNot_singleton);
3572 return IsNot_singleton;
3573 case In:
3574 Py_INCREF(In_singleton);
3575 return In_singleton;
3576 case NotIn:
3577 Py_INCREF(NotIn_singleton);
3578 return NotIn_singleton;
3579 default:
3580 /* should never happen, but just in case ... */
3581 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3582 return NULL;
3583 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003584}
3585PyObject*
3586ast2obj_comprehension(void* _o)
3587{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003588 comprehension_ty o = (comprehension_ty)_o;
3589 PyObject *result = NULL, *value = NULL;
3590 if (!o) {
3591 Py_INCREF(Py_None);
3592 return Py_None;
3593 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003594
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003595 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3596 if (!result) return NULL;
3597 value = ast2obj_expr(o->target);
3598 if (!value) goto failed;
3599 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3600 goto failed;
3601 Py_DECREF(value);
3602 value = ast2obj_expr(o->iter);
3603 if (!value) goto failed;
3604 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3605 goto failed;
3606 Py_DECREF(value);
3607 value = ast2obj_list(o->ifs, ast2obj_expr);
3608 if (!value) goto failed;
3609 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3610 goto failed;
3611 Py_DECREF(value);
3612 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003613failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003614 Py_XDECREF(value);
3615 Py_XDECREF(result);
3616 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617}
3618
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003619PyObject*
3620ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003622 excepthandler_ty o = (excepthandler_ty)_o;
3623 PyObject *result = NULL, *value = NULL;
3624 if (!o) {
3625 Py_INCREF(Py_None);
3626 return Py_None;
3627 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003628
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003629 switch (o->kind) {
3630 case ExceptHandler_kind:
3631 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3632 if (!result) goto failed;
3633 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003634 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003635 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3636 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003637 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003638 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003639 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003640 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3641 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003642 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003643 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3644 if (!value) goto failed;
3645 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3646 goto failed;
3647 Py_DECREF(value);
3648 break;
3649 }
3650 value = ast2obj_int(o->lineno);
3651 if (!value) goto failed;
3652 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3653 goto failed;
3654 Py_DECREF(value);
3655 value = ast2obj_int(o->col_offset);
3656 if (!value) goto failed;
3657 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3658 goto failed;
3659 Py_DECREF(value);
3660 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003661failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003662 Py_XDECREF(value);
3663 Py_XDECREF(result);
3664 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665}
3666
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003667PyObject*
3668ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003670 arguments_ty o = (arguments_ty)_o;
3671 PyObject *result = NULL, *value = NULL;
3672 if (!o) {
3673 Py_INCREF(Py_None);
3674 return Py_None;
3675 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003676
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003677 result = PyType_GenericNew(arguments_type, NULL, NULL);
3678 if (!result) return NULL;
3679 value = ast2obj_list(o->args, ast2obj_arg);
3680 if (!value) goto failed;
3681 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3682 goto failed;
3683 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003684 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003685 if (!value) goto failed;
3686 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3687 goto failed;
3688 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3690 if (!value) goto failed;
3691 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3692 goto failed;
3693 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003694 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003695 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003696 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003697 goto failed;
3698 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003699 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003700 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003701 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003702 goto failed;
3703 Py_DECREF(value);
3704 value = ast2obj_list(o->defaults, ast2obj_expr);
3705 if (!value) goto failed;
3706 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3707 goto failed;
3708 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003709 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003710failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003711 Py_XDECREF(value);
3712 Py_XDECREF(result);
3713 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714}
3715
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003716PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003717ast2obj_arg(void* _o)
3718{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003719 arg_ty o = (arg_ty)_o;
3720 PyObject *result = NULL, *value = NULL;
3721 if (!o) {
3722 Py_INCREF(Py_None);
3723 return Py_None;
3724 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003725
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003726 result = PyType_GenericNew(arg_type, NULL, NULL);
3727 if (!result) return NULL;
3728 value = ast2obj_identifier(o->arg);
3729 if (!value) goto failed;
3730 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3731 goto failed;
3732 Py_DECREF(value);
3733 value = ast2obj_expr(o->annotation);
3734 if (!value) goto failed;
3735 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3736 goto failed;
3737 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003738 value = ast2obj_int(o->lineno);
3739 if (!value) goto failed;
3740 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3741 goto failed;
3742 Py_DECREF(value);
3743 value = ast2obj_int(o->col_offset);
3744 if (!value) goto failed;
3745 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3746 goto failed;
3747 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003748 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003749failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003750 Py_XDECREF(value);
3751 Py_XDECREF(result);
3752 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003753}
3754
3755PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003756ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003758 keyword_ty o = (keyword_ty)_o;
3759 PyObject *result = NULL, *value = NULL;
3760 if (!o) {
3761 Py_INCREF(Py_None);
3762 return Py_None;
3763 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003764
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003765 result = PyType_GenericNew(keyword_type, NULL, NULL);
3766 if (!result) return NULL;
3767 value = ast2obj_identifier(o->arg);
3768 if (!value) goto failed;
3769 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3770 goto failed;
3771 Py_DECREF(value);
3772 value = ast2obj_expr(o->value);
3773 if (!value) goto failed;
3774 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3775 goto failed;
3776 Py_DECREF(value);
3777 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003778failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003779 Py_XDECREF(value);
3780 Py_XDECREF(result);
3781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782}
3783
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003784PyObject*
3785ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003787 alias_ty o = (alias_ty)_o;
3788 PyObject *result = NULL, *value = NULL;
3789 if (!o) {
3790 Py_INCREF(Py_None);
3791 return Py_None;
3792 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003793
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003794 result = PyType_GenericNew(alias_type, NULL, NULL);
3795 if (!result) return NULL;
3796 value = ast2obj_identifier(o->name);
3797 if (!value) goto failed;
3798 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3799 goto failed;
3800 Py_DECREF(value);
3801 value = ast2obj_identifier(o->asname);
3802 if (!value) goto failed;
3803 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3804 goto failed;
3805 Py_DECREF(value);
3806 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003807failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003808 Py_XDECREF(value);
3809 Py_XDECREF(result);
3810 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811}
3812
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003813PyObject*
3814ast2obj_withitem(void* _o)
3815{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003816 withitem_ty o = (withitem_ty)_o;
3817 PyObject *result = NULL, *value = NULL;
3818 if (!o) {
3819 Py_INCREF(Py_None);
3820 return Py_None;
3821 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003822
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003823 result = PyType_GenericNew(withitem_type, NULL, NULL);
3824 if (!result) return NULL;
3825 value = ast2obj_expr(o->context_expr);
3826 if (!value) goto failed;
3827 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3828 goto failed;
3829 Py_DECREF(value);
3830 value = ast2obj_expr(o->optional_vars);
3831 if (!value) goto failed;
3832 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3833 goto failed;
3834 Py_DECREF(value);
3835 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003836failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003837 Py_XDECREF(value);
3838 Py_XDECREF(result);
3839 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003840}
3841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003843int
3844obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3845{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003846 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003847
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003848 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003849
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003850 if (obj == Py_None) {
3851 *out = NULL;
3852 return 0;
3853 }
3854 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3855 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003856 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003857 }
3858 if (isinstance) {
3859 asdl_seq* body;
3860
3861 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3862 int res;
3863 Py_ssize_t len;
3864 Py_ssize_t i;
3865 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3866 if (tmp == NULL) goto failed;
3867 if (!PyList_Check(tmp)) {
3868 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3869 goto failed;
3870 }
3871 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003872 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003873 if (body == NULL) goto failed;
3874 for (i = 0; i < len; i++) {
3875 stmt_ty value;
3876 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3877 if (res != 0) goto failed;
3878 asdl_seq_SET(body, i, value);
3879 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003880 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003881 } else {
3882 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3883 return 1;
3884 }
3885 *out = Module(body, arena);
3886 if (*out == NULL) goto failed;
3887 return 0;
3888 }
3889 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3890 if (isinstance == -1) {
3891 return 1;
3892 }
3893 if (isinstance) {
3894 asdl_seq* body;
3895
3896 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3897 int res;
3898 Py_ssize_t len;
3899 Py_ssize_t i;
3900 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3901 if (tmp == NULL) goto failed;
3902 if (!PyList_Check(tmp)) {
3903 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3904 goto failed;
3905 }
3906 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003907 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003908 if (body == NULL) goto failed;
3909 for (i = 0; i < len; i++) {
3910 stmt_ty value;
3911 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3912 if (res != 0) goto failed;
3913 asdl_seq_SET(body, i, value);
3914 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003915 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003916 } else {
3917 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3918 return 1;
3919 }
3920 *out = Interactive(body, arena);
3921 if (*out == NULL) goto failed;
3922 return 0;
3923 }
3924 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3925 if (isinstance == -1) {
3926 return 1;
3927 }
3928 if (isinstance) {
3929 expr_ty body;
3930
3931 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3932 int res;
3933 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3934 if (tmp == NULL) goto failed;
3935 res = obj2ast_expr(tmp, &body, arena);
3936 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003937 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3940 return 1;
3941 }
3942 *out = Expression(body, arena);
3943 if (*out == NULL) goto failed;
3944 return 0;
3945 }
3946 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3947 if (isinstance == -1) {
3948 return 1;
3949 }
3950 if (isinstance) {
3951 asdl_seq* body;
3952
3953 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3954 int res;
3955 Py_ssize_t len;
3956 Py_ssize_t i;
3957 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3958 if (tmp == NULL) goto failed;
3959 if (!PyList_Check(tmp)) {
3960 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3961 goto failed;
3962 }
3963 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003964 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003965 if (body == NULL) goto failed;
3966 for (i = 0; i < len; i++) {
3967 stmt_ty value;
3968 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3969 if (res != 0) goto failed;
3970 asdl_seq_SET(body, i, value);
3971 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003972 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003973 } else {
3974 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3975 return 1;
3976 }
3977 *out = Suite(body, arena);
3978 if (*out == NULL) goto failed;
3979 return 0;
3980 }
3981
3982 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3983 failed:
3984 Py_XDECREF(tmp);
3985 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003986}
3987
3988int
3989obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3990{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003991 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003992
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003993 PyObject *tmp = NULL;
3994 int lineno;
3995 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003996
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003997 if (obj == Py_None) {
3998 *out = NULL;
3999 return 0;
4000 }
4001 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4002 int res;
4003 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4004 if (tmp == NULL) goto failed;
4005 res = obj2ast_int(tmp, &lineno, arena);
4006 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004007 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004008 } else {
4009 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004010 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 }
4012 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4013 int res;
4014 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4015 if (tmp == NULL) goto failed;
4016 res = obj2ast_int(tmp, &col_offset, arena);
4017 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004018 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004019 } else {
4020 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4021 return 1;
4022 }
4023 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4024 if (isinstance == -1) {
4025 return 1;
4026 }
4027 if (isinstance) {
4028 identifier name;
4029 arguments_ty args;
4030 asdl_seq* body;
4031 asdl_seq* decorator_list;
4032 expr_ty returns;
4033
4034 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4035 int res;
4036 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4037 if (tmp == NULL) goto failed;
4038 res = obj2ast_identifier(tmp, &name, arena);
4039 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004041 } else {
4042 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4043 return 1;
4044 }
4045 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4046 int res;
4047 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4048 if (tmp == NULL) goto failed;
4049 res = obj2ast_arguments(tmp, &args, arena);
4050 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004051 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004052 } else {
4053 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4054 return 1;
4055 }
4056 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4057 int res;
4058 Py_ssize_t len;
4059 Py_ssize_t i;
4060 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4061 if (tmp == NULL) goto failed;
4062 if (!PyList_Check(tmp)) {
4063 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4064 goto failed;
4065 }
4066 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004067 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004068 if (body == NULL) goto failed;
4069 for (i = 0; i < len; i++) {
4070 stmt_ty value;
4071 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4072 if (res != 0) goto failed;
4073 asdl_seq_SET(body, i, value);
4074 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004075 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004076 } else {
4077 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4078 return 1;
4079 }
4080 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4081 int res;
4082 Py_ssize_t len;
4083 Py_ssize_t i;
4084 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4085 if (tmp == NULL) goto failed;
4086 if (!PyList_Check(tmp)) {
4087 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4088 goto failed;
4089 }
4090 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004091 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004092 if (decorator_list == NULL) goto failed;
4093 for (i = 0; i < len; i++) {
4094 expr_ty value;
4095 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4096 if (res != 0) goto failed;
4097 asdl_seq_SET(decorator_list, i, value);
4098 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004099 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004100 } else {
4101 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4102 return 1;
4103 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004104 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004105 int res;
4106 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4107 if (tmp == NULL) goto failed;
4108 res = obj2ast_expr(tmp, &returns, arena);
4109 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004110 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004111 } else {
4112 returns = NULL;
4113 }
4114 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4115 col_offset, arena);
4116 if (*out == NULL) goto failed;
4117 return 0;
4118 }
Yury Selivanov75445082015-05-11 22:57:16 -04004119 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4120 if (isinstance == -1) {
4121 return 1;
4122 }
4123 if (isinstance) {
4124 identifier name;
4125 arguments_ty args;
4126 asdl_seq* body;
4127 asdl_seq* decorator_list;
4128 expr_ty returns;
4129
4130 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4131 int res;
4132 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4133 if (tmp == NULL) goto failed;
4134 res = obj2ast_identifier(tmp, &name, arena);
4135 if (res != 0) goto failed;
4136 Py_CLEAR(tmp);
4137 } else {
4138 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4139 return 1;
4140 }
4141 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4142 int res;
4143 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4144 if (tmp == NULL) goto failed;
4145 res = obj2ast_arguments(tmp, &args, arena);
4146 if (res != 0) goto failed;
4147 Py_CLEAR(tmp);
4148 } else {
4149 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4150 return 1;
4151 }
4152 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4153 int res;
4154 Py_ssize_t len;
4155 Py_ssize_t i;
4156 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4157 if (tmp == NULL) goto failed;
4158 if (!PyList_Check(tmp)) {
4159 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4160 goto failed;
4161 }
4162 len = PyList_GET_SIZE(tmp);
4163 body = _Py_asdl_seq_new(len, arena);
4164 if (body == NULL) goto failed;
4165 for (i = 0; i < len; i++) {
4166 stmt_ty value;
4167 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4168 if (res != 0) goto failed;
4169 asdl_seq_SET(body, i, value);
4170 }
4171 Py_CLEAR(tmp);
4172 } else {
4173 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4174 return 1;
4175 }
4176 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4177 int res;
4178 Py_ssize_t len;
4179 Py_ssize_t i;
4180 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4181 if (tmp == NULL) goto failed;
4182 if (!PyList_Check(tmp)) {
4183 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4184 goto failed;
4185 }
4186 len = PyList_GET_SIZE(tmp);
4187 decorator_list = _Py_asdl_seq_new(len, arena);
4188 if (decorator_list == NULL) goto failed;
4189 for (i = 0; i < len; i++) {
4190 expr_ty value;
4191 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4192 if (res != 0) goto failed;
4193 asdl_seq_SET(decorator_list, i, value);
4194 }
4195 Py_CLEAR(tmp);
4196 } else {
4197 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4198 return 1;
4199 }
4200 if (exists_not_none(obj, &PyId_returns)) {
4201 int res;
4202 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4203 if (tmp == NULL) goto failed;
4204 res = obj2ast_expr(tmp, &returns, arena);
4205 if (res != 0) goto failed;
4206 Py_CLEAR(tmp);
4207 } else {
4208 returns = NULL;
4209 }
4210 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4211 lineno, col_offset, arena);
4212 if (*out == NULL) goto failed;
4213 return 0;
4214 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004215 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4216 if (isinstance == -1) {
4217 return 1;
4218 }
4219 if (isinstance) {
4220 identifier name;
4221 asdl_seq* bases;
4222 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004223 asdl_seq* body;
4224 asdl_seq* decorator_list;
4225
4226 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4227 int res;
4228 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4229 if (tmp == NULL) goto failed;
4230 res = obj2ast_identifier(tmp, &name, arena);
4231 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004232 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004233 } else {
4234 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4235 return 1;
4236 }
4237 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4238 int res;
4239 Py_ssize_t len;
4240 Py_ssize_t i;
4241 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4242 if (tmp == NULL) goto failed;
4243 if (!PyList_Check(tmp)) {
4244 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4245 goto failed;
4246 }
4247 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004248 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004249 if (bases == NULL) goto failed;
4250 for (i = 0; i < len; i++) {
4251 expr_ty value;
4252 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4253 if (res != 0) goto failed;
4254 asdl_seq_SET(bases, i, value);
4255 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004256 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004257 } else {
4258 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4259 return 1;
4260 }
4261 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4262 int res;
4263 Py_ssize_t len;
4264 Py_ssize_t i;
4265 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4266 if (tmp == NULL) goto failed;
4267 if (!PyList_Check(tmp)) {
4268 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4269 goto failed;
4270 }
4271 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004272 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004273 if (keywords == NULL) goto failed;
4274 for (i = 0; i < len; i++) {
4275 keyword_ty value;
4276 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4277 if (res != 0) goto failed;
4278 asdl_seq_SET(keywords, i, value);
4279 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004280 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004281 } else {
4282 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4283 return 1;
4284 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004285 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4286 int res;
4287 Py_ssize_t len;
4288 Py_ssize_t i;
4289 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4290 if (tmp == NULL) goto failed;
4291 if (!PyList_Check(tmp)) {
4292 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4293 goto failed;
4294 }
4295 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004296 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004297 if (body == NULL) goto failed;
4298 for (i = 0; i < len; i++) {
4299 stmt_ty value;
4300 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4301 if (res != 0) goto failed;
4302 asdl_seq_SET(body, i, value);
4303 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004304 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004305 } else {
4306 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4307 return 1;
4308 }
4309 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4310 int res;
4311 Py_ssize_t len;
4312 Py_ssize_t i;
4313 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4314 if (tmp == NULL) goto failed;
4315 if (!PyList_Check(tmp)) {
4316 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4317 goto failed;
4318 }
4319 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004320 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004321 if (decorator_list == NULL) goto failed;
4322 for (i = 0; i < len; i++) {
4323 expr_ty value;
4324 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4325 if (res != 0) goto failed;
4326 asdl_seq_SET(decorator_list, i, value);
4327 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004328 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004329 } else {
4330 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4331 return 1;
4332 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004333 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4334 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004335 if (*out == NULL) goto failed;
4336 return 0;
4337 }
4338 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4339 if (isinstance == -1) {
4340 return 1;
4341 }
4342 if (isinstance) {
4343 expr_ty value;
4344
Victor Stinneree4b59c2013-07-27 00:01:35 +02004345 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004346 int res;
4347 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4348 if (tmp == NULL) goto failed;
4349 res = obj2ast_expr(tmp, &value, arena);
4350 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004351 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004352 } else {
4353 value = NULL;
4354 }
4355 *out = Return(value, lineno, col_offset, arena);
4356 if (*out == NULL) goto failed;
4357 return 0;
4358 }
4359 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4360 if (isinstance == -1) {
4361 return 1;
4362 }
4363 if (isinstance) {
4364 asdl_seq* targets;
4365
4366 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4367 int res;
4368 Py_ssize_t len;
4369 Py_ssize_t i;
4370 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4371 if (tmp == NULL) goto failed;
4372 if (!PyList_Check(tmp)) {
4373 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4374 goto failed;
4375 }
4376 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004377 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004378 if (targets == NULL) goto failed;
4379 for (i = 0; i < len; i++) {
4380 expr_ty value;
4381 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4382 if (res != 0) goto failed;
4383 asdl_seq_SET(targets, i, value);
4384 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004385 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004386 } else {
4387 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4388 return 1;
4389 }
4390 *out = Delete(targets, lineno, col_offset, arena);
4391 if (*out == NULL) goto failed;
4392 return 0;
4393 }
4394 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4395 if (isinstance == -1) {
4396 return 1;
4397 }
4398 if (isinstance) {
4399 asdl_seq* targets;
4400 expr_ty value;
4401
4402 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4403 int res;
4404 Py_ssize_t len;
4405 Py_ssize_t i;
4406 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4407 if (tmp == NULL) goto failed;
4408 if (!PyList_Check(tmp)) {
4409 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4410 goto failed;
4411 }
4412 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004413 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004414 if (targets == NULL) goto failed;
4415 for (i = 0; i < len; i++) {
4416 expr_ty value;
4417 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4418 if (res != 0) goto failed;
4419 asdl_seq_SET(targets, i, value);
4420 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004421 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004422 } else {
4423 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4424 return 1;
4425 }
4426 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4427 int res;
4428 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4429 if (tmp == NULL) goto failed;
4430 res = obj2ast_expr(tmp, &value, arena);
4431 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004432 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004433 } else {
4434 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4435 return 1;
4436 }
4437 *out = Assign(targets, value, lineno, col_offset, arena);
4438 if (*out == NULL) goto failed;
4439 return 0;
4440 }
4441 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4442 if (isinstance == -1) {
4443 return 1;
4444 }
4445 if (isinstance) {
4446 expr_ty target;
4447 operator_ty op;
4448 expr_ty value;
4449
4450 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4451 int res;
4452 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4453 if (tmp == NULL) goto failed;
4454 res = obj2ast_expr(tmp, &target, arena);
4455 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004456 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004457 } else {
4458 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4459 return 1;
4460 }
4461 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4462 int res;
4463 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4464 if (tmp == NULL) goto failed;
4465 res = obj2ast_operator(tmp, &op, arena);
4466 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004467 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004468 } else {
4469 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4470 return 1;
4471 }
4472 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4473 int res;
4474 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4475 if (tmp == NULL) goto failed;
4476 res = obj2ast_expr(tmp, &value, arena);
4477 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004478 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004479 } else {
4480 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4481 return 1;
4482 }
4483 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4484 if (*out == NULL) goto failed;
4485 return 0;
4486 }
4487 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4488 if (isinstance == -1) {
4489 return 1;
4490 }
4491 if (isinstance) {
4492 expr_ty target;
4493 expr_ty iter;
4494 asdl_seq* body;
4495 asdl_seq* orelse;
4496
4497 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4498 int res;
4499 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4500 if (tmp == NULL) goto failed;
4501 res = obj2ast_expr(tmp, &target, arena);
4502 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004503 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004504 } else {
4505 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4506 return 1;
4507 }
4508 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4509 int res;
4510 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4511 if (tmp == NULL) goto failed;
4512 res = obj2ast_expr(tmp, &iter, arena);
4513 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004514 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004515 } else {
4516 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4517 return 1;
4518 }
4519 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4520 int res;
4521 Py_ssize_t len;
4522 Py_ssize_t i;
4523 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4524 if (tmp == NULL) goto failed;
4525 if (!PyList_Check(tmp)) {
4526 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4527 goto failed;
4528 }
4529 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004530 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004531 if (body == NULL) goto failed;
4532 for (i = 0; i < len; i++) {
4533 stmt_ty value;
4534 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4535 if (res != 0) goto failed;
4536 asdl_seq_SET(body, i, value);
4537 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004539 } else {
4540 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4541 return 1;
4542 }
4543 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4544 int res;
4545 Py_ssize_t len;
4546 Py_ssize_t i;
4547 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4548 if (tmp == NULL) goto failed;
4549 if (!PyList_Check(tmp)) {
4550 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4551 goto failed;
4552 }
4553 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004554 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004555 if (orelse == NULL) goto failed;
4556 for (i = 0; i < len; i++) {
4557 stmt_ty value;
4558 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4559 if (res != 0) goto failed;
4560 asdl_seq_SET(orelse, i, value);
4561 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004562 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004563 } else {
4564 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4565 return 1;
4566 }
4567 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4568 if (*out == NULL) goto failed;
4569 return 0;
4570 }
Yury Selivanov75445082015-05-11 22:57:16 -04004571 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4572 if (isinstance == -1) {
4573 return 1;
4574 }
4575 if (isinstance) {
4576 expr_ty target;
4577 expr_ty iter;
4578 asdl_seq* body;
4579 asdl_seq* orelse;
4580
4581 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4582 int res;
4583 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4584 if (tmp == NULL) goto failed;
4585 res = obj2ast_expr(tmp, &target, arena);
4586 if (res != 0) goto failed;
4587 Py_CLEAR(tmp);
4588 } else {
4589 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4590 return 1;
4591 }
4592 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4593 int res;
4594 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4595 if (tmp == NULL) goto failed;
4596 res = obj2ast_expr(tmp, &iter, arena);
4597 if (res != 0) goto failed;
4598 Py_CLEAR(tmp);
4599 } else {
4600 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4601 return 1;
4602 }
4603 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4604 int res;
4605 Py_ssize_t len;
4606 Py_ssize_t i;
4607 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4608 if (tmp == NULL) goto failed;
4609 if (!PyList_Check(tmp)) {
4610 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4611 goto failed;
4612 }
4613 len = PyList_GET_SIZE(tmp);
4614 body = _Py_asdl_seq_new(len, arena);
4615 if (body == NULL) goto failed;
4616 for (i = 0; i < len; i++) {
4617 stmt_ty value;
4618 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4619 if (res != 0) goto failed;
4620 asdl_seq_SET(body, i, value);
4621 }
4622 Py_CLEAR(tmp);
4623 } else {
4624 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4625 return 1;
4626 }
4627 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4628 int res;
4629 Py_ssize_t len;
4630 Py_ssize_t i;
4631 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4632 if (tmp == NULL) goto failed;
4633 if (!PyList_Check(tmp)) {
4634 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4635 goto failed;
4636 }
4637 len = PyList_GET_SIZE(tmp);
4638 orelse = _Py_asdl_seq_new(len, arena);
4639 if (orelse == NULL) goto failed;
4640 for (i = 0; i < len; i++) {
4641 stmt_ty value;
4642 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4643 if (res != 0) goto failed;
4644 asdl_seq_SET(orelse, i, value);
4645 }
4646 Py_CLEAR(tmp);
4647 } else {
4648 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4649 return 1;
4650 }
4651 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4652 if (*out == NULL) goto failed;
4653 return 0;
4654 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004655 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4656 if (isinstance == -1) {
4657 return 1;
4658 }
4659 if (isinstance) {
4660 expr_ty test;
4661 asdl_seq* body;
4662 asdl_seq* orelse;
4663
4664 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4665 int res;
4666 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4667 if (tmp == NULL) goto failed;
4668 res = obj2ast_expr(tmp, &test, arena);
4669 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004670 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004671 } else {
4672 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4673 return 1;
4674 }
4675 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4676 int res;
4677 Py_ssize_t len;
4678 Py_ssize_t i;
4679 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4680 if (tmp == NULL) goto failed;
4681 if (!PyList_Check(tmp)) {
4682 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4683 goto failed;
4684 }
4685 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004686 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004687 if (body == NULL) goto failed;
4688 for (i = 0; i < len; i++) {
4689 stmt_ty value;
4690 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4691 if (res != 0) goto failed;
4692 asdl_seq_SET(body, i, value);
4693 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004694 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004695 } else {
4696 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4697 return 1;
4698 }
4699 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4700 int res;
4701 Py_ssize_t len;
4702 Py_ssize_t i;
4703 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4704 if (tmp == NULL) goto failed;
4705 if (!PyList_Check(tmp)) {
4706 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4707 goto failed;
4708 }
4709 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004710 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004711 if (orelse == NULL) goto failed;
4712 for (i = 0; i < len; i++) {
4713 stmt_ty value;
4714 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4715 if (res != 0) goto failed;
4716 asdl_seq_SET(orelse, i, value);
4717 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004719 } else {
4720 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4721 return 1;
4722 }
4723 *out = While(test, body, orelse, lineno, col_offset, arena);
4724 if (*out == NULL) goto failed;
4725 return 0;
4726 }
4727 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4728 if (isinstance == -1) {
4729 return 1;
4730 }
4731 if (isinstance) {
4732 expr_ty test;
4733 asdl_seq* body;
4734 asdl_seq* orelse;
4735
4736 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4737 int res;
4738 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4739 if (tmp == NULL) goto failed;
4740 res = obj2ast_expr(tmp, &test, arena);
4741 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004742 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004743 } else {
4744 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4745 return 1;
4746 }
4747 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4748 int res;
4749 Py_ssize_t len;
4750 Py_ssize_t i;
4751 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4752 if (tmp == NULL) goto failed;
4753 if (!PyList_Check(tmp)) {
4754 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4755 goto failed;
4756 }
4757 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004758 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004759 if (body == NULL) goto failed;
4760 for (i = 0; i < len; i++) {
4761 stmt_ty value;
4762 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4763 if (res != 0) goto failed;
4764 asdl_seq_SET(body, i, value);
4765 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004766 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004767 } else {
4768 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4769 return 1;
4770 }
4771 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4772 int res;
4773 Py_ssize_t len;
4774 Py_ssize_t i;
4775 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4776 if (tmp == NULL) goto failed;
4777 if (!PyList_Check(tmp)) {
4778 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4779 goto failed;
4780 }
4781 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004782 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004783 if (orelse == NULL) goto failed;
4784 for (i = 0; i < len; i++) {
4785 stmt_ty value;
4786 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4787 if (res != 0) goto failed;
4788 asdl_seq_SET(orelse, i, value);
4789 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004790 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004791 } else {
4792 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4793 return 1;
4794 }
4795 *out = If(test, body, orelse, lineno, col_offset, arena);
4796 if (*out == NULL) goto failed;
4797 return 0;
4798 }
4799 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4800 if (isinstance == -1) {
4801 return 1;
4802 }
4803 if (isinstance) {
4804 asdl_seq* items;
4805 asdl_seq* body;
4806
4807 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4808 int res;
4809 Py_ssize_t len;
4810 Py_ssize_t i;
4811 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4812 if (tmp == NULL) goto failed;
4813 if (!PyList_Check(tmp)) {
4814 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4815 goto failed;
4816 }
4817 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004818 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004819 if (items == NULL) goto failed;
4820 for (i = 0; i < len; i++) {
4821 withitem_ty value;
4822 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4823 if (res != 0) goto failed;
4824 asdl_seq_SET(items, i, value);
4825 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004826 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004827 } else {
4828 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4829 return 1;
4830 }
4831 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4832 int res;
4833 Py_ssize_t len;
4834 Py_ssize_t i;
4835 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4836 if (tmp == NULL) goto failed;
4837 if (!PyList_Check(tmp)) {
4838 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4839 goto failed;
4840 }
4841 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004842 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004843 if (body == NULL) goto failed;
4844 for (i = 0; i < len; i++) {
4845 stmt_ty value;
4846 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4847 if (res != 0) goto failed;
4848 asdl_seq_SET(body, i, value);
4849 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004850 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004851 } else {
4852 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4853 return 1;
4854 }
4855 *out = With(items, body, lineno, col_offset, arena);
4856 if (*out == NULL) goto failed;
4857 return 0;
4858 }
Yury Selivanov75445082015-05-11 22:57:16 -04004859 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4860 if (isinstance == -1) {
4861 return 1;
4862 }
4863 if (isinstance) {
4864 asdl_seq* items;
4865 asdl_seq* body;
4866
4867 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4868 int res;
4869 Py_ssize_t len;
4870 Py_ssize_t i;
4871 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4872 if (tmp == NULL) goto failed;
4873 if (!PyList_Check(tmp)) {
4874 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4875 goto failed;
4876 }
4877 len = PyList_GET_SIZE(tmp);
4878 items = _Py_asdl_seq_new(len, arena);
4879 if (items == NULL) goto failed;
4880 for (i = 0; i < len; i++) {
4881 withitem_ty value;
4882 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4883 if (res != 0) goto failed;
4884 asdl_seq_SET(items, i, value);
4885 }
4886 Py_CLEAR(tmp);
4887 } else {
4888 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4889 return 1;
4890 }
4891 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4892 int res;
4893 Py_ssize_t len;
4894 Py_ssize_t i;
4895 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4896 if (tmp == NULL) goto failed;
4897 if (!PyList_Check(tmp)) {
4898 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4899 goto failed;
4900 }
4901 len = PyList_GET_SIZE(tmp);
4902 body = _Py_asdl_seq_new(len, arena);
4903 if (body == NULL) goto failed;
4904 for (i = 0; i < len; i++) {
4905 stmt_ty value;
4906 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4907 if (res != 0) goto failed;
4908 asdl_seq_SET(body, i, value);
4909 }
4910 Py_CLEAR(tmp);
4911 } else {
4912 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4913 return 1;
4914 }
4915 *out = AsyncWith(items, body, lineno, col_offset, arena);
4916 if (*out == NULL) goto failed;
4917 return 0;
4918 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004919 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4920 if (isinstance == -1) {
4921 return 1;
4922 }
4923 if (isinstance) {
4924 expr_ty exc;
4925 expr_ty cause;
4926
Victor Stinneree4b59c2013-07-27 00:01:35 +02004927 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004928 int res;
4929 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4930 if (tmp == NULL) goto failed;
4931 res = obj2ast_expr(tmp, &exc, arena);
4932 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004933 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004934 } else {
4935 exc = NULL;
4936 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004937 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004938 int res;
4939 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4940 if (tmp == NULL) goto failed;
4941 res = obj2ast_expr(tmp, &cause, arena);
4942 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004943 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004944 } else {
4945 cause = NULL;
4946 }
4947 *out = Raise(exc, cause, lineno, col_offset, arena);
4948 if (*out == NULL) goto failed;
4949 return 0;
4950 }
4951 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4952 if (isinstance == -1) {
4953 return 1;
4954 }
4955 if (isinstance) {
4956 asdl_seq* body;
4957 asdl_seq* handlers;
4958 asdl_seq* orelse;
4959 asdl_seq* finalbody;
4960
4961 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4962 int res;
4963 Py_ssize_t len;
4964 Py_ssize_t i;
4965 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4966 if (tmp == NULL) goto failed;
4967 if (!PyList_Check(tmp)) {
4968 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4969 goto failed;
4970 }
4971 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004972 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004973 if (body == NULL) goto failed;
4974 for (i = 0; i < len; i++) {
4975 stmt_ty value;
4976 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4977 if (res != 0) goto failed;
4978 asdl_seq_SET(body, i, value);
4979 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004980 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004981 } else {
4982 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4983 return 1;
4984 }
4985 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4986 int res;
4987 Py_ssize_t len;
4988 Py_ssize_t i;
4989 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4990 if (tmp == NULL) goto failed;
4991 if (!PyList_Check(tmp)) {
4992 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4993 goto failed;
4994 }
4995 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004996 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004997 if (handlers == NULL) goto failed;
4998 for (i = 0; i < len; i++) {
4999 excepthandler_ty value;
5000 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5001 if (res != 0) goto failed;
5002 asdl_seq_SET(handlers, i, value);
5003 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005004 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005005 } else {
5006 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5007 return 1;
5008 }
5009 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5010 int res;
5011 Py_ssize_t len;
5012 Py_ssize_t i;
5013 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5014 if (tmp == NULL) goto failed;
5015 if (!PyList_Check(tmp)) {
5016 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5017 goto failed;
5018 }
5019 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005020 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005021 if (orelse == NULL) goto failed;
5022 for (i = 0; i < len; i++) {
5023 stmt_ty value;
5024 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5025 if (res != 0) goto failed;
5026 asdl_seq_SET(orelse, i, value);
5027 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005028 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005029 } else {
5030 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5031 return 1;
5032 }
5033 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5034 int res;
5035 Py_ssize_t len;
5036 Py_ssize_t i;
5037 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5038 if (tmp == NULL) goto failed;
5039 if (!PyList_Check(tmp)) {
5040 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5041 goto failed;
5042 }
5043 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005044 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005045 if (finalbody == NULL) goto failed;
5046 for (i = 0; i < len; i++) {
5047 stmt_ty value;
5048 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5049 if (res != 0) goto failed;
5050 asdl_seq_SET(finalbody, i, value);
5051 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005052 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005053 } else {
5054 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5055 return 1;
5056 }
5057 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5058 arena);
5059 if (*out == NULL) goto failed;
5060 return 0;
5061 }
5062 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5063 if (isinstance == -1) {
5064 return 1;
5065 }
5066 if (isinstance) {
5067 expr_ty test;
5068 expr_ty msg;
5069
5070 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5071 int res;
5072 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5073 if (tmp == NULL) goto failed;
5074 res = obj2ast_expr(tmp, &test, arena);
5075 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005076 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005077 } else {
5078 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5079 return 1;
5080 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005081 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005082 int res;
5083 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5084 if (tmp == NULL) goto failed;
5085 res = obj2ast_expr(tmp, &msg, arena);
5086 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005087 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005088 } else {
5089 msg = NULL;
5090 }
5091 *out = Assert(test, msg, lineno, col_offset, arena);
5092 if (*out == NULL) goto failed;
5093 return 0;
5094 }
5095 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5096 if (isinstance == -1) {
5097 return 1;
5098 }
5099 if (isinstance) {
5100 asdl_seq* names;
5101
5102 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5103 int res;
5104 Py_ssize_t len;
5105 Py_ssize_t i;
5106 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5107 if (tmp == NULL) goto failed;
5108 if (!PyList_Check(tmp)) {
5109 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5110 goto failed;
5111 }
5112 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005113 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005114 if (names == NULL) goto failed;
5115 for (i = 0; i < len; i++) {
5116 alias_ty value;
5117 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5118 if (res != 0) goto failed;
5119 asdl_seq_SET(names, i, value);
5120 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005121 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005122 } else {
5123 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5124 return 1;
5125 }
5126 *out = Import(names, lineno, col_offset, arena);
5127 if (*out == NULL) goto failed;
5128 return 0;
5129 }
5130 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5131 if (isinstance == -1) {
5132 return 1;
5133 }
5134 if (isinstance) {
5135 identifier module;
5136 asdl_seq* names;
5137 int level;
5138
Victor Stinneree4b59c2013-07-27 00:01:35 +02005139 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005140 int res;
5141 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5142 if (tmp == NULL) goto failed;
5143 res = obj2ast_identifier(tmp, &module, arena);
5144 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005145 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005146 } else {
5147 module = NULL;
5148 }
5149 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5150 int res;
5151 Py_ssize_t len;
5152 Py_ssize_t i;
5153 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5154 if (tmp == NULL) goto failed;
5155 if (!PyList_Check(tmp)) {
5156 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5157 goto failed;
5158 }
5159 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005160 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005161 if (names == NULL) goto failed;
5162 for (i = 0; i < len; i++) {
5163 alias_ty value;
5164 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5165 if (res != 0) goto failed;
5166 asdl_seq_SET(names, i, value);
5167 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005168 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005169 } else {
5170 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5171 return 1;
5172 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005173 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005174 int res;
5175 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5176 if (tmp == NULL) goto failed;
5177 res = obj2ast_int(tmp, &level, arena);
5178 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005179 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005180 } else {
5181 level = 0;
5182 }
5183 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5184 if (*out == NULL) goto failed;
5185 return 0;
5186 }
5187 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5188 if (isinstance == -1) {
5189 return 1;
5190 }
5191 if (isinstance) {
5192 asdl_seq* names;
5193
5194 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5195 int res;
5196 Py_ssize_t len;
5197 Py_ssize_t i;
5198 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5199 if (tmp == NULL) goto failed;
5200 if (!PyList_Check(tmp)) {
5201 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5202 goto failed;
5203 }
5204 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005205 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005206 if (names == NULL) goto failed;
5207 for (i = 0; i < len; i++) {
5208 identifier value;
5209 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5210 if (res != 0) goto failed;
5211 asdl_seq_SET(names, i, value);
5212 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005213 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005214 } else {
5215 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5216 return 1;
5217 }
5218 *out = Global(names, lineno, col_offset, arena);
5219 if (*out == NULL) goto failed;
5220 return 0;
5221 }
5222 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5223 if (isinstance == -1) {
5224 return 1;
5225 }
5226 if (isinstance) {
5227 asdl_seq* names;
5228
5229 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5230 int res;
5231 Py_ssize_t len;
5232 Py_ssize_t i;
5233 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5234 if (tmp == NULL) goto failed;
5235 if (!PyList_Check(tmp)) {
5236 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5237 goto failed;
5238 }
5239 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005240 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005241 if (names == NULL) goto failed;
5242 for (i = 0; i < len; i++) {
5243 identifier value;
5244 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5245 if (res != 0) goto failed;
5246 asdl_seq_SET(names, i, value);
5247 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005248 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005249 } else {
5250 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5251 return 1;
5252 }
5253 *out = Nonlocal(names, lineno, col_offset, arena);
5254 if (*out == NULL) goto failed;
5255 return 0;
5256 }
5257 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5258 if (isinstance == -1) {
5259 return 1;
5260 }
5261 if (isinstance) {
5262 expr_ty value;
5263
5264 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5265 int res;
5266 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5267 if (tmp == NULL) goto failed;
5268 res = obj2ast_expr(tmp, &value, arena);
5269 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005270 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005271 } else {
5272 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5273 return 1;
5274 }
5275 *out = Expr(value, lineno, col_offset, arena);
5276 if (*out == NULL) goto failed;
5277 return 0;
5278 }
5279 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5280 if (isinstance == -1) {
5281 return 1;
5282 }
5283 if (isinstance) {
5284
5285 *out = Pass(lineno, col_offset, arena);
5286 if (*out == NULL) goto failed;
5287 return 0;
5288 }
5289 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5290 if (isinstance == -1) {
5291 return 1;
5292 }
5293 if (isinstance) {
5294
5295 *out = Break(lineno, col_offset, arena);
5296 if (*out == NULL) goto failed;
5297 return 0;
5298 }
5299 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5300 if (isinstance == -1) {
5301 return 1;
5302 }
5303 if (isinstance) {
5304
5305 *out = Continue(lineno, col_offset, arena);
5306 if (*out == NULL) goto failed;
5307 return 0;
5308 }
5309
5310 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5311 failed:
5312 Py_XDECREF(tmp);
5313 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005314}
5315
5316int
5317obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5318{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005319 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005320
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005321 PyObject *tmp = NULL;
5322 int lineno;
5323 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005324
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005325 if (obj == Py_None) {
5326 *out = NULL;
5327 return 0;
5328 }
5329 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5330 int res;
5331 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5332 if (tmp == NULL) goto failed;
5333 res = obj2ast_int(tmp, &lineno, 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 \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005338 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005339 }
5340 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5341 int res;
5342 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5343 if (tmp == NULL) goto failed;
5344 res = obj2ast_int(tmp, &col_offset, 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 \"col_offset\" missing from expr");
5349 return 1;
5350 }
5351 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5352 if (isinstance == -1) {
5353 return 1;
5354 }
5355 if (isinstance) {
5356 boolop_ty op;
5357 asdl_seq* values;
5358
5359 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5360 int res;
5361 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5362 if (tmp == NULL) goto failed;
5363 res = obj2ast_boolop(tmp, &op, arena);
5364 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005365 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005366 } else {
5367 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5368 return 1;
5369 }
5370 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5371 int res;
5372 Py_ssize_t len;
5373 Py_ssize_t i;
5374 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5375 if (tmp == NULL) goto failed;
5376 if (!PyList_Check(tmp)) {
5377 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5378 goto failed;
5379 }
5380 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005381 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005382 if (values == NULL) goto failed;
5383 for (i = 0; i < len; i++) {
5384 expr_ty value;
5385 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5386 if (res != 0) goto failed;
5387 asdl_seq_SET(values, i, value);
5388 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005389 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005390 } else {
5391 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5392 return 1;
5393 }
5394 *out = BoolOp(op, values, lineno, col_offset, arena);
5395 if (*out == NULL) goto failed;
5396 return 0;
5397 }
5398 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5399 if (isinstance == -1) {
5400 return 1;
5401 }
5402 if (isinstance) {
5403 expr_ty left;
5404 operator_ty op;
5405 expr_ty right;
5406
5407 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5408 int res;
5409 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5410 if (tmp == NULL) goto failed;
5411 res = obj2ast_expr(tmp, &left, arena);
5412 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005413 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005414 } else {
5415 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5416 return 1;
5417 }
5418 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5419 int res;
5420 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5421 if (tmp == NULL) goto failed;
5422 res = obj2ast_operator(tmp, &op, arena);
5423 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005424 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005425 } else {
5426 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5427 return 1;
5428 }
5429 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5430 int res;
5431 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5432 if (tmp == NULL) goto failed;
5433 res = obj2ast_expr(tmp, &right, arena);
5434 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005436 } else {
5437 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5438 return 1;
5439 }
5440 *out = BinOp(left, op, right, lineno, col_offset, arena);
5441 if (*out == NULL) goto failed;
5442 return 0;
5443 }
5444 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5445 if (isinstance == -1) {
5446 return 1;
5447 }
5448 if (isinstance) {
5449 unaryop_ty op;
5450 expr_ty operand;
5451
5452 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5453 int res;
5454 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5455 if (tmp == NULL) goto failed;
5456 res = obj2ast_unaryop(tmp, &op, arena);
5457 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005458 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005459 } else {
5460 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5461 return 1;
5462 }
5463 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5464 int res;
5465 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5466 if (tmp == NULL) goto failed;
5467 res = obj2ast_expr(tmp, &operand, arena);
5468 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005469 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005470 } else {
5471 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5472 return 1;
5473 }
5474 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5475 if (*out == NULL) goto failed;
5476 return 0;
5477 }
5478 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5479 if (isinstance == -1) {
5480 return 1;
5481 }
5482 if (isinstance) {
5483 arguments_ty args;
5484 expr_ty body;
5485
5486 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5487 int res;
5488 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5489 if (tmp == NULL) goto failed;
5490 res = obj2ast_arguments(tmp, &args, arena);
5491 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005492 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005493 } else {
5494 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5495 return 1;
5496 }
5497 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5498 int res;
5499 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5500 if (tmp == NULL) goto failed;
5501 res = obj2ast_expr(tmp, &body, arena);
5502 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005503 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005504 } else {
5505 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5506 return 1;
5507 }
5508 *out = Lambda(args, body, lineno, col_offset, arena);
5509 if (*out == NULL) goto failed;
5510 return 0;
5511 }
5512 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5513 if (isinstance == -1) {
5514 return 1;
5515 }
5516 if (isinstance) {
5517 expr_ty test;
5518 expr_ty body;
5519 expr_ty orelse;
5520
5521 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5522 int res;
5523 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5524 if (tmp == NULL) goto failed;
5525 res = obj2ast_expr(tmp, &test, arena);
5526 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005527 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005528 } else {
5529 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5530 return 1;
5531 }
5532 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5533 int res;
5534 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5535 if (tmp == NULL) goto failed;
5536 res = obj2ast_expr(tmp, &body, arena);
5537 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005538 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005539 } else {
5540 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5541 return 1;
5542 }
5543 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5544 int res;
5545 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5546 if (tmp == NULL) goto failed;
5547 res = obj2ast_expr(tmp, &orelse, arena);
5548 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005549 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005550 } else {
5551 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5552 return 1;
5553 }
5554 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5555 if (*out == NULL) goto failed;
5556 return 0;
5557 }
5558 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5559 if (isinstance == -1) {
5560 return 1;
5561 }
5562 if (isinstance) {
5563 asdl_seq* keys;
5564 asdl_seq* values;
5565
5566 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5567 int res;
5568 Py_ssize_t len;
5569 Py_ssize_t i;
5570 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5571 if (tmp == NULL) goto failed;
5572 if (!PyList_Check(tmp)) {
5573 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5574 goto failed;
5575 }
5576 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005577 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005578 if (keys == NULL) goto failed;
5579 for (i = 0; i < len; i++) {
5580 expr_ty value;
5581 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5582 if (res != 0) goto failed;
5583 asdl_seq_SET(keys, i, value);
5584 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005586 } else {
5587 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5588 return 1;
5589 }
5590 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5591 int res;
5592 Py_ssize_t len;
5593 Py_ssize_t i;
5594 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5595 if (tmp == NULL) goto failed;
5596 if (!PyList_Check(tmp)) {
5597 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5598 goto failed;
5599 }
5600 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005601 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005602 if (values == NULL) goto failed;
5603 for (i = 0; i < len; i++) {
5604 expr_ty value;
5605 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5606 if (res != 0) goto failed;
5607 asdl_seq_SET(values, i, value);
5608 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005609 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005610 } else {
5611 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5612 return 1;
5613 }
5614 *out = Dict(keys, values, lineno, col_offset, arena);
5615 if (*out == NULL) goto failed;
5616 return 0;
5617 }
5618 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5619 if (isinstance == -1) {
5620 return 1;
5621 }
5622 if (isinstance) {
5623 asdl_seq* elts;
5624
5625 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5626 int res;
5627 Py_ssize_t len;
5628 Py_ssize_t i;
5629 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5630 if (tmp == NULL) goto failed;
5631 if (!PyList_Check(tmp)) {
5632 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5633 goto failed;
5634 }
5635 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005636 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005637 if (elts == NULL) goto failed;
5638 for (i = 0; i < len; i++) {
5639 expr_ty value;
5640 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5641 if (res != 0) goto failed;
5642 asdl_seq_SET(elts, i, value);
5643 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005644 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005645 } else {
5646 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5647 return 1;
5648 }
5649 *out = Set(elts, lineno, col_offset, arena);
5650 if (*out == NULL) goto failed;
5651 return 0;
5652 }
5653 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5654 if (isinstance == -1) {
5655 return 1;
5656 }
5657 if (isinstance) {
5658 expr_ty elt;
5659 asdl_seq* generators;
5660
5661 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5662 int res;
5663 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5664 if (tmp == NULL) goto failed;
5665 res = obj2ast_expr(tmp, &elt, arena);
5666 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005667 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005668 } else {
5669 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5670 return 1;
5671 }
5672 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5673 int res;
5674 Py_ssize_t len;
5675 Py_ssize_t i;
5676 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5677 if (tmp == NULL) goto failed;
5678 if (!PyList_Check(tmp)) {
5679 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5680 goto failed;
5681 }
5682 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005683 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005684 if (generators == NULL) goto failed;
5685 for (i = 0; i < len; i++) {
5686 comprehension_ty value;
5687 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5688 if (res != 0) goto failed;
5689 asdl_seq_SET(generators, i, value);
5690 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005691 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005692 } else {
5693 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5694 return 1;
5695 }
5696 *out = ListComp(elt, generators, lineno, col_offset, arena);
5697 if (*out == NULL) goto failed;
5698 return 0;
5699 }
5700 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5701 if (isinstance == -1) {
5702 return 1;
5703 }
5704 if (isinstance) {
5705 expr_ty elt;
5706 asdl_seq* generators;
5707
5708 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5709 int res;
5710 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5711 if (tmp == NULL) goto failed;
5712 res = obj2ast_expr(tmp, &elt, arena);
5713 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005714 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005715 } else {
5716 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5717 return 1;
5718 }
5719 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5720 int res;
5721 Py_ssize_t len;
5722 Py_ssize_t i;
5723 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5724 if (tmp == NULL) goto failed;
5725 if (!PyList_Check(tmp)) {
5726 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5727 goto failed;
5728 }
5729 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005730 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005731 if (generators == NULL) goto failed;
5732 for (i = 0; i < len; i++) {
5733 comprehension_ty value;
5734 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5735 if (res != 0) goto failed;
5736 asdl_seq_SET(generators, i, value);
5737 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005738 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005739 } else {
5740 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5741 return 1;
5742 }
5743 *out = SetComp(elt, generators, lineno, col_offset, arena);
5744 if (*out == NULL) goto failed;
5745 return 0;
5746 }
5747 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5748 if (isinstance == -1) {
5749 return 1;
5750 }
5751 if (isinstance) {
5752 expr_ty key;
5753 expr_ty value;
5754 asdl_seq* generators;
5755
5756 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5757 int res;
5758 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5759 if (tmp == NULL) goto failed;
5760 res = obj2ast_expr(tmp, &key, arena);
5761 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005762 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005763 } else {
5764 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5765 return 1;
5766 }
5767 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5768 int res;
5769 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5770 if (tmp == NULL) goto failed;
5771 res = obj2ast_expr(tmp, &value, arena);
5772 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005773 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005774 } else {
5775 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5776 return 1;
5777 }
5778 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5779 int res;
5780 Py_ssize_t len;
5781 Py_ssize_t i;
5782 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5783 if (tmp == NULL) goto failed;
5784 if (!PyList_Check(tmp)) {
5785 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5786 goto failed;
5787 }
5788 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005789 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005790 if (generators == NULL) goto failed;
5791 for (i = 0; i < len; i++) {
5792 comprehension_ty value;
5793 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5794 if (res != 0) goto failed;
5795 asdl_seq_SET(generators, i, value);
5796 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005797 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005798 } else {
5799 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5800 return 1;
5801 }
5802 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5803 if (*out == NULL) goto failed;
5804 return 0;
5805 }
5806 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5807 if (isinstance == -1) {
5808 return 1;
5809 }
5810 if (isinstance) {
5811 expr_ty elt;
5812 asdl_seq* generators;
5813
5814 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5815 int res;
5816 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5817 if (tmp == NULL) goto failed;
5818 res = obj2ast_expr(tmp, &elt, arena);
5819 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005820 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005821 } else {
5822 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5823 return 1;
5824 }
5825 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5826 int res;
5827 Py_ssize_t len;
5828 Py_ssize_t i;
5829 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5830 if (tmp == NULL) goto failed;
5831 if (!PyList_Check(tmp)) {
5832 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5833 goto failed;
5834 }
5835 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005836 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005837 if (generators == NULL) goto failed;
5838 for (i = 0; i < len; i++) {
5839 comprehension_ty value;
5840 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5841 if (res != 0) goto failed;
5842 asdl_seq_SET(generators, i, value);
5843 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005844 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005845 } else {
5846 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5847 return 1;
5848 }
5849 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5850 if (*out == NULL) goto failed;
5851 return 0;
5852 }
Yury Selivanov75445082015-05-11 22:57:16 -04005853 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5854 if (isinstance == -1) {
5855 return 1;
5856 }
5857 if (isinstance) {
5858 expr_ty value;
5859
5860 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5861 int res;
5862 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5863 if (tmp == NULL) goto failed;
5864 res = obj2ast_expr(tmp, &value, arena);
5865 if (res != 0) goto failed;
5866 Py_CLEAR(tmp);
5867 } else {
5868 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5869 return 1;
5870 }
5871 *out = Await(value, lineno, col_offset, arena);
5872 if (*out == NULL) goto failed;
5873 return 0;
5874 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005875 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5876 if (isinstance == -1) {
5877 return 1;
5878 }
5879 if (isinstance) {
5880 expr_ty value;
5881
Victor Stinneree4b59c2013-07-27 00:01:35 +02005882 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005883 int res;
5884 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5885 if (tmp == NULL) goto failed;
5886 res = obj2ast_expr(tmp, &value, arena);
5887 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005888 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005889 } else {
5890 value = NULL;
5891 }
5892 *out = Yield(value, lineno, col_offset, arena);
5893 if (*out == NULL) goto failed;
5894 return 0;
5895 }
5896 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5897 if (isinstance == -1) {
5898 return 1;
5899 }
5900 if (isinstance) {
5901 expr_ty value;
5902
5903 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5904 int res;
5905 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5906 if (tmp == NULL) goto failed;
5907 res = obj2ast_expr(tmp, &value, arena);
5908 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005909 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005910 } else {
5911 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5912 return 1;
5913 }
5914 *out = YieldFrom(value, lineno, col_offset, arena);
5915 if (*out == NULL) goto failed;
5916 return 0;
5917 }
5918 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5919 if (isinstance == -1) {
5920 return 1;
5921 }
5922 if (isinstance) {
5923 expr_ty left;
5924 asdl_int_seq* ops;
5925 asdl_seq* comparators;
5926
5927 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5928 int res;
5929 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5930 if (tmp == NULL) goto failed;
5931 res = obj2ast_expr(tmp, &left, arena);
5932 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005933 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005934 } else {
5935 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5936 return 1;
5937 }
5938 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5939 int res;
5940 Py_ssize_t len;
5941 Py_ssize_t i;
5942 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5943 if (tmp == NULL) goto failed;
5944 if (!PyList_Check(tmp)) {
5945 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5946 goto failed;
5947 }
5948 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005949 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005950 if (ops == NULL) goto failed;
5951 for (i = 0; i < len; i++) {
5952 cmpop_ty value;
5953 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5954 if (res != 0) goto failed;
5955 asdl_seq_SET(ops, i, value);
5956 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005957 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005958 } else {
5959 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5960 return 1;
5961 }
5962 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5963 int res;
5964 Py_ssize_t len;
5965 Py_ssize_t i;
5966 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5967 if (tmp == NULL) goto failed;
5968 if (!PyList_Check(tmp)) {
5969 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5970 goto failed;
5971 }
5972 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005973 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005974 if (comparators == NULL) goto failed;
5975 for (i = 0; i < len; i++) {
5976 expr_ty value;
5977 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5978 if (res != 0) goto failed;
5979 asdl_seq_SET(comparators, i, value);
5980 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005981 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005982 } else {
5983 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5984 return 1;
5985 }
5986 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5987 if (*out == NULL) goto failed;
5988 return 0;
5989 }
5990 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5991 if (isinstance == -1) {
5992 return 1;
5993 }
5994 if (isinstance) {
5995 expr_ty func;
5996 asdl_seq* args;
5997 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005998
5999 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6000 int res;
6001 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6002 if (tmp == NULL) goto failed;
6003 res = obj2ast_expr(tmp, &func, arena);
6004 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006005 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006006 } else {
6007 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6008 return 1;
6009 }
6010 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6011 int res;
6012 Py_ssize_t len;
6013 Py_ssize_t i;
6014 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6015 if (tmp == NULL) goto failed;
6016 if (!PyList_Check(tmp)) {
6017 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6018 goto failed;
6019 }
6020 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006021 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006022 if (args == NULL) goto failed;
6023 for (i = 0; i < len; i++) {
6024 expr_ty value;
6025 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6026 if (res != 0) goto failed;
6027 asdl_seq_SET(args, i, value);
6028 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006029 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006030 } else {
6031 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6032 return 1;
6033 }
6034 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6035 int res;
6036 Py_ssize_t len;
6037 Py_ssize_t i;
6038 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6039 if (tmp == NULL) goto failed;
6040 if (!PyList_Check(tmp)) {
6041 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6042 goto failed;
6043 }
6044 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006045 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006046 if (keywords == NULL) goto failed;
6047 for (i = 0; i < len; i++) {
6048 keyword_ty value;
6049 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6050 if (res != 0) goto failed;
6051 asdl_seq_SET(keywords, i, value);
6052 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006053 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006054 } else {
6055 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6056 return 1;
6057 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006058 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006059 if (*out == NULL) goto failed;
6060 return 0;
6061 }
6062 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6063 if (isinstance == -1) {
6064 return 1;
6065 }
6066 if (isinstance) {
6067 object n;
6068
6069 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6070 int res;
6071 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6072 if (tmp == NULL) goto failed;
6073 res = obj2ast_object(tmp, &n, arena);
6074 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006075 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006076 } else {
6077 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6078 return 1;
6079 }
6080 *out = Num(n, lineno, col_offset, arena);
6081 if (*out == NULL) goto failed;
6082 return 0;
6083 }
6084 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6085 if (isinstance == -1) {
6086 return 1;
6087 }
6088 if (isinstance) {
6089 string s;
6090
6091 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6092 int res;
6093 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6094 if (tmp == NULL) goto failed;
6095 res = obj2ast_string(tmp, &s, 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 \"s\" missing from Str");
6100 return 1;
6101 }
6102 *out = Str(s, lineno, col_offset, arena);
6103 if (*out == NULL) goto failed;
6104 return 0;
6105 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006106 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6107 if (isinstance == -1) {
6108 return 1;
6109 }
6110 if (isinstance) {
6111 expr_ty value;
6112 int conversion;
6113 expr_ty format_spec;
6114
6115 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6116 int res;
6117 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6118 if (tmp == NULL) goto failed;
6119 res = obj2ast_expr(tmp, &value, arena);
6120 if (res != 0) goto failed;
6121 Py_CLEAR(tmp);
6122 } else {
6123 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6124 return 1;
6125 }
6126 if (exists_not_none(obj, &PyId_conversion)) {
6127 int res;
6128 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6129 if (tmp == NULL) goto failed;
6130 res = obj2ast_int(tmp, &conversion, arena);
6131 if (res != 0) goto failed;
6132 Py_CLEAR(tmp);
6133 } else {
6134 conversion = 0;
6135 }
6136 if (exists_not_none(obj, &PyId_format_spec)) {
6137 int res;
6138 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6139 if (tmp == NULL) goto failed;
6140 res = obj2ast_expr(tmp, &format_spec, arena);
6141 if (res != 0) goto failed;
6142 Py_CLEAR(tmp);
6143 } else {
6144 format_spec = NULL;
6145 }
6146 *out = FormattedValue(value, conversion, format_spec, lineno,
6147 col_offset, arena);
6148 if (*out == NULL) goto failed;
6149 return 0;
6150 }
6151 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6152 if (isinstance == -1) {
6153 return 1;
6154 }
6155 if (isinstance) {
6156 asdl_seq* values;
6157
6158 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6159 int res;
6160 Py_ssize_t len;
6161 Py_ssize_t i;
6162 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6163 if (tmp == NULL) goto failed;
6164 if (!PyList_Check(tmp)) {
6165 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6166 goto failed;
6167 }
6168 len = PyList_GET_SIZE(tmp);
6169 values = _Py_asdl_seq_new(len, arena);
6170 if (values == NULL) goto failed;
6171 for (i = 0; i < len; i++) {
6172 expr_ty value;
6173 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6174 if (res != 0) goto failed;
6175 asdl_seq_SET(values, i, value);
6176 }
6177 Py_CLEAR(tmp);
6178 } else {
6179 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6180 return 1;
6181 }
6182 *out = JoinedStr(values, lineno, col_offset, arena);
6183 if (*out == NULL) goto failed;
6184 return 0;
6185 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006186 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
6191 bytes s;
6192
6193 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6194 int res;
6195 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6196 if (tmp == NULL) goto failed;
6197 res = obj2ast_bytes(tmp, &s, arena);
6198 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006199 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006200 } else {
6201 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6202 return 1;
6203 }
6204 *out = Bytes(s, lineno, col_offset, arena);
6205 if (*out == NULL) goto failed;
6206 return 0;
6207 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006208 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6209 if (isinstance == -1) {
6210 return 1;
6211 }
6212 if (isinstance) {
6213 singleton value;
6214
6215 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6216 int res;
6217 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6218 if (tmp == NULL) goto failed;
6219 res = obj2ast_singleton(tmp, &value, arena);
6220 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006221 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006222 } else {
6223 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6224 return 1;
6225 }
6226 *out = NameConstant(value, lineno, col_offset, arena);
6227 if (*out == NULL) goto failed;
6228 return 0;
6229 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006230 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6231 if (isinstance == -1) {
6232 return 1;
6233 }
6234 if (isinstance) {
6235
6236 *out = Ellipsis(lineno, col_offset, arena);
6237 if (*out == NULL) goto failed;
6238 return 0;
6239 }
6240 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6241 if (isinstance == -1) {
6242 return 1;
6243 }
6244 if (isinstance) {
6245 expr_ty value;
6246 identifier attr;
6247 expr_context_ty ctx;
6248
6249 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6250 int res;
6251 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6252 if (tmp == NULL) goto failed;
6253 res = obj2ast_expr(tmp, &value, arena);
6254 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006255 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006256 } else {
6257 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6258 return 1;
6259 }
6260 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6261 int res;
6262 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6263 if (tmp == NULL) goto failed;
6264 res = obj2ast_identifier(tmp, &attr, arena);
6265 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006266 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006267 } else {
6268 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6269 return 1;
6270 }
6271 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6272 int res;
6273 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6274 if (tmp == NULL) goto failed;
6275 res = obj2ast_expr_context(tmp, &ctx, arena);
6276 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006277 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006278 } else {
6279 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6280 return 1;
6281 }
6282 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6283 if (*out == NULL) goto failed;
6284 return 0;
6285 }
6286 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6287 if (isinstance == -1) {
6288 return 1;
6289 }
6290 if (isinstance) {
6291 expr_ty value;
6292 slice_ty slice;
6293 expr_context_ty ctx;
6294
6295 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6296 int res;
6297 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6298 if (tmp == NULL) goto failed;
6299 res = obj2ast_expr(tmp, &value, arena);
6300 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006301 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006302 } else {
6303 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6304 return 1;
6305 }
6306 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6307 int res;
6308 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6309 if (tmp == NULL) goto failed;
6310 res = obj2ast_slice(tmp, &slice, arena);
6311 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006312 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006313 } else {
6314 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6315 return 1;
6316 }
6317 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6318 int res;
6319 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6320 if (tmp == NULL) goto failed;
6321 res = obj2ast_expr_context(tmp, &ctx, arena);
6322 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006323 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006324 } else {
6325 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6326 return 1;
6327 }
6328 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6329 if (*out == NULL) goto failed;
6330 return 0;
6331 }
6332 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6333 if (isinstance == -1) {
6334 return 1;
6335 }
6336 if (isinstance) {
6337 expr_ty value;
6338 expr_context_ty ctx;
6339
6340 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6341 int res;
6342 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6343 if (tmp == NULL) goto failed;
6344 res = obj2ast_expr(tmp, &value, arena);
6345 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006346 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006347 } else {
6348 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6349 return 1;
6350 }
6351 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6352 int res;
6353 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6354 if (tmp == NULL) goto failed;
6355 res = obj2ast_expr_context(tmp, &ctx, arena);
6356 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006357 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006358 } else {
6359 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6360 return 1;
6361 }
6362 *out = Starred(value, ctx, lineno, col_offset, arena);
6363 if (*out == NULL) goto failed;
6364 return 0;
6365 }
6366 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6367 if (isinstance == -1) {
6368 return 1;
6369 }
6370 if (isinstance) {
6371 identifier id;
6372 expr_context_ty ctx;
6373
6374 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6375 int res;
6376 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6377 if (tmp == NULL) goto failed;
6378 res = obj2ast_identifier(tmp, &id, arena);
6379 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006380 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006381 } else {
6382 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6383 return 1;
6384 }
6385 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6386 int res;
6387 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6388 if (tmp == NULL) goto failed;
6389 res = obj2ast_expr_context(tmp, &ctx, arena);
6390 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006391 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006392 } else {
6393 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6394 return 1;
6395 }
6396 *out = Name(id, ctx, lineno, col_offset, arena);
6397 if (*out == NULL) goto failed;
6398 return 0;
6399 }
6400 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6401 if (isinstance == -1) {
6402 return 1;
6403 }
6404 if (isinstance) {
6405 asdl_seq* elts;
6406 expr_context_ty ctx;
6407
6408 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6409 int res;
6410 Py_ssize_t len;
6411 Py_ssize_t i;
6412 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6413 if (tmp == NULL) goto failed;
6414 if (!PyList_Check(tmp)) {
6415 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6416 goto failed;
6417 }
6418 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006419 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006420 if (elts == NULL) goto failed;
6421 for (i = 0; i < len; i++) {
6422 expr_ty value;
6423 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6424 if (res != 0) goto failed;
6425 asdl_seq_SET(elts, i, value);
6426 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006427 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006428 } else {
6429 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6430 return 1;
6431 }
6432 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6433 int res;
6434 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6435 if (tmp == NULL) goto failed;
6436 res = obj2ast_expr_context(tmp, &ctx, arena);
6437 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006439 } else {
6440 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6441 return 1;
6442 }
6443 *out = List(elts, ctx, lineno, col_offset, arena);
6444 if (*out == NULL) goto failed;
6445 return 0;
6446 }
6447 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6448 if (isinstance == -1) {
6449 return 1;
6450 }
6451 if (isinstance) {
6452 asdl_seq* elts;
6453 expr_context_ty ctx;
6454
6455 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6456 int res;
6457 Py_ssize_t len;
6458 Py_ssize_t i;
6459 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6460 if (tmp == NULL) goto failed;
6461 if (!PyList_Check(tmp)) {
6462 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6463 goto failed;
6464 }
6465 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006466 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006467 if (elts == NULL) goto failed;
6468 for (i = 0; i < len; i++) {
6469 expr_ty value;
6470 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6471 if (res != 0) goto failed;
6472 asdl_seq_SET(elts, i, value);
6473 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006474 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006475 } else {
6476 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6477 return 1;
6478 }
6479 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6480 int res;
6481 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6482 if (tmp == NULL) goto failed;
6483 res = obj2ast_expr_context(tmp, &ctx, arena);
6484 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006485 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006486 } else {
6487 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6488 return 1;
6489 }
6490 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6491 if (*out == NULL) goto failed;
6492 return 0;
6493 }
6494
6495 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6496 failed:
6497 Py_XDECREF(tmp);
6498 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006499}
6500
6501int
6502obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6503{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006504 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006505
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006506 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6507 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006508 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006509 }
6510 if (isinstance) {
6511 *out = Load;
6512 return 0;
6513 }
6514 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6515 if (isinstance == -1) {
6516 return 1;
6517 }
6518 if (isinstance) {
6519 *out = Store;
6520 return 0;
6521 }
6522 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6523 if (isinstance == -1) {
6524 return 1;
6525 }
6526 if (isinstance) {
6527 *out = Del;
6528 return 0;
6529 }
6530 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6531 if (isinstance == -1) {
6532 return 1;
6533 }
6534 if (isinstance) {
6535 *out = AugLoad;
6536 return 0;
6537 }
6538 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6539 if (isinstance == -1) {
6540 return 1;
6541 }
6542 if (isinstance) {
6543 *out = AugStore;
6544 return 0;
6545 }
6546 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6547 if (isinstance == -1) {
6548 return 1;
6549 }
6550 if (isinstance) {
6551 *out = Param;
6552 return 0;
6553 }
6554
6555 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6556 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006557}
6558
6559int
6560obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6561{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006562 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006563
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006564 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006565
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006566 if (obj == Py_None) {
6567 *out = NULL;
6568 return 0;
6569 }
6570 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6571 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006572 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006573 }
6574 if (isinstance) {
6575 expr_ty lower;
6576 expr_ty upper;
6577 expr_ty step;
6578
Victor Stinneree4b59c2013-07-27 00:01:35 +02006579 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006580 int res;
6581 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6582 if (tmp == NULL) goto failed;
6583 res = obj2ast_expr(tmp, &lower, arena);
6584 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006585 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006586 } else {
6587 lower = NULL;
6588 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006589 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006590 int res;
6591 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6592 if (tmp == NULL) goto failed;
6593 res = obj2ast_expr(tmp, &upper, arena);
6594 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006595 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006596 } else {
6597 upper = NULL;
6598 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006599 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006600 int res;
6601 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6602 if (tmp == NULL) goto failed;
6603 res = obj2ast_expr(tmp, &step, arena);
6604 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006605 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006606 } else {
6607 step = NULL;
6608 }
6609 *out = Slice(lower, upper, step, arena);
6610 if (*out == NULL) goto failed;
6611 return 0;
6612 }
6613 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6614 if (isinstance == -1) {
6615 return 1;
6616 }
6617 if (isinstance) {
6618 asdl_seq* dims;
6619
6620 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6621 int res;
6622 Py_ssize_t len;
6623 Py_ssize_t i;
6624 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6625 if (tmp == NULL) goto failed;
6626 if (!PyList_Check(tmp)) {
6627 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6628 goto failed;
6629 }
6630 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006631 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006632 if (dims == NULL) goto failed;
6633 for (i = 0; i < len; i++) {
6634 slice_ty value;
6635 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6636 if (res != 0) goto failed;
6637 asdl_seq_SET(dims, i, value);
6638 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006639 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006640 } else {
6641 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6642 return 1;
6643 }
6644 *out = ExtSlice(dims, arena);
6645 if (*out == NULL) goto failed;
6646 return 0;
6647 }
6648 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6649 if (isinstance == -1) {
6650 return 1;
6651 }
6652 if (isinstance) {
6653 expr_ty value;
6654
6655 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6656 int res;
6657 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6658 if (tmp == NULL) goto failed;
6659 res = obj2ast_expr(tmp, &value, arena);
6660 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006661 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006662 } else {
6663 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6664 return 1;
6665 }
6666 *out = Index(value, arena);
6667 if (*out == NULL) goto failed;
6668 return 0;
6669 }
6670
6671 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6672 failed:
6673 Py_XDECREF(tmp);
6674 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006675}
6676
6677int
6678obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6679{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006680 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006681
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006682 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6683 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006684 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006685 }
6686 if (isinstance) {
6687 *out = And;
6688 return 0;
6689 }
6690 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6691 if (isinstance == -1) {
6692 return 1;
6693 }
6694 if (isinstance) {
6695 *out = Or;
6696 return 0;
6697 }
6698
6699 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6700 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006701}
6702
6703int
6704obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6705{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006706 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006707
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006708 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6709 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006710 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006711 }
6712 if (isinstance) {
6713 *out = Add;
6714 return 0;
6715 }
6716 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6717 if (isinstance == -1) {
6718 return 1;
6719 }
6720 if (isinstance) {
6721 *out = Sub;
6722 return 0;
6723 }
6724 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6725 if (isinstance == -1) {
6726 return 1;
6727 }
6728 if (isinstance) {
6729 *out = Mult;
6730 return 0;
6731 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006732 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6733 if (isinstance == -1) {
6734 return 1;
6735 }
6736 if (isinstance) {
6737 *out = MatMult;
6738 return 0;
6739 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006740 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6741 if (isinstance == -1) {
6742 return 1;
6743 }
6744 if (isinstance) {
6745 *out = Div;
6746 return 0;
6747 }
6748 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6749 if (isinstance == -1) {
6750 return 1;
6751 }
6752 if (isinstance) {
6753 *out = Mod;
6754 return 0;
6755 }
6756 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6757 if (isinstance == -1) {
6758 return 1;
6759 }
6760 if (isinstance) {
6761 *out = Pow;
6762 return 0;
6763 }
6764 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6765 if (isinstance == -1) {
6766 return 1;
6767 }
6768 if (isinstance) {
6769 *out = LShift;
6770 return 0;
6771 }
6772 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6773 if (isinstance == -1) {
6774 return 1;
6775 }
6776 if (isinstance) {
6777 *out = RShift;
6778 return 0;
6779 }
6780 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6781 if (isinstance == -1) {
6782 return 1;
6783 }
6784 if (isinstance) {
6785 *out = BitOr;
6786 return 0;
6787 }
6788 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6789 if (isinstance == -1) {
6790 return 1;
6791 }
6792 if (isinstance) {
6793 *out = BitXor;
6794 return 0;
6795 }
6796 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6797 if (isinstance == -1) {
6798 return 1;
6799 }
6800 if (isinstance) {
6801 *out = BitAnd;
6802 return 0;
6803 }
6804 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6805 if (isinstance == -1) {
6806 return 1;
6807 }
6808 if (isinstance) {
6809 *out = FloorDiv;
6810 return 0;
6811 }
6812
6813 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6814 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006815}
6816
6817int
6818obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6819{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006820 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006821
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006822 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6823 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006824 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006825 }
6826 if (isinstance) {
6827 *out = Invert;
6828 return 0;
6829 }
6830 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6831 if (isinstance == -1) {
6832 return 1;
6833 }
6834 if (isinstance) {
6835 *out = Not;
6836 return 0;
6837 }
6838 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6839 if (isinstance == -1) {
6840 return 1;
6841 }
6842 if (isinstance) {
6843 *out = UAdd;
6844 return 0;
6845 }
6846 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6847 if (isinstance == -1) {
6848 return 1;
6849 }
6850 if (isinstance) {
6851 *out = USub;
6852 return 0;
6853 }
6854
6855 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6856 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006857}
6858
6859int
6860obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6861{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006862 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006863
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006864 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6865 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006866 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006867 }
6868 if (isinstance) {
6869 *out = Eq;
6870 return 0;
6871 }
6872 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6873 if (isinstance == -1) {
6874 return 1;
6875 }
6876 if (isinstance) {
6877 *out = NotEq;
6878 return 0;
6879 }
6880 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6881 if (isinstance == -1) {
6882 return 1;
6883 }
6884 if (isinstance) {
6885 *out = Lt;
6886 return 0;
6887 }
6888 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6889 if (isinstance == -1) {
6890 return 1;
6891 }
6892 if (isinstance) {
6893 *out = LtE;
6894 return 0;
6895 }
6896 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6897 if (isinstance == -1) {
6898 return 1;
6899 }
6900 if (isinstance) {
6901 *out = Gt;
6902 return 0;
6903 }
6904 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6905 if (isinstance == -1) {
6906 return 1;
6907 }
6908 if (isinstance) {
6909 *out = GtE;
6910 return 0;
6911 }
6912 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6913 if (isinstance == -1) {
6914 return 1;
6915 }
6916 if (isinstance) {
6917 *out = Is;
6918 return 0;
6919 }
6920 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6921 if (isinstance == -1) {
6922 return 1;
6923 }
6924 if (isinstance) {
6925 *out = IsNot;
6926 return 0;
6927 }
6928 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6929 if (isinstance == -1) {
6930 return 1;
6931 }
6932 if (isinstance) {
6933 *out = In;
6934 return 0;
6935 }
6936 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6937 if (isinstance == -1) {
6938 return 1;
6939 }
6940 if (isinstance) {
6941 *out = NotIn;
6942 return 0;
6943 }
6944
6945 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6946 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006947}
6948
6949int
6950obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6951{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006952 PyObject* tmp = NULL;
6953 expr_ty target;
6954 expr_ty iter;
6955 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006956
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006957 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6958 int res;
6959 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6960 if (tmp == NULL) goto failed;
6961 res = obj2ast_expr(tmp, &target, arena);
6962 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006963 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006964 } else {
6965 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006966 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006967 }
6968 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6969 int res;
6970 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6971 if (tmp == NULL) goto failed;
6972 res = obj2ast_expr(tmp, &iter, arena);
6973 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006974 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006975 } else {
6976 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6977 return 1;
6978 }
6979 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6980 int res;
6981 Py_ssize_t len;
6982 Py_ssize_t i;
6983 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6984 if (tmp == NULL) goto failed;
6985 if (!PyList_Check(tmp)) {
6986 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6987 goto failed;
6988 }
6989 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006990 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006991 if (ifs == NULL) goto failed;
6992 for (i = 0; i < len; i++) {
6993 expr_ty value;
6994 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6995 if (res != 0) goto failed;
6996 asdl_seq_SET(ifs, i, value);
6997 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006998 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006999 } else {
7000 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7001 return 1;
7002 }
7003 *out = comprehension(target, iter, ifs, arena);
7004 return 0;
7005failed:
7006 Py_XDECREF(tmp);
7007 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007008}
7009
7010int
7011obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7012{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007013 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007014
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007015 PyObject *tmp = NULL;
7016 int lineno;
7017 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007018
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007019 if (obj == Py_None) {
7020 *out = NULL;
7021 return 0;
7022 }
7023 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7024 int res;
7025 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7026 if (tmp == NULL) goto failed;
7027 res = obj2ast_int(tmp, &lineno, arena);
7028 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007029 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007030 } else {
7031 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007032 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007033 }
7034 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7035 int res;
7036 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7037 if (tmp == NULL) goto failed;
7038 res = obj2ast_int(tmp, &col_offset, arena);
7039 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007040 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007041 } else {
7042 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7043 return 1;
7044 }
7045 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7046 if (isinstance == -1) {
7047 return 1;
7048 }
7049 if (isinstance) {
7050 expr_ty type;
7051 identifier name;
7052 asdl_seq* body;
7053
Victor Stinneree4b59c2013-07-27 00:01:35 +02007054 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007055 int res;
7056 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7057 if (tmp == NULL) goto failed;
7058 res = obj2ast_expr(tmp, &type, arena);
7059 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007060 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007061 } else {
7062 type = NULL;
7063 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007064 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007065 int res;
7066 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7067 if (tmp == NULL) goto failed;
7068 res = obj2ast_identifier(tmp, &name, arena);
7069 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007070 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007071 } else {
7072 name = NULL;
7073 }
7074 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7075 int res;
7076 Py_ssize_t len;
7077 Py_ssize_t i;
7078 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7079 if (tmp == NULL) goto failed;
7080 if (!PyList_Check(tmp)) {
7081 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7082 goto failed;
7083 }
7084 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007085 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007086 if (body == NULL) goto failed;
7087 for (i = 0; i < len; i++) {
7088 stmt_ty value;
7089 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7090 if (res != 0) goto failed;
7091 asdl_seq_SET(body, i, value);
7092 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007093 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007094 } else {
7095 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7096 return 1;
7097 }
7098 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7099 if (*out == NULL) goto failed;
7100 return 0;
7101 }
7102
7103 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7104 failed:
7105 Py_XDECREF(tmp);
7106 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007107}
7108
7109int
7110obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7111{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007112 PyObject* tmp = NULL;
7113 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007114 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007115 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007116 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007117 arg_ty kwarg;
7118 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007119
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007120 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7121 int res;
7122 Py_ssize_t len;
7123 Py_ssize_t i;
7124 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7125 if (tmp == NULL) goto failed;
7126 if (!PyList_Check(tmp)) {
7127 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7128 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007129 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007130 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007131 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007132 if (args == NULL) goto failed;
7133 for (i = 0; i < len; i++) {
7134 arg_ty value;
7135 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7136 if (res != 0) goto failed;
7137 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007138 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007139 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007140 } else {
7141 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007142 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007143 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007144 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007145 int res;
7146 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7147 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007148 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007149 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007150 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007151 } else {
7152 vararg = NULL;
7153 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007154 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7155 int res;
7156 Py_ssize_t len;
7157 Py_ssize_t i;
7158 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7159 if (tmp == NULL) goto failed;
7160 if (!PyList_Check(tmp)) {
7161 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7162 goto failed;
7163 }
7164 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007165 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007166 if (kwonlyargs == NULL) goto failed;
7167 for (i = 0; i < len; i++) {
7168 arg_ty value;
7169 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7170 if (res != 0) goto failed;
7171 asdl_seq_SET(kwonlyargs, i, value);
7172 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007173 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007174 } else {
7175 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7176 return 1;
7177 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007178 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7179 int res;
7180 Py_ssize_t len;
7181 Py_ssize_t i;
7182 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7183 if (tmp == NULL) goto failed;
7184 if (!PyList_Check(tmp)) {
7185 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7186 goto failed;
7187 }
7188 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007189 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007190 if (kw_defaults == NULL) goto failed;
7191 for (i = 0; i < len; i++) {
7192 expr_ty value;
7193 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7194 if (res != 0) goto failed;
7195 asdl_seq_SET(kw_defaults, i, value);
7196 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007197 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007198 } else {
7199 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7200 return 1;
7201 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007202 if (exists_not_none(obj, &PyId_kwarg)) {
7203 int res;
7204 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7205 if (tmp == NULL) goto failed;
7206 res = obj2ast_arg(tmp, &kwarg, arena);
7207 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007208 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007209 } else {
7210 kwarg = NULL;
7211 }
7212 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7213 int res;
7214 Py_ssize_t len;
7215 Py_ssize_t i;
7216 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7217 if (tmp == NULL) goto failed;
7218 if (!PyList_Check(tmp)) {
7219 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7220 goto failed;
7221 }
7222 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007223 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007224 if (defaults == NULL) goto failed;
7225 for (i = 0; i < len; i++) {
7226 expr_ty value;
7227 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7228 if (res != 0) goto failed;
7229 asdl_seq_SET(defaults, i, value);
7230 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007231 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007232 } else {
7233 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7234 return 1;
7235 }
7236 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7237 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007238 return 0;
7239failed:
7240 Py_XDECREF(tmp);
7241 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007242}
7243
7244int
7245obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7246{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007247 PyObject* tmp = NULL;
7248 identifier arg;
7249 expr_ty annotation;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007250
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007251 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7252 int res;
7253 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7254 if (tmp == NULL) goto failed;
7255 res = obj2ast_identifier(tmp, &arg, arena);
7256 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007257 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007258 } else {
7259 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007260 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007261 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007262 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007263 int res;
7264 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7265 if (tmp == NULL) goto failed;
7266 res = obj2ast_expr(tmp, &annotation, arena);
7267 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007268 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007269 } else {
7270 annotation = NULL;
7271 }
7272 *out = arg(arg, annotation, arena);
7273 return 0;
7274failed:
7275 Py_XDECREF(tmp);
7276 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007277}
7278
7279int
7280obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7281{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007282 PyObject* tmp = NULL;
7283 identifier arg;
7284 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007285
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007286 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007287 int res;
7288 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7289 if (tmp == NULL) goto failed;
7290 res = obj2ast_identifier(tmp, &arg, arena);
7291 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007292 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007293 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007294 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007295 }
7296 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7297 int res;
7298 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7299 if (tmp == NULL) goto failed;
7300 res = obj2ast_expr(tmp, &value, arena);
7301 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007302 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007303 } else {
7304 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7305 return 1;
7306 }
7307 *out = keyword(arg, value, arena);
7308 return 0;
7309failed:
7310 Py_XDECREF(tmp);
7311 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007312}
7313
7314int
7315obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7316{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007317 PyObject* tmp = NULL;
7318 identifier name;
7319 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007320
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007321 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7322 int res;
7323 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7324 if (tmp == NULL) goto failed;
7325 res = obj2ast_identifier(tmp, &name, arena);
7326 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007327 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007328 } else {
7329 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007330 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007331 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007332 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007333 int res;
7334 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7335 if (tmp == NULL) goto failed;
7336 res = obj2ast_identifier(tmp, &asname, arena);
7337 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007338 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007339 } else {
7340 asname = NULL;
7341 }
7342 *out = alias(name, asname, arena);
7343 return 0;
7344failed:
7345 Py_XDECREF(tmp);
7346 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007347}
7348
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007349int
7350obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7351{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007352 PyObject* tmp = NULL;
7353 expr_ty context_expr;
7354 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007355
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007356 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7357 int res;
7358 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7359 if (tmp == NULL) goto failed;
7360 res = obj2ast_expr(tmp, &context_expr, arena);
7361 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007362 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007363 } else {
7364 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007365 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007366 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007367 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007368 int res;
7369 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7370 if (tmp == NULL) goto failed;
7371 res = obj2ast_expr(tmp, &optional_vars, arena);
7372 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007373 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007374 } else {
7375 optional_vars = NULL;
7376 }
7377 *out = withitem(context_expr, optional_vars, arena);
7378 return 0;
7379failed:
7380 Py_XDECREF(tmp);
7381 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007382}
7383
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007384
Martin v. Löwis1a214512008-06-11 05:26:20 +00007385static struct PyModuleDef _astmodule = {
7386 PyModuleDef_HEAD_INIT, "_ast"
7387};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007388PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007389PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007390{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007391 PyObject *m, *d;
7392 if (!init_types()) return NULL;
7393 m = PyModule_Create(&_astmodule);
7394 if (!m) return NULL;
7395 d = PyModule_GetDict(m);
7396 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007397 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007398 return NULL;
7399 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7400 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7401 NULL;
7402 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7403 0) return NULL;
7404 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7405 return NULL;
7406 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7407 NULL;
7408 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7409 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7410 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007411 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7412 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007413 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7414 return NULL;
7415 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7416 NULL;
7417 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7418 NULL;
7419 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7420 NULL;
7421 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7422 return NULL;
7423 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007424 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7425 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007426 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7427 NULL;
7428 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7429 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007430 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7431 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007432 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7433 NULL;
7434 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7435 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7436 NULL;
7437 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7438 NULL;
7439 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7440 return NULL;
7441 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7442 NULL;
7443 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7444 return NULL;
7445 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7446 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7447 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7448 NULL;
7449 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7450 return NULL;
7451 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7452 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7453 NULL;
7454 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7455 NULL;
7456 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7457 NULL;
7458 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7459 NULL;
7460 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7461 NULL;
7462 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7463 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7464 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7465 return NULL;
7466 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7467 NULL;
7468 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7469 return NULL;
7470 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7471 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007472 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7473 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007474 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7475 NULL;
7476 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7477 return NULL;
7478 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7479 NULL;
7480 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7481 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7482 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007483 if (PyDict_SetItemString(d, "FormattedValue",
7484 (PyObject*)FormattedValue_type) < 0) return NULL;
7485 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7486 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007487 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7488 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007489 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7490 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007491 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7492 return NULL;
7493 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7494 return NULL;
7495 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7496 return NULL;
7497 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7498 NULL;
7499 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7500 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7501 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7502 NULL;
7503 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7504 0) return NULL;
7505 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7506 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7507 NULL;
7508 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7509 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7510 NULL;
7511 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7512 return NULL;
7513 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7514 NULL;
7515 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7516 NULL;
7517 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7518 NULL;
7519 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7520 return NULL;
7521 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7522 NULL;
7523 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7524 NULL;
7525 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7526 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7527 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7528 return NULL;
7529 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7530 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7531 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007532 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7533 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007534 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7535 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7536 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7537 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7538 NULL;
7539 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7540 NULL;
7541 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7542 NULL;
7543 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7544 NULL;
7545 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7546 NULL;
7547 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7548 return NULL;
7549 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7550 NULL;
7551 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7552 NULL;
7553 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7554 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7555 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7556 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7557 NULL;
7558 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7559 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7560 NULL;
7561 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7562 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7563 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7564 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7565 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7566 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7567 NULL;
7568 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7569 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7570 NULL;
7571 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7572 < 0) return NULL;
7573 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7574 < 0) return NULL;
7575 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7576 < 0) return NULL;
7577 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7578 return NULL;
7579 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7580 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7581 NULL;
7582 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7583 NULL;
7584 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7585 return NULL;
7586 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007587}
7588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007589
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007590PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007591{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007592 if (!init_types())
7593 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007594 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007595}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007596
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007597/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7598mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007599{
7600 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007601 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007602 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007603 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007604
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007605 req_type[0] = (PyObject*)Module_type;
7606 req_type[1] = (PyObject*)Expression_type;
7607 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007608
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007609 assert(0 <= mode && mode <= 2);
7610
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007611 if (!init_types())
7612 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007613
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007614 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7615 if (isinstance == -1)
7616 return NULL;
7617 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007618 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7619 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007620 return NULL;
7621 }
7622 if (obj2ast_mod(ast, &res, arena) != 0)
7623 return NULL;
7624 else
7625 return res;
7626}
7627
7628int PyAST_Check(PyObject* obj)
7629{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007630 if (!init_types())
7631 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007632 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007633}
7634
Martin v. Löwis5b222132007-06-10 09:51:05 +00007635