blob: 07d9b3e8f7c8aadf4ff75b223d324e5323481620 [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
Victor Stinnerc106c682015-11-06 17:01:48 +01002428arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2429 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002430{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002431 arg_ty p;
2432 if (!arg) {
2433 PyErr_SetString(PyExc_ValueError,
2434 "field arg is required for arg");
2435 return NULL;
2436 }
2437 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2438 if (!p)
2439 return NULL;
2440 p->arg = arg;
2441 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002442 p->lineno = lineno;
2443 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002444 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002445}
2446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002448keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002450 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002451 if (!value) {
2452 PyErr_SetString(PyExc_ValueError,
2453 "field value is required for keyword");
2454 return NULL;
2455 }
2456 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2457 if (!p)
2458 return NULL;
2459 p->arg = arg;
2460 p->value = value;
2461 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
2464alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002465alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002467 alias_ty p;
2468 if (!name) {
2469 PyErr_SetString(PyExc_ValueError,
2470 "field name is required for alias");
2471 return NULL;
2472 }
2473 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2474 if (!p)
2475 return NULL;
2476 p->name = name;
2477 p->asname = asname;
2478 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479}
2480
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002481withitem_ty
2482withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2483{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002484 withitem_ty p;
2485 if (!context_expr) {
2486 PyErr_SetString(PyExc_ValueError,
2487 "field context_expr is required for withitem");
2488 return NULL;
2489 }
2490 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2491 if (!p)
2492 return NULL;
2493 p->context_expr = context_expr;
2494 p->optional_vars = optional_vars;
2495 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002496}
2497
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002498
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002499PyObject*
2500ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002501{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002502 mod_ty o = (mod_ty)_o;
2503 PyObject *result = NULL, *value = NULL;
2504 if (!o) {
2505 Py_INCREF(Py_None);
2506 return Py_None;
2507 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002508
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002509 switch (o->kind) {
2510 case Module_kind:
2511 result = PyType_GenericNew(Module_type, NULL, NULL);
2512 if (!result) goto failed;
2513 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2514 if (!value) goto failed;
2515 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2516 goto failed;
2517 Py_DECREF(value);
2518 break;
2519 case Interactive_kind:
2520 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2521 if (!result) goto failed;
2522 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2523 if (!value) goto failed;
2524 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2525 goto failed;
2526 Py_DECREF(value);
2527 break;
2528 case Expression_kind:
2529 result = PyType_GenericNew(Expression_type, NULL, NULL);
2530 if (!result) goto failed;
2531 value = ast2obj_expr(o->v.Expression.body);
2532 if (!value) goto failed;
2533 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2534 goto failed;
2535 Py_DECREF(value);
2536 break;
2537 case Suite_kind:
2538 result = PyType_GenericNew(Suite_type, NULL, NULL);
2539 if (!result) goto failed;
2540 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2541 if (!value) goto failed;
2542 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2543 goto failed;
2544 Py_DECREF(value);
2545 break;
2546 }
2547 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002548failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002549 Py_XDECREF(value);
2550 Py_XDECREF(result);
2551 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002554PyObject*
2555ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002557 stmt_ty o = (stmt_ty)_o;
2558 PyObject *result = NULL, *value = NULL;
2559 if (!o) {
2560 Py_INCREF(Py_None);
2561 return Py_None;
2562 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002563
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002564 switch (o->kind) {
2565 case FunctionDef_kind:
2566 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2567 if (!result) goto failed;
2568 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002569 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002570 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2571 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002572 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002573 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002574 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002575 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2576 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002577 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002578 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2579 if (!value) goto failed;
2580 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2581 goto failed;
2582 Py_DECREF(value);
2583 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2584 if (!value) goto failed;
2585 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2586 goto failed;
2587 Py_DECREF(value);
2588 value = ast2obj_expr(o->v.FunctionDef.returns);
2589 if (!value) goto failed;
2590 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2591 goto failed;
2592 Py_DECREF(value);
2593 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002594 case AsyncFunctionDef_kind:
2595 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2596 if (!result) goto failed;
2597 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2598 if (!value) goto failed;
2599 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2600 goto failed;
2601 Py_DECREF(value);
2602 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2603 if (!value) goto failed;
2604 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2605 goto failed;
2606 Py_DECREF(value);
2607 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2608 if (!value) goto failed;
2609 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2610 goto failed;
2611 Py_DECREF(value);
2612 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2613 ast2obj_expr);
2614 if (!value) goto failed;
2615 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
2618 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2619 if (!value) goto failed;
2620 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2621 goto failed;
2622 Py_DECREF(value);
2623 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002624 case ClassDef_kind:
2625 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2626 if (!result) goto failed;
2627 value = ast2obj_identifier(o->v.ClassDef.name);
2628 if (!value) goto failed;
2629 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
2632 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2633 if (!value) goto failed;
2634 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2638 if (!value) goto failed;
2639 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2640 goto failed;
2641 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002642 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2643 if (!value) goto failed;
2644 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2645 goto failed;
2646 Py_DECREF(value);
2647 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2648 if (!value) goto failed;
2649 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2650 goto failed;
2651 Py_DECREF(value);
2652 break;
2653 case Return_kind:
2654 result = PyType_GenericNew(Return_type, NULL, NULL);
2655 if (!result) goto failed;
2656 value = ast2obj_expr(o->v.Return.value);
2657 if (!value) goto failed;
2658 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
2661 break;
2662 case Delete_kind:
2663 result = PyType_GenericNew(Delete_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2666 if (!value) goto failed;
2667 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
2670 break;
2671 case Assign_kind:
2672 result = PyType_GenericNew(Assign_type, NULL, NULL);
2673 if (!result) goto failed;
2674 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2675 if (!value) goto failed;
2676 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2677 goto failed;
2678 Py_DECREF(value);
2679 value = ast2obj_expr(o->v.Assign.value);
2680 if (!value) goto failed;
2681 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
2684 break;
2685 case AugAssign_kind:
2686 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2687 if (!result) goto failed;
2688 value = ast2obj_expr(o->v.AugAssign.target);
2689 if (!value) goto failed;
2690 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
2693 value = ast2obj_operator(o->v.AugAssign.op);
2694 if (!value) goto failed;
2695 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2696 goto failed;
2697 Py_DECREF(value);
2698 value = ast2obj_expr(o->v.AugAssign.value);
2699 if (!value) goto failed;
2700 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2701 goto failed;
2702 Py_DECREF(value);
2703 break;
2704 case For_kind:
2705 result = PyType_GenericNew(For_type, NULL, NULL);
2706 if (!result) goto failed;
2707 value = ast2obj_expr(o->v.For.target);
2708 if (!value) goto failed;
2709 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2710 goto failed;
2711 Py_DECREF(value);
2712 value = ast2obj_expr(o->v.For.iter);
2713 if (!value) goto failed;
2714 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2718 if (!value) goto failed;
2719 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2723 if (!value) goto failed;
2724 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
2727 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002728 case AsyncFor_kind:
2729 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2730 if (!result) goto failed;
2731 value = ast2obj_expr(o->v.AsyncFor.target);
2732 if (!value) goto failed;
2733 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 value = ast2obj_expr(o->v.AsyncFor.iter);
2737 if (!value) goto failed;
2738 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
2741 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2742 if (!value) goto failed;
2743 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002752 case While_kind:
2753 result = PyType_GenericNew(While_type, NULL, NULL);
2754 if (!result) goto failed;
2755 value = ast2obj_expr(o->v.While.test);
2756 if (!value) goto failed;
2757 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
2760 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2761 if (!value) goto failed;
2762 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
2765 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2766 if (!value) goto failed;
2767 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2768 goto failed;
2769 Py_DECREF(value);
2770 break;
2771 case If_kind:
2772 result = PyType_GenericNew(If_type, NULL, NULL);
2773 if (!result) goto failed;
2774 value = ast2obj_expr(o->v.If.test);
2775 if (!value) goto failed;
2776 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2780 if (!value) goto failed;
2781 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2785 if (!value) goto failed;
2786 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
2789 break;
2790 case With_kind:
2791 result = PyType_GenericNew(With_type, NULL, NULL);
2792 if (!result) goto failed;
2793 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2794 if (!value) goto failed;
2795 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
2798 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2799 if (!value) goto failed;
2800 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2801 goto failed;
2802 Py_DECREF(value);
2803 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002804 case AsyncWith_kind:
2805 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2806 if (!result) goto failed;
2807 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2808 if (!value) goto failed;
2809 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
2812 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2813 if (!value) goto failed;
2814 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2815 goto failed;
2816 Py_DECREF(value);
2817 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002818 case Raise_kind:
2819 result = PyType_GenericNew(Raise_type, NULL, NULL);
2820 if (!result) goto failed;
2821 value = ast2obj_expr(o->v.Raise.exc);
2822 if (!value) goto failed;
2823 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2824 goto failed;
2825 Py_DECREF(value);
2826 value = ast2obj_expr(o->v.Raise.cause);
2827 if (!value) goto failed;
2828 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2829 goto failed;
2830 Py_DECREF(value);
2831 break;
2832 case Try_kind:
2833 result = PyType_GenericNew(Try_type, NULL, NULL);
2834 if (!result) goto failed;
2835 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2836 if (!value) goto failed;
2837 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2838 goto failed;
2839 Py_DECREF(value);
2840 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2841 if (!value) goto failed;
2842 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2843 goto failed;
2844 Py_DECREF(value);
2845 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2846 if (!value) goto failed;
2847 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2848 goto failed;
2849 Py_DECREF(value);
2850 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2851 if (!value) goto failed;
2852 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2853 goto failed;
2854 Py_DECREF(value);
2855 break;
2856 case Assert_kind:
2857 result = PyType_GenericNew(Assert_type, NULL, NULL);
2858 if (!result) goto failed;
2859 value = ast2obj_expr(o->v.Assert.test);
2860 if (!value) goto failed;
2861 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2862 goto failed;
2863 Py_DECREF(value);
2864 value = ast2obj_expr(o->v.Assert.msg);
2865 if (!value) goto failed;
2866 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2867 goto failed;
2868 Py_DECREF(value);
2869 break;
2870 case Import_kind:
2871 result = PyType_GenericNew(Import_type, NULL, NULL);
2872 if (!result) goto failed;
2873 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2874 if (!value) goto failed;
2875 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2876 goto failed;
2877 Py_DECREF(value);
2878 break;
2879 case ImportFrom_kind:
2880 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2881 if (!result) goto failed;
2882 value = ast2obj_identifier(o->v.ImportFrom.module);
2883 if (!value) goto failed;
2884 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2885 goto failed;
2886 Py_DECREF(value);
2887 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2888 if (!value) goto failed;
2889 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2890 goto failed;
2891 Py_DECREF(value);
2892 value = ast2obj_int(o->v.ImportFrom.level);
2893 if (!value) goto failed;
2894 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2895 goto failed;
2896 Py_DECREF(value);
2897 break;
2898 case Global_kind:
2899 result = PyType_GenericNew(Global_type, NULL, NULL);
2900 if (!result) goto failed;
2901 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2902 if (!value) goto failed;
2903 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2904 goto failed;
2905 Py_DECREF(value);
2906 break;
2907 case Nonlocal_kind:
2908 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2909 if (!result) goto failed;
2910 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2911 if (!value) goto failed;
2912 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2913 goto failed;
2914 Py_DECREF(value);
2915 break;
2916 case Expr_kind:
2917 result = PyType_GenericNew(Expr_type, NULL, NULL);
2918 if (!result) goto failed;
2919 value = ast2obj_expr(o->v.Expr.value);
2920 if (!value) goto failed;
2921 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2922 goto failed;
2923 Py_DECREF(value);
2924 break;
2925 case Pass_kind:
2926 result = PyType_GenericNew(Pass_type, NULL, NULL);
2927 if (!result) goto failed;
2928 break;
2929 case Break_kind:
2930 result = PyType_GenericNew(Break_type, NULL, NULL);
2931 if (!result) goto failed;
2932 break;
2933 case Continue_kind:
2934 result = PyType_GenericNew(Continue_type, NULL, NULL);
2935 if (!result) goto failed;
2936 break;
2937 }
2938 value = ast2obj_int(o->lineno);
2939 if (!value) goto failed;
2940 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2941 goto failed;
2942 Py_DECREF(value);
2943 value = ast2obj_int(o->col_offset);
2944 if (!value) goto failed;
2945 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2946 goto failed;
2947 Py_DECREF(value);
2948 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002949failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002950 Py_XDECREF(value);
2951 Py_XDECREF(result);
2952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953}
2954
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002955PyObject*
2956ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002958 expr_ty o = (expr_ty)_o;
2959 PyObject *result = NULL, *value = NULL;
2960 if (!o) {
2961 Py_INCREF(Py_None);
2962 return Py_None;
2963 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002964
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002965 switch (o->kind) {
2966 case BoolOp_kind:
2967 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2968 if (!result) goto failed;
2969 value = ast2obj_boolop(o->v.BoolOp.op);
2970 if (!value) goto failed;
2971 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
2974 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2975 if (!value) goto failed;
2976 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
2977 goto failed;
2978 Py_DECREF(value);
2979 break;
2980 case BinOp_kind:
2981 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2982 if (!result) goto failed;
2983 value = ast2obj_expr(o->v.BinOp.left);
2984 if (!value) goto failed;
2985 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
2986 goto failed;
2987 Py_DECREF(value);
2988 value = ast2obj_operator(o->v.BinOp.op);
2989 if (!value) goto failed;
2990 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2991 goto failed;
2992 Py_DECREF(value);
2993 value = ast2obj_expr(o->v.BinOp.right);
2994 if (!value) goto failed;
2995 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
2996 goto failed;
2997 Py_DECREF(value);
2998 break;
2999 case UnaryOp_kind:
3000 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3001 if (!result) goto failed;
3002 value = ast2obj_unaryop(o->v.UnaryOp.op);
3003 if (!value) goto failed;
3004 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3005 goto failed;
3006 Py_DECREF(value);
3007 value = ast2obj_expr(o->v.UnaryOp.operand);
3008 if (!value) goto failed;
3009 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3010 goto failed;
3011 Py_DECREF(value);
3012 break;
3013 case Lambda_kind:
3014 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3015 if (!result) goto failed;
3016 value = ast2obj_arguments(o->v.Lambda.args);
3017 if (!value) goto failed;
3018 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
3021 value = ast2obj_expr(o->v.Lambda.body);
3022 if (!value) goto failed;
3023 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3024 goto failed;
3025 Py_DECREF(value);
3026 break;
3027 case IfExp_kind:
3028 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3029 if (!result) goto failed;
3030 value = ast2obj_expr(o->v.IfExp.test);
3031 if (!value) goto failed;
3032 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3033 goto failed;
3034 Py_DECREF(value);
3035 value = ast2obj_expr(o->v.IfExp.body);
3036 if (!value) goto failed;
3037 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3038 goto failed;
3039 Py_DECREF(value);
3040 value = ast2obj_expr(o->v.IfExp.orelse);
3041 if (!value) goto failed;
3042 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3043 goto failed;
3044 Py_DECREF(value);
3045 break;
3046 case Dict_kind:
3047 result = PyType_GenericNew(Dict_type, NULL, NULL);
3048 if (!result) goto failed;
3049 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3050 if (!value) goto failed;
3051 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3052 goto failed;
3053 Py_DECREF(value);
3054 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3055 if (!value) goto failed;
3056 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3057 goto failed;
3058 Py_DECREF(value);
3059 break;
3060 case Set_kind:
3061 result = PyType_GenericNew(Set_type, NULL, NULL);
3062 if (!result) goto failed;
3063 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3064 if (!value) goto failed;
3065 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3066 goto failed;
3067 Py_DECREF(value);
3068 break;
3069 case ListComp_kind:
3070 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3071 if (!result) goto failed;
3072 value = ast2obj_expr(o->v.ListComp.elt);
3073 if (!value) goto failed;
3074 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3075 goto failed;
3076 Py_DECREF(value);
3077 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3078 if (!value) goto failed;
3079 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3080 goto failed;
3081 Py_DECREF(value);
3082 break;
3083 case SetComp_kind:
3084 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3085 if (!result) goto failed;
3086 value = ast2obj_expr(o->v.SetComp.elt);
3087 if (!value) goto failed;
3088 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3089 goto failed;
3090 Py_DECREF(value);
3091 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3092 if (!value) goto failed;
3093 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3094 goto failed;
3095 Py_DECREF(value);
3096 break;
3097 case DictComp_kind:
3098 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3099 if (!result) goto failed;
3100 value = ast2obj_expr(o->v.DictComp.key);
3101 if (!value) goto failed;
3102 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3103 goto failed;
3104 Py_DECREF(value);
3105 value = ast2obj_expr(o->v.DictComp.value);
3106 if (!value) goto failed;
3107 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3108 goto failed;
3109 Py_DECREF(value);
3110 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3111 if (!value) goto failed;
3112 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3113 goto failed;
3114 Py_DECREF(value);
3115 break;
3116 case GeneratorExp_kind:
3117 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3118 if (!result) goto failed;
3119 value = ast2obj_expr(o->v.GeneratorExp.elt);
3120 if (!value) goto failed;
3121 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3122 goto failed;
3123 Py_DECREF(value);
3124 value = ast2obj_list(o->v.GeneratorExp.generators,
3125 ast2obj_comprehension);
3126 if (!value) goto failed;
3127 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
3130 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003131 case Await_kind:
3132 result = PyType_GenericNew(Await_type, NULL, NULL);
3133 if (!result) goto failed;
3134 value = ast2obj_expr(o->v.Await.value);
3135 if (!value) goto failed;
3136 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3137 goto failed;
3138 Py_DECREF(value);
3139 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003140 case Yield_kind:
3141 result = PyType_GenericNew(Yield_type, NULL, NULL);
3142 if (!result) goto failed;
3143 value = ast2obj_expr(o->v.Yield.value);
3144 if (!value) goto failed;
3145 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
3148 break;
3149 case YieldFrom_kind:
3150 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3151 if (!result) goto failed;
3152 value = ast2obj_expr(o->v.YieldFrom.value);
3153 if (!value) goto failed;
3154 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3155 goto failed;
3156 Py_DECREF(value);
3157 break;
3158 case Compare_kind:
3159 result = PyType_GenericNew(Compare_type, NULL, NULL);
3160 if (!result) goto failed;
3161 value = ast2obj_expr(o->v.Compare.left);
3162 if (!value) goto failed;
3163 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
3166 {
3167 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3168 value = PyList_New(n);
3169 if (!value) goto failed;
3170 for(i = 0; i < n; i++)
3171 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003173 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003174 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3175 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003176 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003177 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003178 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003179 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3180 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003181 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003182 break;
3183 case Call_kind:
3184 result = PyType_GenericNew(Call_type, NULL, NULL);
3185 if (!result) goto failed;
3186 value = ast2obj_expr(o->v.Call.func);
3187 if (!value) goto failed;
3188 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3189 goto failed;
3190 Py_DECREF(value);
3191 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3192 if (!value) goto failed;
3193 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3194 goto failed;
3195 Py_DECREF(value);
3196 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3197 if (!value) goto failed;
3198 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3199 goto failed;
3200 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003201 break;
3202 case Num_kind:
3203 result = PyType_GenericNew(Num_type, NULL, NULL);
3204 if (!result) goto failed;
3205 value = ast2obj_object(o->v.Num.n);
3206 if (!value) goto failed;
3207 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3208 goto failed;
3209 Py_DECREF(value);
3210 break;
3211 case Str_kind:
3212 result = PyType_GenericNew(Str_type, NULL, NULL);
3213 if (!result) goto failed;
3214 value = ast2obj_string(o->v.Str.s);
3215 if (!value) goto failed;
3216 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3217 goto failed;
3218 Py_DECREF(value);
3219 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003220 case FormattedValue_kind:
3221 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3222 if (!result) goto failed;
3223 value = ast2obj_expr(o->v.FormattedValue.value);
3224 if (!value) goto failed;
3225 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3226 goto failed;
3227 Py_DECREF(value);
3228 value = ast2obj_int(o->v.FormattedValue.conversion);
3229 if (!value) goto failed;
3230 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3231 goto failed;
3232 Py_DECREF(value);
3233 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3234 if (!value) goto failed;
3235 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3236 goto failed;
3237 Py_DECREF(value);
3238 break;
3239 case JoinedStr_kind:
3240 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3241 if (!result) goto failed;
3242 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3243 if (!value) goto failed;
3244 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3245 goto failed;
3246 Py_DECREF(value);
3247 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003248 case Bytes_kind:
3249 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3250 if (!result) goto failed;
3251 value = ast2obj_bytes(o->v.Bytes.s);
3252 if (!value) goto failed;
3253 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3254 goto failed;
3255 Py_DECREF(value);
3256 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003257 case NameConstant_kind:
3258 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3259 if (!result) goto failed;
3260 value = ast2obj_singleton(o->v.NameConstant.value);
3261 if (!value) goto failed;
3262 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3263 goto failed;
3264 Py_DECREF(value);
3265 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003266 case Ellipsis_kind:
3267 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3268 if (!result) goto failed;
3269 break;
3270 case Attribute_kind:
3271 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3272 if (!result) goto failed;
3273 value = ast2obj_expr(o->v.Attribute.value);
3274 if (!value) goto failed;
3275 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3276 goto failed;
3277 Py_DECREF(value);
3278 value = ast2obj_identifier(o->v.Attribute.attr);
3279 if (!value) goto failed;
3280 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3281 goto failed;
3282 Py_DECREF(value);
3283 value = ast2obj_expr_context(o->v.Attribute.ctx);
3284 if (!value) goto failed;
3285 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3286 goto failed;
3287 Py_DECREF(value);
3288 break;
3289 case Subscript_kind:
3290 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3291 if (!result) goto failed;
3292 value = ast2obj_expr(o->v.Subscript.value);
3293 if (!value) goto failed;
3294 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3295 goto failed;
3296 Py_DECREF(value);
3297 value = ast2obj_slice(o->v.Subscript.slice);
3298 if (!value) goto failed;
3299 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3300 goto failed;
3301 Py_DECREF(value);
3302 value = ast2obj_expr_context(o->v.Subscript.ctx);
3303 if (!value) goto failed;
3304 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3305 goto failed;
3306 Py_DECREF(value);
3307 break;
3308 case Starred_kind:
3309 result = PyType_GenericNew(Starred_type, NULL, NULL);
3310 if (!result) goto failed;
3311 value = ast2obj_expr(o->v.Starred.value);
3312 if (!value) goto failed;
3313 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3314 goto failed;
3315 Py_DECREF(value);
3316 value = ast2obj_expr_context(o->v.Starred.ctx);
3317 if (!value) goto failed;
3318 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3319 goto failed;
3320 Py_DECREF(value);
3321 break;
3322 case Name_kind:
3323 result = PyType_GenericNew(Name_type, NULL, NULL);
3324 if (!result) goto failed;
3325 value = ast2obj_identifier(o->v.Name.id);
3326 if (!value) goto failed;
3327 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3328 goto failed;
3329 Py_DECREF(value);
3330 value = ast2obj_expr_context(o->v.Name.ctx);
3331 if (!value) goto failed;
3332 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3333 goto failed;
3334 Py_DECREF(value);
3335 break;
3336 case List_kind:
3337 result = PyType_GenericNew(List_type, NULL, NULL);
3338 if (!result) goto failed;
3339 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3340 if (!value) goto failed;
3341 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3342 goto failed;
3343 Py_DECREF(value);
3344 value = ast2obj_expr_context(o->v.List.ctx);
3345 if (!value) goto failed;
3346 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3347 goto failed;
3348 Py_DECREF(value);
3349 break;
3350 case Tuple_kind:
3351 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3352 if (!result) goto failed;
3353 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3354 if (!value) goto failed;
3355 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3356 goto failed;
3357 Py_DECREF(value);
3358 value = ast2obj_expr_context(o->v.Tuple.ctx);
3359 if (!value) goto failed;
3360 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3361 goto failed;
3362 Py_DECREF(value);
3363 break;
3364 }
3365 value = ast2obj_int(o->lineno);
3366 if (!value) goto failed;
3367 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3368 goto failed;
3369 Py_DECREF(value);
3370 value = ast2obj_int(o->col_offset);
3371 if (!value) goto failed;
3372 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3373 goto failed;
3374 Py_DECREF(value);
3375 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003376failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003377 Py_XDECREF(value);
3378 Py_XDECREF(result);
3379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380}
3381
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003382PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003384 switch(o) {
3385 case Load:
3386 Py_INCREF(Load_singleton);
3387 return Load_singleton;
3388 case Store:
3389 Py_INCREF(Store_singleton);
3390 return Store_singleton;
3391 case Del:
3392 Py_INCREF(Del_singleton);
3393 return Del_singleton;
3394 case AugLoad:
3395 Py_INCREF(AugLoad_singleton);
3396 return AugLoad_singleton;
3397 case AugStore:
3398 Py_INCREF(AugStore_singleton);
3399 return AugStore_singleton;
3400 case Param:
3401 Py_INCREF(Param_singleton);
3402 return Param_singleton;
3403 default:
3404 /* should never happen, but just in case ... */
3405 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3406 return NULL;
3407 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003409PyObject*
3410ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003412 slice_ty o = (slice_ty)_o;
3413 PyObject *result = NULL, *value = NULL;
3414 if (!o) {
3415 Py_INCREF(Py_None);
3416 return Py_None;
3417 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003418
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003419 switch (o->kind) {
3420 case Slice_kind:
3421 result = PyType_GenericNew(Slice_type, NULL, NULL);
3422 if (!result) goto failed;
3423 value = ast2obj_expr(o->v.Slice.lower);
3424 if (!value) goto failed;
3425 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3426 goto failed;
3427 Py_DECREF(value);
3428 value = ast2obj_expr(o->v.Slice.upper);
3429 if (!value) goto failed;
3430 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3431 goto failed;
3432 Py_DECREF(value);
3433 value = ast2obj_expr(o->v.Slice.step);
3434 if (!value) goto failed;
3435 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3436 goto failed;
3437 Py_DECREF(value);
3438 break;
3439 case ExtSlice_kind:
3440 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3441 if (!result) goto failed;
3442 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3443 if (!value) goto failed;
3444 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3445 goto failed;
3446 Py_DECREF(value);
3447 break;
3448 case Index_kind:
3449 result = PyType_GenericNew(Index_type, NULL, NULL);
3450 if (!result) goto failed;
3451 value = ast2obj_expr(o->v.Index.value);
3452 if (!value) goto failed;
3453 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3454 goto failed;
3455 Py_DECREF(value);
3456 break;
3457 }
3458 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003459failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003460 Py_XDECREF(value);
3461 Py_XDECREF(result);
3462 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463}
3464
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003465PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003467 switch(o) {
3468 case And:
3469 Py_INCREF(And_singleton);
3470 return And_singleton;
3471 case Or:
3472 Py_INCREF(Or_singleton);
3473 return Or_singleton;
3474 default:
3475 /* should never happen, but just in case ... */
3476 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3477 return NULL;
3478 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003479}
3480PyObject* ast2obj_operator(operator_ty o)
3481{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003482 switch(o) {
3483 case Add:
3484 Py_INCREF(Add_singleton);
3485 return Add_singleton;
3486 case Sub:
3487 Py_INCREF(Sub_singleton);
3488 return Sub_singleton;
3489 case Mult:
3490 Py_INCREF(Mult_singleton);
3491 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003492 case MatMult:
3493 Py_INCREF(MatMult_singleton);
3494 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003495 case Div:
3496 Py_INCREF(Div_singleton);
3497 return Div_singleton;
3498 case Mod:
3499 Py_INCREF(Mod_singleton);
3500 return Mod_singleton;
3501 case Pow:
3502 Py_INCREF(Pow_singleton);
3503 return Pow_singleton;
3504 case LShift:
3505 Py_INCREF(LShift_singleton);
3506 return LShift_singleton;
3507 case RShift:
3508 Py_INCREF(RShift_singleton);
3509 return RShift_singleton;
3510 case BitOr:
3511 Py_INCREF(BitOr_singleton);
3512 return BitOr_singleton;
3513 case BitXor:
3514 Py_INCREF(BitXor_singleton);
3515 return BitXor_singleton;
3516 case BitAnd:
3517 Py_INCREF(BitAnd_singleton);
3518 return BitAnd_singleton;
3519 case FloorDiv:
3520 Py_INCREF(FloorDiv_singleton);
3521 return FloorDiv_singleton;
3522 default:
3523 /* should never happen, but just in case ... */
3524 PyErr_Format(PyExc_SystemError, "unknown operator found");
3525 return NULL;
3526 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003527}
3528PyObject* ast2obj_unaryop(unaryop_ty o)
3529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 switch(o) {
3531 case Invert:
3532 Py_INCREF(Invert_singleton);
3533 return Invert_singleton;
3534 case Not:
3535 Py_INCREF(Not_singleton);
3536 return Not_singleton;
3537 case UAdd:
3538 Py_INCREF(UAdd_singleton);
3539 return UAdd_singleton;
3540 case USub:
3541 Py_INCREF(USub_singleton);
3542 return USub_singleton;
3543 default:
3544 /* should never happen, but just in case ... */
3545 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3546 return NULL;
3547 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003548}
3549PyObject* ast2obj_cmpop(cmpop_ty o)
3550{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003551 switch(o) {
3552 case Eq:
3553 Py_INCREF(Eq_singleton);
3554 return Eq_singleton;
3555 case NotEq:
3556 Py_INCREF(NotEq_singleton);
3557 return NotEq_singleton;
3558 case Lt:
3559 Py_INCREF(Lt_singleton);
3560 return Lt_singleton;
3561 case LtE:
3562 Py_INCREF(LtE_singleton);
3563 return LtE_singleton;
3564 case Gt:
3565 Py_INCREF(Gt_singleton);
3566 return Gt_singleton;
3567 case GtE:
3568 Py_INCREF(GtE_singleton);
3569 return GtE_singleton;
3570 case Is:
3571 Py_INCREF(Is_singleton);
3572 return Is_singleton;
3573 case IsNot:
3574 Py_INCREF(IsNot_singleton);
3575 return IsNot_singleton;
3576 case In:
3577 Py_INCREF(In_singleton);
3578 return In_singleton;
3579 case NotIn:
3580 Py_INCREF(NotIn_singleton);
3581 return NotIn_singleton;
3582 default:
3583 /* should never happen, but just in case ... */
3584 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3585 return NULL;
3586 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003587}
3588PyObject*
3589ast2obj_comprehension(void* _o)
3590{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003591 comprehension_ty o = (comprehension_ty)_o;
3592 PyObject *result = NULL, *value = NULL;
3593 if (!o) {
3594 Py_INCREF(Py_None);
3595 return Py_None;
3596 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003597
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003598 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3599 if (!result) return NULL;
3600 value = ast2obj_expr(o->target);
3601 if (!value) goto failed;
3602 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3603 goto failed;
3604 Py_DECREF(value);
3605 value = ast2obj_expr(o->iter);
3606 if (!value) goto failed;
3607 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3608 goto failed;
3609 Py_DECREF(value);
3610 value = ast2obj_list(o->ifs, ast2obj_expr);
3611 if (!value) goto failed;
3612 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
3613 goto failed;
3614 Py_DECREF(value);
3615 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003616failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003617 Py_XDECREF(value);
3618 Py_XDECREF(result);
3619 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620}
3621
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003622PyObject*
3623ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003625 excepthandler_ty o = (excepthandler_ty)_o;
3626 PyObject *result = NULL, *value = NULL;
3627 if (!o) {
3628 Py_INCREF(Py_None);
3629 return Py_None;
3630 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003631
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003632 switch (o->kind) {
3633 case ExceptHandler_kind:
3634 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3635 if (!result) goto failed;
3636 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003637 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003638 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3639 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003640 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003641 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003642 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003643 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3644 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003645 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003646 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3647 if (!value) goto failed;
3648 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3649 goto failed;
3650 Py_DECREF(value);
3651 break;
3652 }
3653 value = ast2obj_int(o->lineno);
3654 if (!value) goto failed;
3655 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3656 goto failed;
3657 Py_DECREF(value);
3658 value = ast2obj_int(o->col_offset);
3659 if (!value) goto failed;
3660 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3661 goto failed;
3662 Py_DECREF(value);
3663 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003664failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003665 Py_XDECREF(value);
3666 Py_XDECREF(result);
3667 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668}
3669
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003670PyObject*
3671ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003673 arguments_ty o = (arguments_ty)_o;
3674 PyObject *result = NULL, *value = NULL;
3675 if (!o) {
3676 Py_INCREF(Py_None);
3677 return Py_None;
3678 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003679
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003680 result = PyType_GenericNew(arguments_type, NULL, NULL);
3681 if (!result) return NULL;
3682 value = ast2obj_list(o->args, ast2obj_arg);
3683 if (!value) goto failed;
3684 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3685 goto failed;
3686 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003687 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003688 if (!value) goto failed;
3689 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3690 goto failed;
3691 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003692 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3693 if (!value) goto failed;
3694 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3695 goto failed;
3696 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003697 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003698 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003699 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003700 goto failed;
3701 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003702 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003703 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003704 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003705 goto failed;
3706 Py_DECREF(value);
3707 value = ast2obj_list(o->defaults, ast2obj_expr);
3708 if (!value) goto failed;
3709 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3710 goto failed;
3711 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003712 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003713failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003714 Py_XDECREF(value);
3715 Py_XDECREF(result);
3716 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717}
3718
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003719PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003720ast2obj_arg(void* _o)
3721{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003722 arg_ty o = (arg_ty)_o;
3723 PyObject *result = NULL, *value = NULL;
3724 if (!o) {
3725 Py_INCREF(Py_None);
3726 return Py_None;
3727 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003728
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003729 result = PyType_GenericNew(arg_type, NULL, NULL);
3730 if (!result) return NULL;
3731 value = ast2obj_identifier(o->arg);
3732 if (!value) goto failed;
3733 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3734 goto failed;
3735 Py_DECREF(value);
3736 value = ast2obj_expr(o->annotation);
3737 if (!value) goto failed;
3738 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3739 goto failed;
3740 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003741 value = ast2obj_int(o->lineno);
3742 if (!value) goto failed;
3743 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3744 goto failed;
3745 Py_DECREF(value);
3746 value = ast2obj_int(o->col_offset);
3747 if (!value) goto failed;
3748 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3749 goto failed;
3750 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003752failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003753 Py_XDECREF(value);
3754 Py_XDECREF(result);
3755 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003756}
3757
3758PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003759ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003761 keyword_ty o = (keyword_ty)_o;
3762 PyObject *result = NULL, *value = NULL;
3763 if (!o) {
3764 Py_INCREF(Py_None);
3765 return Py_None;
3766 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003767
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003768 result = PyType_GenericNew(keyword_type, NULL, NULL);
3769 if (!result) return NULL;
3770 value = ast2obj_identifier(o->arg);
3771 if (!value) goto failed;
3772 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3773 goto failed;
3774 Py_DECREF(value);
3775 value = ast2obj_expr(o->value);
3776 if (!value) goto failed;
3777 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3778 goto failed;
3779 Py_DECREF(value);
3780 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003781failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003782 Py_XDECREF(value);
3783 Py_XDECREF(result);
3784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785}
3786
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003787PyObject*
3788ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003790 alias_ty o = (alias_ty)_o;
3791 PyObject *result = NULL, *value = NULL;
3792 if (!o) {
3793 Py_INCREF(Py_None);
3794 return Py_None;
3795 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003796
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003797 result = PyType_GenericNew(alias_type, NULL, NULL);
3798 if (!result) return NULL;
3799 value = ast2obj_identifier(o->name);
3800 if (!value) goto failed;
3801 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3802 goto failed;
3803 Py_DECREF(value);
3804 value = ast2obj_identifier(o->asname);
3805 if (!value) goto failed;
3806 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3807 goto failed;
3808 Py_DECREF(value);
3809 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003810failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003811 Py_XDECREF(value);
3812 Py_XDECREF(result);
3813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814}
3815
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003816PyObject*
3817ast2obj_withitem(void* _o)
3818{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003819 withitem_ty o = (withitem_ty)_o;
3820 PyObject *result = NULL, *value = NULL;
3821 if (!o) {
3822 Py_INCREF(Py_None);
3823 return Py_None;
3824 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003825
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003826 result = PyType_GenericNew(withitem_type, NULL, NULL);
3827 if (!result) return NULL;
3828 value = ast2obj_expr(o->context_expr);
3829 if (!value) goto failed;
3830 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3831 goto failed;
3832 Py_DECREF(value);
3833 value = ast2obj_expr(o->optional_vars);
3834 if (!value) goto failed;
3835 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3836 goto failed;
3837 Py_DECREF(value);
3838 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003839failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003840 Py_XDECREF(value);
3841 Py_XDECREF(result);
3842 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003843}
3844
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003846int
3847obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3848{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003849 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003850
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003851 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003852
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003853 if (obj == Py_None) {
3854 *out = NULL;
3855 return 0;
3856 }
3857 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3858 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003859 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003860 }
3861 if (isinstance) {
3862 asdl_seq* body;
3863
3864 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3865 int res;
3866 Py_ssize_t len;
3867 Py_ssize_t i;
3868 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3869 if (tmp == NULL) goto failed;
3870 if (!PyList_Check(tmp)) {
3871 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3872 goto failed;
3873 }
3874 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003875 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003876 if (body == NULL) goto failed;
3877 for (i = 0; i < len; i++) {
3878 stmt_ty value;
3879 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3880 if (res != 0) goto failed;
3881 asdl_seq_SET(body, i, value);
3882 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003883 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003884 } else {
3885 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3886 return 1;
3887 }
3888 *out = Module(body, arena);
3889 if (*out == NULL) goto failed;
3890 return 0;
3891 }
3892 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3893 if (isinstance == -1) {
3894 return 1;
3895 }
3896 if (isinstance) {
3897 asdl_seq* body;
3898
3899 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3900 int res;
3901 Py_ssize_t len;
3902 Py_ssize_t i;
3903 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3904 if (tmp == NULL) goto failed;
3905 if (!PyList_Check(tmp)) {
3906 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3907 goto failed;
3908 }
3909 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003910 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003911 if (body == NULL) goto failed;
3912 for (i = 0; i < len; i++) {
3913 stmt_ty value;
3914 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3915 if (res != 0) goto failed;
3916 asdl_seq_SET(body, i, value);
3917 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003918 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003919 } else {
3920 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3921 return 1;
3922 }
3923 *out = Interactive(body, arena);
3924 if (*out == NULL) goto failed;
3925 return 0;
3926 }
3927 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3928 if (isinstance == -1) {
3929 return 1;
3930 }
3931 if (isinstance) {
3932 expr_ty body;
3933
3934 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3935 int res;
3936 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3937 if (tmp == NULL) goto failed;
3938 res = obj2ast_expr(tmp, &body, arena);
3939 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003940 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003941 } else {
3942 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3943 return 1;
3944 }
3945 *out = Expression(body, arena);
3946 if (*out == NULL) goto failed;
3947 return 0;
3948 }
3949 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3950 if (isinstance == -1) {
3951 return 1;
3952 }
3953 if (isinstance) {
3954 asdl_seq* body;
3955
3956 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3957 int res;
3958 Py_ssize_t len;
3959 Py_ssize_t i;
3960 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3961 if (tmp == NULL) goto failed;
3962 if (!PyList_Check(tmp)) {
3963 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3964 goto failed;
3965 }
3966 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003967 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003968 if (body == NULL) goto failed;
3969 for (i = 0; i < len; i++) {
3970 stmt_ty value;
3971 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3972 if (res != 0) goto failed;
3973 asdl_seq_SET(body, i, value);
3974 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003975 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003976 } else {
3977 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3978 return 1;
3979 }
3980 *out = Suite(body, arena);
3981 if (*out == NULL) goto failed;
3982 return 0;
3983 }
3984
3985 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3986 failed:
3987 Py_XDECREF(tmp);
3988 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003989}
3990
3991int
3992obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3993{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003994 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003995
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003996 PyObject *tmp = NULL;
3997 int lineno;
3998 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003999
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004000 if (obj == Py_None) {
4001 *out = NULL;
4002 return 0;
4003 }
4004 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4005 int res;
4006 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4007 if (tmp == NULL) goto failed;
4008 res = obj2ast_int(tmp, &lineno, arena);
4009 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004010 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004011 } else {
4012 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004013 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004014 }
4015 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4016 int res;
4017 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4018 if (tmp == NULL) goto failed;
4019 res = obj2ast_int(tmp, &col_offset, arena);
4020 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004021 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004022 } else {
4023 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4024 return 1;
4025 }
4026 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4027 if (isinstance == -1) {
4028 return 1;
4029 }
4030 if (isinstance) {
4031 identifier name;
4032 arguments_ty args;
4033 asdl_seq* body;
4034 asdl_seq* decorator_list;
4035 expr_ty returns;
4036
4037 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4038 int res;
4039 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4040 if (tmp == NULL) goto failed;
4041 res = obj2ast_identifier(tmp, &name, arena);
4042 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004043 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004044 } else {
4045 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4046 return 1;
4047 }
4048 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4049 int res;
4050 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4051 if (tmp == NULL) goto failed;
4052 res = obj2ast_arguments(tmp, &args, arena);
4053 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004054 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004055 } else {
4056 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
4057 return 1;
4058 }
4059 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4060 int res;
4061 Py_ssize_t len;
4062 Py_ssize_t i;
4063 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4064 if (tmp == NULL) goto failed;
4065 if (!PyList_Check(tmp)) {
4066 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4067 goto failed;
4068 }
4069 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004070 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004071 if (body == NULL) goto failed;
4072 for (i = 0; i < len; i++) {
4073 stmt_ty value;
4074 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4075 if (res != 0) goto failed;
4076 asdl_seq_SET(body, i, value);
4077 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004078 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004079 } else {
4080 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4081 return 1;
4082 }
4083 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4084 int res;
4085 Py_ssize_t len;
4086 Py_ssize_t i;
4087 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4088 if (tmp == NULL) goto failed;
4089 if (!PyList_Check(tmp)) {
4090 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4091 goto failed;
4092 }
4093 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004094 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004095 if (decorator_list == NULL) goto failed;
4096 for (i = 0; i < len; i++) {
4097 expr_ty value;
4098 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4099 if (res != 0) goto failed;
4100 asdl_seq_SET(decorator_list, i, value);
4101 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004102 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004103 } else {
4104 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4105 return 1;
4106 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004107 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004108 int res;
4109 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4110 if (tmp == NULL) goto failed;
4111 res = obj2ast_expr(tmp, &returns, arena);
4112 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004113 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004114 } else {
4115 returns = NULL;
4116 }
4117 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4118 col_offset, arena);
4119 if (*out == NULL) goto failed;
4120 return 0;
4121 }
Yury Selivanov75445082015-05-11 22:57:16 -04004122 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4123 if (isinstance == -1) {
4124 return 1;
4125 }
4126 if (isinstance) {
4127 identifier name;
4128 arguments_ty args;
4129 asdl_seq* body;
4130 asdl_seq* decorator_list;
4131 expr_ty returns;
4132
4133 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4134 int res;
4135 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4136 if (tmp == NULL) goto failed;
4137 res = obj2ast_identifier(tmp, &name, arena);
4138 if (res != 0) goto failed;
4139 Py_CLEAR(tmp);
4140 } else {
4141 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4142 return 1;
4143 }
4144 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4145 int res;
4146 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4147 if (tmp == NULL) goto failed;
4148 res = obj2ast_arguments(tmp, &args, arena);
4149 if (res != 0) goto failed;
4150 Py_CLEAR(tmp);
4151 } else {
4152 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4153 return 1;
4154 }
4155 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4156 int res;
4157 Py_ssize_t len;
4158 Py_ssize_t i;
4159 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4160 if (tmp == NULL) goto failed;
4161 if (!PyList_Check(tmp)) {
4162 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4163 goto failed;
4164 }
4165 len = PyList_GET_SIZE(tmp);
4166 body = _Py_asdl_seq_new(len, arena);
4167 if (body == NULL) goto failed;
4168 for (i = 0; i < len; i++) {
4169 stmt_ty value;
4170 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4171 if (res != 0) goto failed;
4172 asdl_seq_SET(body, i, value);
4173 }
4174 Py_CLEAR(tmp);
4175 } else {
4176 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4177 return 1;
4178 }
4179 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4180 int res;
4181 Py_ssize_t len;
4182 Py_ssize_t i;
4183 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4184 if (tmp == NULL) goto failed;
4185 if (!PyList_Check(tmp)) {
4186 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4187 goto failed;
4188 }
4189 len = PyList_GET_SIZE(tmp);
4190 decorator_list = _Py_asdl_seq_new(len, arena);
4191 if (decorator_list == NULL) goto failed;
4192 for (i = 0; i < len; i++) {
4193 expr_ty value;
4194 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4195 if (res != 0) goto failed;
4196 asdl_seq_SET(decorator_list, i, value);
4197 }
4198 Py_CLEAR(tmp);
4199 } else {
4200 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4201 return 1;
4202 }
4203 if (exists_not_none(obj, &PyId_returns)) {
4204 int res;
4205 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4206 if (tmp == NULL) goto failed;
4207 res = obj2ast_expr(tmp, &returns, arena);
4208 if (res != 0) goto failed;
4209 Py_CLEAR(tmp);
4210 } else {
4211 returns = NULL;
4212 }
4213 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4214 lineno, col_offset, arena);
4215 if (*out == NULL) goto failed;
4216 return 0;
4217 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004218 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4219 if (isinstance == -1) {
4220 return 1;
4221 }
4222 if (isinstance) {
4223 identifier name;
4224 asdl_seq* bases;
4225 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004226 asdl_seq* body;
4227 asdl_seq* decorator_list;
4228
4229 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4230 int res;
4231 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4232 if (tmp == NULL) goto failed;
4233 res = obj2ast_identifier(tmp, &name, arena);
4234 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004235 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004236 } else {
4237 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
4238 return 1;
4239 }
4240 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4241 int res;
4242 Py_ssize_t len;
4243 Py_ssize_t i;
4244 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4245 if (tmp == NULL) goto failed;
4246 if (!PyList_Check(tmp)) {
4247 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4248 goto failed;
4249 }
4250 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004251 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004252 if (bases == NULL) goto failed;
4253 for (i = 0; i < len; i++) {
4254 expr_ty value;
4255 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4256 if (res != 0) goto failed;
4257 asdl_seq_SET(bases, i, value);
4258 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004259 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004260 } else {
4261 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
4262 return 1;
4263 }
4264 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4265 int res;
4266 Py_ssize_t len;
4267 Py_ssize_t i;
4268 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4269 if (tmp == NULL) goto failed;
4270 if (!PyList_Check(tmp)) {
4271 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4272 goto failed;
4273 }
4274 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004275 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004276 if (keywords == NULL) goto failed;
4277 for (i = 0; i < len; i++) {
4278 keyword_ty value;
4279 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4280 if (res != 0) goto failed;
4281 asdl_seq_SET(keywords, i, value);
4282 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004283 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004284 } else {
4285 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
4286 return 1;
4287 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004288 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4289 int res;
4290 Py_ssize_t len;
4291 Py_ssize_t i;
4292 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4293 if (tmp == NULL) goto failed;
4294 if (!PyList_Check(tmp)) {
4295 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4296 goto failed;
4297 }
4298 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004299 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004300 if (body == NULL) goto failed;
4301 for (i = 0; i < len; i++) {
4302 stmt_ty value;
4303 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4304 if (res != 0) goto failed;
4305 asdl_seq_SET(body, i, value);
4306 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004307 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004308 } else {
4309 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4310 return 1;
4311 }
4312 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4313 int res;
4314 Py_ssize_t len;
4315 Py_ssize_t i;
4316 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4317 if (tmp == NULL) goto failed;
4318 if (!PyList_Check(tmp)) {
4319 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4320 goto failed;
4321 }
4322 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004323 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004324 if (decorator_list == NULL) goto failed;
4325 for (i = 0; i < len; i++) {
4326 expr_ty value;
4327 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4328 if (res != 0) goto failed;
4329 asdl_seq_SET(decorator_list, i, value);
4330 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004331 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004332 } else {
4333 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4334 return 1;
4335 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004336 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4337 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004338 if (*out == NULL) goto failed;
4339 return 0;
4340 }
4341 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4342 if (isinstance == -1) {
4343 return 1;
4344 }
4345 if (isinstance) {
4346 expr_ty value;
4347
Victor Stinneree4b59c2013-07-27 00:01:35 +02004348 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004349 int res;
4350 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4351 if (tmp == NULL) goto failed;
4352 res = obj2ast_expr(tmp, &value, arena);
4353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004355 } else {
4356 value = NULL;
4357 }
4358 *out = Return(value, lineno, col_offset, arena);
4359 if (*out == NULL) goto failed;
4360 return 0;
4361 }
4362 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4363 if (isinstance == -1) {
4364 return 1;
4365 }
4366 if (isinstance) {
4367 asdl_seq* targets;
4368
4369 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4370 int res;
4371 Py_ssize_t len;
4372 Py_ssize_t i;
4373 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4374 if (tmp == NULL) goto failed;
4375 if (!PyList_Check(tmp)) {
4376 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4377 goto failed;
4378 }
4379 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004380 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004381 if (targets == NULL) goto failed;
4382 for (i = 0; i < len; i++) {
4383 expr_ty value;
4384 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4385 if (res != 0) goto failed;
4386 asdl_seq_SET(targets, i, value);
4387 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004388 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004389 } else {
4390 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4391 return 1;
4392 }
4393 *out = Delete(targets, lineno, col_offset, arena);
4394 if (*out == NULL) goto failed;
4395 return 0;
4396 }
4397 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4398 if (isinstance == -1) {
4399 return 1;
4400 }
4401 if (isinstance) {
4402 asdl_seq* targets;
4403 expr_ty value;
4404
4405 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4406 int res;
4407 Py_ssize_t len;
4408 Py_ssize_t i;
4409 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4410 if (tmp == NULL) goto failed;
4411 if (!PyList_Check(tmp)) {
4412 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4413 goto failed;
4414 }
4415 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004416 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004417 if (targets == NULL) goto failed;
4418 for (i = 0; i < len; i++) {
4419 expr_ty value;
4420 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4421 if (res != 0) goto failed;
4422 asdl_seq_SET(targets, i, value);
4423 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004424 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004425 } else {
4426 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4427 return 1;
4428 }
4429 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4430 int res;
4431 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4432 if (tmp == NULL) goto failed;
4433 res = obj2ast_expr(tmp, &value, arena);
4434 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004436 } else {
4437 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4438 return 1;
4439 }
4440 *out = Assign(targets, value, lineno, col_offset, arena);
4441 if (*out == NULL) goto failed;
4442 return 0;
4443 }
4444 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4445 if (isinstance == -1) {
4446 return 1;
4447 }
4448 if (isinstance) {
4449 expr_ty target;
4450 operator_ty op;
4451 expr_ty value;
4452
4453 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4454 int res;
4455 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4456 if (tmp == NULL) goto failed;
4457 res = obj2ast_expr(tmp, &target, arena);
4458 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004459 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004460 } else {
4461 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4462 return 1;
4463 }
4464 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4465 int res;
4466 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4467 if (tmp == NULL) goto failed;
4468 res = obj2ast_operator(tmp, &op, arena);
4469 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004470 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004471 } else {
4472 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4473 return 1;
4474 }
4475 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4476 int res;
4477 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4478 if (tmp == NULL) goto failed;
4479 res = obj2ast_expr(tmp, &value, arena);
4480 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004481 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004482 } else {
4483 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4484 return 1;
4485 }
4486 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4487 if (*out == NULL) goto failed;
4488 return 0;
4489 }
4490 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4491 if (isinstance == -1) {
4492 return 1;
4493 }
4494 if (isinstance) {
4495 expr_ty target;
4496 expr_ty iter;
4497 asdl_seq* body;
4498 asdl_seq* orelse;
4499
4500 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4501 int res;
4502 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4503 if (tmp == NULL) goto failed;
4504 res = obj2ast_expr(tmp, &target, arena);
4505 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004507 } else {
4508 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4509 return 1;
4510 }
4511 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4512 int res;
4513 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4514 if (tmp == NULL) goto failed;
4515 res = obj2ast_expr(tmp, &iter, arena);
4516 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004517 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004518 } else {
4519 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
4520 return 1;
4521 }
4522 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4523 int res;
4524 Py_ssize_t len;
4525 Py_ssize_t i;
4526 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4527 if (tmp == NULL) goto failed;
4528 if (!PyList_Check(tmp)) {
4529 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4530 goto failed;
4531 }
4532 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004533 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004534 if (body == NULL) goto failed;
4535 for (i = 0; i < len; i++) {
4536 stmt_ty value;
4537 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4538 if (res != 0) goto failed;
4539 asdl_seq_SET(body, i, value);
4540 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004541 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004542 } else {
4543 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4544 return 1;
4545 }
4546 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4547 int res;
4548 Py_ssize_t len;
4549 Py_ssize_t i;
4550 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4551 if (tmp == NULL) goto failed;
4552 if (!PyList_Check(tmp)) {
4553 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4554 goto failed;
4555 }
4556 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004557 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004558 if (orelse == NULL) goto failed;
4559 for (i = 0; i < len; i++) {
4560 stmt_ty value;
4561 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4562 if (res != 0) goto failed;
4563 asdl_seq_SET(orelse, i, value);
4564 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004565 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004566 } else {
4567 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4568 return 1;
4569 }
4570 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4571 if (*out == NULL) goto failed;
4572 return 0;
4573 }
Yury Selivanov75445082015-05-11 22:57:16 -04004574 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4575 if (isinstance == -1) {
4576 return 1;
4577 }
4578 if (isinstance) {
4579 expr_ty target;
4580 expr_ty iter;
4581 asdl_seq* body;
4582 asdl_seq* orelse;
4583
4584 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4585 int res;
4586 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4587 if (tmp == NULL) goto failed;
4588 res = obj2ast_expr(tmp, &target, arena);
4589 if (res != 0) goto failed;
4590 Py_CLEAR(tmp);
4591 } else {
4592 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4593 return 1;
4594 }
4595 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4596 int res;
4597 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4598 if (tmp == NULL) goto failed;
4599 res = obj2ast_expr(tmp, &iter, arena);
4600 if (res != 0) goto failed;
4601 Py_CLEAR(tmp);
4602 } else {
4603 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4604 return 1;
4605 }
4606 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4607 int res;
4608 Py_ssize_t len;
4609 Py_ssize_t i;
4610 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4611 if (tmp == NULL) goto failed;
4612 if (!PyList_Check(tmp)) {
4613 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4614 goto failed;
4615 }
4616 len = PyList_GET_SIZE(tmp);
4617 body = _Py_asdl_seq_new(len, arena);
4618 if (body == NULL) goto failed;
4619 for (i = 0; i < len; i++) {
4620 stmt_ty value;
4621 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4622 if (res != 0) goto failed;
4623 asdl_seq_SET(body, i, value);
4624 }
4625 Py_CLEAR(tmp);
4626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4628 return 1;
4629 }
4630 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4631 int res;
4632 Py_ssize_t len;
4633 Py_ssize_t i;
4634 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4635 if (tmp == NULL) goto failed;
4636 if (!PyList_Check(tmp)) {
4637 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4638 goto failed;
4639 }
4640 len = PyList_GET_SIZE(tmp);
4641 orelse = _Py_asdl_seq_new(len, arena);
4642 if (orelse == NULL) goto failed;
4643 for (i = 0; i < len; i++) {
4644 stmt_ty value;
4645 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4646 if (res != 0) goto failed;
4647 asdl_seq_SET(orelse, i, value);
4648 }
4649 Py_CLEAR(tmp);
4650 } else {
4651 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4652 return 1;
4653 }
4654 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4655 if (*out == NULL) goto failed;
4656 return 0;
4657 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004658 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4659 if (isinstance == -1) {
4660 return 1;
4661 }
4662 if (isinstance) {
4663 expr_ty test;
4664 asdl_seq* body;
4665 asdl_seq* orelse;
4666
4667 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4668 int res;
4669 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4670 if (tmp == NULL) goto failed;
4671 res = obj2ast_expr(tmp, &test, arena);
4672 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004673 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004674 } else {
4675 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4676 return 1;
4677 }
4678 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4679 int res;
4680 Py_ssize_t len;
4681 Py_ssize_t i;
4682 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4683 if (tmp == NULL) goto failed;
4684 if (!PyList_Check(tmp)) {
4685 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4686 goto failed;
4687 }
4688 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004689 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004690 if (body == NULL) goto failed;
4691 for (i = 0; i < len; i++) {
4692 stmt_ty value;
4693 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4694 if (res != 0) goto failed;
4695 asdl_seq_SET(body, i, value);
4696 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004697 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004698 } else {
4699 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4700 return 1;
4701 }
4702 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4703 int res;
4704 Py_ssize_t len;
4705 Py_ssize_t i;
4706 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4707 if (tmp == NULL) goto failed;
4708 if (!PyList_Check(tmp)) {
4709 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4710 goto failed;
4711 }
4712 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004713 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004714 if (orelse == NULL) goto failed;
4715 for (i = 0; i < len; i++) {
4716 stmt_ty value;
4717 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4718 if (res != 0) goto failed;
4719 asdl_seq_SET(orelse, i, value);
4720 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004721 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004722 } else {
4723 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4724 return 1;
4725 }
4726 *out = While(test, body, orelse, lineno, col_offset, arena);
4727 if (*out == NULL) goto failed;
4728 return 0;
4729 }
4730 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4731 if (isinstance == -1) {
4732 return 1;
4733 }
4734 if (isinstance) {
4735 expr_ty test;
4736 asdl_seq* body;
4737 asdl_seq* orelse;
4738
4739 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4740 int res;
4741 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4742 if (tmp == NULL) goto failed;
4743 res = obj2ast_expr(tmp, &test, arena);
4744 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004745 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4748 return 1;
4749 }
4750 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4758 goto failed;
4759 }
4760 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004761 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004762 if (body == NULL) goto failed;
4763 for (i = 0; i < len; i++) {
4764 stmt_ty value;
4765 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4766 if (res != 0) goto failed;
4767 asdl_seq_SET(body, i, value);
4768 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004769 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004770 } else {
4771 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4772 return 1;
4773 }
4774 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4775 int res;
4776 Py_ssize_t len;
4777 Py_ssize_t i;
4778 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4779 if (tmp == NULL) goto failed;
4780 if (!PyList_Check(tmp)) {
4781 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4782 goto failed;
4783 }
4784 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004785 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004786 if (orelse == NULL) goto failed;
4787 for (i = 0; i < len; i++) {
4788 stmt_ty value;
4789 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4790 if (res != 0) goto failed;
4791 asdl_seq_SET(orelse, i, value);
4792 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004793 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004794 } else {
4795 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4796 return 1;
4797 }
4798 *out = If(test, body, orelse, lineno, col_offset, arena);
4799 if (*out == NULL) goto failed;
4800 return 0;
4801 }
4802 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4803 if (isinstance == -1) {
4804 return 1;
4805 }
4806 if (isinstance) {
4807 asdl_seq* items;
4808 asdl_seq* body;
4809
4810 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4811 int res;
4812 Py_ssize_t len;
4813 Py_ssize_t i;
4814 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4815 if (tmp == NULL) goto failed;
4816 if (!PyList_Check(tmp)) {
4817 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4818 goto failed;
4819 }
4820 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004821 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004822 if (items == NULL) goto failed;
4823 for (i = 0; i < len; i++) {
4824 withitem_ty value;
4825 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4826 if (res != 0) goto failed;
4827 asdl_seq_SET(items, i, value);
4828 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004829 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004830 } else {
4831 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4832 return 1;
4833 }
4834 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4835 int res;
4836 Py_ssize_t len;
4837 Py_ssize_t i;
4838 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4839 if (tmp == NULL) goto failed;
4840 if (!PyList_Check(tmp)) {
4841 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4842 goto failed;
4843 }
4844 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004845 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004846 if (body == NULL) goto failed;
4847 for (i = 0; i < len; i++) {
4848 stmt_ty value;
4849 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4850 if (res != 0) goto failed;
4851 asdl_seq_SET(body, i, value);
4852 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004853 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004854 } else {
4855 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4856 return 1;
4857 }
4858 *out = With(items, body, lineno, col_offset, arena);
4859 if (*out == NULL) goto failed;
4860 return 0;
4861 }
Yury Selivanov75445082015-05-11 22:57:16 -04004862 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4863 if (isinstance == -1) {
4864 return 1;
4865 }
4866 if (isinstance) {
4867 asdl_seq* items;
4868 asdl_seq* body;
4869
4870 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4871 int res;
4872 Py_ssize_t len;
4873 Py_ssize_t i;
4874 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4875 if (tmp == NULL) goto failed;
4876 if (!PyList_Check(tmp)) {
4877 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4878 goto failed;
4879 }
4880 len = PyList_GET_SIZE(tmp);
4881 items = _Py_asdl_seq_new(len, arena);
4882 if (items == NULL) goto failed;
4883 for (i = 0; i < len; i++) {
4884 withitem_ty value;
4885 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4886 if (res != 0) goto failed;
4887 asdl_seq_SET(items, i, value);
4888 }
4889 Py_CLEAR(tmp);
4890 } else {
4891 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4892 return 1;
4893 }
4894 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4895 int res;
4896 Py_ssize_t len;
4897 Py_ssize_t i;
4898 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4899 if (tmp == NULL) goto failed;
4900 if (!PyList_Check(tmp)) {
4901 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4902 goto failed;
4903 }
4904 len = PyList_GET_SIZE(tmp);
4905 body = _Py_asdl_seq_new(len, arena);
4906 if (body == NULL) goto failed;
4907 for (i = 0; i < len; i++) {
4908 stmt_ty value;
4909 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4910 if (res != 0) goto failed;
4911 asdl_seq_SET(body, i, value);
4912 }
4913 Py_CLEAR(tmp);
4914 } else {
4915 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4916 return 1;
4917 }
4918 *out = AsyncWith(items, body, lineno, col_offset, arena);
4919 if (*out == NULL) goto failed;
4920 return 0;
4921 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004922 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4923 if (isinstance == -1) {
4924 return 1;
4925 }
4926 if (isinstance) {
4927 expr_ty exc;
4928 expr_ty cause;
4929
Victor Stinneree4b59c2013-07-27 00:01:35 +02004930 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004931 int res;
4932 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4933 if (tmp == NULL) goto failed;
4934 res = obj2ast_expr(tmp, &exc, arena);
4935 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004936 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004937 } else {
4938 exc = NULL;
4939 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004940 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004941 int res;
4942 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4943 if (tmp == NULL) goto failed;
4944 res = obj2ast_expr(tmp, &cause, arena);
4945 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004946 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004947 } else {
4948 cause = NULL;
4949 }
4950 *out = Raise(exc, cause, lineno, col_offset, arena);
4951 if (*out == NULL) goto failed;
4952 return 0;
4953 }
4954 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
4955 if (isinstance == -1) {
4956 return 1;
4957 }
4958 if (isinstance) {
4959 asdl_seq* body;
4960 asdl_seq* handlers;
4961 asdl_seq* orelse;
4962 asdl_seq* finalbody;
4963
4964 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4965 int res;
4966 Py_ssize_t len;
4967 Py_ssize_t i;
4968 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4969 if (tmp == NULL) goto failed;
4970 if (!PyList_Check(tmp)) {
4971 PyErr_Format(PyExc_TypeError, "Try field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4972 goto failed;
4973 }
4974 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004975 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004976 if (body == NULL) goto failed;
4977 for (i = 0; i < len; i++) {
4978 stmt_ty value;
4979 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4980 if (res != 0) goto failed;
4981 asdl_seq_SET(body, i, value);
4982 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004983 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004984 } else {
4985 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Try");
4986 return 1;
4987 }
4988 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
4989 int res;
4990 Py_ssize_t len;
4991 Py_ssize_t i;
4992 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
4993 if (tmp == NULL) goto failed;
4994 if (!PyList_Check(tmp)) {
4995 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4996 goto failed;
4997 }
4998 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004999 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005000 if (handlers == NULL) goto failed;
5001 for (i = 0; i < len; i++) {
5002 excepthandler_ty value;
5003 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5004 if (res != 0) goto failed;
5005 asdl_seq_SET(handlers, i, value);
5006 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005007 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005008 } else {
5009 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from Try");
5010 return 1;
5011 }
5012 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5013 int res;
5014 Py_ssize_t len;
5015 Py_ssize_t i;
5016 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5017 if (tmp == NULL) goto failed;
5018 if (!PyList_Check(tmp)) {
5019 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5020 goto failed;
5021 }
5022 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005023 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005024 if (orelse == NULL) goto failed;
5025 for (i = 0; i < len; i++) {
5026 stmt_ty value;
5027 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5028 if (res != 0) goto failed;
5029 asdl_seq_SET(orelse, i, value);
5030 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005031 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005032 } else {
5033 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from Try");
5034 return 1;
5035 }
5036 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5037 int res;
5038 Py_ssize_t len;
5039 Py_ssize_t i;
5040 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5041 if (tmp == NULL) goto failed;
5042 if (!PyList_Check(tmp)) {
5043 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5044 goto failed;
5045 }
5046 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005047 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005048 if (finalbody == NULL) goto failed;
5049 for (i = 0; i < len; i++) {
5050 stmt_ty value;
5051 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5052 if (res != 0) goto failed;
5053 asdl_seq_SET(finalbody, i, value);
5054 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005055 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005056 } else {
5057 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5058 return 1;
5059 }
5060 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5061 arena);
5062 if (*out == NULL) goto failed;
5063 return 0;
5064 }
5065 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5066 if (isinstance == -1) {
5067 return 1;
5068 }
5069 if (isinstance) {
5070 expr_ty test;
5071 expr_ty msg;
5072
5073 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5074 int res;
5075 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5076 if (tmp == NULL) goto failed;
5077 res = obj2ast_expr(tmp, &test, arena);
5078 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005079 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005080 } else {
5081 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5082 return 1;
5083 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005084 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005085 int res;
5086 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5087 if (tmp == NULL) goto failed;
5088 res = obj2ast_expr(tmp, &msg, arena);
5089 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005090 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005091 } else {
5092 msg = NULL;
5093 }
5094 *out = Assert(test, msg, lineno, col_offset, arena);
5095 if (*out == NULL) goto failed;
5096 return 0;
5097 }
5098 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5099 if (isinstance == -1) {
5100 return 1;
5101 }
5102 if (isinstance) {
5103 asdl_seq* names;
5104
5105 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5106 int res;
5107 Py_ssize_t len;
5108 Py_ssize_t i;
5109 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5110 if (tmp == NULL) goto failed;
5111 if (!PyList_Check(tmp)) {
5112 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5113 goto failed;
5114 }
5115 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005116 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005117 if (names == NULL) goto failed;
5118 for (i = 0; i < len; i++) {
5119 alias_ty value;
5120 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5121 if (res != 0) goto failed;
5122 asdl_seq_SET(names, i, value);
5123 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005124 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005125 } else {
5126 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5127 return 1;
5128 }
5129 *out = Import(names, lineno, col_offset, arena);
5130 if (*out == NULL) goto failed;
5131 return 0;
5132 }
5133 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5134 if (isinstance == -1) {
5135 return 1;
5136 }
5137 if (isinstance) {
5138 identifier module;
5139 asdl_seq* names;
5140 int level;
5141
Victor Stinneree4b59c2013-07-27 00:01:35 +02005142 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005143 int res;
5144 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5145 if (tmp == NULL) goto failed;
5146 res = obj2ast_identifier(tmp, &module, arena);
5147 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005148 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005149 } else {
5150 module = NULL;
5151 }
5152 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5153 int res;
5154 Py_ssize_t len;
5155 Py_ssize_t i;
5156 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5157 if (tmp == NULL) goto failed;
5158 if (!PyList_Check(tmp)) {
5159 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5160 goto failed;
5161 }
5162 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005163 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005164 if (names == NULL) goto failed;
5165 for (i = 0; i < len; i++) {
5166 alias_ty value;
5167 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5168 if (res != 0) goto failed;
5169 asdl_seq_SET(names, i, value);
5170 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005171 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005172 } else {
5173 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5174 return 1;
5175 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005176 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005177 int res;
5178 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5179 if (tmp == NULL) goto failed;
5180 res = obj2ast_int(tmp, &level, arena);
5181 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005182 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005183 } else {
5184 level = 0;
5185 }
5186 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5187 if (*out == NULL) goto failed;
5188 return 0;
5189 }
5190 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5191 if (isinstance == -1) {
5192 return 1;
5193 }
5194 if (isinstance) {
5195 asdl_seq* names;
5196
5197 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5198 int res;
5199 Py_ssize_t len;
5200 Py_ssize_t i;
5201 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5202 if (tmp == NULL) goto failed;
5203 if (!PyList_Check(tmp)) {
5204 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5205 goto failed;
5206 }
5207 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005208 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005209 if (names == NULL) goto failed;
5210 for (i = 0; i < len; i++) {
5211 identifier value;
5212 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5213 if (res != 0) goto failed;
5214 asdl_seq_SET(names, i, value);
5215 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005216 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005217 } else {
5218 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5219 return 1;
5220 }
5221 *out = Global(names, lineno, col_offset, arena);
5222 if (*out == NULL) goto failed;
5223 return 0;
5224 }
5225 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5226 if (isinstance == -1) {
5227 return 1;
5228 }
5229 if (isinstance) {
5230 asdl_seq* names;
5231
5232 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5233 int res;
5234 Py_ssize_t len;
5235 Py_ssize_t i;
5236 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5237 if (tmp == NULL) goto failed;
5238 if (!PyList_Check(tmp)) {
5239 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5240 goto failed;
5241 }
5242 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005243 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005244 if (names == NULL) goto failed;
5245 for (i = 0; i < len; i++) {
5246 identifier value;
5247 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5248 if (res != 0) goto failed;
5249 asdl_seq_SET(names, i, value);
5250 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005251 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005252 } else {
5253 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5254 return 1;
5255 }
5256 *out = Nonlocal(names, lineno, col_offset, arena);
5257 if (*out == NULL) goto failed;
5258 return 0;
5259 }
5260 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5261 if (isinstance == -1) {
5262 return 1;
5263 }
5264 if (isinstance) {
5265 expr_ty value;
5266
5267 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5268 int res;
5269 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5270 if (tmp == NULL) goto failed;
5271 res = obj2ast_expr(tmp, &value, arena);
5272 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005273 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005274 } else {
5275 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5276 return 1;
5277 }
5278 *out = Expr(value, lineno, col_offset, arena);
5279 if (*out == NULL) goto failed;
5280 return 0;
5281 }
5282 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5283 if (isinstance == -1) {
5284 return 1;
5285 }
5286 if (isinstance) {
5287
5288 *out = Pass(lineno, col_offset, arena);
5289 if (*out == NULL) goto failed;
5290 return 0;
5291 }
5292 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5293 if (isinstance == -1) {
5294 return 1;
5295 }
5296 if (isinstance) {
5297
5298 *out = Break(lineno, col_offset, arena);
5299 if (*out == NULL) goto failed;
5300 return 0;
5301 }
5302 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5303 if (isinstance == -1) {
5304 return 1;
5305 }
5306 if (isinstance) {
5307
5308 *out = Continue(lineno, col_offset, arena);
5309 if (*out == NULL) goto failed;
5310 return 0;
5311 }
5312
5313 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5314 failed:
5315 Py_XDECREF(tmp);
5316 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005317}
5318
5319int
5320obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5321{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005322 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005323
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005324 PyObject *tmp = NULL;
5325 int lineno;
5326 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005327
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005328 if (obj == Py_None) {
5329 *out = NULL;
5330 return 0;
5331 }
5332 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5333 int res;
5334 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5335 if (tmp == NULL) goto failed;
5336 res = obj2ast_int(tmp, &lineno, arena);
5337 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005338 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005339 } else {
5340 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005341 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005342 }
5343 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5344 int res;
5345 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5346 if (tmp == NULL) goto failed;
5347 res = obj2ast_int(tmp, &col_offset, arena);
5348 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005349 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005350 } else {
5351 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5352 return 1;
5353 }
5354 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5355 if (isinstance == -1) {
5356 return 1;
5357 }
5358 if (isinstance) {
5359 boolop_ty op;
5360 asdl_seq* values;
5361
5362 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5363 int res;
5364 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5365 if (tmp == NULL) goto failed;
5366 res = obj2ast_boolop(tmp, &op, arena);
5367 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005368 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005369 } else {
5370 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
5371 return 1;
5372 }
5373 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5374 int res;
5375 Py_ssize_t len;
5376 Py_ssize_t i;
5377 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5378 if (tmp == NULL) goto failed;
5379 if (!PyList_Check(tmp)) {
5380 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5381 goto failed;
5382 }
5383 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005384 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005385 if (values == NULL) goto failed;
5386 for (i = 0; i < len; i++) {
5387 expr_ty value;
5388 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5389 if (res != 0) goto failed;
5390 asdl_seq_SET(values, i, value);
5391 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005392 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005393 } else {
5394 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5395 return 1;
5396 }
5397 *out = BoolOp(op, values, lineno, col_offset, arena);
5398 if (*out == NULL) goto failed;
5399 return 0;
5400 }
5401 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5402 if (isinstance == -1) {
5403 return 1;
5404 }
5405 if (isinstance) {
5406 expr_ty left;
5407 operator_ty op;
5408 expr_ty right;
5409
5410 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5411 int res;
5412 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5413 if (tmp == NULL) goto failed;
5414 res = obj2ast_expr(tmp, &left, arena);
5415 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005416 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005417 } else {
5418 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5419 return 1;
5420 }
5421 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5422 int res;
5423 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5424 if (tmp == NULL) goto failed;
5425 res = obj2ast_operator(tmp, &op, arena);
5426 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005427 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005428 } else {
5429 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5430 return 1;
5431 }
5432 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5433 int res;
5434 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5435 if (tmp == NULL) goto failed;
5436 res = obj2ast_expr(tmp, &right, arena);
5437 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005438 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005439 } else {
5440 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5441 return 1;
5442 }
5443 *out = BinOp(left, op, right, lineno, col_offset, arena);
5444 if (*out == NULL) goto failed;
5445 return 0;
5446 }
5447 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5448 if (isinstance == -1) {
5449 return 1;
5450 }
5451 if (isinstance) {
5452 unaryop_ty op;
5453 expr_ty operand;
5454
5455 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5456 int res;
5457 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5458 if (tmp == NULL) goto failed;
5459 res = obj2ast_unaryop(tmp, &op, arena);
5460 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005461 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005462 } else {
5463 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5464 return 1;
5465 }
5466 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5467 int res;
5468 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5469 if (tmp == NULL) goto failed;
5470 res = obj2ast_expr(tmp, &operand, arena);
5471 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005472 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005473 } else {
5474 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5475 return 1;
5476 }
5477 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5478 if (*out == NULL) goto failed;
5479 return 0;
5480 }
5481 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5482 if (isinstance == -1) {
5483 return 1;
5484 }
5485 if (isinstance) {
5486 arguments_ty args;
5487 expr_ty body;
5488
5489 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5490 int res;
5491 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5492 if (tmp == NULL) goto failed;
5493 res = obj2ast_arguments(tmp, &args, arena);
5494 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005495 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005496 } else {
5497 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5498 return 1;
5499 }
5500 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5501 int res;
5502 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5503 if (tmp == NULL) goto failed;
5504 res = obj2ast_expr(tmp, &body, arena);
5505 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005506 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005507 } else {
5508 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5509 return 1;
5510 }
5511 *out = Lambda(args, body, lineno, col_offset, arena);
5512 if (*out == NULL) goto failed;
5513 return 0;
5514 }
5515 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5516 if (isinstance == -1) {
5517 return 1;
5518 }
5519 if (isinstance) {
5520 expr_ty test;
5521 expr_ty body;
5522 expr_ty orelse;
5523
5524 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5525 int res;
5526 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5527 if (tmp == NULL) goto failed;
5528 res = obj2ast_expr(tmp, &test, arena);
5529 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005530 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005531 } else {
5532 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5533 return 1;
5534 }
5535 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5536 int res;
5537 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5538 if (tmp == NULL) goto failed;
5539 res = obj2ast_expr(tmp, &body, arena);
5540 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005541 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005542 } else {
5543 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5544 return 1;
5545 }
5546 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5547 int res;
5548 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5549 if (tmp == NULL) goto failed;
5550 res = obj2ast_expr(tmp, &orelse, arena);
5551 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005552 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005553 } else {
5554 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5555 return 1;
5556 }
5557 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5558 if (*out == NULL) goto failed;
5559 return 0;
5560 }
5561 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5562 if (isinstance == -1) {
5563 return 1;
5564 }
5565 if (isinstance) {
5566 asdl_seq* keys;
5567 asdl_seq* values;
5568
5569 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5570 int res;
5571 Py_ssize_t len;
5572 Py_ssize_t i;
5573 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5574 if (tmp == NULL) goto failed;
5575 if (!PyList_Check(tmp)) {
5576 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5577 goto failed;
5578 }
5579 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005580 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005581 if (keys == NULL) goto failed;
5582 for (i = 0; i < len; i++) {
5583 expr_ty value;
5584 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5585 if (res != 0) goto failed;
5586 asdl_seq_SET(keys, i, value);
5587 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005588 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005589 } else {
5590 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5591 return 1;
5592 }
5593 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5594 int res;
5595 Py_ssize_t len;
5596 Py_ssize_t i;
5597 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5598 if (tmp == NULL) goto failed;
5599 if (!PyList_Check(tmp)) {
5600 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5601 goto failed;
5602 }
5603 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005604 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005605 if (values == NULL) goto failed;
5606 for (i = 0; i < len; i++) {
5607 expr_ty value;
5608 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5609 if (res != 0) goto failed;
5610 asdl_seq_SET(values, i, value);
5611 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005612 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005613 } else {
5614 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5615 return 1;
5616 }
5617 *out = Dict(keys, values, lineno, col_offset, arena);
5618 if (*out == NULL) goto failed;
5619 return 0;
5620 }
5621 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5622 if (isinstance == -1) {
5623 return 1;
5624 }
5625 if (isinstance) {
5626 asdl_seq* elts;
5627
5628 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5629 int res;
5630 Py_ssize_t len;
5631 Py_ssize_t i;
5632 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5633 if (tmp == NULL) goto failed;
5634 if (!PyList_Check(tmp)) {
5635 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5636 goto failed;
5637 }
5638 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005639 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005640 if (elts == NULL) goto failed;
5641 for (i = 0; i < len; i++) {
5642 expr_ty value;
5643 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5644 if (res != 0) goto failed;
5645 asdl_seq_SET(elts, i, value);
5646 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005647 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005648 } else {
5649 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5650 return 1;
5651 }
5652 *out = Set(elts, lineno, col_offset, arena);
5653 if (*out == NULL) goto failed;
5654 return 0;
5655 }
5656 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5657 if (isinstance == -1) {
5658 return 1;
5659 }
5660 if (isinstance) {
5661 expr_ty elt;
5662 asdl_seq* generators;
5663
5664 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5665 int res;
5666 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5667 if (tmp == NULL) goto failed;
5668 res = obj2ast_expr(tmp, &elt, arena);
5669 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005670 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005671 } else {
5672 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5673 return 1;
5674 }
5675 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5676 int res;
5677 Py_ssize_t len;
5678 Py_ssize_t i;
5679 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5680 if (tmp == NULL) goto failed;
5681 if (!PyList_Check(tmp)) {
5682 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5683 goto failed;
5684 }
5685 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005686 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005687 if (generators == NULL) goto failed;
5688 for (i = 0; i < len; i++) {
5689 comprehension_ty value;
5690 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5691 if (res != 0) goto failed;
5692 asdl_seq_SET(generators, i, value);
5693 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005694 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005695 } else {
5696 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5697 return 1;
5698 }
5699 *out = ListComp(elt, generators, lineno, col_offset, arena);
5700 if (*out == NULL) goto failed;
5701 return 0;
5702 }
5703 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5704 if (isinstance == -1) {
5705 return 1;
5706 }
5707 if (isinstance) {
5708 expr_ty elt;
5709 asdl_seq* generators;
5710
5711 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5712 int res;
5713 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5714 if (tmp == NULL) goto failed;
5715 res = obj2ast_expr(tmp, &elt, arena);
5716 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005717 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005718 } else {
5719 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5720 return 1;
5721 }
5722 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5723 int res;
5724 Py_ssize_t len;
5725 Py_ssize_t i;
5726 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5727 if (tmp == NULL) goto failed;
5728 if (!PyList_Check(tmp)) {
5729 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5730 goto failed;
5731 }
5732 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005733 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005734 if (generators == NULL) goto failed;
5735 for (i = 0; i < len; i++) {
5736 comprehension_ty value;
5737 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5738 if (res != 0) goto failed;
5739 asdl_seq_SET(generators, i, value);
5740 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005741 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005742 } else {
5743 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5744 return 1;
5745 }
5746 *out = SetComp(elt, generators, lineno, col_offset, arena);
5747 if (*out == NULL) goto failed;
5748 return 0;
5749 }
5750 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5751 if (isinstance == -1) {
5752 return 1;
5753 }
5754 if (isinstance) {
5755 expr_ty key;
5756 expr_ty value;
5757 asdl_seq* generators;
5758
5759 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5760 int res;
5761 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5762 if (tmp == NULL) goto failed;
5763 res = obj2ast_expr(tmp, &key, arena);
5764 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005765 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005766 } else {
5767 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5768 return 1;
5769 }
5770 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5771 int res;
5772 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5773 if (tmp == NULL) goto failed;
5774 res = obj2ast_expr(tmp, &value, arena);
5775 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005776 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005777 } else {
5778 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5779 return 1;
5780 }
5781 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5782 int res;
5783 Py_ssize_t len;
5784 Py_ssize_t i;
5785 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5786 if (tmp == NULL) goto failed;
5787 if (!PyList_Check(tmp)) {
5788 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5789 goto failed;
5790 }
5791 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005792 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005793 if (generators == NULL) goto failed;
5794 for (i = 0; i < len; i++) {
5795 comprehension_ty value;
5796 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5797 if (res != 0) goto failed;
5798 asdl_seq_SET(generators, i, value);
5799 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005800 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005801 } else {
5802 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5803 return 1;
5804 }
5805 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5806 if (*out == NULL) goto failed;
5807 return 0;
5808 }
5809 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5810 if (isinstance == -1) {
5811 return 1;
5812 }
5813 if (isinstance) {
5814 expr_ty elt;
5815 asdl_seq* generators;
5816
5817 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5818 int res;
5819 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5820 if (tmp == NULL) goto failed;
5821 res = obj2ast_expr(tmp, &elt, arena);
5822 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005823 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005824 } else {
5825 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5826 return 1;
5827 }
5828 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5829 int res;
5830 Py_ssize_t len;
5831 Py_ssize_t i;
5832 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5833 if (tmp == NULL) goto failed;
5834 if (!PyList_Check(tmp)) {
5835 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5836 goto failed;
5837 }
5838 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005839 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005840 if (generators == NULL) goto failed;
5841 for (i = 0; i < len; i++) {
5842 comprehension_ty value;
5843 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5844 if (res != 0) goto failed;
5845 asdl_seq_SET(generators, i, value);
5846 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005847 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005848 } else {
5849 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5850 return 1;
5851 }
5852 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5853 if (*out == NULL) goto failed;
5854 return 0;
5855 }
Yury Selivanov75445082015-05-11 22:57:16 -04005856 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5857 if (isinstance == -1) {
5858 return 1;
5859 }
5860 if (isinstance) {
5861 expr_ty value;
5862
5863 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5864 int res;
5865 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5866 if (tmp == NULL) goto failed;
5867 res = obj2ast_expr(tmp, &value, arena);
5868 if (res != 0) goto failed;
5869 Py_CLEAR(tmp);
5870 } else {
5871 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5872 return 1;
5873 }
5874 *out = Await(value, lineno, col_offset, arena);
5875 if (*out == NULL) goto failed;
5876 return 0;
5877 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005878 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5879 if (isinstance == -1) {
5880 return 1;
5881 }
5882 if (isinstance) {
5883 expr_ty value;
5884
Victor Stinneree4b59c2013-07-27 00:01:35 +02005885 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005886 int res;
5887 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5888 if (tmp == NULL) goto failed;
5889 res = obj2ast_expr(tmp, &value, arena);
5890 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005891 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005892 } else {
5893 value = NULL;
5894 }
5895 *out = Yield(value, lineno, col_offset, arena);
5896 if (*out == NULL) goto failed;
5897 return 0;
5898 }
5899 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5900 if (isinstance == -1) {
5901 return 1;
5902 }
5903 if (isinstance) {
5904 expr_ty value;
5905
5906 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5907 int res;
5908 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5909 if (tmp == NULL) goto failed;
5910 res = obj2ast_expr(tmp, &value, arena);
5911 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005912 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005913 } else {
5914 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from YieldFrom");
5915 return 1;
5916 }
5917 *out = YieldFrom(value, lineno, col_offset, arena);
5918 if (*out == NULL) goto failed;
5919 return 0;
5920 }
5921 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5922 if (isinstance == -1) {
5923 return 1;
5924 }
5925 if (isinstance) {
5926 expr_ty left;
5927 asdl_int_seq* ops;
5928 asdl_seq* comparators;
5929
5930 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5931 int res;
5932 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5933 if (tmp == NULL) goto failed;
5934 res = obj2ast_expr(tmp, &left, arena);
5935 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005936 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005937 } else {
5938 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5939 return 1;
5940 }
5941 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5942 int res;
5943 Py_ssize_t len;
5944 Py_ssize_t i;
5945 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5946 if (tmp == NULL) goto failed;
5947 if (!PyList_Check(tmp)) {
5948 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5949 goto failed;
5950 }
5951 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005952 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005953 if (ops == NULL) goto failed;
5954 for (i = 0; i < len; i++) {
5955 cmpop_ty value;
5956 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5957 if (res != 0) goto failed;
5958 asdl_seq_SET(ops, i, value);
5959 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005960 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005961 } else {
5962 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5963 return 1;
5964 }
5965 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
5966 int res;
5967 Py_ssize_t len;
5968 Py_ssize_t i;
5969 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
5970 if (tmp == NULL) goto failed;
5971 if (!PyList_Check(tmp)) {
5972 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5973 goto failed;
5974 }
5975 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005976 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005977 if (comparators == NULL) goto failed;
5978 for (i = 0; i < len; i++) {
5979 expr_ty value;
5980 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5981 if (res != 0) goto failed;
5982 asdl_seq_SET(comparators, i, value);
5983 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005984 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005985 } else {
5986 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5987 return 1;
5988 }
5989 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
5990 if (*out == NULL) goto failed;
5991 return 0;
5992 }
5993 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5994 if (isinstance == -1) {
5995 return 1;
5996 }
5997 if (isinstance) {
5998 expr_ty func;
5999 asdl_seq* args;
6000 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006001
6002 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6003 int res;
6004 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6005 if (tmp == NULL) goto failed;
6006 res = obj2ast_expr(tmp, &func, arena);
6007 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006008 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006009 } else {
6010 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
6011 return 1;
6012 }
6013 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6014 int res;
6015 Py_ssize_t len;
6016 Py_ssize_t i;
6017 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6018 if (tmp == NULL) goto failed;
6019 if (!PyList_Check(tmp)) {
6020 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6021 goto failed;
6022 }
6023 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006024 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006025 if (args == NULL) goto failed;
6026 for (i = 0; i < len; i++) {
6027 expr_ty value;
6028 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6029 if (res != 0) goto failed;
6030 asdl_seq_SET(args, i, value);
6031 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006032 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006033 } else {
6034 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6035 return 1;
6036 }
6037 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6038 int res;
6039 Py_ssize_t len;
6040 Py_ssize_t i;
6041 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6042 if (tmp == NULL) goto failed;
6043 if (!PyList_Check(tmp)) {
6044 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6045 goto failed;
6046 }
6047 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006048 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006049 if (keywords == NULL) goto failed;
6050 for (i = 0; i < len; i++) {
6051 keyword_ty value;
6052 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6053 if (res != 0) goto failed;
6054 asdl_seq_SET(keywords, i, value);
6055 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006056 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006057 } else {
6058 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6059 return 1;
6060 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006061 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006062 if (*out == NULL) goto failed;
6063 return 0;
6064 }
6065 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6066 if (isinstance == -1) {
6067 return 1;
6068 }
6069 if (isinstance) {
6070 object n;
6071
6072 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6073 int res;
6074 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6075 if (tmp == NULL) goto failed;
6076 res = obj2ast_object(tmp, &n, arena);
6077 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006078 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006079 } else {
6080 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6081 return 1;
6082 }
6083 *out = Num(n, lineno, col_offset, arena);
6084 if (*out == NULL) goto failed;
6085 return 0;
6086 }
6087 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6088 if (isinstance == -1) {
6089 return 1;
6090 }
6091 if (isinstance) {
6092 string s;
6093
6094 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6095 int res;
6096 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6097 if (tmp == NULL) goto failed;
6098 res = obj2ast_string(tmp, &s, arena);
6099 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006100 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006101 } else {
6102 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6103 return 1;
6104 }
6105 *out = Str(s, lineno, col_offset, arena);
6106 if (*out == NULL) goto failed;
6107 return 0;
6108 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006109 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6110 if (isinstance == -1) {
6111 return 1;
6112 }
6113 if (isinstance) {
6114 expr_ty value;
6115 int conversion;
6116 expr_ty format_spec;
6117
6118 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6119 int res;
6120 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6121 if (tmp == NULL) goto failed;
6122 res = obj2ast_expr(tmp, &value, arena);
6123 if (res != 0) goto failed;
6124 Py_CLEAR(tmp);
6125 } else {
6126 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6127 return 1;
6128 }
6129 if (exists_not_none(obj, &PyId_conversion)) {
6130 int res;
6131 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6132 if (tmp == NULL) goto failed;
6133 res = obj2ast_int(tmp, &conversion, arena);
6134 if (res != 0) goto failed;
6135 Py_CLEAR(tmp);
6136 } else {
6137 conversion = 0;
6138 }
6139 if (exists_not_none(obj, &PyId_format_spec)) {
6140 int res;
6141 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6142 if (tmp == NULL) goto failed;
6143 res = obj2ast_expr(tmp, &format_spec, arena);
6144 if (res != 0) goto failed;
6145 Py_CLEAR(tmp);
6146 } else {
6147 format_spec = NULL;
6148 }
6149 *out = FormattedValue(value, conversion, format_spec, lineno,
6150 col_offset, arena);
6151 if (*out == NULL) goto failed;
6152 return 0;
6153 }
6154 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6155 if (isinstance == -1) {
6156 return 1;
6157 }
6158 if (isinstance) {
6159 asdl_seq* values;
6160
6161 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6162 int res;
6163 Py_ssize_t len;
6164 Py_ssize_t i;
6165 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6166 if (tmp == NULL) goto failed;
6167 if (!PyList_Check(tmp)) {
6168 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6169 goto failed;
6170 }
6171 len = PyList_GET_SIZE(tmp);
6172 values = _Py_asdl_seq_new(len, arena);
6173 if (values == NULL) goto failed;
6174 for (i = 0; i < len; i++) {
6175 expr_ty value;
6176 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6177 if (res != 0) goto failed;
6178 asdl_seq_SET(values, i, value);
6179 }
6180 Py_CLEAR(tmp);
6181 } else {
6182 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6183 return 1;
6184 }
6185 *out = JoinedStr(values, lineno, col_offset, arena);
6186 if (*out == NULL) goto failed;
6187 return 0;
6188 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006189 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6190 if (isinstance == -1) {
6191 return 1;
6192 }
6193 if (isinstance) {
6194 bytes s;
6195
6196 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6197 int res;
6198 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6199 if (tmp == NULL) goto failed;
6200 res = obj2ast_bytes(tmp, &s, arena);
6201 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006202 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006203 } else {
6204 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6205 return 1;
6206 }
6207 *out = Bytes(s, lineno, col_offset, arena);
6208 if (*out == NULL) goto failed;
6209 return 0;
6210 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006211 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6212 if (isinstance == -1) {
6213 return 1;
6214 }
6215 if (isinstance) {
6216 singleton value;
6217
6218 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6219 int res;
6220 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6221 if (tmp == NULL) goto failed;
6222 res = obj2ast_singleton(tmp, &value, arena);
6223 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006224 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006225 } else {
6226 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6227 return 1;
6228 }
6229 *out = NameConstant(value, lineno, col_offset, arena);
6230 if (*out == NULL) goto failed;
6231 return 0;
6232 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006233 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6234 if (isinstance == -1) {
6235 return 1;
6236 }
6237 if (isinstance) {
6238
6239 *out = Ellipsis(lineno, col_offset, arena);
6240 if (*out == NULL) goto failed;
6241 return 0;
6242 }
6243 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6244 if (isinstance == -1) {
6245 return 1;
6246 }
6247 if (isinstance) {
6248 expr_ty value;
6249 identifier attr;
6250 expr_context_ty ctx;
6251
6252 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6253 int res;
6254 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6255 if (tmp == NULL) goto failed;
6256 res = obj2ast_expr(tmp, &value, arena);
6257 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006258 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006259 } else {
6260 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6261 return 1;
6262 }
6263 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6264 int res;
6265 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6266 if (tmp == NULL) goto failed;
6267 res = obj2ast_identifier(tmp, &attr, arena);
6268 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006269 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006270 } else {
6271 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6272 return 1;
6273 }
6274 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6275 int res;
6276 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6277 if (tmp == NULL) goto failed;
6278 res = obj2ast_expr_context(tmp, &ctx, arena);
6279 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006280 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006281 } else {
6282 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6283 return 1;
6284 }
6285 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6286 if (*out == NULL) goto failed;
6287 return 0;
6288 }
6289 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6290 if (isinstance == -1) {
6291 return 1;
6292 }
6293 if (isinstance) {
6294 expr_ty value;
6295 slice_ty slice;
6296 expr_context_ty ctx;
6297
6298 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6299 int res;
6300 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6301 if (tmp == NULL) goto failed;
6302 res = obj2ast_expr(tmp, &value, arena);
6303 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006304 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006305 } else {
6306 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6307 return 1;
6308 }
6309 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6310 int res;
6311 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6312 if (tmp == NULL) goto failed;
6313 res = obj2ast_slice(tmp, &slice, arena);
6314 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006315 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006316 } else {
6317 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6318 return 1;
6319 }
6320 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6321 int res;
6322 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6323 if (tmp == NULL) goto failed;
6324 res = obj2ast_expr_context(tmp, &ctx, arena);
6325 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006326 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006327 } else {
6328 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6329 return 1;
6330 }
6331 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6332 if (*out == NULL) goto failed;
6333 return 0;
6334 }
6335 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6336 if (isinstance == -1) {
6337 return 1;
6338 }
6339 if (isinstance) {
6340 expr_ty value;
6341 expr_context_ty ctx;
6342
6343 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6344 int res;
6345 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6346 if (tmp == NULL) goto failed;
6347 res = obj2ast_expr(tmp, &value, arena);
6348 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006349 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006350 } else {
6351 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6352 return 1;
6353 }
6354 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6355 int res;
6356 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6357 if (tmp == NULL) goto failed;
6358 res = obj2ast_expr_context(tmp, &ctx, arena);
6359 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006360 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006361 } else {
6362 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
6363 return 1;
6364 }
6365 *out = Starred(value, ctx, lineno, col_offset, arena);
6366 if (*out == NULL) goto failed;
6367 return 0;
6368 }
6369 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6370 if (isinstance == -1) {
6371 return 1;
6372 }
6373 if (isinstance) {
6374 identifier id;
6375 expr_context_ty ctx;
6376
6377 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6378 int res;
6379 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6380 if (tmp == NULL) goto failed;
6381 res = obj2ast_identifier(tmp, &id, arena);
6382 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006383 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006384 } else {
6385 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6386 return 1;
6387 }
6388 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6389 int res;
6390 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6391 if (tmp == NULL) goto failed;
6392 res = obj2ast_expr_context(tmp, &ctx, arena);
6393 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006394 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006395 } else {
6396 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6397 return 1;
6398 }
6399 *out = Name(id, ctx, lineno, col_offset, arena);
6400 if (*out == NULL) goto failed;
6401 return 0;
6402 }
6403 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6404 if (isinstance == -1) {
6405 return 1;
6406 }
6407 if (isinstance) {
6408 asdl_seq* elts;
6409 expr_context_ty ctx;
6410
6411 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6412 int res;
6413 Py_ssize_t len;
6414 Py_ssize_t i;
6415 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6416 if (tmp == NULL) goto failed;
6417 if (!PyList_Check(tmp)) {
6418 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6419 goto failed;
6420 }
6421 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006422 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006423 if (elts == NULL) goto failed;
6424 for (i = 0; i < len; i++) {
6425 expr_ty value;
6426 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6427 if (res != 0) goto failed;
6428 asdl_seq_SET(elts, i, value);
6429 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006430 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006431 } else {
6432 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6433 return 1;
6434 }
6435 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6436 int res;
6437 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6438 if (tmp == NULL) goto failed;
6439 res = obj2ast_expr_context(tmp, &ctx, arena);
6440 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006441 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006442 } else {
6443 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6444 return 1;
6445 }
6446 *out = List(elts, ctx, lineno, col_offset, arena);
6447 if (*out == NULL) goto failed;
6448 return 0;
6449 }
6450 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6451 if (isinstance == -1) {
6452 return 1;
6453 }
6454 if (isinstance) {
6455 asdl_seq* elts;
6456 expr_context_ty ctx;
6457
6458 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6459 int res;
6460 Py_ssize_t len;
6461 Py_ssize_t i;
6462 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6463 if (tmp == NULL) goto failed;
6464 if (!PyList_Check(tmp)) {
6465 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6466 goto failed;
6467 }
6468 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006469 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006470 if (elts == NULL) goto failed;
6471 for (i = 0; i < len; i++) {
6472 expr_ty value;
6473 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6474 if (res != 0) goto failed;
6475 asdl_seq_SET(elts, i, value);
6476 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006477 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006478 } else {
6479 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6480 return 1;
6481 }
6482 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6483 int res;
6484 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6485 if (tmp == NULL) goto failed;
6486 res = obj2ast_expr_context(tmp, &ctx, arena);
6487 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006488 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006489 } else {
6490 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6491 return 1;
6492 }
6493 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6494 if (*out == NULL) goto failed;
6495 return 0;
6496 }
6497
6498 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6499 failed:
6500 Py_XDECREF(tmp);
6501 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006502}
6503
6504int
6505obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6506{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006507 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006508
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006509 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6510 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006511 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006512 }
6513 if (isinstance) {
6514 *out = Load;
6515 return 0;
6516 }
6517 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6518 if (isinstance == -1) {
6519 return 1;
6520 }
6521 if (isinstance) {
6522 *out = Store;
6523 return 0;
6524 }
6525 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6526 if (isinstance == -1) {
6527 return 1;
6528 }
6529 if (isinstance) {
6530 *out = Del;
6531 return 0;
6532 }
6533 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6534 if (isinstance == -1) {
6535 return 1;
6536 }
6537 if (isinstance) {
6538 *out = AugLoad;
6539 return 0;
6540 }
6541 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6542 if (isinstance == -1) {
6543 return 1;
6544 }
6545 if (isinstance) {
6546 *out = AugStore;
6547 return 0;
6548 }
6549 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6550 if (isinstance == -1) {
6551 return 1;
6552 }
6553 if (isinstance) {
6554 *out = Param;
6555 return 0;
6556 }
6557
6558 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6559 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006560}
6561
6562int
6563obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6564{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006565 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006566
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006567 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006568
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006569 if (obj == Py_None) {
6570 *out = NULL;
6571 return 0;
6572 }
6573 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6574 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006575 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006576 }
6577 if (isinstance) {
6578 expr_ty lower;
6579 expr_ty upper;
6580 expr_ty step;
6581
Victor Stinneree4b59c2013-07-27 00:01:35 +02006582 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006583 int res;
6584 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6585 if (tmp == NULL) goto failed;
6586 res = obj2ast_expr(tmp, &lower, arena);
6587 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006588 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006589 } else {
6590 lower = NULL;
6591 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006592 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006593 int res;
6594 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6595 if (tmp == NULL) goto failed;
6596 res = obj2ast_expr(tmp, &upper, arena);
6597 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006598 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006599 } else {
6600 upper = NULL;
6601 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006602 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006603 int res;
6604 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6605 if (tmp == NULL) goto failed;
6606 res = obj2ast_expr(tmp, &step, arena);
6607 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006608 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006609 } else {
6610 step = NULL;
6611 }
6612 *out = Slice(lower, upper, step, arena);
6613 if (*out == NULL) goto failed;
6614 return 0;
6615 }
6616 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6617 if (isinstance == -1) {
6618 return 1;
6619 }
6620 if (isinstance) {
6621 asdl_seq* dims;
6622
6623 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6624 int res;
6625 Py_ssize_t len;
6626 Py_ssize_t i;
6627 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6628 if (tmp == NULL) goto failed;
6629 if (!PyList_Check(tmp)) {
6630 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6631 goto failed;
6632 }
6633 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006634 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006635 if (dims == NULL) goto failed;
6636 for (i = 0; i < len; i++) {
6637 slice_ty value;
6638 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6639 if (res != 0) goto failed;
6640 asdl_seq_SET(dims, i, value);
6641 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006642 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006643 } else {
6644 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6645 return 1;
6646 }
6647 *out = ExtSlice(dims, arena);
6648 if (*out == NULL) goto failed;
6649 return 0;
6650 }
6651 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6652 if (isinstance == -1) {
6653 return 1;
6654 }
6655 if (isinstance) {
6656 expr_ty value;
6657
6658 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6659 int res;
6660 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6661 if (tmp == NULL) goto failed;
6662 res = obj2ast_expr(tmp, &value, arena);
6663 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006664 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006665 } else {
6666 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6667 return 1;
6668 }
6669 *out = Index(value, arena);
6670 if (*out == NULL) goto failed;
6671 return 0;
6672 }
6673
6674 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6675 failed:
6676 Py_XDECREF(tmp);
6677 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006678}
6679
6680int
6681obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6682{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006683 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006684
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006685 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6686 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006687 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006688 }
6689 if (isinstance) {
6690 *out = And;
6691 return 0;
6692 }
6693 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6694 if (isinstance == -1) {
6695 return 1;
6696 }
6697 if (isinstance) {
6698 *out = Or;
6699 return 0;
6700 }
6701
6702 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6703 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006704}
6705
6706int
6707obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6708{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006709 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006710
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006711 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6712 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006713 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006714 }
6715 if (isinstance) {
6716 *out = Add;
6717 return 0;
6718 }
6719 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6720 if (isinstance == -1) {
6721 return 1;
6722 }
6723 if (isinstance) {
6724 *out = Sub;
6725 return 0;
6726 }
6727 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6728 if (isinstance == -1) {
6729 return 1;
6730 }
6731 if (isinstance) {
6732 *out = Mult;
6733 return 0;
6734 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006735 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6736 if (isinstance == -1) {
6737 return 1;
6738 }
6739 if (isinstance) {
6740 *out = MatMult;
6741 return 0;
6742 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006743 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6744 if (isinstance == -1) {
6745 return 1;
6746 }
6747 if (isinstance) {
6748 *out = Div;
6749 return 0;
6750 }
6751 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6752 if (isinstance == -1) {
6753 return 1;
6754 }
6755 if (isinstance) {
6756 *out = Mod;
6757 return 0;
6758 }
6759 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6760 if (isinstance == -1) {
6761 return 1;
6762 }
6763 if (isinstance) {
6764 *out = Pow;
6765 return 0;
6766 }
6767 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6768 if (isinstance == -1) {
6769 return 1;
6770 }
6771 if (isinstance) {
6772 *out = LShift;
6773 return 0;
6774 }
6775 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6776 if (isinstance == -1) {
6777 return 1;
6778 }
6779 if (isinstance) {
6780 *out = RShift;
6781 return 0;
6782 }
6783 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6784 if (isinstance == -1) {
6785 return 1;
6786 }
6787 if (isinstance) {
6788 *out = BitOr;
6789 return 0;
6790 }
6791 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6792 if (isinstance == -1) {
6793 return 1;
6794 }
6795 if (isinstance) {
6796 *out = BitXor;
6797 return 0;
6798 }
6799 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6800 if (isinstance == -1) {
6801 return 1;
6802 }
6803 if (isinstance) {
6804 *out = BitAnd;
6805 return 0;
6806 }
6807 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6808 if (isinstance == -1) {
6809 return 1;
6810 }
6811 if (isinstance) {
6812 *out = FloorDiv;
6813 return 0;
6814 }
6815
6816 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6817 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006818}
6819
6820int
6821obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6822{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006823 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006824
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006825 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6826 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006827 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006828 }
6829 if (isinstance) {
6830 *out = Invert;
6831 return 0;
6832 }
6833 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6834 if (isinstance == -1) {
6835 return 1;
6836 }
6837 if (isinstance) {
6838 *out = Not;
6839 return 0;
6840 }
6841 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6842 if (isinstance == -1) {
6843 return 1;
6844 }
6845 if (isinstance) {
6846 *out = UAdd;
6847 return 0;
6848 }
6849 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6850 if (isinstance == -1) {
6851 return 1;
6852 }
6853 if (isinstance) {
6854 *out = USub;
6855 return 0;
6856 }
6857
6858 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6859 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006860}
6861
6862int
6863obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6864{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006865 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006866
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006867 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6868 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006869 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006870 }
6871 if (isinstance) {
6872 *out = Eq;
6873 return 0;
6874 }
6875 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6876 if (isinstance == -1) {
6877 return 1;
6878 }
6879 if (isinstance) {
6880 *out = NotEq;
6881 return 0;
6882 }
6883 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6884 if (isinstance == -1) {
6885 return 1;
6886 }
6887 if (isinstance) {
6888 *out = Lt;
6889 return 0;
6890 }
6891 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6892 if (isinstance == -1) {
6893 return 1;
6894 }
6895 if (isinstance) {
6896 *out = LtE;
6897 return 0;
6898 }
6899 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6900 if (isinstance == -1) {
6901 return 1;
6902 }
6903 if (isinstance) {
6904 *out = Gt;
6905 return 0;
6906 }
6907 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6908 if (isinstance == -1) {
6909 return 1;
6910 }
6911 if (isinstance) {
6912 *out = GtE;
6913 return 0;
6914 }
6915 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6916 if (isinstance == -1) {
6917 return 1;
6918 }
6919 if (isinstance) {
6920 *out = Is;
6921 return 0;
6922 }
6923 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6924 if (isinstance == -1) {
6925 return 1;
6926 }
6927 if (isinstance) {
6928 *out = IsNot;
6929 return 0;
6930 }
6931 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6932 if (isinstance == -1) {
6933 return 1;
6934 }
6935 if (isinstance) {
6936 *out = In;
6937 return 0;
6938 }
6939 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6940 if (isinstance == -1) {
6941 return 1;
6942 }
6943 if (isinstance) {
6944 *out = NotIn;
6945 return 0;
6946 }
6947
6948 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
6949 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006950}
6951
6952int
6953obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6954{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006955 PyObject* tmp = NULL;
6956 expr_ty target;
6957 expr_ty iter;
6958 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006959
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006960 if (_PyObject_HasAttrId(obj, &PyId_target)) {
6961 int res;
6962 tmp = _PyObject_GetAttrId(obj, &PyId_target);
6963 if (tmp == NULL) goto failed;
6964 res = obj2ast_expr(tmp, &target, arena);
6965 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006966 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006967 } else {
6968 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006969 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006970 }
6971 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
6972 int res;
6973 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
6974 if (tmp == NULL) goto failed;
6975 res = obj2ast_expr(tmp, &iter, arena);
6976 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006977 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006978 } else {
6979 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6980 return 1;
6981 }
6982 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
6983 int res;
6984 Py_ssize_t len;
6985 Py_ssize_t i;
6986 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
6987 if (tmp == NULL) goto failed;
6988 if (!PyList_Check(tmp)) {
6989 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6990 goto failed;
6991 }
6992 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006993 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006994 if (ifs == NULL) goto failed;
6995 for (i = 0; i < len; i++) {
6996 expr_ty value;
6997 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6998 if (res != 0) goto failed;
6999 asdl_seq_SET(ifs, i, value);
7000 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007001 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007002 } else {
7003 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7004 return 1;
7005 }
7006 *out = comprehension(target, iter, ifs, arena);
7007 return 0;
7008failed:
7009 Py_XDECREF(tmp);
7010 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007011}
7012
7013int
7014obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7015{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007016 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007017
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007018 PyObject *tmp = NULL;
7019 int lineno;
7020 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007021
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007022 if (obj == Py_None) {
7023 *out = NULL;
7024 return 0;
7025 }
7026 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7027 int res;
7028 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7029 if (tmp == NULL) goto failed;
7030 res = obj2ast_int(tmp, &lineno, arena);
7031 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007032 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007033 } else {
7034 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007035 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007036 }
7037 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7038 int res;
7039 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7040 if (tmp == NULL) goto failed;
7041 res = obj2ast_int(tmp, &col_offset, arena);
7042 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007043 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007044 } else {
7045 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7046 return 1;
7047 }
7048 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7049 if (isinstance == -1) {
7050 return 1;
7051 }
7052 if (isinstance) {
7053 expr_ty type;
7054 identifier name;
7055 asdl_seq* body;
7056
Victor Stinneree4b59c2013-07-27 00:01:35 +02007057 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007058 int res;
7059 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7060 if (tmp == NULL) goto failed;
7061 res = obj2ast_expr(tmp, &type, arena);
7062 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007063 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007064 } else {
7065 type = NULL;
7066 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007067 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007068 int res;
7069 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7070 if (tmp == NULL) goto failed;
7071 res = obj2ast_identifier(tmp, &name, arena);
7072 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007073 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007074 } else {
7075 name = NULL;
7076 }
7077 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7078 int res;
7079 Py_ssize_t len;
7080 Py_ssize_t i;
7081 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7082 if (tmp == NULL) goto failed;
7083 if (!PyList_Check(tmp)) {
7084 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7085 goto failed;
7086 }
7087 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007088 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007089 if (body == NULL) goto failed;
7090 for (i = 0; i < len; i++) {
7091 stmt_ty value;
7092 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7093 if (res != 0) goto failed;
7094 asdl_seq_SET(body, i, value);
7095 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007096 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007097 } else {
7098 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7099 return 1;
7100 }
7101 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7102 if (*out == NULL) goto failed;
7103 return 0;
7104 }
7105
7106 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7107 failed:
7108 Py_XDECREF(tmp);
7109 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007110}
7111
7112int
7113obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7114{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007115 PyObject* tmp = NULL;
7116 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007117 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007118 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007119 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007120 arg_ty kwarg;
7121 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007122
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007123 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7124 int res;
7125 Py_ssize_t len;
7126 Py_ssize_t i;
7127 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7128 if (tmp == NULL) goto failed;
7129 if (!PyList_Check(tmp)) {
7130 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7131 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007132 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007133 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007134 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007135 if (args == NULL) goto failed;
7136 for (i = 0; i < len; i++) {
7137 arg_ty value;
7138 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7139 if (res != 0) goto failed;
7140 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007141 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007142 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007143 } else {
7144 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007145 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007146 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007147 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007148 int res;
7149 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7150 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007151 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007152 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007153 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007154 } else {
7155 vararg = NULL;
7156 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007157 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7158 int res;
7159 Py_ssize_t len;
7160 Py_ssize_t i;
7161 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7162 if (tmp == NULL) goto failed;
7163 if (!PyList_Check(tmp)) {
7164 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7165 goto failed;
7166 }
7167 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007168 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007169 if (kwonlyargs == NULL) goto failed;
7170 for (i = 0; i < len; i++) {
7171 arg_ty value;
7172 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7173 if (res != 0) goto failed;
7174 asdl_seq_SET(kwonlyargs, i, value);
7175 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007176 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007177 } else {
7178 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7179 return 1;
7180 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007181 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7182 int res;
7183 Py_ssize_t len;
7184 Py_ssize_t i;
7185 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7186 if (tmp == NULL) goto failed;
7187 if (!PyList_Check(tmp)) {
7188 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7189 goto failed;
7190 }
7191 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007192 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007193 if (kw_defaults == NULL) goto failed;
7194 for (i = 0; i < len; i++) {
7195 expr_ty value;
7196 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7197 if (res != 0) goto failed;
7198 asdl_seq_SET(kw_defaults, i, value);
7199 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007200 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007201 } else {
7202 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7203 return 1;
7204 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007205 if (exists_not_none(obj, &PyId_kwarg)) {
7206 int res;
7207 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7208 if (tmp == NULL) goto failed;
7209 res = obj2ast_arg(tmp, &kwarg, arena);
7210 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007211 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007212 } else {
7213 kwarg = NULL;
7214 }
7215 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7216 int res;
7217 Py_ssize_t len;
7218 Py_ssize_t i;
7219 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7220 if (tmp == NULL) goto failed;
7221 if (!PyList_Check(tmp)) {
7222 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7223 goto failed;
7224 }
7225 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007226 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007227 if (defaults == NULL) goto failed;
7228 for (i = 0; i < len; i++) {
7229 expr_ty value;
7230 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7231 if (res != 0) goto failed;
7232 asdl_seq_SET(defaults, i, value);
7233 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007234 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007235 } else {
7236 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7237 return 1;
7238 }
7239 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7240 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007241 return 0;
7242failed:
7243 Py_XDECREF(tmp);
7244 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007245}
7246
7247int
7248obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7249{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007250 PyObject* tmp = NULL;
7251 identifier arg;
7252 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007253 int lineno;
7254 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007255
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007256 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7257 int res;
7258 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7259 if (tmp == NULL) goto failed;
7260 res = obj2ast_identifier(tmp, &arg, arena);
7261 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007262 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007263 } else {
7264 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007265 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007266 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007267 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007268 int res;
7269 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7270 if (tmp == NULL) goto failed;
7271 res = obj2ast_expr(tmp, &annotation, arena);
7272 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007273 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007274 } else {
7275 annotation = NULL;
7276 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007277 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7278 int res;
7279 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7280 if (tmp == NULL) goto failed;
7281 res = obj2ast_int(tmp, &lineno, arena);
7282 if (res != 0) goto failed;
7283 Py_CLEAR(tmp);
7284 } else {
7285 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7286 return 1;
7287 }
7288 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7289 int res;
7290 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7291 if (tmp == NULL) goto failed;
7292 res = obj2ast_int(tmp, &col_offset, arena);
7293 if (res != 0) goto failed;
7294 Py_CLEAR(tmp);
7295 } else {
7296 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7297 return 1;
7298 }
7299 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007300 return 0;
7301failed:
7302 Py_XDECREF(tmp);
7303 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007304}
7305
7306int
7307obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7308{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007309 PyObject* tmp = NULL;
7310 identifier arg;
7311 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007312
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007313 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007314 int res;
7315 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7316 if (tmp == NULL) goto failed;
7317 res = obj2ast_identifier(tmp, &arg, arena);
7318 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007319 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007320 } else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007321 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007322 }
7323 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7324 int res;
7325 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7326 if (tmp == NULL) goto failed;
7327 res = obj2ast_expr(tmp, &value, arena);
7328 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007329 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007330 } else {
7331 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7332 return 1;
7333 }
7334 *out = keyword(arg, value, arena);
7335 return 0;
7336failed:
7337 Py_XDECREF(tmp);
7338 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007339}
7340
7341int
7342obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7343{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007344 PyObject* tmp = NULL;
7345 identifier name;
7346 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007347
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007348 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7349 int res;
7350 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7351 if (tmp == NULL) goto failed;
7352 res = obj2ast_identifier(tmp, &name, arena);
7353 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007354 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007355 } else {
7356 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007357 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007358 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007359 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007360 int res;
7361 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7362 if (tmp == NULL) goto failed;
7363 res = obj2ast_identifier(tmp, &asname, arena);
7364 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007365 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007366 } else {
7367 asname = NULL;
7368 }
7369 *out = alias(name, asname, arena);
7370 return 0;
7371failed:
7372 Py_XDECREF(tmp);
7373 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007374}
7375
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007376int
7377obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7378{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007379 PyObject* tmp = NULL;
7380 expr_ty context_expr;
7381 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007382
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007383 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7384 int res;
7385 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7386 if (tmp == NULL) goto failed;
7387 res = obj2ast_expr(tmp, &context_expr, arena);
7388 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007389 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007390 } else {
7391 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007392 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007393 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007394 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007395 int res;
7396 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7397 if (tmp == NULL) goto failed;
7398 res = obj2ast_expr(tmp, &optional_vars, arena);
7399 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007400 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007401 } else {
7402 optional_vars = NULL;
7403 }
7404 *out = withitem(context_expr, optional_vars, arena);
7405 return 0;
7406failed:
7407 Py_XDECREF(tmp);
7408 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007409}
7410
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007411
Martin v. Löwis1a214512008-06-11 05:26:20 +00007412static struct PyModuleDef _astmodule = {
7413 PyModuleDef_HEAD_INIT, "_ast"
7414};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007415PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007416PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007417{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007418 PyObject *m, *d;
7419 if (!init_types()) return NULL;
7420 m = PyModule_Create(&_astmodule);
7421 if (!m) return NULL;
7422 d = PyModule_GetDict(m);
7423 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007424 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007425 return NULL;
7426 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7427 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7428 NULL;
7429 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7430 0) return NULL;
7431 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7432 return NULL;
7433 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7434 NULL;
7435 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7436 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7437 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007438 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7439 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007440 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7441 return NULL;
7442 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7443 NULL;
7444 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7445 NULL;
7446 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7447 NULL;
7448 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7449 return NULL;
7450 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007451 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7452 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007453 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7454 NULL;
7455 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7456 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007457 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7458 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007459 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7460 NULL;
7461 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7462 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7463 NULL;
7464 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7465 NULL;
7466 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7467 return NULL;
7468 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7469 NULL;
7470 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7471 return NULL;
7472 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7473 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7474 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7475 NULL;
7476 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7477 return NULL;
7478 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7479 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7480 NULL;
7481 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7482 NULL;
7483 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7484 NULL;
7485 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7486 NULL;
7487 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7488 NULL;
7489 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7490 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7491 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7492 return NULL;
7493 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7494 NULL;
7495 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7496 return NULL;
7497 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7498 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007499 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7500 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007501 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7502 NULL;
7503 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7504 return NULL;
7505 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7506 NULL;
7507 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7508 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7509 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007510 if (PyDict_SetItemString(d, "FormattedValue",
7511 (PyObject*)FormattedValue_type) < 0) return NULL;
7512 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7513 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007514 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7515 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007516 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7517 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007518 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7519 return NULL;
7520 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7521 return NULL;
7522 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7523 return NULL;
7524 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7525 NULL;
7526 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7527 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7528 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7529 NULL;
7530 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7531 0) return NULL;
7532 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7533 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7534 NULL;
7535 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7536 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7537 NULL;
7538 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7539 return NULL;
7540 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7541 NULL;
7542 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7543 NULL;
7544 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7545 NULL;
7546 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7547 return NULL;
7548 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7549 NULL;
7550 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7551 NULL;
7552 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7553 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7554 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7555 return NULL;
7556 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7557 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7558 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007559 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7560 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007561 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7562 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7563 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7564 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7565 NULL;
7566 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7567 NULL;
7568 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7569 NULL;
7570 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7571 NULL;
7572 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7573 NULL;
7574 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7575 return NULL;
7576 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7577 NULL;
7578 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7579 NULL;
7580 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7581 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7582 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7583 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7584 NULL;
7585 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7586 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7587 NULL;
7588 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7589 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7590 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7591 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7592 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7593 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7594 NULL;
7595 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7596 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7597 NULL;
7598 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7599 < 0) return NULL;
7600 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7601 < 0) return NULL;
7602 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7603 < 0) return NULL;
7604 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7605 return NULL;
7606 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7607 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7608 NULL;
7609 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7610 NULL;
7611 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7612 return NULL;
7613 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007614}
7615
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007616
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007617PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007618{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007619 if (!init_types())
7620 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007621 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007622}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007623
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007624/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7625mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007626{
7627 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007628 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007629 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007630 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007631
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007632 req_type[0] = (PyObject*)Module_type;
7633 req_type[1] = (PyObject*)Expression_type;
7634 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007635
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007636 assert(0 <= mode && mode <= 2);
7637
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007638 if (!init_types())
7639 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007640
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007641 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7642 if (isinstance == -1)
7643 return NULL;
7644 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007645 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7646 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007647 return NULL;
7648 }
7649 if (obj2ast_mod(ast, &res, arena) != 0)
7650 return NULL;
7651 else
7652 return res;
7653}
7654
7655int PyAST_Check(PyObject* obj)
7656{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007657 if (!init_types())
7658 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007659 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007660}
7661
Martin v. Löwis5b222132007-06-10 09:51:05 +00007662