blob: 1193c7c66beaebb632cb3d8fc4084711f83cfa4b [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;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100309static PyTypeObject *Constant_type;
310static char *Constant_fields[]={
311 "value",
312};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000313static PyTypeObject *Attribute_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200314_Py_IDENTIFIER(attr);
315_Py_IDENTIFIER(ctx);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000316static char *Attribute_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200317 "value",
318 "attr",
319 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000320};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000321static PyTypeObject *Subscript_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200322_Py_IDENTIFIER(slice);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000323static char *Subscript_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200324 "value",
325 "slice",
326 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000327};
Guido van Rossum0368b722007-05-11 16:50:42 +0000328static PyTypeObject *Starred_type;
329static char *Starred_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200330 "value",
331 "ctx",
Guido van Rossum0368b722007-05-11 16:50:42 +0000332};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static PyTypeObject *Name_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200334_Py_IDENTIFIER(id);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000335static char *Name_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200336 "id",
337 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000338};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000339static PyTypeObject *List_type;
340static char *List_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200341 "elts",
342 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000343};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000344static PyTypeObject *Tuple_type;
345static char *Tuple_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200346 "elts",
347 "ctx",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000348};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000350static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
351*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
352static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *Load_type;
354static PyTypeObject *Store_type;
355static PyTypeObject *Del_type;
356static PyTypeObject *AugLoad_type;
357static PyTypeObject *AugStore_type;
358static PyTypeObject *Param_type;
359static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000360static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000361static PyTypeObject *Slice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200362_Py_IDENTIFIER(lower);
363_Py_IDENTIFIER(upper);
364_Py_IDENTIFIER(step);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000365static char *Slice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200366 "lower",
367 "upper",
368 "step",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000370static PyTypeObject *ExtSlice_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200371_Py_IDENTIFIER(dims);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000372static char *ExtSlice_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200373 "dims",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000375static PyTypeObject *Index_type;
376static char *Index_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200377 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000378};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000379static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000380static PyObject *And_singleton, *Or_singleton;
381static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000382static PyTypeObject *And_type;
383static PyTypeObject *Or_type;
384static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000385static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
Benjamin Petersond51374e2014-04-09 23:55:56 -0400386*MatMult_singleton, *Div_singleton, *Mod_singleton, *Pow_singleton,
387*LShift_singleton, *RShift_singleton, *BitOr_singleton, *BitXor_singleton,
388*BitAnd_singleton, *FloorDiv_singleton;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000389static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000390static PyTypeObject *Add_type;
391static PyTypeObject *Sub_type;
392static PyTypeObject *Mult_type;
Benjamin Petersond51374e2014-04-09 23:55:56 -0400393static PyTypeObject *MatMult_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000394static PyTypeObject *Div_type;
395static PyTypeObject *Mod_type;
396static PyTypeObject *Pow_type;
397static PyTypeObject *LShift_type;
398static PyTypeObject *RShift_type;
399static PyTypeObject *BitOr_type;
400static PyTypeObject *BitXor_type;
401static PyTypeObject *BitAnd_type;
402static PyTypeObject *FloorDiv_type;
403static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000404static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
405*USub_singleton;
406static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000407static PyTypeObject *Invert_type;
408static PyTypeObject *Not_type;
409static PyTypeObject *UAdd_type;
410static PyTypeObject *USub_type;
411static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000412static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
413*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
414*NotIn_singleton;
415static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000416static PyTypeObject *Eq_type;
417static PyTypeObject *NotEq_type;
418static PyTypeObject *Lt_type;
419static PyTypeObject *LtE_type;
420static PyTypeObject *Gt_type;
421static PyTypeObject *GtE_type;
422static PyTypeObject *Is_type;
423static PyTypeObject *IsNot_type;
424static PyTypeObject *In_type;
425static PyTypeObject *NotIn_type;
426static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000427static PyObject* ast2obj_comprehension(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200428_Py_IDENTIFIER(ifs);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000429static char *comprehension_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200430 "target",
431 "iter",
432 "ifs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000433};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000434static PyTypeObject *excepthandler_type;
Neal Norwitzad74aa82008-03-31 05:14:30 +0000435static char *excepthandler_attributes[] = {
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200436 "lineno",
437 "col_offset",
Neal Norwitzad74aa82008-03-31 05:14:30 +0000438};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000439static PyObject* ast2obj_excepthandler(void*);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000440static PyTypeObject *ExceptHandler_type;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200441_Py_IDENTIFIER(type);
Neal Norwitzad74aa82008-03-31 05:14:30 +0000442static char *ExceptHandler_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200443 "type",
444 "name",
445 "body",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000446};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000447static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000448static PyObject* ast2obj_arguments(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200449_Py_IDENTIFIER(vararg);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200450_Py_IDENTIFIER(kwonlyargs);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200451_Py_IDENTIFIER(kw_defaults);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700452_Py_IDENTIFIER(kwarg);
453_Py_IDENTIFIER(defaults);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000454static char *arguments_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200455 "args",
456 "vararg",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200457 "kwonlyargs",
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200458 "kw_defaults",
Victor Stinneree4b59c2013-07-27 00:01:35 +0200459 "kwarg",
460 "defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000461};
Neal Norwitzc1505362006-12-28 06:47:50 +0000462static PyTypeObject *arg_type;
463static PyObject* ast2obj_arg(void*);
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700464static char *arg_attributes[] = {
Victor Stinneree4b59c2013-07-27 00:01:35 +0200465 "lineno",
466 "col_offset",
Benjamin Petersoncda75be2013-03-18 10:48:58 -0700467};
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200468_Py_IDENTIFIER(arg);
469_Py_IDENTIFIER(annotation);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000470static char *arg_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200471 "arg",
472 "annotation",
Neal Norwitzc1505362006-12-28 06:47:50 +0000473};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000474static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000475static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000476static char *keyword_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200477 "arg",
478 "value",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000479};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000480static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000481static PyObject* ast2obj_alias(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200482_Py_IDENTIFIER(asname);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000483static char *alias_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200484 "name",
485 "asname",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000486};
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500487static PyTypeObject *withitem_type;
488static PyObject* ast2obj_withitem(void*);
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200489_Py_IDENTIFIER(context_expr);
490_Py_IDENTIFIER(optional_vars);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500491static char *withitem_fields[]={
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200492 "context_expr",
493 "optional_vars",
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500494};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000495
496
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700497typedef struct {
Victor Stinner45e50de2012-03-13 01:17:31 +0100498 PyObject_HEAD
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700499 PyObject *dict;
500} AST_object;
501
Benjamin Peterson1767e022012-03-14 21:50:29 -0500502static void
503ast_dealloc(AST_object *self)
504{
505 Py_CLEAR(self->dict);
Antoine Pitrou50750742012-07-08 12:43:32 +0200506 Py_TYPE(self)->tp_free(self);
Benjamin Peterson1767e022012-03-14 21:50:29 -0500507}
508
Neal Norwitz207c9f32008-03-31 04:42:11 +0000509static int
Benjamin Peterson81071762012-07-08 11:03:46 -0700510ast_traverse(AST_object *self, visitproc visit, void *arg)
511{
512 Py_VISIT(self->dict);
513 return 0;
514}
515
516static void
517ast_clear(AST_object *self)
518{
519 Py_CLEAR(self->dict);
520}
521
522static int
Neal Norwitz207c9f32008-03-31 04:42:11 +0000523ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
524{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200525 _Py_IDENTIFIER(_fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000526 Py_ssize_t i, numfields = 0;
527 int res = -1;
528 PyObject *key, *value, *fields;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200529 fields = _PyObject_GetAttrId((PyObject*)Py_TYPE(self), &PyId__fields);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000530 if (!fields)
531 PyErr_Clear();
532 if (fields) {
533 numfields = PySequence_Size(fields);
534 if (numfields == -1)
535 goto cleanup;
536 }
537 res = 0; /* if no error occurs, this stays 0 to the end */
538 if (PyTuple_GET_SIZE(args) > 0) {
539 if (numfields != PyTuple_GET_SIZE(args)) {
540 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc245c70b2008-09-10 22:24:24 +0000541 "%zd positional argument%s",
Neal Norwitz207c9f32008-03-31 04:42:11 +0000542 Py_TYPE(self)->tp_name,
543 numfields == 0 ? "" : "either 0 or ",
544 numfields, numfields == 1 ? "" : "s");
545 res = -1;
546 goto cleanup;
547 }
548 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
549 /* cannot be reached when fields is NULL */
550 PyObject *name = PySequence_GetItem(fields, i);
551 if (!name) {
552 res = -1;
553 goto cleanup;
554 }
555 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
556 Py_DECREF(name);
557 if (res < 0)
558 goto cleanup;
559 }
560 }
561 if (kw) {
562 i = 0; /* needed by PyDict_Next */
563 while (PyDict_Next(kw, &i, &key, &value)) {
564 res = PyObject_SetAttr(self, key, value);
565 if (res < 0)
566 goto cleanup;
567 }
568 }
569 cleanup:
570 Py_XDECREF(fields);
571 return res;
572}
573
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000574/* Pickling support */
575static PyObject *
576ast_type_reduce(PyObject *self, PyObject *unused)
577{
578 PyObject *res;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200579 _Py_IDENTIFIER(__dict__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200580 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000581 if (dict == NULL) {
582 if (PyErr_ExceptionMatches(PyExc_AttributeError))
583 PyErr_Clear();
584 else
585 return NULL;
586 }
587 if (dict) {
588 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
589 Py_DECREF(dict);
590 return res;
591 }
592 return Py_BuildValue("O()", Py_TYPE(self));
593}
594
595static PyMethodDef ast_type_methods[] = {
596 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
597 {NULL}
598};
599
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700600static PyGetSetDef ast_type_getsets[] = {
601 {"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict},
602 {NULL}
603};
604
Neal Norwitz207c9f32008-03-31 04:42:11 +0000605static PyTypeObject AST_type = {
606 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000607 "_ast.AST",
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700608 sizeof(AST_object),
Neal Norwitz207c9f32008-03-31 04:42:11 +0000609 0,
Benjamin Peterson1767e022012-03-14 21:50:29 -0500610 (destructor)ast_dealloc, /* tp_dealloc */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000611 0, /* tp_print */
612 0, /* tp_getattr */
613 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000614 0, /* tp_reserved */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000615 0, /* tp_repr */
616 0, /* tp_as_number */
617 0, /* tp_as_sequence */
618 0, /* tp_as_mapping */
619 0, /* tp_hash */
620 0, /* tp_call */
621 0, /* tp_str */
622 PyObject_GenericGetAttr, /* tp_getattro */
623 PyObject_GenericSetAttr, /* tp_setattro */
624 0, /* tp_as_buffer */
Benjamin Peterson81071762012-07-08 11:03:46 -0700625 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000626 0, /* tp_doc */
Benjamin Peterson81071762012-07-08 11:03:46 -0700627 (traverseproc)ast_traverse, /* tp_traverse */
628 (inquiry)ast_clear, /* tp_clear */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000629 0, /* tp_richcompare */
630 0, /* tp_weaklistoffset */
631 0, /* tp_iter */
632 0, /* tp_iternext */
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000633 ast_type_methods, /* tp_methods */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000634 0, /* tp_members */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700635 ast_type_getsets, /* tp_getset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000636 0, /* tp_base */
637 0, /* tp_dict */
638 0, /* tp_descr_get */
639 0, /* tp_descr_set */
Benjamin Peterson7e0dbfb2012-03-12 09:46:44 -0700640 offsetof(AST_object, dict),/* tp_dictoffset */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000641 (initproc)ast_type_init, /* tp_init */
642 PyType_GenericAlloc, /* tp_alloc */
643 PyType_GenericNew, /* tp_new */
Benjamin Peterson81071762012-07-08 11:03:46 -0700644 PyObject_GC_Del, /* tp_free */
Neal Norwitz207c9f32008-03-31 04:42:11 +0000645};
646
647
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
649{
650 PyObject *fnames, *result;
651 int i;
Neal Norwitzee9b10a2008-03-31 05:29:39 +0000652 fnames = PyTuple_New(num_fields);
653 if (!fnames) return NULL;
654 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000655 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 if (!field) {
657 Py_DECREF(fnames);
658 return NULL;
659 }
660 PyTuple_SET_ITEM(fnames, i, field);
661 }
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000662 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 Py_DECREF(fnames);
665 return (PyTypeObject*)result;
666}
667
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
669{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 int i, result;
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200671 _Py_IDENTIFIER(_attributes);
Neal Norwitz207c9f32008-03-31 04:42:11 +0000672 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson3e5cd1d2010-06-27 21:45:24 +0000673 if (!l)
674 return 0;
675 for (i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000676 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!s) {
678 Py_DECREF(l);
679 return 0;
680 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000681 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 }
Martin v. Löwis1c67dd92011-10-14 15:16:45 +0200683 result = _PyObject_SetAttrId((PyObject*)type, &PyId__attributes, l) >= 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 Py_DECREF(l);
685 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686}
687
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000688/* Conversion AST -> Python */
689
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
691{
Benjamin Peterson77fa9372012-05-15 10:10:27 -0700692 Py_ssize_t i, n = asdl_seq_LEN(seq);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 PyObject *result = PyList_New(n);
694 PyObject *value;
695 if (!result)
696 return NULL;
697 for (i = 0; i < n; i++) {
698 value = func(asdl_seq_GET(seq, i));
699 if (!value) {
700 Py_DECREF(result);
701 return NULL;
702 }
703 PyList_SET_ITEM(result, i, value);
704 }
705 return result;
706}
707
708static PyObject* ast2obj_object(void *o)
709{
710 if (!o)
711 o = Py_None;
712 Py_INCREF((PyObject*)o);
713 return (PyObject*)o;
714}
Benjamin Peterson442f2092012-12-06 17:41:04 -0500715#define ast2obj_singleton ast2obj_object
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100716#define ast2obj_constant ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717#define ast2obj_identifier ast2obj_object
718#define ast2obj_string ast2obj_object
Benjamin Petersone2498412011-08-09 16:08:39 -0500719#define ast2obj_bytes ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000721static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722{
Christian Heimes217cfd12007-12-02 14:31:20 +0000723 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724}
725
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000726/* Conversion Python -> AST */
727
Benjamin Peterson442f2092012-12-06 17:41:04 -0500728static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena)
729{
730 if (obj != Py_None && obj != Py_True && obj != Py_False) {
731 PyErr_SetString(PyExc_ValueError,
732 "AST singleton must be True, False, or None");
733 return 1;
734 }
735 *out = obj;
736 return 0;
737}
738
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000739static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
740{
741 if (obj == Py_None)
742 obj = NULL;
Christian Heimes70c94e72013-07-27 00:33:13 +0200743 if (obj) {
744 if (PyArena_AddPyObject(arena, obj) < 0) {
745 *out = NULL;
746 return -1;
747 }
748 Py_INCREF(obj);
749 }
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000750 *out = obj;
751 return 0;
752}
753
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100754static int obj2ast_constant(PyObject* obj, PyObject** out, PyArena* arena)
755{
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100756 if (obj) {
757 if (PyArena_AddPyObject(arena, obj) < 0) {
758 *out = NULL;
759 return -1;
760 }
761 Py_INCREF(obj);
762 }
763 *out = obj;
764 return 0;
765}
766
Benjamin Peterson180e6352011-07-22 11:09:07 -0500767static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500768{
Benjamin Peterson180e6352011-07-22 11:09:07 -0500769 if (!PyUnicode_CheckExact(obj) && obj != Py_None) {
770 PyErr_SetString(PyExc_TypeError, "AST identifier must be of type str");
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500771 return 1;
772 }
773 return obj2ast_object(obj, out, arena);
774}
775
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500776static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
777{
Benjamin Peterson0224d4e2011-08-31 22:13:03 -0400778 if (!PyUnicode_CheckExact(obj) && !PyBytes_CheckExact(obj)) {
Benjamin Peterson180e6352011-07-22 11:09:07 -0500779 PyErr_SetString(PyExc_TypeError, "AST string must be of type str");
780 return 1;
781 }
782 return obj2ast_object(obj, out, arena);
Benjamin Peterson2193d2b2011-07-22 10:50:23 -0500783}
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000784
Benjamin Petersone2498412011-08-09 16:08:39 -0500785static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
786{
787 if (!PyBytes_CheckExact(obj)) {
788 PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
789 return 1;
790 }
791 return obj2ast_object(obj, out, arena);
792}
793
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000794static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
795{
796 int i;
797 if (!PyLong_Check(obj)) {
Amaury Forgeot d'Arc5e8f8102011-11-22 21:52:30 +0100798 PyErr_Format(PyExc_ValueError, "invalid integer value: %R", obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000799 return 1;
800 }
801
Serhiy Storchaka56f6e762015-09-06 21:25:30 +0300802 i = _PyLong_AsInt(obj);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000803 if (i == -1 && PyErr_Occurred())
804 return 1;
805 *out = i;
806 return 0;
807}
808
Benjamin Peterson1a6e0d02008-10-25 15:49:17 +0000809static int add_ast_fields(void)
Benjamin Petersonce825f12008-10-24 23:11:02 +0000810{
811 PyObject *empty_tuple, *d;
812 if (PyType_Ready(&AST_type) < 0)
813 return -1;
814 d = AST_type.tp_dict;
815 empty_tuple = PyTuple_New(0);
816 if (!empty_tuple ||
817 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
818 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
819 Py_XDECREF(empty_tuple);
820 return -1;
821 }
822 Py_DECREF(empty_tuple);
823 return 0;
824}
825
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700826static int exists_not_none(PyObject *obj, _Py_Identifier *id)
827{
Benjamin Peterson3a619432013-03-18 23:40:53 -0700828 int isnone;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700829 PyObject *attr = _PyObject_GetAttrId(obj, id);
830 if (!attr) {
831 PyErr_Clear();
832 return 0;
833 }
Benjamin Peterson3a619432013-03-18 23:40:53 -0700834 isnone = attr == Py_None;
Benjamin Petersonb72406b2013-03-18 23:24:41 -0700835 Py_DECREF(attr);
836 return !isnone;
837}
838
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000839
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840static int init_types(void)
841{
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200842 static int initialized;
843 if (initialized) return 1;
844 if (add_ast_fields() < 0) return 0;
845 mod_type = make_type("mod", &AST_type, NULL, 0);
846 if (!mod_type) return 0;
847 if (!add_attributes(mod_type, NULL, 0)) return 0;
848 Module_type = make_type("Module", mod_type, Module_fields, 1);
849 if (!Module_type) return 0;
850 Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
851 1);
852 if (!Interactive_type) return 0;
853 Expression_type = make_type("Expression", mod_type, Expression_fields, 1);
854 if (!Expression_type) return 0;
855 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
856 if (!Suite_type) return 0;
857 stmt_type = make_type("stmt", &AST_type, NULL, 0);
858 if (!stmt_type) return 0;
859 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
860 FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
861 5);
862 if (!FunctionDef_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400863 AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
864 AsyncFunctionDef_fields, 5);
865 if (!AsyncFunctionDef_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400866 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200867 if (!ClassDef_type) return 0;
868 Return_type = make_type("Return", stmt_type, Return_fields, 1);
869 if (!Return_type) return 0;
870 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
871 if (!Delete_type) return 0;
872 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
873 if (!Assign_type) return 0;
874 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
875 if (!AugAssign_type) return 0;
876 For_type = make_type("For", stmt_type, For_fields, 4);
877 if (!For_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400878 AsyncFor_type = make_type("AsyncFor", stmt_type, AsyncFor_fields, 4);
879 if (!AsyncFor_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200880 While_type = make_type("While", stmt_type, While_fields, 3);
881 if (!While_type) return 0;
882 If_type = make_type("If", stmt_type, If_fields, 3);
883 if (!If_type) return 0;
884 With_type = make_type("With", stmt_type, With_fields, 2);
885 if (!With_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400886 AsyncWith_type = make_type("AsyncWith", stmt_type, AsyncWith_fields, 2);
887 if (!AsyncWith_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200888 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
889 if (!Raise_type) return 0;
890 Try_type = make_type("Try", stmt_type, Try_fields, 4);
891 if (!Try_type) return 0;
892 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
893 if (!Assert_type) return 0;
894 Import_type = make_type("Import", stmt_type, Import_fields, 1);
895 if (!Import_type) return 0;
896 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields, 3);
897 if (!ImportFrom_type) return 0;
898 Global_type = make_type("Global", stmt_type, Global_fields, 1);
899 if (!Global_type) return 0;
900 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
901 if (!Nonlocal_type) return 0;
902 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
903 if (!Expr_type) return 0;
904 Pass_type = make_type("Pass", stmt_type, NULL, 0);
905 if (!Pass_type) return 0;
906 Break_type = make_type("Break", stmt_type, NULL, 0);
907 if (!Break_type) return 0;
908 Continue_type = make_type("Continue", stmt_type, NULL, 0);
909 if (!Continue_type) return 0;
910 expr_type = make_type("expr", &AST_type, NULL, 0);
911 if (!expr_type) return 0;
912 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
913 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
914 if (!BoolOp_type) return 0;
915 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
916 if (!BinOp_type) return 0;
917 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
918 if (!UnaryOp_type) return 0;
919 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
920 if (!Lambda_type) return 0;
921 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
922 if (!IfExp_type) return 0;
923 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
924 if (!Dict_type) return 0;
925 Set_type = make_type("Set", expr_type, Set_fields, 1);
926 if (!Set_type) return 0;
927 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
928 if (!ListComp_type) return 0;
929 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
930 if (!SetComp_type) return 0;
931 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
932 if (!DictComp_type) return 0;
933 GeneratorExp_type = make_type("GeneratorExp", expr_type,
934 GeneratorExp_fields, 2);
935 if (!GeneratorExp_type) return 0;
Yury Selivanov75445082015-05-11 22:57:16 -0400936 Await_type = make_type("Await", expr_type, Await_fields, 1);
937 if (!Await_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200938 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
939 if (!Yield_type) return 0;
940 YieldFrom_type = make_type("YieldFrom", expr_type, YieldFrom_fields, 1);
941 if (!YieldFrom_type) return 0;
942 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
943 if (!Compare_type) return 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400944 Call_type = make_type("Call", expr_type, Call_fields, 3);
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200945 if (!Call_type) return 0;
946 Num_type = make_type("Num", expr_type, Num_fields, 1);
947 if (!Num_type) return 0;
948 Str_type = make_type("Str", expr_type, Str_fields, 1);
949 if (!Str_type) return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -0400950 FormattedValue_type = make_type("FormattedValue", expr_type,
951 FormattedValue_fields, 3);
952 if (!FormattedValue_type) return 0;
953 JoinedStr_type = make_type("JoinedStr", expr_type, JoinedStr_fields, 1);
954 if (!JoinedStr_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200955 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
956 if (!Bytes_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +0200957 NameConstant_type = make_type("NameConstant", expr_type,
958 NameConstant_fields, 1);
959 if (!NameConstant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200960 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
961 if (!Ellipsis_type) return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +0100962 Constant_type = make_type("Constant", expr_type, Constant_fields, 1);
963 if (!Constant_type) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +0200964 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
965 if (!Attribute_type) return 0;
966 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
967 if (!Subscript_type) return 0;
968 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
969 if (!Starred_type) return 0;
970 Name_type = make_type("Name", expr_type, Name_fields, 2);
971 if (!Name_type) return 0;
972 List_type = make_type("List", expr_type, List_fields, 2);
973 if (!List_type) return 0;
974 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
975 if (!Tuple_type) return 0;
976 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
977 if (!expr_context_type) return 0;
978 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
979 Load_type = make_type("Load", expr_context_type, NULL, 0);
980 if (!Load_type) return 0;
981 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
982 if (!Load_singleton) return 0;
983 Store_type = make_type("Store", expr_context_type, NULL, 0);
984 if (!Store_type) return 0;
985 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
986 if (!Store_singleton) return 0;
987 Del_type = make_type("Del", expr_context_type, NULL, 0);
988 if (!Del_type) return 0;
989 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
990 if (!Del_singleton) return 0;
991 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
992 if (!AugLoad_type) return 0;
993 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
994 if (!AugLoad_singleton) return 0;
995 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
996 if (!AugStore_type) return 0;
997 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
998 if (!AugStore_singleton) return 0;
999 Param_type = make_type("Param", expr_context_type, NULL, 0);
1000 if (!Param_type) return 0;
1001 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
1002 if (!Param_singleton) return 0;
1003 slice_type = make_type("slice", &AST_type, NULL, 0);
1004 if (!slice_type) return 0;
1005 if (!add_attributes(slice_type, NULL, 0)) return 0;
1006 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
1007 if (!Slice_type) return 0;
1008 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
1009 if (!ExtSlice_type) return 0;
1010 Index_type = make_type("Index", slice_type, Index_fields, 1);
1011 if (!Index_type) return 0;
1012 boolop_type = make_type("boolop", &AST_type, NULL, 0);
1013 if (!boolop_type) return 0;
1014 if (!add_attributes(boolop_type, NULL, 0)) return 0;
1015 And_type = make_type("And", boolop_type, NULL, 0);
1016 if (!And_type) return 0;
1017 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
1018 if (!And_singleton) return 0;
1019 Or_type = make_type("Or", boolop_type, NULL, 0);
1020 if (!Or_type) return 0;
1021 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
1022 if (!Or_singleton) return 0;
1023 operator_type = make_type("operator", &AST_type, NULL, 0);
1024 if (!operator_type) return 0;
1025 if (!add_attributes(operator_type, NULL, 0)) return 0;
1026 Add_type = make_type("Add", operator_type, NULL, 0);
1027 if (!Add_type) return 0;
1028 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
1029 if (!Add_singleton) return 0;
1030 Sub_type = make_type("Sub", operator_type, NULL, 0);
1031 if (!Sub_type) return 0;
1032 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
1033 if (!Sub_singleton) return 0;
1034 Mult_type = make_type("Mult", operator_type, NULL, 0);
1035 if (!Mult_type) return 0;
1036 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
1037 if (!Mult_singleton) return 0;
Benjamin Petersond51374e2014-04-09 23:55:56 -04001038 MatMult_type = make_type("MatMult", operator_type, NULL, 0);
1039 if (!MatMult_type) return 0;
1040 MatMult_singleton = PyType_GenericNew(MatMult_type, NULL, NULL);
1041 if (!MatMult_singleton) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001042 Div_type = make_type("Div", operator_type, NULL, 0);
1043 if (!Div_type) return 0;
1044 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
1045 if (!Div_singleton) return 0;
1046 Mod_type = make_type("Mod", operator_type, NULL, 0);
1047 if (!Mod_type) return 0;
1048 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
1049 if (!Mod_singleton) return 0;
1050 Pow_type = make_type("Pow", operator_type, NULL, 0);
1051 if (!Pow_type) return 0;
1052 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
1053 if (!Pow_singleton) return 0;
1054 LShift_type = make_type("LShift", operator_type, NULL, 0);
1055 if (!LShift_type) return 0;
1056 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
1057 if (!LShift_singleton) return 0;
1058 RShift_type = make_type("RShift", operator_type, NULL, 0);
1059 if (!RShift_type) return 0;
1060 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
1061 if (!RShift_singleton) return 0;
1062 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
1063 if (!BitOr_type) return 0;
1064 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
1065 if (!BitOr_singleton) return 0;
1066 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
1067 if (!BitXor_type) return 0;
1068 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
1069 if (!BitXor_singleton) return 0;
1070 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
1071 if (!BitAnd_type) return 0;
1072 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
1073 if (!BitAnd_singleton) return 0;
1074 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
1075 if (!FloorDiv_type) return 0;
1076 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
1077 if (!FloorDiv_singleton) return 0;
1078 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
1079 if (!unaryop_type) return 0;
1080 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
1081 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
1082 if (!Invert_type) return 0;
1083 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
1084 if (!Invert_singleton) return 0;
1085 Not_type = make_type("Not", unaryop_type, NULL, 0);
1086 if (!Not_type) return 0;
1087 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
1088 if (!Not_singleton) return 0;
1089 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
1090 if (!UAdd_type) return 0;
1091 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
1092 if (!UAdd_singleton) return 0;
1093 USub_type = make_type("USub", unaryop_type, NULL, 0);
1094 if (!USub_type) return 0;
1095 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
1096 if (!USub_singleton) return 0;
1097 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
1098 if (!cmpop_type) return 0;
1099 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
1100 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
1101 if (!Eq_type) return 0;
1102 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
1103 if (!Eq_singleton) return 0;
1104 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
1105 if (!NotEq_type) return 0;
1106 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
1107 if (!NotEq_singleton) return 0;
1108 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
1109 if (!Lt_type) return 0;
1110 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
1111 if (!Lt_singleton) return 0;
1112 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
1113 if (!LtE_type) return 0;
1114 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
1115 if (!LtE_singleton) return 0;
1116 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
1117 if (!Gt_type) return 0;
1118 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
1119 if (!Gt_singleton) return 0;
1120 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
1121 if (!GtE_type) return 0;
1122 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
1123 if (!GtE_singleton) return 0;
1124 Is_type = make_type("Is", cmpop_type, NULL, 0);
1125 if (!Is_type) return 0;
1126 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
1127 if (!Is_singleton) return 0;
1128 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
1129 if (!IsNot_type) return 0;
1130 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
1131 if (!IsNot_singleton) return 0;
1132 In_type = make_type("In", cmpop_type, NULL, 0);
1133 if (!In_type) return 0;
1134 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
1135 if (!In_singleton) return 0;
1136 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
1137 if (!NotIn_type) return 0;
1138 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
1139 if (!NotIn_singleton) return 0;
1140 comprehension_type = make_type("comprehension", &AST_type,
1141 comprehension_fields, 3);
1142 if (!comprehension_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001143 if (!add_attributes(comprehension_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001144 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
1145 if (!excepthandler_type) return 0;
1146 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
1147 return 0;
1148 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
1149 ExceptHandler_fields, 3);
1150 if (!ExceptHandler_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001151 arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001152 if (!arguments_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001153 if (!add_attributes(arguments_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001154 arg_type = make_type("arg", &AST_type, arg_fields, 2);
1155 if (!arg_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001156 if (!add_attributes(arg_type, arg_attributes, 2)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001157 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
1158 if (!keyword_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001159 if (!add_attributes(keyword_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001160 alias_type = make_type("alias", &AST_type, alias_fields, 2);
1161 if (!alias_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001162 if (!add_attributes(alias_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001163 withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);
1164 if (!withitem_type) return 0;
Victor Stinneree4b59c2013-07-27 00:01:35 +02001165 if (!add_attributes(withitem_type, NULL, 0)) return 0;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001166 initialized = 1;
1167 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001168}
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001169
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001170static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
1171static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
1172static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
1173static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
1174 arena);
1175static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
1176static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
1177static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
1178static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
1179static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
1180static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
1181 arena);
1182static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
1183 arena);
1184static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
1185static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
1186static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
1187static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001188static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00001189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001191Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001193 mod_ty p;
1194 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1195 if (!p)
1196 return NULL;
1197 p->kind = Module_kind;
1198 p->v.Module.body = body;
1199 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200}
1201
1202mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001203Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001205 mod_ty p;
1206 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1207 if (!p)
1208 return NULL;
1209 p->kind = Interactive_kind;
1210 p->v.Interactive.body = body;
1211 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212}
1213
1214mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001215Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001217 mod_ty p;
1218 if (!body) {
1219 PyErr_SetString(PyExc_ValueError,
1220 "field body is required for Expression");
1221 return NULL;
1222 }
1223 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1224 if (!p)
1225 return NULL;
1226 p->kind = Expression_kind;
1227 p->v.Expression.body = body;
1228 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229}
1230
1231mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001234 mod_ty p;
1235 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
1236 if (!p)
1237 return NULL;
1238 p->kind = Suite_kind;
1239 p->v.Suite.body = body;
1240 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
1243stmt_ty
1244FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001245 decorator_list, expr_ty returns, int lineno, int col_offset,
1246 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001248 stmt_ty p;
1249 if (!name) {
1250 PyErr_SetString(PyExc_ValueError,
1251 "field name is required for FunctionDef");
1252 return NULL;
1253 }
1254 if (!args) {
1255 PyErr_SetString(PyExc_ValueError,
1256 "field args is required for FunctionDef");
1257 return NULL;
1258 }
1259 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1260 if (!p)
1261 return NULL;
1262 p->kind = FunctionDef_kind;
1263 p->v.FunctionDef.name = name;
1264 p->v.FunctionDef.args = args;
1265 p->v.FunctionDef.body = body;
1266 p->v.FunctionDef.decorator_list = decorator_list;
1267 p->v.FunctionDef.returns = returns;
1268 p->lineno = lineno;
1269 p->col_offset = col_offset;
1270 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271}
1272
1273stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001274AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
1275 * decorator_list, expr_ty returns, int lineno, int col_offset,
1276 PyArena *arena)
1277{
1278 stmt_ty p;
1279 if (!name) {
1280 PyErr_SetString(PyExc_ValueError,
1281 "field name is required for AsyncFunctionDef");
1282 return NULL;
1283 }
1284 if (!args) {
1285 PyErr_SetString(PyExc_ValueError,
1286 "field args is required for AsyncFunctionDef");
1287 return NULL;
1288 }
1289 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1290 if (!p)
1291 return NULL;
1292 p->kind = AsyncFunctionDef_kind;
1293 p->v.AsyncFunctionDef.name = name;
1294 p->v.AsyncFunctionDef.args = args;
1295 p->v.AsyncFunctionDef.body = body;
1296 p->v.AsyncFunctionDef.decorator_list = decorator_list;
1297 p->v.AsyncFunctionDef.returns = returns;
1298 p->lineno = lineno;
1299 p->col_offset = col_offset;
1300 return p;
1301}
1302
1303stmt_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001304ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
1305 body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
1306 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001308 stmt_ty p;
1309 if (!name) {
1310 PyErr_SetString(PyExc_ValueError,
1311 "field name is required for ClassDef");
1312 return NULL;
1313 }
1314 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1315 if (!p)
1316 return NULL;
1317 p->kind = ClassDef_kind;
1318 p->v.ClassDef.name = name;
1319 p->v.ClassDef.bases = bases;
1320 p->v.ClassDef.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001321 p->v.ClassDef.body = body;
1322 p->v.ClassDef.decorator_list = decorator_list;
1323 p->lineno = lineno;
1324 p->col_offset = col_offset;
1325 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326}
1327
1328stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001331 stmt_ty p;
1332 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1333 if (!p)
1334 return NULL;
1335 p->kind = Return_kind;
1336 p->v.Return.value = value;
1337 p->lineno = lineno;
1338 p->col_offset = col_offset;
1339 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340}
1341
1342stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001343Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001345 stmt_ty p;
1346 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1347 if (!p)
1348 return NULL;
1349 p->kind = Delete_kind;
1350 p->v.Delete.targets = targets;
1351 p->lineno = lineno;
1352 p->col_offset = col_offset;
1353 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354}
1355
1356stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001357Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1358 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001360 stmt_ty p;
1361 if (!value) {
1362 PyErr_SetString(PyExc_ValueError,
1363 "field value is required for Assign");
1364 return NULL;
1365 }
1366 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1367 if (!p)
1368 return NULL;
1369 p->kind = Assign_kind;
1370 p->v.Assign.targets = targets;
1371 p->v.Assign.value = value;
1372 p->lineno = lineno;
1373 p->col_offset = col_offset;
1374 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375}
1376
1377stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001378AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1379 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001381 stmt_ty p;
1382 if (!target) {
1383 PyErr_SetString(PyExc_ValueError,
1384 "field target is required for AugAssign");
1385 return NULL;
1386 }
1387 if (!op) {
1388 PyErr_SetString(PyExc_ValueError,
1389 "field op is required for AugAssign");
1390 return NULL;
1391 }
1392 if (!value) {
1393 PyErr_SetString(PyExc_ValueError,
1394 "field value is required for AugAssign");
1395 return NULL;
1396 }
1397 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1398 if (!p)
1399 return NULL;
1400 p->kind = AugAssign_kind;
1401 p->v.AugAssign.target = target;
1402 p->v.AugAssign.op = op;
1403 p->v.AugAssign.value = value;
1404 p->lineno = lineno;
1405 p->col_offset = col_offset;
1406 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407}
1408
1409stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001411 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001413 stmt_ty p;
1414 if (!target) {
1415 PyErr_SetString(PyExc_ValueError,
1416 "field target is required for For");
1417 return NULL;
1418 }
1419 if (!iter) {
1420 PyErr_SetString(PyExc_ValueError,
1421 "field iter is required for For");
1422 return NULL;
1423 }
1424 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1425 if (!p)
1426 return NULL;
1427 p->kind = For_kind;
1428 p->v.For.target = target;
1429 p->v.For.iter = iter;
1430 p->v.For.body = body;
1431 p->v.For.orelse = orelse;
1432 p->lineno = lineno;
1433 p->col_offset = col_offset;
1434 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
1437stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001438AsyncFor(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
1439 lineno, int col_offset, PyArena *arena)
1440{
1441 stmt_ty p;
1442 if (!target) {
1443 PyErr_SetString(PyExc_ValueError,
1444 "field target is required for AsyncFor");
1445 return NULL;
1446 }
1447 if (!iter) {
1448 PyErr_SetString(PyExc_ValueError,
1449 "field iter is required for AsyncFor");
1450 return NULL;
1451 }
1452 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1453 if (!p)
1454 return NULL;
1455 p->kind = AsyncFor_kind;
1456 p->v.AsyncFor.target = target;
1457 p->v.AsyncFor.iter = iter;
1458 p->v.AsyncFor.body = body;
1459 p->v.AsyncFor.orelse = orelse;
1460 p->lineno = lineno;
1461 p->col_offset = col_offset;
1462 return p;
1463}
1464
1465stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001466While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1467 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001469 stmt_ty p;
1470 if (!test) {
1471 PyErr_SetString(PyExc_ValueError,
1472 "field test is required for While");
1473 return NULL;
1474 }
1475 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1476 if (!p)
1477 return NULL;
1478 p->kind = While_kind;
1479 p->v.While.test = test;
1480 p->v.While.body = body;
1481 p->v.While.orelse = orelse;
1482 p->lineno = lineno;
1483 p->col_offset = col_offset;
1484 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485}
1486
1487stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001488If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1489 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001491 stmt_ty p;
1492 if (!test) {
1493 PyErr_SetString(PyExc_ValueError,
1494 "field test is required for If");
1495 return NULL;
1496 }
1497 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1498 if (!p)
1499 return NULL;
1500 p->kind = If_kind;
1501 p->v.If.test = test;
1502 p->v.If.body = body;
1503 p->v.If.orelse = orelse;
1504 p->lineno = lineno;
1505 p->col_offset = col_offset;
1506 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507}
1508
1509stmt_ty
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001510With(asdl_seq * items, asdl_seq * body, int lineno, int col_offset, PyArena
1511 *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001512{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001513 stmt_ty p;
1514 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1515 if (!p)
1516 return NULL;
1517 p->kind = With_kind;
1518 p->v.With.items = items;
1519 p->v.With.body = body;
1520 p->lineno = lineno;
1521 p->col_offset = col_offset;
1522 return p;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001523}
1524
1525stmt_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001526AsyncWith(asdl_seq * items, asdl_seq * body, int lineno, int col_offset,
1527 PyArena *arena)
1528{
1529 stmt_ty p;
1530 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1531 if (!p)
1532 return NULL;
1533 p->kind = AsyncWith_kind;
1534 p->v.AsyncWith.items = items;
1535 p->v.AsyncWith.body = body;
1536 p->lineno = lineno;
1537 p->col_offset = col_offset;
1538 return p;
1539}
1540
1541stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001542Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001544 stmt_ty p;
1545 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1546 if (!p)
1547 return NULL;
1548 p->kind = Raise_kind;
1549 p->v.Raise.exc = exc;
1550 p->v.Raise.cause = cause;
1551 p->lineno = lineno;
1552 p->col_offset = col_offset;
1553 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554}
1555
1556stmt_ty
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001557Try(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, asdl_seq *
1558 finalbody, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001560 stmt_ty p;
1561 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1562 if (!p)
1563 return NULL;
1564 p->kind = Try_kind;
1565 p->v.Try.body = body;
1566 p->v.Try.handlers = handlers;
1567 p->v.Try.orelse = orelse;
1568 p->v.Try.finalbody = finalbody;
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 +00001575Assert(expr_ty test, expr_ty msg, 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 if (!test) {
1579 PyErr_SetString(PyExc_ValueError,
1580 "field test is required for Assert");
1581 return NULL;
1582 }
1583 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1584 if (!p)
1585 return NULL;
1586 p->kind = Assert_kind;
1587 p->v.Assert.test = test;
1588 p->v.Assert.msg = msg;
1589 p->lineno = lineno;
1590 p->col_offset = col_offset;
1591 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592}
1593
1594stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001595Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001597 stmt_ty p;
1598 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1599 if (!p)
1600 return NULL;
1601 p->kind = Import_kind;
1602 p->v.Import.names = names;
1603 p->lineno = lineno;
1604 p->col_offset = col_offset;
1605 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606}
1607
1608stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001609ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1610 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001612 stmt_ty p;
1613 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1614 if (!p)
1615 return NULL;
1616 p->kind = ImportFrom_kind;
1617 p->v.ImportFrom.module = module;
1618 p->v.ImportFrom.names = names;
1619 p->v.ImportFrom.level = level;
1620 p->lineno = lineno;
1621 p->col_offset = col_offset;
1622 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623}
1624
1625stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001626Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001628 stmt_ty p;
1629 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1630 if (!p)
1631 return NULL;
1632 p->kind = Global_kind;
1633 p->v.Global.names = names;
1634 p->lineno = lineno;
1635 p->col_offset = col_offset;
1636 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637}
1638
1639stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001640Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1641{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001642 stmt_ty p;
1643 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1644 if (!p)
1645 return NULL;
1646 p->kind = Nonlocal_kind;
1647 p->v.Nonlocal.names = names;
1648 p->lineno = lineno;
1649 p->col_offset = col_offset;
1650 return p;
Jeremy Hylton81e95022007-02-27 06:50:52 +00001651}
1652
1653stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001654Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001656 stmt_ty p;
1657 if (!value) {
1658 PyErr_SetString(PyExc_ValueError,
1659 "field value is required for Expr");
1660 return NULL;
1661 }
1662 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1663 if (!p)
1664 return NULL;
1665 p->kind = Expr_kind;
1666 p->v.Expr.value = value;
1667 p->lineno = lineno;
1668 p->col_offset = col_offset;
1669 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670}
1671
1672stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001673Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001675 stmt_ty p;
1676 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1677 if (!p)
1678 return NULL;
1679 p->kind = Pass_kind;
1680 p->lineno = lineno;
1681 p->col_offset = col_offset;
1682 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683}
1684
1685stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001686Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001688 stmt_ty p;
1689 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1690 if (!p)
1691 return NULL;
1692 p->kind = Break_kind;
1693 p->lineno = lineno;
1694 p->col_offset = col_offset;
1695 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696}
1697
1698stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001699Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001701 stmt_ty p;
1702 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1703 if (!p)
1704 return NULL;
1705 p->kind = Continue_kind;
1706 p->lineno = lineno;
1707 p->col_offset = col_offset;
1708 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709}
1710
1711expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001712BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1713 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001715 expr_ty p;
1716 if (!op) {
1717 PyErr_SetString(PyExc_ValueError,
1718 "field op is required for BoolOp");
1719 return NULL;
1720 }
1721 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1722 if (!p)
1723 return NULL;
1724 p->kind = BoolOp_kind;
1725 p->v.BoolOp.op = op;
1726 p->v.BoolOp.values = values;
1727 p->lineno = lineno;
1728 p->col_offset = col_offset;
1729 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730}
1731
1732expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001733BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1734 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001736 expr_ty p;
1737 if (!left) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "field left is required for BinOp");
1740 return NULL;
1741 }
1742 if (!op) {
1743 PyErr_SetString(PyExc_ValueError,
1744 "field op is required for BinOp");
1745 return NULL;
1746 }
1747 if (!right) {
1748 PyErr_SetString(PyExc_ValueError,
1749 "field right is required for BinOp");
1750 return NULL;
1751 }
1752 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1753 if (!p)
1754 return NULL;
1755 p->kind = BinOp_kind;
1756 p->v.BinOp.left = left;
1757 p->v.BinOp.op = op;
1758 p->v.BinOp.right = right;
1759 p->lineno = lineno;
1760 p->col_offset = col_offset;
1761 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762}
1763
1764expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001765UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1766 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001768 expr_ty p;
1769 if (!op) {
1770 PyErr_SetString(PyExc_ValueError,
1771 "field op is required for UnaryOp");
1772 return NULL;
1773 }
1774 if (!operand) {
1775 PyErr_SetString(PyExc_ValueError,
1776 "field operand is required for UnaryOp");
1777 return NULL;
1778 }
1779 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1780 if (!p)
1781 return NULL;
1782 p->kind = UnaryOp_kind;
1783 p->v.UnaryOp.op = op;
1784 p->v.UnaryOp.operand = operand;
1785 p->lineno = lineno;
1786 p->col_offset = col_offset;
1787 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788}
1789
1790expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001791Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1792 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001794 expr_ty p;
1795 if (!args) {
1796 PyErr_SetString(PyExc_ValueError,
1797 "field args is required for Lambda");
1798 return NULL;
1799 }
1800 if (!body) {
1801 PyErr_SetString(PyExc_ValueError,
1802 "field body is required for Lambda");
1803 return NULL;
1804 }
1805 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1806 if (!p)
1807 return NULL;
1808 p->kind = Lambda_kind;
1809 p->v.Lambda.args = args;
1810 p->v.Lambda.body = body;
1811 p->lineno = lineno;
1812 p->col_offset = col_offset;
1813 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814}
1815
1816expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001817IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1818 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001819{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001820 expr_ty p;
1821 if (!test) {
1822 PyErr_SetString(PyExc_ValueError,
1823 "field test is required for IfExp");
1824 return NULL;
1825 }
1826 if (!body) {
1827 PyErr_SetString(PyExc_ValueError,
1828 "field body is required for IfExp");
1829 return NULL;
1830 }
1831 if (!orelse) {
1832 PyErr_SetString(PyExc_ValueError,
1833 "field orelse is required for IfExp");
1834 return NULL;
1835 }
1836 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1837 if (!p)
1838 return NULL;
1839 p->kind = IfExp_kind;
1840 p->v.IfExp.test = test;
1841 p->v.IfExp.body = body;
1842 p->v.IfExp.orelse = orelse;
1843 p->lineno = lineno;
1844 p->col_offset = col_offset;
1845 return p;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001846}
1847
1848expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001849Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1850 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001852 expr_ty p;
1853 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1854 if (!p)
1855 return NULL;
1856 p->kind = Dict_kind;
1857 p->v.Dict.keys = keys;
1858 p->v.Dict.values = values;
1859 p->lineno = lineno;
1860 p->col_offset = col_offset;
1861 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862}
1863
1864expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001865Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1866{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001867 expr_ty p;
1868 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1869 if (!p)
1870 return NULL;
1871 p->kind = Set_kind;
1872 p->v.Set.elts = elts;
1873 p->lineno = lineno;
1874 p->col_offset = col_offset;
1875 return p;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001876}
1877
1878expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001879ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1880 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001882 expr_ty p;
1883 if (!elt) {
1884 PyErr_SetString(PyExc_ValueError,
1885 "field elt is required for ListComp");
1886 return NULL;
1887 }
1888 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1889 if (!p)
1890 return NULL;
1891 p->kind = ListComp_kind;
1892 p->v.ListComp.elt = elt;
1893 p->v.ListComp.generators = generators;
1894 p->lineno = lineno;
1895 p->col_offset = col_offset;
1896 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
1899expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001900SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1901 *arena)
1902{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001903 expr_ty p;
1904 if (!elt) {
1905 PyErr_SetString(PyExc_ValueError,
1906 "field elt is required for SetComp");
1907 return NULL;
1908 }
1909 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1910 if (!p)
1911 return NULL;
1912 p->kind = SetComp_kind;
1913 p->v.SetComp.elt = elt;
1914 p->v.SetComp.generators = generators;
1915 p->lineno = lineno;
1916 p->col_offset = col_offset;
1917 return p;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001918}
1919
1920expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001921DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1922 col_offset, PyArena *arena)
1923{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001924 expr_ty p;
1925 if (!key) {
1926 PyErr_SetString(PyExc_ValueError,
1927 "field key is required for DictComp");
1928 return NULL;
1929 }
1930 if (!value) {
1931 PyErr_SetString(PyExc_ValueError,
1932 "field value is required for DictComp");
1933 return NULL;
1934 }
1935 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1936 if (!p)
1937 return NULL;
1938 p->kind = DictComp_kind;
1939 p->v.DictComp.key = key;
1940 p->v.DictComp.value = value;
1941 p->v.DictComp.generators = generators;
1942 p->lineno = lineno;
1943 p->col_offset = col_offset;
1944 return p;
Guido van Rossum992d4a32007-07-11 13:09:30 +00001945}
1946
1947expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001948GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1949 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001951 expr_ty p;
1952 if (!elt) {
1953 PyErr_SetString(PyExc_ValueError,
1954 "field elt is required for GeneratorExp");
1955 return NULL;
1956 }
1957 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1958 if (!p)
1959 return NULL;
1960 p->kind = GeneratorExp_kind;
1961 p->v.GeneratorExp.elt = elt;
1962 p->v.GeneratorExp.generators = generators;
1963 p->lineno = lineno;
1964 p->col_offset = col_offset;
1965 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966}
1967
1968expr_ty
Yury Selivanov75445082015-05-11 22:57:16 -04001969Await(expr_ty value, int lineno, int col_offset, PyArena *arena)
1970{
1971 expr_ty p;
1972 if (!value) {
1973 PyErr_SetString(PyExc_ValueError,
1974 "field value is required for Await");
1975 return NULL;
1976 }
1977 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1978 if (!p)
1979 return NULL;
1980 p->kind = Await_kind;
1981 p->v.Await.value = value;
1982 p->lineno = lineno;
1983 p->col_offset = col_offset;
1984 return p;
1985}
1986
1987expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05001988Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02001990 expr_ty p;
1991 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1992 if (!p)
1993 return NULL;
1994 p->kind = Yield_kind;
1995 p->v.Yield.value = value;
1996 p->lineno = lineno;
1997 p->col_offset = col_offset;
1998 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
2001expr_ty
Benjamin Peterson527c6222012-01-14 08:58:23 -05002002YieldFrom(expr_ty value, int lineno, int col_offset, PyArena *arena)
2003{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002004 expr_ty p;
2005 if (!value) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field value is required for YieldFrom");
2008 return NULL;
2009 }
2010 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2011 if (!p)
2012 return NULL;
2013 p->kind = YieldFrom_kind;
2014 p->v.YieldFrom.value = value;
2015 p->lineno = lineno;
2016 p->col_offset = col_offset;
2017 return p;
Benjamin Peterson527c6222012-01-14 08:58:23 -05002018}
2019
2020expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002021Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
2022 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002024 expr_ty p;
2025 if (!left) {
2026 PyErr_SetString(PyExc_ValueError,
2027 "field left is required for Compare");
2028 return NULL;
2029 }
2030 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2031 if (!p)
2032 return NULL;
2033 p->kind = Compare_kind;
2034 p->v.Compare.left = left;
2035 p->v.Compare.ops = ops;
2036 p->v.Compare.comparators = comparators;
2037 p->lineno = lineno;
2038 p->col_offset = col_offset;
2039 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040}
2041
2042expr_ty
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002043Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, int lineno, int
2044 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002046 expr_ty p;
2047 if (!func) {
2048 PyErr_SetString(PyExc_ValueError,
2049 "field func is required for Call");
2050 return NULL;
2051 }
2052 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2053 if (!p)
2054 return NULL;
2055 p->kind = Call_kind;
2056 p->v.Call.func = func;
2057 p->v.Call.args = args;
2058 p->v.Call.keywords = keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002059 p->lineno = lineno;
2060 p->col_offset = col_offset;
2061 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062}
2063
2064expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002065Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002067 expr_ty p;
2068 if (!n) {
2069 PyErr_SetString(PyExc_ValueError,
2070 "field n is required for Num");
2071 return NULL;
2072 }
2073 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2074 if (!p)
2075 return NULL;
2076 p->kind = Num_kind;
2077 p->v.Num.n = n;
2078 p->lineno = lineno;
2079 p->col_offset = col_offset;
2080 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081}
2082
2083expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002084Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002086 expr_ty p;
2087 if (!s) {
2088 PyErr_SetString(PyExc_ValueError,
2089 "field s is required for Str");
2090 return NULL;
2091 }
2092 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2093 if (!p)
2094 return NULL;
2095 p->kind = Str_kind;
2096 p->v.Str.s = s;
2097 p->lineno = lineno;
2098 p->col_offset = col_offset;
2099 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100}
2101
2102expr_ty
Eric V. Smith235a6f02015-09-19 14:51:32 -04002103FormattedValue(expr_ty value, int conversion, expr_ty format_spec, int lineno,
2104 int col_offset, PyArena *arena)
2105{
2106 expr_ty p;
2107 if (!value) {
2108 PyErr_SetString(PyExc_ValueError,
2109 "field value is required for FormattedValue");
2110 return NULL;
2111 }
2112 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2113 if (!p)
2114 return NULL;
2115 p->kind = FormattedValue_kind;
2116 p->v.FormattedValue.value = value;
2117 p->v.FormattedValue.conversion = conversion;
2118 p->v.FormattedValue.format_spec = format_spec;
2119 p->lineno = lineno;
2120 p->col_offset = col_offset;
2121 return p;
2122}
2123
2124expr_ty
2125JoinedStr(asdl_seq * values, int lineno, int col_offset, PyArena *arena)
2126{
2127 expr_ty p;
2128 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2129 if (!p)
2130 return NULL;
2131 p->kind = JoinedStr_kind;
2132 p->v.JoinedStr.values = values;
2133 p->lineno = lineno;
2134 p->col_offset = col_offset;
2135 return p;
2136}
2137
2138expr_ty
Benjamin Petersone2498412011-08-09 16:08:39 -05002139Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
Thomas Wouters00e41de2007-02-23 19:56:57 +00002140{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002141 expr_ty p;
2142 if (!s) {
2143 PyErr_SetString(PyExc_ValueError,
2144 "field s is required for Bytes");
2145 return NULL;
2146 }
2147 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2148 if (!p)
2149 return NULL;
2150 p->kind = Bytes_kind;
2151 p->v.Bytes.s = s;
2152 p->lineno = lineno;
2153 p->col_offset = col_offset;
2154 return p;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002155}
2156
2157expr_ty
Benjamin Peterson442f2092012-12-06 17:41:04 -05002158NameConstant(singleton value, int lineno, int col_offset, PyArena *arena)
2159{
Victor Stinneree4b59c2013-07-27 00:01:35 +02002160 expr_ty p;
2161 if (!value) {
2162 PyErr_SetString(PyExc_ValueError,
2163 "field value is required for NameConstant");
2164 return NULL;
2165 }
2166 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2167 if (!p)
2168 return NULL;
2169 p->kind = NameConstant_kind;
2170 p->v.NameConstant.value = value;
2171 p->lineno = lineno;
2172 p->col_offset = col_offset;
2173 return p;
Benjamin Peterson442f2092012-12-06 17:41:04 -05002174}
2175
2176expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00002177Ellipsis(int lineno, int col_offset, PyArena *arena)
2178{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002179 expr_ty p;
2180 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2181 if (!p)
2182 return NULL;
2183 p->kind = Ellipsis_kind;
2184 p->lineno = lineno;
2185 p->col_offset = col_offset;
2186 return p;
Georg Brandl52318d62006-09-06 07:06:08 +00002187}
2188
2189expr_ty
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002190Constant(constant value, int lineno, int col_offset, PyArena *arena)
2191{
2192 expr_ty p;
2193 if (!value) {
2194 PyErr_SetString(PyExc_ValueError,
2195 "field value is required for Constant");
2196 return NULL;
2197 }
2198 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2199 if (!p)
2200 return NULL;
2201 p->kind = Constant_kind;
2202 p->v.Constant.value = value;
2203 p->lineno = lineno;
2204 p->col_offset = col_offset;
2205 return p;
2206}
2207
2208expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002209Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
2210 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002212 expr_ty p;
2213 if (!value) {
2214 PyErr_SetString(PyExc_ValueError,
2215 "field value is required for Attribute");
2216 return NULL;
2217 }
2218 if (!attr) {
2219 PyErr_SetString(PyExc_ValueError,
2220 "field attr is required for Attribute");
2221 return NULL;
2222 }
2223 if (!ctx) {
2224 PyErr_SetString(PyExc_ValueError,
2225 "field ctx is required for Attribute");
2226 return NULL;
2227 }
2228 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2229 if (!p)
2230 return NULL;
2231 p->kind = Attribute_kind;
2232 p->v.Attribute.value = value;
2233 p->v.Attribute.attr = attr;
2234 p->v.Attribute.ctx = ctx;
2235 p->lineno = lineno;
2236 p->col_offset = col_offset;
2237 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238}
2239
2240expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002241Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
2242 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002244 expr_ty p;
2245 if (!value) {
2246 PyErr_SetString(PyExc_ValueError,
2247 "field value is required for Subscript");
2248 return NULL;
2249 }
2250 if (!slice) {
2251 PyErr_SetString(PyExc_ValueError,
2252 "field slice is required for Subscript");
2253 return NULL;
2254 }
2255 if (!ctx) {
2256 PyErr_SetString(PyExc_ValueError,
2257 "field ctx is required for Subscript");
2258 return NULL;
2259 }
2260 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2261 if (!p)
2262 return NULL;
2263 p->kind = Subscript_kind;
2264 p->v.Subscript.value = value;
2265 p->v.Subscript.slice = slice;
2266 p->v.Subscript.ctx = ctx;
2267 p->lineno = lineno;
2268 p->col_offset = col_offset;
2269 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270}
2271
2272expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00002273Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
2274 *arena)
2275{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002276 expr_ty p;
2277 if (!value) {
2278 PyErr_SetString(PyExc_ValueError,
2279 "field value is required for Starred");
2280 return NULL;
2281 }
2282 if (!ctx) {
2283 PyErr_SetString(PyExc_ValueError,
2284 "field ctx is required for Starred");
2285 return NULL;
2286 }
2287 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2288 if (!p)
2289 return NULL;
2290 p->kind = Starred_kind;
2291 p->v.Starred.value = value;
2292 p->v.Starred.ctx = ctx;
2293 p->lineno = lineno;
2294 p->col_offset = col_offset;
2295 return p;
Guido van Rossum0368b722007-05-11 16:50:42 +00002296}
2297
2298expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002299Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
2300 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002302 expr_ty p;
2303 if (!id) {
2304 PyErr_SetString(PyExc_ValueError,
2305 "field id is required for Name");
2306 return NULL;
2307 }
2308 if (!ctx) {
2309 PyErr_SetString(PyExc_ValueError,
2310 "field ctx is required for Name");
2311 return NULL;
2312 }
2313 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2314 if (!p)
2315 return NULL;
2316 p->kind = Name_kind;
2317 p->v.Name.id = id;
2318 p->v.Name.ctx = ctx;
2319 p->lineno = lineno;
2320 p->col_offset = col_offset;
2321 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322}
2323
2324expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002325List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2326 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002328 expr_ty p;
2329 if (!ctx) {
2330 PyErr_SetString(PyExc_ValueError,
2331 "field ctx is required for List");
2332 return NULL;
2333 }
2334 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2335 if (!p)
2336 return NULL;
2337 p->kind = List_kind;
2338 p->v.List.elts = elts;
2339 p->v.List.ctx = ctx;
2340 p->lineno = lineno;
2341 p->col_offset = col_offset;
2342 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343}
2344
2345expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002346Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
2347 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002349 expr_ty p;
2350 if (!ctx) {
2351 PyErr_SetString(PyExc_ValueError,
2352 "field ctx is required for Tuple");
2353 return NULL;
2354 }
2355 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
2356 if (!p)
2357 return NULL;
2358 p->kind = Tuple_kind;
2359 p->v.Tuple.elts = elts;
2360 p->v.Tuple.ctx = ctx;
2361 p->lineno = lineno;
2362 p->col_offset = col_offset;
2363 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364}
2365
2366slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002367Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002369 slice_ty p;
2370 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2371 if (!p)
2372 return NULL;
2373 p->kind = Slice_kind;
2374 p->v.Slice.lower = lower;
2375 p->v.Slice.upper = upper;
2376 p->v.Slice.step = step;
2377 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378}
2379
2380slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002381ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002383 slice_ty p;
2384 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2385 if (!p)
2386 return NULL;
2387 p->kind = ExtSlice_kind;
2388 p->v.ExtSlice.dims = dims;
2389 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002390}
2391
2392slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002393Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002394{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002395 slice_ty p;
2396 if (!value) {
2397 PyErr_SetString(PyExc_ValueError,
2398 "field value is required for Index");
2399 return NULL;
2400 }
2401 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
2402 if (!p)
2403 return NULL;
2404 p->kind = Index_kind;
2405 p->v.Index.value = value;
2406 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407}
2408
2409comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002410comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002412 comprehension_ty p;
2413 if (!target) {
2414 PyErr_SetString(PyExc_ValueError,
2415 "field target is required for comprehension");
2416 return NULL;
2417 }
2418 if (!iter) {
2419 PyErr_SetString(PyExc_ValueError,
2420 "field iter is required for comprehension");
2421 return NULL;
2422 }
2423 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
2424 if (!p)
2425 return NULL;
2426 p->target = target;
2427 p->iter = iter;
2428 p->ifs = ifs;
2429 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430}
2431
2432excepthandler_ty
Neal Norwitzad74aa82008-03-31 05:14:30 +00002433ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002434 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002436 excepthandler_ty p;
2437 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
2438 if (!p)
2439 return NULL;
2440 p->kind = ExceptHandler_kind;
2441 p->v.ExceptHandler.type = type;
2442 p->v.ExceptHandler.name = name;
2443 p->v.ExceptHandler.body = body;
2444 p->lineno = lineno;
2445 p->col_offset = col_offset;
2446 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447}
2448
2449arguments_ty
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002450arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq *
2451 kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002453 arguments_ty p;
2454 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
2455 if (!p)
2456 return NULL;
2457 p->args = args;
2458 p->vararg = vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002459 p->kwonlyargs = kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002460 p->kw_defaults = kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02002461 p->kwarg = kwarg;
2462 p->defaults = defaults;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002463 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464}
2465
Neal Norwitzc1505362006-12-28 06:47:50 +00002466arg_ty
Victor Stinnerc106c682015-11-06 17:01:48 +01002467arg(identifier arg, expr_ty annotation, int lineno, int col_offset, PyArena
2468 *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002469{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002470 arg_ty p;
2471 if (!arg) {
2472 PyErr_SetString(PyExc_ValueError,
2473 "field arg is required for arg");
2474 return NULL;
2475 }
2476 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
2477 if (!p)
2478 return NULL;
2479 p->arg = arg;
2480 p->annotation = annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01002481 p->lineno = lineno;
2482 p->col_offset = col_offset;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002483 return p;
Neal Norwitzc1505362006-12-28 06:47:50 +00002484}
2485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002487keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002489 keyword_ty p;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002490 if (!value) {
2491 PyErr_SetString(PyExc_ValueError,
2492 "field value is required for keyword");
2493 return NULL;
2494 }
2495 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
2496 if (!p)
2497 return NULL;
2498 p->arg = arg;
2499 p->value = value;
2500 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501}
2502
2503alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002504alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002506 alias_ty p;
2507 if (!name) {
2508 PyErr_SetString(PyExc_ValueError,
2509 "field name is required for alias");
2510 return NULL;
2511 }
2512 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
2513 if (!p)
2514 return NULL;
2515 p->name = name;
2516 p->asname = asname;
2517 return p;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518}
2519
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002520withitem_ty
2521withitem(expr_ty context_expr, expr_ty optional_vars, PyArena *arena)
2522{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002523 withitem_ty p;
2524 if (!context_expr) {
2525 PyErr_SetString(PyExc_ValueError,
2526 "field context_expr is required for withitem");
2527 return NULL;
2528 }
2529 p = (withitem_ty)PyArena_Malloc(arena, sizeof(*p));
2530 if (!p)
2531 return NULL;
2532 p->context_expr = context_expr;
2533 p->optional_vars = optional_vars;
2534 return p;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002535}
2536
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002537
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002538PyObject*
2539ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002540{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002541 mod_ty o = (mod_ty)_o;
2542 PyObject *result = NULL, *value = NULL;
2543 if (!o) {
2544 Py_INCREF(Py_None);
2545 return Py_None;
2546 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002547
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002548 switch (o->kind) {
2549 case Module_kind:
2550 result = PyType_GenericNew(Module_type, NULL, NULL);
2551 if (!result) goto failed;
2552 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2553 if (!value) goto failed;
2554 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2555 goto failed;
2556 Py_DECREF(value);
2557 break;
2558 case Interactive_kind:
2559 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2560 if (!result) goto failed;
2561 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2562 if (!value) goto failed;
2563 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2564 goto failed;
2565 Py_DECREF(value);
2566 break;
2567 case Expression_kind:
2568 result = PyType_GenericNew(Expression_type, NULL, NULL);
2569 if (!result) goto failed;
2570 value = ast2obj_expr(o->v.Expression.body);
2571 if (!value) goto failed;
2572 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2573 goto failed;
2574 Py_DECREF(value);
2575 break;
2576 case Suite_kind:
2577 result = PyType_GenericNew(Suite_type, NULL, NULL);
2578 if (!result) goto failed;
2579 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2580 if (!value) goto failed;
2581 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2582 goto failed;
2583 Py_DECREF(value);
2584 break;
2585 }
2586 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002587failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002588 Py_XDECREF(value);
2589 Py_XDECREF(result);
2590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591}
2592
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002593PyObject*
2594ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002596 stmt_ty o = (stmt_ty)_o;
2597 PyObject *result = NULL, *value = NULL;
2598 if (!o) {
2599 Py_INCREF(Py_None);
2600 return Py_None;
2601 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002602
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002603 switch (o->kind) {
2604 case FunctionDef_kind:
2605 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2606 if (!result) goto failed;
2607 value = ast2obj_identifier(o->v.FunctionDef.name);
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002608 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002609 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2610 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002611 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002612 value = ast2obj_arguments(o->v.FunctionDef.args);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002613 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002614 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2615 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002616 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002617 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2618 if (!value) goto failed;
2619 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
2622 value = ast2obj_list(o->v.FunctionDef.decorator_list, ast2obj_expr);
2623 if (!value) goto failed;
2624 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 value = ast2obj_expr(o->v.FunctionDef.returns);
2628 if (!value) goto failed;
2629 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
2632 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002633 case AsyncFunctionDef_kind:
2634 result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
2635 if (!result) goto failed;
2636 value = ast2obj_identifier(o->v.AsyncFunctionDef.name);
2637 if (!value) goto failed;
2638 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
2641 value = ast2obj_arguments(o->v.AsyncFunctionDef.args);
2642 if (!value) goto failed;
2643 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 value = ast2obj_list(o->v.AsyncFunctionDef.body, ast2obj_stmt);
2647 if (!value) goto failed;
2648 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 value = ast2obj_list(o->v.AsyncFunctionDef.decorator_list,
2652 ast2obj_expr);
2653 if (!value) goto failed;
2654 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
2657 value = ast2obj_expr(o->v.AsyncFunctionDef.returns);
2658 if (!value) goto failed;
2659 if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002663 case ClassDef_kind:
2664 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2665 if (!result) goto failed;
2666 value = ast2obj_identifier(o->v.ClassDef.name);
2667 if (!value) goto failed;
2668 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
2669 goto failed;
2670 Py_DECREF(value);
2671 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2672 if (!value) goto failed;
2673 if (_PyObject_SetAttrId(result, &PyId_bases, value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
2676 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2677 if (!value) goto failed;
2678 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002681 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2682 if (!value) goto failed;
2683 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2684 goto failed;
2685 Py_DECREF(value);
2686 value = ast2obj_list(o->v.ClassDef.decorator_list, ast2obj_expr);
2687 if (!value) goto failed;
2688 if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
2689 goto failed;
2690 Py_DECREF(value);
2691 break;
2692 case Return_kind:
2693 result = PyType_GenericNew(Return_type, NULL, NULL);
2694 if (!result) goto failed;
2695 value = ast2obj_expr(o->v.Return.value);
2696 if (!value) goto failed;
2697 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 break;
2701 case Delete_kind:
2702 result = PyType_GenericNew(Delete_type, NULL, NULL);
2703 if (!result) goto failed;
2704 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2705 if (!value) goto failed;
2706 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2707 goto failed;
2708 Py_DECREF(value);
2709 break;
2710 case Assign_kind:
2711 result = PyType_GenericNew(Assign_type, NULL, NULL);
2712 if (!result) goto failed;
2713 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2714 if (!value) goto failed;
2715 if (_PyObject_SetAttrId(result, &PyId_targets, value) == -1)
2716 goto failed;
2717 Py_DECREF(value);
2718 value = ast2obj_expr(o->v.Assign.value);
2719 if (!value) goto failed;
2720 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2721 goto failed;
2722 Py_DECREF(value);
2723 break;
2724 case AugAssign_kind:
2725 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2726 if (!result) goto failed;
2727 value = ast2obj_expr(o->v.AugAssign.target);
2728 if (!value) goto failed;
2729 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2730 goto failed;
2731 Py_DECREF(value);
2732 value = ast2obj_operator(o->v.AugAssign.op);
2733 if (!value) goto failed;
2734 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
2735 goto failed;
2736 Py_DECREF(value);
2737 value = ast2obj_expr(o->v.AugAssign.value);
2738 if (!value) goto failed;
2739 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2740 goto failed;
2741 Py_DECREF(value);
2742 break;
2743 case For_kind:
2744 result = PyType_GenericNew(For_type, NULL, NULL);
2745 if (!result) goto failed;
2746 value = ast2obj_expr(o->v.For.target);
2747 if (!value) goto failed;
2748 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 value = ast2obj_expr(o->v.For.iter);
2752 if (!value) goto failed;
2753 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
2756 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2757 if (!value) goto failed;
2758 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
2761 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2762 if (!value) goto failed;
2763 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2764 goto failed;
2765 Py_DECREF(value);
2766 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002767 case AsyncFor_kind:
2768 result = PyType_GenericNew(AsyncFor_type, NULL, NULL);
2769 if (!result) goto failed;
2770 value = ast2obj_expr(o->v.AsyncFor.target);
2771 if (!value) goto failed;
2772 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
2773 goto failed;
2774 Py_DECREF(value);
2775 value = ast2obj_expr(o->v.AsyncFor.iter);
2776 if (!value) goto failed;
2777 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
2778 goto failed;
2779 Py_DECREF(value);
2780 value = ast2obj_list(o->v.AsyncFor.body, ast2obj_stmt);
2781 if (!value) goto failed;
2782 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2783 goto failed;
2784 Py_DECREF(value);
2785 value = ast2obj_list(o->v.AsyncFor.orelse, ast2obj_stmt);
2786 if (!value) goto failed;
2787 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2788 goto failed;
2789 Py_DECREF(value);
2790 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002791 case While_kind:
2792 result = PyType_GenericNew(While_type, NULL, NULL);
2793 if (!result) goto failed;
2794 value = ast2obj_expr(o->v.While.test);
2795 if (!value) goto failed;
2796 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2797 goto failed;
2798 Py_DECREF(value);
2799 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2800 if (!value) goto failed;
2801 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2802 goto failed;
2803 Py_DECREF(value);
2804 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2805 if (!value) goto failed;
2806 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2807 goto failed;
2808 Py_DECREF(value);
2809 break;
2810 case If_kind:
2811 result = PyType_GenericNew(If_type, NULL, NULL);
2812 if (!result) goto failed;
2813 value = ast2obj_expr(o->v.If.test);
2814 if (!value) goto failed;
2815 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2816 goto failed;
2817 Py_DECREF(value);
2818 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2819 if (!value) goto failed;
2820 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2821 goto failed;
2822 Py_DECREF(value);
2823 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2824 if (!value) goto failed;
2825 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2826 goto failed;
2827 Py_DECREF(value);
2828 break;
2829 case With_kind:
2830 result = PyType_GenericNew(With_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_list(o->v.With.items, ast2obj_withitem);
2833 if (!value) goto failed;
2834 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2838 if (!value) goto failed;
2839 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 break;
Yury Selivanov75445082015-05-11 22:57:16 -04002843 case AsyncWith_kind:
2844 result = PyType_GenericNew(AsyncWith_type, NULL, NULL);
2845 if (!result) goto failed;
2846 value = ast2obj_list(o->v.AsyncWith.items, ast2obj_withitem);
2847 if (!value) goto failed;
2848 if (_PyObject_SetAttrId(result, &PyId_items, value) == -1)
2849 goto failed;
2850 Py_DECREF(value);
2851 value = ast2obj_list(o->v.AsyncWith.body, ast2obj_stmt);
2852 if (!value) goto failed;
2853 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
2856 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002857 case Raise_kind:
2858 result = PyType_GenericNew(Raise_type, NULL, NULL);
2859 if (!result) goto failed;
2860 value = ast2obj_expr(o->v.Raise.exc);
2861 if (!value) goto failed;
2862 if (_PyObject_SetAttrId(result, &PyId_exc, value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
2865 value = ast2obj_expr(o->v.Raise.cause);
2866 if (!value) goto failed;
2867 if (_PyObject_SetAttrId(result, &PyId_cause, value) == -1)
2868 goto failed;
2869 Py_DECREF(value);
2870 break;
2871 case Try_kind:
2872 result = PyType_GenericNew(Try_type, NULL, NULL);
2873 if (!result) goto failed;
2874 value = ast2obj_list(o->v.Try.body, ast2obj_stmt);
2875 if (!value) goto failed;
2876 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
2877 goto failed;
2878 Py_DECREF(value);
2879 value = ast2obj_list(o->v.Try.handlers, ast2obj_excepthandler);
2880 if (!value) goto failed;
2881 if (_PyObject_SetAttrId(result, &PyId_handlers, value) == -1)
2882 goto failed;
2883 Py_DECREF(value);
2884 value = ast2obj_list(o->v.Try.orelse, ast2obj_stmt);
2885 if (!value) goto failed;
2886 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
2887 goto failed;
2888 Py_DECREF(value);
2889 value = ast2obj_list(o->v.Try.finalbody, ast2obj_stmt);
2890 if (!value) goto failed;
2891 if (_PyObject_SetAttrId(result, &PyId_finalbody, value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 break;
2895 case Assert_kind:
2896 result = PyType_GenericNew(Assert_type, NULL, NULL);
2897 if (!result) goto failed;
2898 value = ast2obj_expr(o->v.Assert.test);
2899 if (!value) goto failed;
2900 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
2901 goto failed;
2902 Py_DECREF(value);
2903 value = ast2obj_expr(o->v.Assert.msg);
2904 if (!value) goto failed;
2905 if (_PyObject_SetAttrId(result, &PyId_msg, value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 break;
2909 case Import_kind:
2910 result = PyType_GenericNew(Import_type, NULL, NULL);
2911 if (!result) goto failed;
2912 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2913 if (!value) goto failed;
2914 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2915 goto failed;
2916 Py_DECREF(value);
2917 break;
2918 case ImportFrom_kind:
2919 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2920 if (!result) goto failed;
2921 value = ast2obj_identifier(o->v.ImportFrom.module);
2922 if (!value) goto failed;
2923 if (_PyObject_SetAttrId(result, &PyId_module, value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
2926 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2927 if (!value) goto failed;
2928 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2929 goto failed;
2930 Py_DECREF(value);
2931 value = ast2obj_int(o->v.ImportFrom.level);
2932 if (!value) goto failed;
2933 if (_PyObject_SetAttrId(result, &PyId_level, value) == -1)
2934 goto failed;
2935 Py_DECREF(value);
2936 break;
2937 case Global_kind:
2938 result = PyType_GenericNew(Global_type, NULL, NULL);
2939 if (!result) goto failed;
2940 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2941 if (!value) goto failed;
2942 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2943 goto failed;
2944 Py_DECREF(value);
2945 break;
2946 case Nonlocal_kind:
2947 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2948 if (!result) goto failed;
2949 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2950 if (!value) goto failed;
2951 if (_PyObject_SetAttrId(result, &PyId_names, value) == -1)
2952 goto failed;
2953 Py_DECREF(value);
2954 break;
2955 case Expr_kind:
2956 result = PyType_GenericNew(Expr_type, NULL, NULL);
2957 if (!result) goto failed;
2958 value = ast2obj_expr(o->v.Expr.value);
2959 if (!value) goto failed;
2960 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
2961 goto failed;
2962 Py_DECREF(value);
2963 break;
2964 case Pass_kind:
2965 result = PyType_GenericNew(Pass_type, NULL, NULL);
2966 if (!result) goto failed;
2967 break;
2968 case Break_kind:
2969 result = PyType_GenericNew(Break_type, NULL, NULL);
2970 if (!result) goto failed;
2971 break;
2972 case Continue_kind:
2973 result = PyType_GenericNew(Continue_type, NULL, NULL);
2974 if (!result) goto failed;
2975 break;
2976 }
2977 value = ast2obj_int(o->lineno);
2978 if (!value) goto failed;
2979 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
2980 goto failed;
2981 Py_DECREF(value);
2982 value = ast2obj_int(o->col_offset);
2983 if (!value) goto failed;
2984 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
2985 goto failed;
2986 Py_DECREF(value);
2987 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002988failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002989 Py_XDECREF(value);
2990 Py_XDECREF(result);
2991 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992}
2993
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002994PyObject*
2995ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02002997 expr_ty o = (expr_ty)_o;
2998 PyObject *result = NULL, *value = NULL;
2999 if (!o) {
3000 Py_INCREF(Py_None);
3001 return Py_None;
3002 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003003
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003004 switch (o->kind) {
3005 case BoolOp_kind:
3006 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
3007 if (!result) goto failed;
3008 value = ast2obj_boolop(o->v.BoolOp.op);
3009 if (!value) goto failed;
3010 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3011 goto failed;
3012 Py_DECREF(value);
3013 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
3014 if (!value) goto failed;
3015 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3016 goto failed;
3017 Py_DECREF(value);
3018 break;
3019 case BinOp_kind:
3020 result = PyType_GenericNew(BinOp_type, NULL, NULL);
3021 if (!result) goto failed;
3022 value = ast2obj_expr(o->v.BinOp.left);
3023 if (!value) goto failed;
3024 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3025 goto failed;
3026 Py_DECREF(value);
3027 value = ast2obj_operator(o->v.BinOp.op);
3028 if (!value) goto failed;
3029 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3030 goto failed;
3031 Py_DECREF(value);
3032 value = ast2obj_expr(o->v.BinOp.right);
3033 if (!value) goto failed;
3034 if (_PyObject_SetAttrId(result, &PyId_right, value) == -1)
3035 goto failed;
3036 Py_DECREF(value);
3037 break;
3038 case UnaryOp_kind:
3039 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
3040 if (!result) goto failed;
3041 value = ast2obj_unaryop(o->v.UnaryOp.op);
3042 if (!value) goto failed;
3043 if (_PyObject_SetAttrId(result, &PyId_op, value) == -1)
3044 goto failed;
3045 Py_DECREF(value);
3046 value = ast2obj_expr(o->v.UnaryOp.operand);
3047 if (!value) goto failed;
3048 if (_PyObject_SetAttrId(result, &PyId_operand, value) == -1)
3049 goto failed;
3050 Py_DECREF(value);
3051 break;
3052 case Lambda_kind:
3053 result = PyType_GenericNew(Lambda_type, NULL, NULL);
3054 if (!result) goto failed;
3055 value = ast2obj_arguments(o->v.Lambda.args);
3056 if (!value) goto failed;
3057 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3058 goto failed;
3059 Py_DECREF(value);
3060 value = ast2obj_expr(o->v.Lambda.body);
3061 if (!value) goto failed;
3062 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3063 goto failed;
3064 Py_DECREF(value);
3065 break;
3066 case IfExp_kind:
3067 result = PyType_GenericNew(IfExp_type, NULL, NULL);
3068 if (!result) goto failed;
3069 value = ast2obj_expr(o->v.IfExp.test);
3070 if (!value) goto failed;
3071 if (_PyObject_SetAttrId(result, &PyId_test, value) == -1)
3072 goto failed;
3073 Py_DECREF(value);
3074 value = ast2obj_expr(o->v.IfExp.body);
3075 if (!value) goto failed;
3076 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3077 goto failed;
3078 Py_DECREF(value);
3079 value = ast2obj_expr(o->v.IfExp.orelse);
3080 if (!value) goto failed;
3081 if (_PyObject_SetAttrId(result, &PyId_orelse, value) == -1)
3082 goto failed;
3083 Py_DECREF(value);
3084 break;
3085 case Dict_kind:
3086 result = PyType_GenericNew(Dict_type, NULL, NULL);
3087 if (!result) goto failed;
3088 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
3089 if (!value) goto failed;
3090 if (_PyObject_SetAttrId(result, &PyId_keys, value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
3094 if (!value) goto failed;
3095 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3096 goto failed;
3097 Py_DECREF(value);
3098 break;
3099 case Set_kind:
3100 result = PyType_GenericNew(Set_type, NULL, NULL);
3101 if (!result) goto failed;
3102 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
3103 if (!value) goto failed;
3104 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3105 goto failed;
3106 Py_DECREF(value);
3107 break;
3108 case ListComp_kind:
3109 result = PyType_GenericNew(ListComp_type, NULL, NULL);
3110 if (!result) goto failed;
3111 value = ast2obj_expr(o->v.ListComp.elt);
3112 if (!value) goto failed;
3113 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3114 goto failed;
3115 Py_DECREF(value);
3116 value = ast2obj_list(o->v.ListComp.generators, ast2obj_comprehension);
3117 if (!value) goto failed;
3118 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3119 goto failed;
3120 Py_DECREF(value);
3121 break;
3122 case SetComp_kind:
3123 result = PyType_GenericNew(SetComp_type, NULL, NULL);
3124 if (!result) goto failed;
3125 value = ast2obj_expr(o->v.SetComp.elt);
3126 if (!value) goto failed;
3127 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
3130 value = ast2obj_list(o->v.SetComp.generators, ast2obj_comprehension);
3131 if (!value) goto failed;
3132 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3133 goto failed;
3134 Py_DECREF(value);
3135 break;
3136 case DictComp_kind:
3137 result = PyType_GenericNew(DictComp_type, NULL, NULL);
3138 if (!result) goto failed;
3139 value = ast2obj_expr(o->v.DictComp.key);
3140 if (!value) goto failed;
3141 if (_PyObject_SetAttrId(result, &PyId_key, value) == -1)
3142 goto failed;
3143 Py_DECREF(value);
3144 value = ast2obj_expr(o->v.DictComp.value);
3145 if (!value) goto failed;
3146 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3147 goto failed;
3148 Py_DECREF(value);
3149 value = ast2obj_list(o->v.DictComp.generators, ast2obj_comprehension);
3150 if (!value) goto failed;
3151 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3152 goto failed;
3153 Py_DECREF(value);
3154 break;
3155 case GeneratorExp_kind:
3156 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
3157 if (!result) goto failed;
3158 value = ast2obj_expr(o->v.GeneratorExp.elt);
3159 if (!value) goto failed;
3160 if (_PyObject_SetAttrId(result, &PyId_elt, value) == -1)
3161 goto failed;
3162 Py_DECREF(value);
3163 value = ast2obj_list(o->v.GeneratorExp.generators,
3164 ast2obj_comprehension);
3165 if (!value) goto failed;
3166 if (_PyObject_SetAttrId(result, &PyId_generators, value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
3169 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003170 case Await_kind:
3171 result = PyType_GenericNew(Await_type, NULL, NULL);
3172 if (!result) goto failed;
3173 value = ast2obj_expr(o->v.Await.value);
3174 if (!value) goto failed;
3175 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3176 goto failed;
3177 Py_DECREF(value);
3178 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003179 case Yield_kind:
3180 result = PyType_GenericNew(Yield_type, NULL, NULL);
3181 if (!result) goto failed;
3182 value = ast2obj_expr(o->v.Yield.value);
3183 if (!value) goto failed;
3184 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3185 goto failed;
3186 Py_DECREF(value);
3187 break;
3188 case YieldFrom_kind:
3189 result = PyType_GenericNew(YieldFrom_type, NULL, NULL);
3190 if (!result) goto failed;
3191 value = ast2obj_expr(o->v.YieldFrom.value);
3192 if (!value) goto failed;
3193 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3194 goto failed;
3195 Py_DECREF(value);
3196 break;
3197 case Compare_kind:
3198 result = PyType_GenericNew(Compare_type, NULL, NULL);
3199 if (!result) goto failed;
3200 value = ast2obj_expr(o->v.Compare.left);
3201 if (!value) goto failed;
3202 if (_PyObject_SetAttrId(result, &PyId_left, value) == -1)
3203 goto failed;
3204 Py_DECREF(value);
3205 {
3206 Py_ssize_t i, n = asdl_seq_LEN(o->v.Compare.ops);
3207 value = PyList_New(n);
3208 if (!value) goto failed;
3209 for(i = 0; i < n; i++)
3210 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00003212 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003213 if (_PyObject_SetAttrId(result, &PyId_ops, value) == -1)
3214 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003215 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003216 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003217 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003218 if (_PyObject_SetAttrId(result, &PyId_comparators, value) == -1)
3219 goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00003220 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003221 break;
3222 case Call_kind:
3223 result = PyType_GenericNew(Call_type, NULL, NULL);
3224 if (!result) goto failed;
3225 value = ast2obj_expr(o->v.Call.func);
3226 if (!value) goto failed;
3227 if (_PyObject_SetAttrId(result, &PyId_func, value) == -1)
3228 goto failed;
3229 Py_DECREF(value);
3230 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
3231 if (!value) goto failed;
3232 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
3235 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
3236 if (!value) goto failed;
3237 if (_PyObject_SetAttrId(result, &PyId_keywords, value) == -1)
3238 goto failed;
3239 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003240 break;
3241 case Num_kind:
3242 result = PyType_GenericNew(Num_type, NULL, NULL);
3243 if (!result) goto failed;
3244 value = ast2obj_object(o->v.Num.n);
3245 if (!value) goto failed;
3246 if (_PyObject_SetAttrId(result, &PyId_n, value) == -1)
3247 goto failed;
3248 Py_DECREF(value);
3249 break;
3250 case Str_kind:
3251 result = PyType_GenericNew(Str_type, NULL, NULL);
3252 if (!result) goto failed;
3253 value = ast2obj_string(o->v.Str.s);
3254 if (!value) goto failed;
3255 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3256 goto failed;
3257 Py_DECREF(value);
3258 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003259 case FormattedValue_kind:
3260 result = PyType_GenericNew(FormattedValue_type, NULL, NULL);
3261 if (!result) goto failed;
3262 value = ast2obj_expr(o->v.FormattedValue.value);
3263 if (!value) goto failed;
3264 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3265 goto failed;
3266 Py_DECREF(value);
3267 value = ast2obj_int(o->v.FormattedValue.conversion);
3268 if (!value) goto failed;
3269 if (_PyObject_SetAttrId(result, &PyId_conversion, value) == -1)
3270 goto failed;
3271 Py_DECREF(value);
3272 value = ast2obj_expr(o->v.FormattedValue.format_spec);
3273 if (!value) goto failed;
3274 if (_PyObject_SetAttrId(result, &PyId_format_spec, value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
3277 break;
3278 case JoinedStr_kind:
3279 result = PyType_GenericNew(JoinedStr_type, NULL, NULL);
3280 if (!result) goto failed;
3281 value = ast2obj_list(o->v.JoinedStr.values, ast2obj_expr);
3282 if (!value) goto failed;
3283 if (_PyObject_SetAttrId(result, &PyId_values, value) == -1)
3284 goto failed;
3285 Py_DECREF(value);
3286 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003287 case Bytes_kind:
3288 result = PyType_GenericNew(Bytes_type, NULL, NULL);
3289 if (!result) goto failed;
3290 value = ast2obj_bytes(o->v.Bytes.s);
3291 if (!value) goto failed;
3292 if (_PyObject_SetAttrId(result, &PyId_s, value) == -1)
3293 goto failed;
3294 Py_DECREF(value);
3295 break;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003296 case NameConstant_kind:
3297 result = PyType_GenericNew(NameConstant_type, NULL, NULL);
3298 if (!result) goto failed;
3299 value = ast2obj_singleton(o->v.NameConstant.value);
3300 if (!value) goto failed;
3301 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3302 goto failed;
3303 Py_DECREF(value);
3304 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003305 case Ellipsis_kind:
3306 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
3307 if (!result) goto failed;
3308 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003309 case Constant_kind:
3310 result = PyType_GenericNew(Constant_type, NULL, NULL);
3311 if (!result) goto failed;
3312 value = ast2obj_constant(o->v.Constant.value);
3313 if (!value) goto failed;
3314 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3315 goto failed;
3316 Py_DECREF(value);
3317 break;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003318 case Attribute_kind:
3319 result = PyType_GenericNew(Attribute_type, NULL, NULL);
3320 if (!result) goto failed;
3321 value = ast2obj_expr(o->v.Attribute.value);
3322 if (!value) goto failed;
3323 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3324 goto failed;
3325 Py_DECREF(value);
3326 value = ast2obj_identifier(o->v.Attribute.attr);
3327 if (!value) goto failed;
3328 if (_PyObject_SetAttrId(result, &PyId_attr, value) == -1)
3329 goto failed;
3330 Py_DECREF(value);
3331 value = ast2obj_expr_context(o->v.Attribute.ctx);
3332 if (!value) goto failed;
3333 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3334 goto failed;
3335 Py_DECREF(value);
3336 break;
3337 case Subscript_kind:
3338 result = PyType_GenericNew(Subscript_type, NULL, NULL);
3339 if (!result) goto failed;
3340 value = ast2obj_expr(o->v.Subscript.value);
3341 if (!value) goto failed;
3342 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3343 goto failed;
3344 Py_DECREF(value);
3345 value = ast2obj_slice(o->v.Subscript.slice);
3346 if (!value) goto failed;
3347 if (_PyObject_SetAttrId(result, &PyId_slice, value) == -1)
3348 goto failed;
3349 Py_DECREF(value);
3350 value = ast2obj_expr_context(o->v.Subscript.ctx);
3351 if (!value) goto failed;
3352 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3353 goto failed;
3354 Py_DECREF(value);
3355 break;
3356 case Starred_kind:
3357 result = PyType_GenericNew(Starred_type, NULL, NULL);
3358 if (!result) goto failed;
3359 value = ast2obj_expr(o->v.Starred.value);
3360 if (!value) goto failed;
3361 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3362 goto failed;
3363 Py_DECREF(value);
3364 value = ast2obj_expr_context(o->v.Starred.ctx);
3365 if (!value) goto failed;
3366 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3367 goto failed;
3368 Py_DECREF(value);
3369 break;
3370 case Name_kind:
3371 result = PyType_GenericNew(Name_type, NULL, NULL);
3372 if (!result) goto failed;
3373 value = ast2obj_identifier(o->v.Name.id);
3374 if (!value) goto failed;
3375 if (_PyObject_SetAttrId(result, &PyId_id, value) == -1)
3376 goto failed;
3377 Py_DECREF(value);
3378 value = ast2obj_expr_context(o->v.Name.ctx);
3379 if (!value) goto failed;
3380 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3381 goto failed;
3382 Py_DECREF(value);
3383 break;
3384 case List_kind:
3385 result = PyType_GenericNew(List_type, NULL, NULL);
3386 if (!result) goto failed;
3387 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
3388 if (!value) goto failed;
3389 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3390 goto failed;
3391 Py_DECREF(value);
3392 value = ast2obj_expr_context(o->v.List.ctx);
3393 if (!value) goto failed;
3394 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3395 goto failed;
3396 Py_DECREF(value);
3397 break;
3398 case Tuple_kind:
3399 result = PyType_GenericNew(Tuple_type, NULL, NULL);
3400 if (!result) goto failed;
3401 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
3402 if (!value) goto failed;
3403 if (_PyObject_SetAttrId(result, &PyId_elts, value) == -1)
3404 goto failed;
3405 Py_DECREF(value);
3406 value = ast2obj_expr_context(o->v.Tuple.ctx);
3407 if (!value) goto failed;
3408 if (_PyObject_SetAttrId(result, &PyId_ctx, value) == -1)
3409 goto failed;
3410 Py_DECREF(value);
3411 break;
3412 }
3413 value = ast2obj_int(o->lineno);
3414 if (!value) goto failed;
3415 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3416 goto failed;
3417 Py_DECREF(value);
3418 value = ast2obj_int(o->col_offset);
3419 if (!value) goto failed;
3420 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3421 goto failed;
3422 Py_DECREF(value);
3423 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003424failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003425 Py_XDECREF(value);
3426 Py_XDECREF(result);
3427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428}
3429
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003430PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003431{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003432 switch(o) {
3433 case Load:
3434 Py_INCREF(Load_singleton);
3435 return Load_singleton;
3436 case Store:
3437 Py_INCREF(Store_singleton);
3438 return Store_singleton;
3439 case Del:
3440 Py_INCREF(Del_singleton);
3441 return Del_singleton;
3442 case AugLoad:
3443 Py_INCREF(AugLoad_singleton);
3444 return AugLoad_singleton;
3445 case AugStore:
3446 Py_INCREF(AugStore_singleton);
3447 return AugStore_singleton;
3448 case Param:
3449 Py_INCREF(Param_singleton);
3450 return Param_singleton;
3451 default:
3452 /* should never happen, but just in case ... */
3453 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
3454 return NULL;
3455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003457PyObject*
3458ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003460 slice_ty o = (slice_ty)_o;
3461 PyObject *result = NULL, *value = NULL;
3462 if (!o) {
3463 Py_INCREF(Py_None);
3464 return Py_None;
3465 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003466
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003467 switch (o->kind) {
3468 case Slice_kind:
3469 result = PyType_GenericNew(Slice_type, NULL, NULL);
3470 if (!result) goto failed;
3471 value = ast2obj_expr(o->v.Slice.lower);
3472 if (!value) goto failed;
3473 if (_PyObject_SetAttrId(result, &PyId_lower, value) == -1)
3474 goto failed;
3475 Py_DECREF(value);
3476 value = ast2obj_expr(o->v.Slice.upper);
3477 if (!value) goto failed;
3478 if (_PyObject_SetAttrId(result, &PyId_upper, value) == -1)
3479 goto failed;
3480 Py_DECREF(value);
3481 value = ast2obj_expr(o->v.Slice.step);
3482 if (!value) goto failed;
3483 if (_PyObject_SetAttrId(result, &PyId_step, value) == -1)
3484 goto failed;
3485 Py_DECREF(value);
3486 break;
3487 case ExtSlice_kind:
3488 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
3489 if (!result) goto failed;
3490 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
3491 if (!value) goto failed;
3492 if (_PyObject_SetAttrId(result, &PyId_dims, value) == -1)
3493 goto failed;
3494 Py_DECREF(value);
3495 break;
3496 case Index_kind:
3497 result = PyType_GenericNew(Index_type, NULL, NULL);
3498 if (!result) goto failed;
3499 value = ast2obj_expr(o->v.Index.value);
3500 if (!value) goto failed;
3501 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3502 goto failed;
3503 Py_DECREF(value);
3504 break;
3505 }
3506 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003507failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003508 Py_XDECREF(value);
3509 Py_XDECREF(result);
3510 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511}
3512
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003513PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003515 switch(o) {
3516 case And:
3517 Py_INCREF(And_singleton);
3518 return And_singleton;
3519 case Or:
3520 Py_INCREF(Or_singleton);
3521 return Or_singleton;
3522 default:
3523 /* should never happen, but just in case ... */
3524 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3525 return NULL;
3526 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003527}
3528PyObject* ast2obj_operator(operator_ty o)
3529{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003530 switch(o) {
3531 case Add:
3532 Py_INCREF(Add_singleton);
3533 return Add_singleton;
3534 case Sub:
3535 Py_INCREF(Sub_singleton);
3536 return Sub_singleton;
3537 case Mult:
3538 Py_INCREF(Mult_singleton);
3539 return Mult_singleton;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003540 case MatMult:
3541 Py_INCREF(MatMult_singleton);
3542 return MatMult_singleton;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003543 case Div:
3544 Py_INCREF(Div_singleton);
3545 return Div_singleton;
3546 case Mod:
3547 Py_INCREF(Mod_singleton);
3548 return Mod_singleton;
3549 case Pow:
3550 Py_INCREF(Pow_singleton);
3551 return Pow_singleton;
3552 case LShift:
3553 Py_INCREF(LShift_singleton);
3554 return LShift_singleton;
3555 case RShift:
3556 Py_INCREF(RShift_singleton);
3557 return RShift_singleton;
3558 case BitOr:
3559 Py_INCREF(BitOr_singleton);
3560 return BitOr_singleton;
3561 case BitXor:
3562 Py_INCREF(BitXor_singleton);
3563 return BitXor_singleton;
3564 case BitAnd:
3565 Py_INCREF(BitAnd_singleton);
3566 return BitAnd_singleton;
3567 case FloorDiv:
3568 Py_INCREF(FloorDiv_singleton);
3569 return FloorDiv_singleton;
3570 default:
3571 /* should never happen, but just in case ... */
3572 PyErr_Format(PyExc_SystemError, "unknown operator found");
3573 return NULL;
3574 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003575}
3576PyObject* ast2obj_unaryop(unaryop_ty o)
3577{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003578 switch(o) {
3579 case Invert:
3580 Py_INCREF(Invert_singleton);
3581 return Invert_singleton;
3582 case Not:
3583 Py_INCREF(Not_singleton);
3584 return Not_singleton;
3585 case UAdd:
3586 Py_INCREF(UAdd_singleton);
3587 return UAdd_singleton;
3588 case USub:
3589 Py_INCREF(USub_singleton);
3590 return USub_singleton;
3591 default:
3592 /* should never happen, but just in case ... */
3593 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3594 return NULL;
3595 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003596}
3597PyObject* ast2obj_cmpop(cmpop_ty o)
3598{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003599 switch(o) {
3600 case Eq:
3601 Py_INCREF(Eq_singleton);
3602 return Eq_singleton;
3603 case NotEq:
3604 Py_INCREF(NotEq_singleton);
3605 return NotEq_singleton;
3606 case Lt:
3607 Py_INCREF(Lt_singleton);
3608 return Lt_singleton;
3609 case LtE:
3610 Py_INCREF(LtE_singleton);
3611 return LtE_singleton;
3612 case Gt:
3613 Py_INCREF(Gt_singleton);
3614 return Gt_singleton;
3615 case GtE:
3616 Py_INCREF(GtE_singleton);
3617 return GtE_singleton;
3618 case Is:
3619 Py_INCREF(Is_singleton);
3620 return Is_singleton;
3621 case IsNot:
3622 Py_INCREF(IsNot_singleton);
3623 return IsNot_singleton;
3624 case In:
3625 Py_INCREF(In_singleton);
3626 return In_singleton;
3627 case NotIn:
3628 Py_INCREF(NotIn_singleton);
3629 return NotIn_singleton;
3630 default:
3631 /* should never happen, but just in case ... */
3632 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3633 return NULL;
3634 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003635}
3636PyObject*
3637ast2obj_comprehension(void* _o)
3638{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003639 comprehension_ty o = (comprehension_ty)_o;
3640 PyObject *result = NULL, *value = NULL;
3641 if (!o) {
3642 Py_INCREF(Py_None);
3643 return Py_None;
3644 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003645
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003646 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3647 if (!result) return NULL;
3648 value = ast2obj_expr(o->target);
3649 if (!value) goto failed;
3650 if (_PyObject_SetAttrId(result, &PyId_target, value) == -1)
3651 goto failed;
3652 Py_DECREF(value);
3653 value = ast2obj_expr(o->iter);
3654 if (!value) goto failed;
3655 if (_PyObject_SetAttrId(result, &PyId_iter, value) == -1)
3656 goto failed;
3657 Py_DECREF(value);
3658 value = ast2obj_list(o->ifs, ast2obj_expr);
3659 if (!value) goto failed;
3660 if (_PyObject_SetAttrId(result, &PyId_ifs, value) == -1)
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_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003673 excepthandler_ty o = (excepthandler_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 switch (o->kind) {
3681 case ExceptHandler_kind:
3682 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3683 if (!result) goto failed;
3684 value = ast2obj_expr(o->v.ExceptHandler.type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003685 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003686 if (_PyObject_SetAttrId(result, &PyId_type, value) == -1)
3687 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003688 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003689 value = ast2obj_identifier(o->v.ExceptHandler.name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003690 if (!value) goto failed;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003691 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3692 goto failed;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003693 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003694 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3695 if (!value) goto failed;
3696 if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
3697 goto failed;
3698 Py_DECREF(value);
3699 break;
3700 }
3701 value = ast2obj_int(o->lineno);
3702 if (!value) goto failed;
3703 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3704 goto failed;
3705 Py_DECREF(value);
3706 value = ast2obj_int(o->col_offset);
3707 if (!value) goto failed;
3708 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3709 goto failed;
3710 Py_DECREF(value);
3711 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003712failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003713 Py_XDECREF(value);
3714 Py_XDECREF(result);
3715 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003716}
3717
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003718PyObject*
3719ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003721 arguments_ty o = (arguments_ty)_o;
3722 PyObject *result = NULL, *value = NULL;
3723 if (!o) {
3724 Py_INCREF(Py_None);
3725 return Py_None;
3726 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003727
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003728 result = PyType_GenericNew(arguments_type, NULL, NULL);
3729 if (!result) return NULL;
3730 value = ast2obj_list(o->args, ast2obj_arg);
3731 if (!value) goto failed;
3732 if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)
3733 goto failed;
3734 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003735 value = ast2obj_arg(o->vararg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003736 if (!value) goto failed;
3737 if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)
3738 goto failed;
3739 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003740 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
3741 if (!value) goto failed;
3742 if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)
3743 goto failed;
3744 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003745 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003746 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003747 if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003748 goto failed;
3749 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003750 value = ast2obj_arg(o->kwarg);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003751 if (!value) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02003752 if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003753 goto failed;
3754 Py_DECREF(value);
3755 value = ast2obj_list(o->defaults, ast2obj_expr);
3756 if (!value) goto failed;
3757 if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)
3758 goto failed;
3759 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003760 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003761failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003762 Py_XDECREF(value);
3763 Py_XDECREF(result);
3764 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765}
3766
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003767PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003768ast2obj_arg(void* _o)
3769{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003770 arg_ty o = (arg_ty)_o;
3771 PyObject *result = NULL, *value = NULL;
3772 if (!o) {
3773 Py_INCREF(Py_None);
3774 return Py_None;
3775 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003776
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003777 result = PyType_GenericNew(arg_type, NULL, NULL);
3778 if (!result) return NULL;
3779 value = ast2obj_identifier(o->arg);
3780 if (!value) goto failed;
3781 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3782 goto failed;
3783 Py_DECREF(value);
3784 value = ast2obj_expr(o->annotation);
3785 if (!value) goto failed;
3786 if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)
3787 goto failed;
3788 Py_DECREF(value);
Victor Stinneree4b59c2013-07-27 00:01:35 +02003789 value = ast2obj_int(o->lineno);
3790 if (!value) goto failed;
3791 if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0)
3792 goto failed;
3793 Py_DECREF(value);
3794 value = ast2obj_int(o->col_offset);
3795 if (!value) goto failed;
3796 if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0)
3797 goto failed;
3798 Py_DECREF(value);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003799 return result;
Neal Norwitzc1505362006-12-28 06:47:50 +00003800failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003801 Py_XDECREF(value);
3802 Py_XDECREF(result);
3803 return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003804}
3805
3806PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003807ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003809 keyword_ty o = (keyword_ty)_o;
3810 PyObject *result = NULL, *value = NULL;
3811 if (!o) {
3812 Py_INCREF(Py_None);
3813 return Py_None;
3814 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003815
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003816 result = PyType_GenericNew(keyword_type, NULL, NULL);
3817 if (!result) return NULL;
3818 value = ast2obj_identifier(o->arg);
3819 if (!value) goto failed;
3820 if (_PyObject_SetAttrId(result, &PyId_arg, value) == -1)
3821 goto failed;
3822 Py_DECREF(value);
3823 value = ast2obj_expr(o->value);
3824 if (!value) goto failed;
3825 if (_PyObject_SetAttrId(result, &PyId_value, value) == -1)
3826 goto failed;
3827 Py_DECREF(value);
3828 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003829failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003830 Py_XDECREF(value);
3831 Py_XDECREF(result);
3832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833}
3834
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003835PyObject*
3836ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003838 alias_ty o = (alias_ty)_o;
3839 PyObject *result = NULL, *value = NULL;
3840 if (!o) {
3841 Py_INCREF(Py_None);
3842 return Py_None;
3843 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003844
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003845 result = PyType_GenericNew(alias_type, NULL, NULL);
3846 if (!result) return NULL;
3847 value = ast2obj_identifier(o->name);
3848 if (!value) goto failed;
3849 if (_PyObject_SetAttrId(result, &PyId_name, value) == -1)
3850 goto failed;
3851 Py_DECREF(value);
3852 value = ast2obj_identifier(o->asname);
3853 if (!value) goto failed;
3854 if (_PyObject_SetAttrId(result, &PyId_asname, value) == -1)
3855 goto failed;
3856 Py_DECREF(value);
3857 return result;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003858failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003859 Py_XDECREF(value);
3860 Py_XDECREF(result);
3861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862}
3863
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003864PyObject*
3865ast2obj_withitem(void* _o)
3866{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003867 withitem_ty o = (withitem_ty)_o;
3868 PyObject *result = NULL, *value = NULL;
3869 if (!o) {
3870 Py_INCREF(Py_None);
3871 return Py_None;
3872 }
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003873
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003874 result = PyType_GenericNew(withitem_type, NULL, NULL);
3875 if (!result) return NULL;
3876 value = ast2obj_expr(o->context_expr);
3877 if (!value) goto failed;
3878 if (_PyObject_SetAttrId(result, &PyId_context_expr, value) == -1)
3879 goto failed;
3880 Py_DECREF(value);
3881 value = ast2obj_expr(o->optional_vars);
3882 if (!value) goto failed;
3883 if (_PyObject_SetAttrId(result, &PyId_optional_vars, value) == -1)
3884 goto failed;
3885 Py_DECREF(value);
3886 return result;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003887failed:
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003888 Py_XDECREF(value);
3889 Py_XDECREF(result);
3890 return NULL;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003891}
3892
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003894int
3895obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3896{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003897 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003898
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003899 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003900
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003901 if (obj == Py_None) {
3902 *out = NULL;
3903 return 0;
3904 }
3905 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3906 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003907 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003908 }
3909 if (isinstance) {
3910 asdl_seq* body;
3911
3912 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3913 int res;
3914 Py_ssize_t len;
3915 Py_ssize_t i;
3916 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3917 if (tmp == NULL) goto failed;
3918 if (!PyList_Check(tmp)) {
3919 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3920 goto failed;
3921 }
3922 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003923 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003924 if (body == NULL) goto failed;
3925 for (i = 0; i < len; i++) {
3926 stmt_ty value;
3927 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3928 if (res != 0) goto failed;
3929 asdl_seq_SET(body, i, value);
3930 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003931 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003932 } else {
3933 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3934 return 1;
3935 }
3936 *out = Module(body, arena);
3937 if (*out == NULL) goto failed;
3938 return 0;
3939 }
3940 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3941 if (isinstance == -1) {
3942 return 1;
3943 }
3944 if (isinstance) {
3945 asdl_seq* body;
3946
3947 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3948 int res;
3949 Py_ssize_t len;
3950 Py_ssize_t i;
3951 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3952 if (tmp == NULL) goto failed;
3953 if (!PyList_Check(tmp)) {
3954 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3955 goto failed;
3956 }
3957 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02003958 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003959 if (body == NULL) goto failed;
3960 for (i = 0; i < len; i++) {
3961 stmt_ty value;
3962 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3963 if (res != 0) goto failed;
3964 asdl_seq_SET(body, i, value);
3965 }
Victor Stinner1acc1292013-07-27 00:03:47 +02003966 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003967 } else {
3968 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3969 return 1;
3970 }
3971 *out = Interactive(body, arena);
3972 if (*out == NULL) goto failed;
3973 return 0;
3974 }
3975 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3976 if (isinstance == -1) {
3977 return 1;
3978 }
3979 if (isinstance) {
3980 expr_ty body;
3981
3982 if (_PyObject_HasAttrId(obj, &PyId_body)) {
3983 int res;
3984 tmp = _PyObject_GetAttrId(obj, &PyId_body);
3985 if (tmp == NULL) goto failed;
3986 res = obj2ast_expr(tmp, &body, arena);
3987 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02003988 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02003989 } else {
3990 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3991 return 1;
3992 }
3993 *out = Expression(body, arena);
3994 if (*out == NULL) goto failed;
3995 return 0;
3996 }
3997 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3998 if (isinstance == -1) {
3999 return 1;
4000 }
4001 if (isinstance) {
4002 asdl_seq* body;
4003
4004 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4005 int res;
4006 Py_ssize_t len;
4007 Py_ssize_t i;
4008 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4009 if (tmp == NULL) goto failed;
4010 if (!PyList_Check(tmp)) {
4011 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4012 goto failed;
4013 }
4014 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004015 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004016 if (body == NULL) goto failed;
4017 for (i = 0; i < len; i++) {
4018 stmt_ty value;
4019 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4020 if (res != 0) goto failed;
4021 asdl_seq_SET(body, i, value);
4022 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004023 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004024 } else {
4025 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
4026 return 1;
4027 }
4028 *out = Suite(body, arena);
4029 if (*out == NULL) goto failed;
4030 return 0;
4031 }
4032
4033 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
4034 failed:
4035 Py_XDECREF(tmp);
4036 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004037}
4038
4039int
4040obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
4041{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004042 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004043
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004044 PyObject *tmp = NULL;
4045 int lineno;
4046 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004047
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004048 if (obj == Py_None) {
4049 *out = NULL;
4050 return 0;
4051 }
4052 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
4053 int res;
4054 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
4055 if (tmp == NULL) goto failed;
4056 res = obj2ast_int(tmp, &lineno, arena);
4057 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004058 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004059 } else {
4060 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00004061 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004062 }
4063 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
4064 int res;
4065 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
4066 if (tmp == NULL) goto failed;
4067 res = obj2ast_int(tmp, &col_offset, arena);
4068 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004069 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004070 } else {
4071 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
4072 return 1;
4073 }
4074 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
4075 if (isinstance == -1) {
4076 return 1;
4077 }
4078 if (isinstance) {
4079 identifier name;
4080 arguments_ty args;
4081 asdl_seq* body;
4082 asdl_seq* decorator_list;
4083 expr_ty returns;
4084
4085 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4086 int res;
4087 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4088 if (tmp == NULL) goto failed;
4089 res = obj2ast_identifier(tmp, &name, arena);
4090 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004091 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004092 } else {
4093 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
4094 return 1;
4095 }
4096 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4097 int res;
4098 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4099 if (tmp == NULL) goto failed;
4100 res = obj2ast_arguments(tmp, &args, arena);
4101 if (res != 0) goto failed;
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 \"args\" missing from FunctionDef");
4105 return 1;
4106 }
4107 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4108 int res;
4109 Py_ssize_t len;
4110 Py_ssize_t i;
4111 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4112 if (tmp == NULL) goto failed;
4113 if (!PyList_Check(tmp)) {
4114 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4115 goto failed;
4116 }
4117 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004118 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004119 if (body == NULL) goto failed;
4120 for (i = 0; i < len; i++) {
4121 stmt_ty value;
4122 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4123 if (res != 0) goto failed;
4124 asdl_seq_SET(body, i, value);
4125 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004126 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004127 } else {
4128 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
4129 return 1;
4130 }
4131 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4132 int res;
4133 Py_ssize_t len;
4134 Py_ssize_t i;
4135 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4136 if (tmp == NULL) goto failed;
4137 if (!PyList_Check(tmp)) {
4138 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4139 goto failed;
4140 }
4141 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004142 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004143 if (decorator_list == NULL) goto failed;
4144 for (i = 0; i < len; i++) {
4145 expr_ty value;
4146 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4147 if (res != 0) goto failed;
4148 asdl_seq_SET(decorator_list, i, value);
4149 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004150 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004151 } else {
4152 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
4153 return 1;
4154 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004155 if (exists_not_none(obj, &PyId_returns)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004156 int res;
4157 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4158 if (tmp == NULL) goto failed;
4159 res = obj2ast_expr(tmp, &returns, arena);
4160 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004161 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004162 } else {
4163 returns = NULL;
4164 }
4165 *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
4166 col_offset, arena);
4167 if (*out == NULL) goto failed;
4168 return 0;
4169 }
Yury Selivanov75445082015-05-11 22:57:16 -04004170 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFunctionDef_type);
4171 if (isinstance == -1) {
4172 return 1;
4173 }
4174 if (isinstance) {
4175 identifier name;
4176 arguments_ty args;
4177 asdl_seq* body;
4178 asdl_seq* decorator_list;
4179 expr_ty returns;
4180
4181 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4182 int res;
4183 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4184 if (tmp == NULL) goto failed;
4185 res = obj2ast_identifier(tmp, &name, arena);
4186 if (res != 0) goto failed;
4187 Py_CLEAR(tmp);
4188 } else {
4189 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from AsyncFunctionDef");
4190 return 1;
4191 }
4192 if (_PyObject_HasAttrId(obj, &PyId_args)) {
4193 int res;
4194 tmp = _PyObject_GetAttrId(obj, &PyId_args);
4195 if (tmp == NULL) goto failed;
4196 res = obj2ast_arguments(tmp, &args, arena);
4197 if (res != 0) goto failed;
4198 Py_CLEAR(tmp);
4199 } else {
4200 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from AsyncFunctionDef");
4201 return 1;
4202 }
4203 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4204 int res;
4205 Py_ssize_t len;
4206 Py_ssize_t i;
4207 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4208 if (tmp == NULL) goto failed;
4209 if (!PyList_Check(tmp)) {
4210 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4211 goto failed;
4212 }
4213 len = PyList_GET_SIZE(tmp);
4214 body = _Py_asdl_seq_new(len, arena);
4215 if (body == NULL) goto failed;
4216 for (i = 0; i < len; i++) {
4217 stmt_ty value;
4218 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4219 if (res != 0) goto failed;
4220 asdl_seq_SET(body, i, value);
4221 }
4222 Py_CLEAR(tmp);
4223 } else {
4224 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFunctionDef");
4225 return 1;
4226 }
4227 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4228 int res;
4229 Py_ssize_t len;
4230 Py_ssize_t i;
4231 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4232 if (tmp == NULL) goto failed;
4233 if (!PyList_Check(tmp)) {
4234 PyErr_Format(PyExc_TypeError, "AsyncFunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4235 goto failed;
4236 }
4237 len = PyList_GET_SIZE(tmp);
4238 decorator_list = _Py_asdl_seq_new(len, arena);
4239 if (decorator_list == NULL) goto failed;
4240 for (i = 0; i < len; i++) {
4241 expr_ty value;
4242 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4243 if (res != 0) goto failed;
4244 asdl_seq_SET(decorator_list, i, value);
4245 }
4246 Py_CLEAR(tmp);
4247 } else {
4248 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from AsyncFunctionDef");
4249 return 1;
4250 }
4251 if (exists_not_none(obj, &PyId_returns)) {
4252 int res;
4253 tmp = _PyObject_GetAttrId(obj, &PyId_returns);
4254 if (tmp == NULL) goto failed;
4255 res = obj2ast_expr(tmp, &returns, arena);
4256 if (res != 0) goto failed;
4257 Py_CLEAR(tmp);
4258 } else {
4259 returns = NULL;
4260 }
4261 *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
4262 lineno, col_offset, arena);
4263 if (*out == NULL) goto failed;
4264 return 0;
4265 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004266 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
4267 if (isinstance == -1) {
4268 return 1;
4269 }
4270 if (isinstance) {
4271 identifier name;
4272 asdl_seq* bases;
4273 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004274 asdl_seq* body;
4275 asdl_seq* decorator_list;
4276
4277 if (_PyObject_HasAttrId(obj, &PyId_name)) {
4278 int res;
4279 tmp = _PyObject_GetAttrId(obj, &PyId_name);
4280 if (tmp == NULL) goto failed;
4281 res = obj2ast_identifier(tmp, &name, arena);
4282 if (res != 0) goto failed;
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 \"name\" missing from ClassDef");
4286 return 1;
4287 }
4288 if (_PyObject_HasAttrId(obj, &PyId_bases)) {
4289 int res;
4290 Py_ssize_t len;
4291 Py_ssize_t i;
4292 tmp = _PyObject_GetAttrId(obj, &PyId_bases);
4293 if (tmp == NULL) goto failed;
4294 if (!PyList_Check(tmp)) {
4295 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" 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 bases = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004300 if (bases == NULL) goto failed;
4301 for (i = 0; i < len; i++) {
4302 expr_ty value;
4303 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4304 if (res != 0) goto failed;
4305 asdl_seq_SET(bases, 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 \"bases\" missing from ClassDef");
4310 return 1;
4311 }
4312 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
4313 int res;
4314 Py_ssize_t len;
4315 Py_ssize_t i;
4316 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
4317 if (tmp == NULL) goto failed;
4318 if (!PyList_Check(tmp)) {
4319 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" 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 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004324 if (keywords == NULL) goto failed;
4325 for (i = 0; i < len; i++) {
4326 keyword_ty value;
4327 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4328 if (res != 0) goto failed;
4329 asdl_seq_SET(keywords, 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 \"keywords\" missing from ClassDef");
4334 return 1;
4335 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004336 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4337 int res;
4338 Py_ssize_t len;
4339 Py_ssize_t i;
4340 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4341 if (tmp == NULL) goto failed;
4342 if (!PyList_Check(tmp)) {
4343 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4344 goto failed;
4345 }
4346 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004347 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004348 if (body == NULL) goto failed;
4349 for (i = 0; i < len; i++) {
4350 stmt_ty value;
4351 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4352 if (res != 0) goto failed;
4353 asdl_seq_SET(body, i, value);
4354 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004355 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004356 } else {
4357 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
4358 return 1;
4359 }
4360 if (_PyObject_HasAttrId(obj, &PyId_decorator_list)) {
4361 int res;
4362 Py_ssize_t len;
4363 Py_ssize_t i;
4364 tmp = _PyObject_GetAttrId(obj, &PyId_decorator_list);
4365 if (tmp == NULL) goto failed;
4366 if (!PyList_Check(tmp)) {
4367 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4368 goto failed;
4369 }
4370 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004371 decorator_list = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004372 if (decorator_list == NULL) goto failed;
4373 for (i = 0; i < len; i++) {
4374 expr_ty value;
4375 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4376 if (res != 0) goto failed;
4377 asdl_seq_SET(decorator_list, i, value);
4378 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004379 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004380 } else {
4381 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
4382 return 1;
4383 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004384 *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
4385 col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004386 if (*out == NULL) goto failed;
4387 return 0;
4388 }
4389 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
4390 if (isinstance == -1) {
4391 return 1;
4392 }
4393 if (isinstance) {
4394 expr_ty value;
4395
Victor Stinneree4b59c2013-07-27 00:01:35 +02004396 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004397 int res;
4398 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4399 if (tmp == NULL) goto failed;
4400 res = obj2ast_expr(tmp, &value, arena);
4401 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004402 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004403 } else {
4404 value = NULL;
4405 }
4406 *out = Return(value, lineno, col_offset, arena);
4407 if (*out == NULL) goto failed;
4408 return 0;
4409 }
4410 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
4411 if (isinstance == -1) {
4412 return 1;
4413 }
4414 if (isinstance) {
4415 asdl_seq* targets;
4416
4417 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4418 int res;
4419 Py_ssize_t len;
4420 Py_ssize_t i;
4421 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4422 if (tmp == NULL) goto failed;
4423 if (!PyList_Check(tmp)) {
4424 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4425 goto failed;
4426 }
4427 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004428 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004429 if (targets == NULL) goto failed;
4430 for (i = 0; i < len; i++) {
4431 expr_ty value;
4432 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4433 if (res != 0) goto failed;
4434 asdl_seq_SET(targets, i, value);
4435 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004436 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004437 } else {
4438 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
4439 return 1;
4440 }
4441 *out = Delete(targets, lineno, col_offset, arena);
4442 if (*out == NULL) goto failed;
4443 return 0;
4444 }
4445 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
4446 if (isinstance == -1) {
4447 return 1;
4448 }
4449 if (isinstance) {
4450 asdl_seq* targets;
4451 expr_ty value;
4452
4453 if (_PyObject_HasAttrId(obj, &PyId_targets)) {
4454 int res;
4455 Py_ssize_t len;
4456 Py_ssize_t i;
4457 tmp = _PyObject_GetAttrId(obj, &PyId_targets);
4458 if (tmp == NULL) goto failed;
4459 if (!PyList_Check(tmp)) {
4460 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4461 goto failed;
4462 }
4463 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004464 targets = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004465 if (targets == NULL) goto failed;
4466 for (i = 0; i < len; i++) {
4467 expr_ty value;
4468 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4469 if (res != 0) goto failed;
4470 asdl_seq_SET(targets, i, value);
4471 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004472 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004473 } else {
4474 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
4475 return 1;
4476 }
4477 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4478 int res;
4479 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4480 if (tmp == NULL) goto failed;
4481 res = obj2ast_expr(tmp, &value, arena);
4482 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004483 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004484 } else {
4485 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
4486 return 1;
4487 }
4488 *out = Assign(targets, value, lineno, col_offset, arena);
4489 if (*out == NULL) goto failed;
4490 return 0;
4491 }
4492 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
4493 if (isinstance == -1) {
4494 return 1;
4495 }
4496 if (isinstance) {
4497 expr_ty target;
4498 operator_ty op;
4499 expr_ty value;
4500
4501 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4502 int res;
4503 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4504 if (tmp == NULL) goto failed;
4505 res = obj2ast_expr(tmp, &target, arena);
4506 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004507 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004508 } else {
4509 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
4510 return 1;
4511 }
4512 if (_PyObject_HasAttrId(obj, &PyId_op)) {
4513 int res;
4514 tmp = _PyObject_GetAttrId(obj, &PyId_op);
4515 if (tmp == NULL) goto failed;
4516 res = obj2ast_operator(tmp, &op, arena);
4517 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004518 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004519 } else {
4520 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
4521 return 1;
4522 }
4523 if (_PyObject_HasAttrId(obj, &PyId_value)) {
4524 int res;
4525 tmp = _PyObject_GetAttrId(obj, &PyId_value);
4526 if (tmp == NULL) goto failed;
4527 res = obj2ast_expr(tmp, &value, arena);
4528 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004529 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004530 } else {
4531 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
4532 return 1;
4533 }
4534 *out = AugAssign(target, op, value, lineno, col_offset, arena);
4535 if (*out == NULL) goto failed;
4536 return 0;
4537 }
4538 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
4539 if (isinstance == -1) {
4540 return 1;
4541 }
4542 if (isinstance) {
4543 expr_ty target;
4544 expr_ty iter;
4545 asdl_seq* body;
4546 asdl_seq* orelse;
4547
4548 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4549 int res;
4550 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4551 if (tmp == NULL) goto failed;
4552 res = obj2ast_expr(tmp, &target, arena);
4553 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004554 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004555 } else {
4556 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
4557 return 1;
4558 }
4559 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4560 int res;
4561 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4562 if (tmp == NULL) goto failed;
4563 res = obj2ast_expr(tmp, &iter, arena);
4564 if (res != 0) goto failed;
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 \"iter\" missing from For");
4568 return 1;
4569 }
4570 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4571 int res;
4572 Py_ssize_t len;
4573 Py_ssize_t i;
4574 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4575 if (tmp == NULL) goto failed;
4576 if (!PyList_Check(tmp)) {
4577 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4578 goto failed;
4579 }
4580 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004581 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004582 if (body == NULL) goto failed;
4583 for (i = 0; i < len; i++) {
4584 stmt_ty value;
4585 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4586 if (res != 0) goto failed;
4587 asdl_seq_SET(body, i, value);
4588 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004590 } else {
4591 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
4592 return 1;
4593 }
4594 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4595 int res;
4596 Py_ssize_t len;
4597 Py_ssize_t i;
4598 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4599 if (tmp == NULL) goto failed;
4600 if (!PyList_Check(tmp)) {
4601 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4602 goto failed;
4603 }
4604 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004605 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004606 if (orelse == NULL) goto failed;
4607 for (i = 0; i < len; i++) {
4608 stmt_ty value;
4609 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4610 if (res != 0) goto failed;
4611 asdl_seq_SET(orelse, i, value);
4612 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004613 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004614 } else {
4615 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
4616 return 1;
4617 }
4618 *out = For(target, iter, body, orelse, lineno, col_offset, arena);
4619 if (*out == NULL) goto failed;
4620 return 0;
4621 }
Yury Selivanov75445082015-05-11 22:57:16 -04004622 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncFor_type);
4623 if (isinstance == -1) {
4624 return 1;
4625 }
4626 if (isinstance) {
4627 expr_ty target;
4628 expr_ty iter;
4629 asdl_seq* body;
4630 asdl_seq* orelse;
4631
4632 if (_PyObject_HasAttrId(obj, &PyId_target)) {
4633 int res;
4634 tmp = _PyObject_GetAttrId(obj, &PyId_target);
4635 if (tmp == NULL) goto failed;
4636 res = obj2ast_expr(tmp, &target, arena);
4637 if (res != 0) goto failed;
4638 Py_CLEAR(tmp);
4639 } else {
4640 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AsyncFor");
4641 return 1;
4642 }
4643 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
4644 int res;
4645 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
4646 if (tmp == NULL) goto failed;
4647 res = obj2ast_expr(tmp, &iter, arena);
4648 if (res != 0) goto failed;
4649 Py_CLEAR(tmp);
4650 } else {
4651 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from AsyncFor");
4652 return 1;
4653 }
4654 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4655 int res;
4656 Py_ssize_t len;
4657 Py_ssize_t i;
4658 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4659 if (tmp == NULL) goto failed;
4660 if (!PyList_Check(tmp)) {
4661 PyErr_Format(PyExc_TypeError, "AsyncFor field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4662 goto failed;
4663 }
4664 len = PyList_GET_SIZE(tmp);
4665 body = _Py_asdl_seq_new(len, arena);
4666 if (body == NULL) goto failed;
4667 for (i = 0; i < len; i++) {
4668 stmt_ty value;
4669 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4670 if (res != 0) goto failed;
4671 asdl_seq_SET(body, i, value);
4672 }
4673 Py_CLEAR(tmp);
4674 } else {
4675 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncFor");
4676 return 1;
4677 }
4678 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4679 int res;
4680 Py_ssize_t len;
4681 Py_ssize_t i;
4682 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4683 if (tmp == NULL) goto failed;
4684 if (!PyList_Check(tmp)) {
4685 PyErr_Format(PyExc_TypeError, "AsyncFor field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4686 goto failed;
4687 }
4688 len = PyList_GET_SIZE(tmp);
4689 orelse = _Py_asdl_seq_new(len, arena);
4690 if (orelse == 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(orelse, i, value);
4696 }
4697 Py_CLEAR(tmp);
4698 } else {
4699 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from AsyncFor");
4700 return 1;
4701 }
4702 *out = AsyncFor(target, iter, body, orelse, lineno, col_offset, arena);
4703 if (*out == NULL) goto failed;
4704 return 0;
4705 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004706 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4707 if (isinstance == -1) {
4708 return 1;
4709 }
4710 if (isinstance) {
4711 expr_ty test;
4712 asdl_seq* body;
4713 asdl_seq* orelse;
4714
4715 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4716 int res;
4717 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4718 if (tmp == NULL) goto failed;
4719 res = obj2ast_expr(tmp, &test, arena);
4720 if (res != 0) goto failed;
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 \"test\" missing from While");
4724 return 1;
4725 }
4726 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4727 int res;
4728 Py_ssize_t len;
4729 Py_ssize_t i;
4730 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4731 if (tmp == NULL) goto failed;
4732 if (!PyList_Check(tmp)) {
4733 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4734 goto failed;
4735 }
4736 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004737 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004738 if (body == NULL) goto failed;
4739 for (i = 0; i < len; i++) {
4740 stmt_ty value;
4741 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4742 if (res != 0) goto failed;
4743 asdl_seq_SET(body, i, value);
4744 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004745 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004746 } else {
4747 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4748 return 1;
4749 }
4750 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4751 int res;
4752 Py_ssize_t len;
4753 Py_ssize_t i;
4754 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4755 if (tmp == NULL) goto failed;
4756 if (!PyList_Check(tmp)) {
4757 PyErr_Format(PyExc_TypeError, "While field \"orelse\" 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 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004762 if (orelse == 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(orelse, 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 \"orelse\" missing from While");
4772 return 1;
4773 }
4774 *out = While(test, body, orelse, lineno, col_offset, arena);
4775 if (*out == NULL) goto failed;
4776 return 0;
4777 }
4778 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4779 if (isinstance == -1) {
4780 return 1;
4781 }
4782 if (isinstance) {
4783 expr_ty test;
4784 asdl_seq* body;
4785 asdl_seq* orelse;
4786
4787 if (_PyObject_HasAttrId(obj, &PyId_test)) {
4788 int res;
4789 tmp = _PyObject_GetAttrId(obj, &PyId_test);
4790 if (tmp == NULL) goto failed;
4791 res = obj2ast_expr(tmp, &test, arena);
4792 if (res != 0) goto failed;
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 \"test\" missing from If");
4796 return 1;
4797 }
4798 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4799 int res;
4800 Py_ssize_t len;
4801 Py_ssize_t i;
4802 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4803 if (tmp == NULL) goto failed;
4804 if (!PyList_Check(tmp)) {
4805 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4806 goto failed;
4807 }
4808 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004809 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004810 if (body == NULL) goto failed;
4811 for (i = 0; i < len; i++) {
4812 stmt_ty value;
4813 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4814 if (res != 0) goto failed;
4815 asdl_seq_SET(body, i, value);
4816 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004817 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004818 } else {
4819 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4820 return 1;
4821 }
4822 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
4823 int res;
4824 Py_ssize_t len;
4825 Py_ssize_t i;
4826 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
4827 if (tmp == NULL) goto failed;
4828 if (!PyList_Check(tmp)) {
4829 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4830 goto failed;
4831 }
4832 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004833 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004834 if (orelse == NULL) goto failed;
4835 for (i = 0; i < len; i++) {
4836 stmt_ty value;
4837 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4838 if (res != 0) goto failed;
4839 asdl_seq_SET(orelse, i, value);
4840 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004841 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004842 } else {
4843 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4844 return 1;
4845 }
4846 *out = If(test, body, orelse, lineno, col_offset, arena);
4847 if (*out == NULL) goto failed;
4848 return 0;
4849 }
4850 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4851 if (isinstance == -1) {
4852 return 1;
4853 }
4854 if (isinstance) {
4855 asdl_seq* items;
4856 asdl_seq* body;
4857
4858 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4859 int res;
4860 Py_ssize_t len;
4861 Py_ssize_t i;
4862 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4863 if (tmp == NULL) goto failed;
4864 if (!PyList_Check(tmp)) {
4865 PyErr_Format(PyExc_TypeError, "With field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4866 goto failed;
4867 }
4868 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004869 items = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004870 if (items == NULL) goto failed;
4871 for (i = 0; i < len; i++) {
4872 withitem_ty value;
4873 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4874 if (res != 0) goto failed;
4875 asdl_seq_SET(items, i, value);
4876 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004877 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004878 } else {
4879 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from With");
4880 return 1;
4881 }
4882 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4883 int res;
4884 Py_ssize_t len;
4885 Py_ssize_t i;
4886 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4887 if (tmp == NULL) goto failed;
4888 if (!PyList_Check(tmp)) {
4889 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4890 goto failed;
4891 }
4892 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02004893 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004894 if (body == NULL) goto failed;
4895 for (i = 0; i < len; i++) {
4896 stmt_ty value;
4897 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4898 if (res != 0) goto failed;
4899 asdl_seq_SET(body, i, value);
4900 }
Victor Stinner1acc1292013-07-27 00:03:47 +02004901 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004902 } else {
4903 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4904 return 1;
4905 }
4906 *out = With(items, body, lineno, col_offset, arena);
4907 if (*out == NULL) goto failed;
4908 return 0;
4909 }
Yury Selivanov75445082015-05-11 22:57:16 -04004910 isinstance = PyObject_IsInstance(obj, (PyObject*)AsyncWith_type);
4911 if (isinstance == -1) {
4912 return 1;
4913 }
4914 if (isinstance) {
4915 asdl_seq* items;
4916 asdl_seq* body;
4917
4918 if (_PyObject_HasAttrId(obj, &PyId_items)) {
4919 int res;
4920 Py_ssize_t len;
4921 Py_ssize_t i;
4922 tmp = _PyObject_GetAttrId(obj, &PyId_items);
4923 if (tmp == NULL) goto failed;
4924 if (!PyList_Check(tmp)) {
4925 PyErr_Format(PyExc_TypeError, "AsyncWith field \"items\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4926 goto failed;
4927 }
4928 len = PyList_GET_SIZE(tmp);
4929 items = _Py_asdl_seq_new(len, arena);
4930 if (items == NULL) goto failed;
4931 for (i = 0; i < len; i++) {
4932 withitem_ty value;
4933 res = obj2ast_withitem(PyList_GET_ITEM(tmp, i), &value, arena);
4934 if (res != 0) goto failed;
4935 asdl_seq_SET(items, i, value);
4936 }
4937 Py_CLEAR(tmp);
4938 } else {
4939 PyErr_SetString(PyExc_TypeError, "required field \"items\" missing from AsyncWith");
4940 return 1;
4941 }
4942 if (_PyObject_HasAttrId(obj, &PyId_body)) {
4943 int res;
4944 Py_ssize_t len;
4945 Py_ssize_t i;
4946 tmp = _PyObject_GetAttrId(obj, &PyId_body);
4947 if (tmp == NULL) goto failed;
4948 if (!PyList_Check(tmp)) {
4949 PyErr_Format(PyExc_TypeError, "AsyncWith field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4950 goto failed;
4951 }
4952 len = PyList_GET_SIZE(tmp);
4953 body = _Py_asdl_seq_new(len, arena);
4954 if (body == NULL) goto failed;
4955 for (i = 0; i < len; i++) {
4956 stmt_ty value;
4957 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4958 if (res != 0) goto failed;
4959 asdl_seq_SET(body, i, value);
4960 }
4961 Py_CLEAR(tmp);
4962 } else {
4963 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from AsyncWith");
4964 return 1;
4965 }
4966 *out = AsyncWith(items, body, lineno, col_offset, arena);
4967 if (*out == NULL) goto failed;
4968 return 0;
4969 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004970 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4971 if (isinstance == -1) {
4972 return 1;
4973 }
4974 if (isinstance) {
4975 expr_ty exc;
4976 expr_ty cause;
4977
Victor Stinneree4b59c2013-07-27 00:01:35 +02004978 if (exists_not_none(obj, &PyId_exc)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004979 int res;
4980 tmp = _PyObject_GetAttrId(obj, &PyId_exc);
4981 if (tmp == NULL) goto failed;
4982 res = obj2ast_expr(tmp, &exc, arena);
4983 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004984 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004985 } else {
4986 exc = NULL;
4987 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02004988 if (exists_not_none(obj, &PyId_cause)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004989 int res;
4990 tmp = _PyObject_GetAttrId(obj, &PyId_cause);
4991 if (tmp == NULL) goto failed;
4992 res = obj2ast_expr(tmp, &cause, arena);
4993 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02004994 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02004995 } else {
4996 cause = NULL;
4997 }
4998 *out = Raise(exc, cause, lineno, col_offset, arena);
4999 if (*out == NULL) goto failed;
5000 return 0;
5001 }
5002 isinstance = PyObject_IsInstance(obj, (PyObject*)Try_type);
5003 if (isinstance == -1) {
5004 return 1;
5005 }
5006 if (isinstance) {
5007 asdl_seq* body;
5008 asdl_seq* handlers;
5009 asdl_seq* orelse;
5010 asdl_seq* finalbody;
5011
5012 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5013 int res;
5014 Py_ssize_t len;
5015 Py_ssize_t i;
5016 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5017 if (tmp == NULL) goto failed;
5018 if (!PyList_Check(tmp)) {
5019 PyErr_Format(PyExc_TypeError, "Try field \"body\" 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 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005024 if (body == 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(body, 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 \"body\" missing from Try");
5034 return 1;
5035 }
5036 if (_PyObject_HasAttrId(obj, &PyId_handlers)) {
5037 int res;
5038 Py_ssize_t len;
5039 Py_ssize_t i;
5040 tmp = _PyObject_GetAttrId(obj, &PyId_handlers);
5041 if (tmp == NULL) goto failed;
5042 if (!PyList_Check(tmp)) {
5043 PyErr_Format(PyExc_TypeError, "Try field \"handlers\" 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 handlers = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005048 if (handlers == NULL) goto failed;
5049 for (i = 0; i < len; i++) {
5050 excepthandler_ty value;
5051 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
5052 if (res != 0) goto failed;
5053 asdl_seq_SET(handlers, 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 \"handlers\" missing from Try");
5058 return 1;
5059 }
5060 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5061 int res;
5062 Py_ssize_t len;
5063 Py_ssize_t i;
5064 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5065 if (tmp == NULL) goto failed;
5066 if (!PyList_Check(tmp)) {
5067 PyErr_Format(PyExc_TypeError, "Try field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5068 goto failed;
5069 }
5070 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005071 orelse = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005072 if (orelse == NULL) goto failed;
5073 for (i = 0; i < len; i++) {
5074 stmt_ty value;
5075 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5076 if (res != 0) goto failed;
5077 asdl_seq_SET(orelse, i, value);
5078 }
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 \"orelse\" missing from Try");
5082 return 1;
5083 }
5084 if (_PyObject_HasAttrId(obj, &PyId_finalbody)) {
5085 int res;
5086 Py_ssize_t len;
5087 Py_ssize_t i;
5088 tmp = _PyObject_GetAttrId(obj, &PyId_finalbody);
5089 if (tmp == NULL) goto failed;
5090 if (!PyList_Check(tmp)) {
5091 PyErr_Format(PyExc_TypeError, "Try field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5092 goto failed;
5093 }
5094 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005095 finalbody = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005096 if (finalbody == NULL) goto failed;
5097 for (i = 0; i < len; i++) {
5098 stmt_ty value;
5099 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5100 if (res != 0) goto failed;
5101 asdl_seq_SET(finalbody, i, value);
5102 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005103 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005104 } else {
5105 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from Try");
5106 return 1;
5107 }
5108 *out = Try(body, handlers, orelse, finalbody, lineno, col_offset,
5109 arena);
5110 if (*out == NULL) goto failed;
5111 return 0;
5112 }
5113 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
5114 if (isinstance == -1) {
5115 return 1;
5116 }
5117 if (isinstance) {
5118 expr_ty test;
5119 expr_ty msg;
5120
5121 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5122 int res;
5123 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5124 if (tmp == NULL) goto failed;
5125 res = obj2ast_expr(tmp, &test, arena);
5126 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005127 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
5130 return 1;
5131 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005132 if (exists_not_none(obj, &PyId_msg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005133 int res;
5134 tmp = _PyObject_GetAttrId(obj, &PyId_msg);
5135 if (tmp == NULL) goto failed;
5136 res = obj2ast_expr(tmp, &msg, arena);
5137 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005138 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005139 } else {
5140 msg = NULL;
5141 }
5142 *out = Assert(test, msg, lineno, col_offset, arena);
5143 if (*out == NULL) goto failed;
5144 return 0;
5145 }
5146 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
5147 if (isinstance == -1) {
5148 return 1;
5149 }
5150 if (isinstance) {
5151 asdl_seq* names;
5152
5153 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5154 int res;
5155 Py_ssize_t len;
5156 Py_ssize_t i;
5157 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5158 if (tmp == NULL) goto failed;
5159 if (!PyList_Check(tmp)) {
5160 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5161 goto failed;
5162 }
5163 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005164 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005165 if (names == NULL) goto failed;
5166 for (i = 0; i < len; i++) {
5167 alias_ty value;
5168 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5169 if (res != 0) goto failed;
5170 asdl_seq_SET(names, i, value);
5171 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005172 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005173 } else {
5174 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
5175 return 1;
5176 }
5177 *out = Import(names, lineno, col_offset, arena);
5178 if (*out == NULL) goto failed;
5179 return 0;
5180 }
5181 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
5182 if (isinstance == -1) {
5183 return 1;
5184 }
5185 if (isinstance) {
5186 identifier module;
5187 asdl_seq* names;
5188 int level;
5189
Victor Stinneree4b59c2013-07-27 00:01:35 +02005190 if (exists_not_none(obj, &PyId_module)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005191 int res;
5192 tmp = _PyObject_GetAttrId(obj, &PyId_module);
5193 if (tmp == NULL) goto failed;
5194 res = obj2ast_identifier(tmp, &module, arena);
5195 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005196 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005197 } else {
5198 module = NULL;
5199 }
5200 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5201 int res;
5202 Py_ssize_t len;
5203 Py_ssize_t i;
5204 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5205 if (tmp == NULL) goto failed;
5206 if (!PyList_Check(tmp)) {
5207 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5208 goto failed;
5209 }
5210 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005211 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005212 if (names == NULL) goto failed;
5213 for (i = 0; i < len; i++) {
5214 alias_ty value;
5215 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
5216 if (res != 0) goto failed;
5217 asdl_seq_SET(names, i, value);
5218 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005219 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005220 } else {
5221 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
5222 return 1;
5223 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02005224 if (exists_not_none(obj, &PyId_level)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005225 int res;
5226 tmp = _PyObject_GetAttrId(obj, &PyId_level);
5227 if (tmp == NULL) goto failed;
5228 res = obj2ast_int(tmp, &level, arena);
5229 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005230 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005231 } else {
5232 level = 0;
5233 }
5234 *out = ImportFrom(module, names, level, lineno, col_offset, arena);
5235 if (*out == NULL) goto failed;
5236 return 0;
5237 }
5238 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
5239 if (isinstance == -1) {
5240 return 1;
5241 }
5242 if (isinstance) {
5243 asdl_seq* names;
5244
5245 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5246 int res;
5247 Py_ssize_t len;
5248 Py_ssize_t i;
5249 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5250 if (tmp == NULL) goto failed;
5251 if (!PyList_Check(tmp)) {
5252 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5253 goto failed;
5254 }
5255 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005256 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005257 if (names == NULL) goto failed;
5258 for (i = 0; i < len; i++) {
5259 identifier value;
5260 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5261 if (res != 0) goto failed;
5262 asdl_seq_SET(names, i, value);
5263 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005264 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
5267 return 1;
5268 }
5269 *out = Global(names, lineno, col_offset, arena);
5270 if (*out == NULL) goto failed;
5271 return 0;
5272 }
5273 isinstance = PyObject_IsInstance(obj, (PyObject*)Nonlocal_type);
5274 if (isinstance == -1) {
5275 return 1;
5276 }
5277 if (isinstance) {
5278 asdl_seq* names;
5279
5280 if (_PyObject_HasAttrId(obj, &PyId_names)) {
5281 int res;
5282 Py_ssize_t len;
5283 Py_ssize_t i;
5284 tmp = _PyObject_GetAttrId(obj, &PyId_names);
5285 if (tmp == NULL) goto failed;
5286 if (!PyList_Check(tmp)) {
5287 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5288 goto failed;
5289 }
5290 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005291 names = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005292 if (names == NULL) goto failed;
5293 for (i = 0; i < len; i++) {
5294 identifier value;
5295 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
5296 if (res != 0) goto failed;
5297 asdl_seq_SET(names, i, value);
5298 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005299 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005300 } else {
5301 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
5302 return 1;
5303 }
5304 *out = Nonlocal(names, lineno, col_offset, arena);
5305 if (*out == NULL) goto failed;
5306 return 0;
5307 }
5308 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
5309 if (isinstance == -1) {
5310 return 1;
5311 }
5312 if (isinstance) {
5313 expr_ty value;
5314
5315 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5316 int res;
5317 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5318 if (tmp == NULL) goto failed;
5319 res = obj2ast_expr(tmp, &value, arena);
5320 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005321 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005322 } else {
5323 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
5324 return 1;
5325 }
5326 *out = Expr(value, lineno, col_offset, arena);
5327 if (*out == NULL) goto failed;
5328 return 0;
5329 }
5330 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
5331 if (isinstance == -1) {
5332 return 1;
5333 }
5334 if (isinstance) {
5335
5336 *out = Pass(lineno, col_offset, arena);
5337 if (*out == NULL) goto failed;
5338 return 0;
5339 }
5340 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
5341 if (isinstance == -1) {
5342 return 1;
5343 }
5344 if (isinstance) {
5345
5346 *out = Break(lineno, col_offset, arena);
5347 if (*out == NULL) goto failed;
5348 return 0;
5349 }
5350 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
5351 if (isinstance == -1) {
5352 return 1;
5353 }
5354 if (isinstance) {
5355
5356 *out = Continue(lineno, col_offset, arena);
5357 if (*out == NULL) goto failed;
5358 return 0;
5359 }
5360
5361 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
5362 failed:
5363 Py_XDECREF(tmp);
5364 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005365}
5366
5367int
5368obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
5369{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005370 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005371
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005372 PyObject *tmp = NULL;
5373 int lineno;
5374 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005375
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005376 if (obj == Py_None) {
5377 *out = NULL;
5378 return 0;
5379 }
5380 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
5381 int res;
5382 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
5383 if (tmp == NULL) goto failed;
5384 res = obj2ast_int(tmp, &lineno, arena);
5385 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005386 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005387 } else {
5388 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00005389 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005390 }
5391 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
5392 int res;
5393 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
5394 if (tmp == NULL) goto failed;
5395 res = obj2ast_int(tmp, &col_offset, arena);
5396 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005397 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005398 } else {
5399 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
5400 return 1;
5401 }
5402 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
5403 if (isinstance == -1) {
5404 return 1;
5405 }
5406 if (isinstance) {
5407 boolop_ty op;
5408 asdl_seq* values;
5409
5410 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5411 int res;
5412 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5413 if (tmp == NULL) goto failed;
5414 res = obj2ast_boolop(tmp, &op, 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 \"op\" missing from BoolOp");
5419 return 1;
5420 }
5421 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5422 int res;
5423 Py_ssize_t len;
5424 Py_ssize_t i;
5425 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5426 if (tmp == NULL) goto failed;
5427 if (!PyList_Check(tmp)) {
5428 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5429 goto failed;
5430 }
5431 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005432 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005433 if (values == NULL) goto failed;
5434 for (i = 0; i < len; i++) {
5435 expr_ty value;
5436 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5437 if (res != 0) goto failed;
5438 asdl_seq_SET(values, i, value);
5439 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005440 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005441 } else {
5442 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
5443 return 1;
5444 }
5445 *out = BoolOp(op, values, lineno, col_offset, arena);
5446 if (*out == NULL) goto failed;
5447 return 0;
5448 }
5449 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
5450 if (isinstance == -1) {
5451 return 1;
5452 }
5453 if (isinstance) {
5454 expr_ty left;
5455 operator_ty op;
5456 expr_ty right;
5457
5458 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5459 int res;
5460 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5461 if (tmp == NULL) goto failed;
5462 res = obj2ast_expr(tmp, &left, arena);
5463 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005464 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005465 } else {
5466 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
5467 return 1;
5468 }
5469 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5470 int res;
5471 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5472 if (tmp == NULL) goto failed;
5473 res = obj2ast_operator(tmp, &op, arena);
5474 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005475 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005476 } else {
5477 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
5478 return 1;
5479 }
5480 if (_PyObject_HasAttrId(obj, &PyId_right)) {
5481 int res;
5482 tmp = _PyObject_GetAttrId(obj, &PyId_right);
5483 if (tmp == NULL) goto failed;
5484 res = obj2ast_expr(tmp, &right, arena);
5485 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005486 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005487 } else {
5488 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
5489 return 1;
5490 }
5491 *out = BinOp(left, op, right, lineno, col_offset, arena);
5492 if (*out == NULL) goto failed;
5493 return 0;
5494 }
5495 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
5496 if (isinstance == -1) {
5497 return 1;
5498 }
5499 if (isinstance) {
5500 unaryop_ty op;
5501 expr_ty operand;
5502
5503 if (_PyObject_HasAttrId(obj, &PyId_op)) {
5504 int res;
5505 tmp = _PyObject_GetAttrId(obj, &PyId_op);
5506 if (tmp == NULL) goto failed;
5507 res = obj2ast_unaryop(tmp, &op, arena);
5508 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005509 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005510 } else {
5511 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
5512 return 1;
5513 }
5514 if (_PyObject_HasAttrId(obj, &PyId_operand)) {
5515 int res;
5516 tmp = _PyObject_GetAttrId(obj, &PyId_operand);
5517 if (tmp == NULL) goto failed;
5518 res = obj2ast_expr(tmp, &operand, arena);
5519 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005520 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005521 } else {
5522 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
5523 return 1;
5524 }
5525 *out = UnaryOp(op, operand, lineno, col_offset, arena);
5526 if (*out == NULL) goto failed;
5527 return 0;
5528 }
5529 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
5530 if (isinstance == -1) {
5531 return 1;
5532 }
5533 if (isinstance) {
5534 arguments_ty args;
5535 expr_ty body;
5536
5537 if (_PyObject_HasAttrId(obj, &PyId_args)) {
5538 int res;
5539 tmp = _PyObject_GetAttrId(obj, &PyId_args);
5540 if (tmp == NULL) goto failed;
5541 res = obj2ast_arguments(tmp, &args, arena);
5542 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005543 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005544 } else {
5545 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
5546 return 1;
5547 }
5548 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5549 int res;
5550 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5551 if (tmp == NULL) goto failed;
5552 res = obj2ast_expr(tmp, &body, arena);
5553 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005554 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005555 } else {
5556 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
5557 return 1;
5558 }
5559 *out = Lambda(args, body, lineno, col_offset, arena);
5560 if (*out == NULL) goto failed;
5561 return 0;
5562 }
5563 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
5564 if (isinstance == -1) {
5565 return 1;
5566 }
5567 if (isinstance) {
5568 expr_ty test;
5569 expr_ty body;
5570 expr_ty orelse;
5571
5572 if (_PyObject_HasAttrId(obj, &PyId_test)) {
5573 int res;
5574 tmp = _PyObject_GetAttrId(obj, &PyId_test);
5575 if (tmp == NULL) goto failed;
5576 res = obj2ast_expr(tmp, &test, arena);
5577 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005578 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005579 } else {
5580 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
5581 return 1;
5582 }
5583 if (_PyObject_HasAttrId(obj, &PyId_body)) {
5584 int res;
5585 tmp = _PyObject_GetAttrId(obj, &PyId_body);
5586 if (tmp == NULL) goto failed;
5587 res = obj2ast_expr(tmp, &body, arena);
5588 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005589 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005590 } else {
5591 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
5592 return 1;
5593 }
5594 if (_PyObject_HasAttrId(obj, &PyId_orelse)) {
5595 int res;
5596 tmp = _PyObject_GetAttrId(obj, &PyId_orelse);
5597 if (tmp == NULL) goto failed;
5598 res = obj2ast_expr(tmp, &orelse, arena);
5599 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005600 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005601 } else {
5602 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
5603 return 1;
5604 }
5605 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
5606 if (*out == NULL) goto failed;
5607 return 0;
5608 }
5609 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
5610 if (isinstance == -1) {
5611 return 1;
5612 }
5613 if (isinstance) {
5614 asdl_seq* keys;
5615 asdl_seq* values;
5616
5617 if (_PyObject_HasAttrId(obj, &PyId_keys)) {
5618 int res;
5619 Py_ssize_t len;
5620 Py_ssize_t i;
5621 tmp = _PyObject_GetAttrId(obj, &PyId_keys);
5622 if (tmp == NULL) goto failed;
5623 if (!PyList_Check(tmp)) {
5624 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5625 goto failed;
5626 }
5627 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005628 keys = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005629 if (keys == NULL) goto failed;
5630 for (i = 0; i < len; i++) {
5631 expr_ty value;
5632 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5633 if (res != 0) goto failed;
5634 asdl_seq_SET(keys, i, value);
5635 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005636 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005637 } else {
5638 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
5639 return 1;
5640 }
5641 if (_PyObject_HasAttrId(obj, &PyId_values)) {
5642 int res;
5643 Py_ssize_t len;
5644 Py_ssize_t i;
5645 tmp = _PyObject_GetAttrId(obj, &PyId_values);
5646 if (tmp == NULL) goto failed;
5647 if (!PyList_Check(tmp)) {
5648 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5649 goto failed;
5650 }
5651 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005652 values = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005653 if (values == NULL) goto failed;
5654 for (i = 0; i < len; i++) {
5655 expr_ty value;
5656 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5657 if (res != 0) goto failed;
5658 asdl_seq_SET(values, i, value);
5659 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005660 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005661 } else {
5662 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5663 return 1;
5664 }
5665 *out = Dict(keys, values, lineno, col_offset, arena);
5666 if (*out == NULL) goto failed;
5667 return 0;
5668 }
5669 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5670 if (isinstance == -1) {
5671 return 1;
5672 }
5673 if (isinstance) {
5674 asdl_seq* elts;
5675
5676 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
5677 int res;
5678 Py_ssize_t len;
5679 Py_ssize_t i;
5680 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
5681 if (tmp == NULL) goto failed;
5682 if (!PyList_Check(tmp)) {
5683 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5684 goto failed;
5685 }
5686 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005687 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005688 if (elts == NULL) goto failed;
5689 for (i = 0; i < len; i++) {
5690 expr_ty value;
5691 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5692 if (res != 0) goto failed;
5693 asdl_seq_SET(elts, i, value);
5694 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005695 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005696 } else {
5697 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5698 return 1;
5699 }
5700 *out = Set(elts, lineno, col_offset, arena);
5701 if (*out == NULL) goto failed;
5702 return 0;
5703 }
5704 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5705 if (isinstance == -1) {
5706 return 1;
5707 }
5708 if (isinstance) {
5709 expr_ty elt;
5710 asdl_seq* generators;
5711
5712 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5713 int res;
5714 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5715 if (tmp == NULL) goto failed;
5716 res = obj2ast_expr(tmp, &elt, arena);
5717 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005718 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005719 } else {
5720 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5721 return 1;
5722 }
5723 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5724 int res;
5725 Py_ssize_t len;
5726 Py_ssize_t i;
5727 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5728 if (tmp == NULL) goto failed;
5729 if (!PyList_Check(tmp)) {
5730 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5731 goto failed;
5732 }
5733 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005734 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005735 if (generators == NULL) goto failed;
5736 for (i = 0; i < len; i++) {
5737 comprehension_ty value;
5738 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5739 if (res != 0) goto failed;
5740 asdl_seq_SET(generators, i, value);
5741 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005742 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005743 } else {
5744 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5745 return 1;
5746 }
5747 *out = ListComp(elt, generators, lineno, col_offset, arena);
5748 if (*out == NULL) goto failed;
5749 return 0;
5750 }
5751 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5752 if (isinstance == -1) {
5753 return 1;
5754 }
5755 if (isinstance) {
5756 expr_ty elt;
5757 asdl_seq* generators;
5758
5759 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5760 int res;
5761 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5762 if (tmp == NULL) goto failed;
5763 res = obj2ast_expr(tmp, &elt, 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 \"elt\" missing from SetComp");
5768 return 1;
5769 }
5770 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5771 int res;
5772 Py_ssize_t len;
5773 Py_ssize_t i;
5774 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5775 if (tmp == NULL) goto failed;
5776 if (!PyList_Check(tmp)) {
5777 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5778 goto failed;
5779 }
5780 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005781 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005782 if (generators == NULL) goto failed;
5783 for (i = 0; i < len; i++) {
5784 comprehension_ty value;
5785 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5786 if (res != 0) goto failed;
5787 asdl_seq_SET(generators, i, value);
5788 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005789 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005790 } else {
5791 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5792 return 1;
5793 }
5794 *out = SetComp(elt, generators, lineno, col_offset, arena);
5795 if (*out == NULL) goto failed;
5796 return 0;
5797 }
5798 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5799 if (isinstance == -1) {
5800 return 1;
5801 }
5802 if (isinstance) {
5803 expr_ty key;
5804 expr_ty value;
5805 asdl_seq* generators;
5806
5807 if (_PyObject_HasAttrId(obj, &PyId_key)) {
5808 int res;
5809 tmp = _PyObject_GetAttrId(obj, &PyId_key);
5810 if (tmp == NULL) goto failed;
5811 res = obj2ast_expr(tmp, &key, arena);
5812 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005813 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005814 } else {
5815 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5816 return 1;
5817 }
5818 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5819 int res;
5820 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5821 if (tmp == NULL) goto failed;
5822 res = obj2ast_expr(tmp, &value, arena);
5823 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005824 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005825 } else {
5826 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5827 return 1;
5828 }
5829 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5830 int res;
5831 Py_ssize_t len;
5832 Py_ssize_t i;
5833 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5834 if (tmp == NULL) goto failed;
5835 if (!PyList_Check(tmp)) {
5836 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5837 goto failed;
5838 }
5839 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005840 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005841 if (generators == NULL) goto failed;
5842 for (i = 0; i < len; i++) {
5843 comprehension_ty value;
5844 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5845 if (res != 0) goto failed;
5846 asdl_seq_SET(generators, i, value);
5847 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005848 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005849 } else {
5850 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5851 return 1;
5852 }
5853 *out = DictComp(key, value, generators, lineno, col_offset, arena);
5854 if (*out == NULL) goto failed;
5855 return 0;
5856 }
5857 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5858 if (isinstance == -1) {
5859 return 1;
5860 }
5861 if (isinstance) {
5862 expr_ty elt;
5863 asdl_seq* generators;
5864
5865 if (_PyObject_HasAttrId(obj, &PyId_elt)) {
5866 int res;
5867 tmp = _PyObject_GetAttrId(obj, &PyId_elt);
5868 if (tmp == NULL) goto failed;
5869 res = obj2ast_expr(tmp, &elt, arena);
5870 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005871 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005872 } else {
5873 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5874 return 1;
5875 }
5876 if (_PyObject_HasAttrId(obj, &PyId_generators)) {
5877 int res;
5878 Py_ssize_t len;
5879 Py_ssize_t i;
5880 tmp = _PyObject_GetAttrId(obj, &PyId_generators);
5881 if (tmp == NULL) goto failed;
5882 if (!PyList_Check(tmp)) {
5883 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5884 goto failed;
5885 }
5886 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02005887 generators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005888 if (generators == NULL) goto failed;
5889 for (i = 0; i < len; i++) {
5890 comprehension_ty value;
5891 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5892 if (res != 0) goto failed;
5893 asdl_seq_SET(generators, i, value);
5894 }
Victor Stinner1acc1292013-07-27 00:03:47 +02005895 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005896 } else {
5897 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5898 return 1;
5899 }
5900 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5901 if (*out == NULL) goto failed;
5902 return 0;
5903 }
Yury Selivanov75445082015-05-11 22:57:16 -04005904 isinstance = PyObject_IsInstance(obj, (PyObject*)Await_type);
5905 if (isinstance == -1) {
5906 return 1;
5907 }
5908 if (isinstance) {
5909 expr_ty value;
5910
5911 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5912 int res;
5913 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5914 if (tmp == NULL) goto failed;
5915 res = obj2ast_expr(tmp, &value, arena);
5916 if (res != 0) goto failed;
5917 Py_CLEAR(tmp);
5918 } else {
5919 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Await");
5920 return 1;
5921 }
5922 *out = Await(value, lineno, col_offset, arena);
5923 if (*out == NULL) goto failed;
5924 return 0;
5925 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005926 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5927 if (isinstance == -1) {
5928 return 1;
5929 }
5930 if (isinstance) {
5931 expr_ty value;
5932
Victor Stinneree4b59c2013-07-27 00:01:35 +02005933 if (exists_not_none(obj, &PyId_value)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005934 int res;
5935 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5936 if (tmp == NULL) goto failed;
5937 res = obj2ast_expr(tmp, &value, arena);
5938 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02005939 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02005940 } else {
5941 value = NULL;
5942 }
5943 *out = Yield(value, lineno, col_offset, arena);
5944 if (*out == NULL) goto failed;
5945 return 0;
5946 }
5947 isinstance = PyObject_IsInstance(obj, (PyObject*)YieldFrom_type);
5948 if (isinstance == -1) {
5949 return 1;
5950 }
5951 if (isinstance) {
5952 expr_ty value;
5953
5954 if (_PyObject_HasAttrId(obj, &PyId_value)) {
5955 int res;
5956 tmp = _PyObject_GetAttrId(obj, &PyId_value);
5957 if (tmp == NULL) goto failed;
5958 res = obj2ast_expr(tmp, &value, arena);
5959 if (res != 0) goto failed;
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 \"value\" missing from YieldFrom");
5963 return 1;
5964 }
5965 *out = YieldFrom(value, lineno, col_offset, arena);
5966 if (*out == NULL) goto failed;
5967 return 0;
5968 }
5969 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5970 if (isinstance == -1) {
5971 return 1;
5972 }
5973 if (isinstance) {
5974 expr_ty left;
5975 asdl_int_seq* ops;
5976 asdl_seq* comparators;
5977
5978 if (_PyObject_HasAttrId(obj, &PyId_left)) {
5979 int res;
5980 tmp = _PyObject_GetAttrId(obj, &PyId_left);
5981 if (tmp == NULL) goto failed;
5982 res = obj2ast_expr(tmp, &left, arena);
5983 if (res != 0) goto failed;
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 \"left\" missing from Compare");
5987 return 1;
5988 }
5989 if (_PyObject_HasAttrId(obj, &PyId_ops)) {
5990 int res;
5991 Py_ssize_t len;
5992 Py_ssize_t i;
5993 tmp = _PyObject_GetAttrId(obj, &PyId_ops);
5994 if (tmp == NULL) goto failed;
5995 if (!PyList_Check(tmp)) {
5996 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5997 goto failed;
5998 }
5999 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006000 ops = _Py_asdl_int_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006001 if (ops == NULL) goto failed;
6002 for (i = 0; i < len; i++) {
6003 cmpop_ty value;
6004 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
6005 if (res != 0) goto failed;
6006 asdl_seq_SET(ops, i, value);
6007 }
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 \"ops\" missing from Compare");
6011 return 1;
6012 }
6013 if (_PyObject_HasAttrId(obj, &PyId_comparators)) {
6014 int res;
6015 Py_ssize_t len;
6016 Py_ssize_t i;
6017 tmp = _PyObject_GetAttrId(obj, &PyId_comparators);
6018 if (tmp == NULL) goto failed;
6019 if (!PyList_Check(tmp)) {
6020 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" 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 comparators = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006025 if (comparators == 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(comparators, 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 \"comparators\" missing from Compare");
6035 return 1;
6036 }
6037 *out = Compare(left, ops, comparators, lineno, col_offset, arena);
6038 if (*out == NULL) goto failed;
6039 return 0;
6040 }
6041 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
6042 if (isinstance == -1) {
6043 return 1;
6044 }
6045 if (isinstance) {
6046 expr_ty func;
6047 asdl_seq* args;
6048 asdl_seq* keywords;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006049
6050 if (_PyObject_HasAttrId(obj, &PyId_func)) {
6051 int res;
6052 tmp = _PyObject_GetAttrId(obj, &PyId_func);
6053 if (tmp == NULL) goto failed;
6054 res = obj2ast_expr(tmp, &func, arena);
6055 if (res != 0) goto failed;
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 \"func\" missing from Call");
6059 return 1;
6060 }
6061 if (_PyObject_HasAttrId(obj, &PyId_args)) {
6062 int res;
6063 Py_ssize_t len;
6064 Py_ssize_t i;
6065 tmp = _PyObject_GetAttrId(obj, &PyId_args);
6066 if (tmp == NULL) goto failed;
6067 if (!PyList_Check(tmp)) {
6068 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6069 goto failed;
6070 }
6071 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006072 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006073 if (args == NULL) goto failed;
6074 for (i = 0; i < len; i++) {
6075 expr_ty value;
6076 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6077 if (res != 0) goto failed;
6078 asdl_seq_SET(args, i, value);
6079 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006080 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006081 } else {
6082 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
6083 return 1;
6084 }
6085 if (_PyObject_HasAttrId(obj, &PyId_keywords)) {
6086 int res;
6087 Py_ssize_t len;
6088 Py_ssize_t i;
6089 tmp = _PyObject_GetAttrId(obj, &PyId_keywords);
6090 if (tmp == NULL) goto failed;
6091 if (!PyList_Check(tmp)) {
6092 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6093 goto failed;
6094 }
6095 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006096 keywords = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006097 if (keywords == NULL) goto failed;
6098 for (i = 0; i < len; i++) {
6099 keyword_ty value;
6100 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
6101 if (res != 0) goto failed;
6102 asdl_seq_SET(keywords, i, value);
6103 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006104 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006105 } else {
6106 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
6107 return 1;
6108 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04006109 *out = Call(func, args, keywords, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006110 if (*out == NULL) goto failed;
6111 return 0;
6112 }
6113 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
6114 if (isinstance == -1) {
6115 return 1;
6116 }
6117 if (isinstance) {
6118 object n;
6119
6120 if (_PyObject_HasAttrId(obj, &PyId_n)) {
6121 int res;
6122 tmp = _PyObject_GetAttrId(obj, &PyId_n);
6123 if (tmp == NULL) goto failed;
6124 res = obj2ast_object(tmp, &n, arena);
6125 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006126 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006127 } else {
6128 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
6129 return 1;
6130 }
6131 *out = Num(n, lineno, col_offset, arena);
6132 if (*out == NULL) goto failed;
6133 return 0;
6134 }
6135 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
6136 if (isinstance == -1) {
6137 return 1;
6138 }
6139 if (isinstance) {
6140 string s;
6141
6142 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6143 int res;
6144 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6145 if (tmp == NULL) goto failed;
6146 res = obj2ast_string(tmp, &s, arena);
6147 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006148 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006149 } else {
6150 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
6151 return 1;
6152 }
6153 *out = Str(s, lineno, col_offset, arena);
6154 if (*out == NULL) goto failed;
6155 return 0;
6156 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04006157 isinstance = PyObject_IsInstance(obj, (PyObject*)FormattedValue_type);
6158 if (isinstance == -1) {
6159 return 1;
6160 }
6161 if (isinstance) {
6162 expr_ty value;
6163 int conversion;
6164 expr_ty format_spec;
6165
6166 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6167 int res;
6168 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6169 if (tmp == NULL) goto failed;
6170 res = obj2ast_expr(tmp, &value, arena);
6171 if (res != 0) goto failed;
6172 Py_CLEAR(tmp);
6173 } else {
6174 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from FormattedValue");
6175 return 1;
6176 }
6177 if (exists_not_none(obj, &PyId_conversion)) {
6178 int res;
6179 tmp = _PyObject_GetAttrId(obj, &PyId_conversion);
6180 if (tmp == NULL) goto failed;
6181 res = obj2ast_int(tmp, &conversion, arena);
6182 if (res != 0) goto failed;
6183 Py_CLEAR(tmp);
6184 } else {
6185 conversion = 0;
6186 }
6187 if (exists_not_none(obj, &PyId_format_spec)) {
6188 int res;
6189 tmp = _PyObject_GetAttrId(obj, &PyId_format_spec);
6190 if (tmp == NULL) goto failed;
6191 res = obj2ast_expr(tmp, &format_spec, arena);
6192 if (res != 0) goto failed;
6193 Py_CLEAR(tmp);
6194 } else {
6195 format_spec = NULL;
6196 }
6197 *out = FormattedValue(value, conversion, format_spec, lineno,
6198 col_offset, arena);
6199 if (*out == NULL) goto failed;
6200 return 0;
6201 }
6202 isinstance = PyObject_IsInstance(obj, (PyObject*)JoinedStr_type);
6203 if (isinstance == -1) {
6204 return 1;
6205 }
6206 if (isinstance) {
6207 asdl_seq* values;
6208
6209 if (_PyObject_HasAttrId(obj, &PyId_values)) {
6210 int res;
6211 Py_ssize_t len;
6212 Py_ssize_t i;
6213 tmp = _PyObject_GetAttrId(obj, &PyId_values);
6214 if (tmp == NULL) goto failed;
6215 if (!PyList_Check(tmp)) {
6216 PyErr_Format(PyExc_TypeError, "JoinedStr field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6217 goto failed;
6218 }
6219 len = PyList_GET_SIZE(tmp);
6220 values = _Py_asdl_seq_new(len, arena);
6221 if (values == NULL) goto failed;
6222 for (i = 0; i < len; i++) {
6223 expr_ty value;
6224 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6225 if (res != 0) goto failed;
6226 asdl_seq_SET(values, i, value);
6227 }
6228 Py_CLEAR(tmp);
6229 } else {
6230 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from JoinedStr");
6231 return 1;
6232 }
6233 *out = JoinedStr(values, lineno, col_offset, arena);
6234 if (*out == NULL) goto failed;
6235 return 0;
6236 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006237 isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type);
6238 if (isinstance == -1) {
6239 return 1;
6240 }
6241 if (isinstance) {
6242 bytes s;
6243
6244 if (_PyObject_HasAttrId(obj, &PyId_s)) {
6245 int res;
6246 tmp = _PyObject_GetAttrId(obj, &PyId_s);
6247 if (tmp == NULL) goto failed;
6248 res = obj2ast_bytes(tmp, &s, arena);
6249 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006250 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006251 } else {
6252 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
6253 return 1;
6254 }
6255 *out = Bytes(s, lineno, col_offset, arena);
6256 if (*out == NULL) goto failed;
6257 return 0;
6258 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006259 isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type);
6260 if (isinstance == -1) {
6261 return 1;
6262 }
6263 if (isinstance) {
6264 singleton value;
6265
6266 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6267 int res;
6268 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6269 if (tmp == NULL) goto failed;
6270 res = obj2ast_singleton(tmp, &value, arena);
6271 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02006272 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02006273 } else {
6274 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant");
6275 return 1;
6276 }
6277 *out = NameConstant(value, lineno, col_offset, arena);
6278 if (*out == NULL) goto failed;
6279 return 0;
6280 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006281 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
6282 if (isinstance == -1) {
6283 return 1;
6284 }
6285 if (isinstance) {
6286
6287 *out = Ellipsis(lineno, col_offset, arena);
6288 if (*out == NULL) goto failed;
6289 return 0;
6290 }
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01006291 isinstance = PyObject_IsInstance(obj, (PyObject*)Constant_type);
6292 if (isinstance == -1) {
6293 return 1;
6294 }
6295 if (isinstance) {
6296 constant value;
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_constant(tmp, &value, arena);
6303 if (res != 0) goto failed;
6304 Py_CLEAR(tmp);
6305 } else {
6306 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Constant");
6307 return 1;
6308 }
6309 *out = Constant(value, lineno, col_offset, arena);
6310 if (*out == NULL) goto failed;
6311 return 0;
6312 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006313 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
6314 if (isinstance == -1) {
6315 return 1;
6316 }
6317 if (isinstance) {
6318 expr_ty value;
6319 identifier attr;
6320 expr_context_ty ctx;
6321
6322 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6323 int res;
6324 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6325 if (tmp == NULL) goto failed;
6326 res = obj2ast_expr(tmp, &value, arena);
6327 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006328 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006329 } else {
6330 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
6331 return 1;
6332 }
6333 if (_PyObject_HasAttrId(obj, &PyId_attr)) {
6334 int res;
6335 tmp = _PyObject_GetAttrId(obj, &PyId_attr);
6336 if (tmp == NULL) goto failed;
6337 res = obj2ast_identifier(tmp, &attr, arena);
6338 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006339 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006340 } else {
6341 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
6342 return 1;
6343 }
6344 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6345 int res;
6346 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6347 if (tmp == NULL) goto failed;
6348 res = obj2ast_expr_context(tmp, &ctx, arena);
6349 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006350 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006351 } else {
6352 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
6353 return 1;
6354 }
6355 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
6356 if (*out == NULL) goto failed;
6357 return 0;
6358 }
6359 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
6360 if (isinstance == -1) {
6361 return 1;
6362 }
6363 if (isinstance) {
6364 expr_ty value;
6365 slice_ty slice;
6366 expr_context_ty ctx;
6367
6368 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6369 int res;
6370 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6371 if (tmp == NULL) goto failed;
6372 res = obj2ast_expr(tmp, &value, arena);
6373 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006374 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006375 } else {
6376 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
6377 return 1;
6378 }
6379 if (_PyObject_HasAttrId(obj, &PyId_slice)) {
6380 int res;
6381 tmp = _PyObject_GetAttrId(obj, &PyId_slice);
6382 if (tmp == NULL) goto failed;
6383 res = obj2ast_slice(tmp, &slice, arena);
6384 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006385 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006386 } else {
6387 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
6388 return 1;
6389 }
6390 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6391 int res;
6392 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6393 if (tmp == NULL) goto failed;
6394 res = obj2ast_expr_context(tmp, &ctx, arena);
6395 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006396 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006397 } else {
6398 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
6399 return 1;
6400 }
6401 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
6402 if (*out == NULL) goto failed;
6403 return 0;
6404 }
6405 isinstance = PyObject_IsInstance(obj, (PyObject*)Starred_type);
6406 if (isinstance == -1) {
6407 return 1;
6408 }
6409 if (isinstance) {
6410 expr_ty value;
6411 expr_context_ty ctx;
6412
6413 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6414 int res;
6415 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6416 if (tmp == NULL) goto failed;
6417 res = obj2ast_expr(tmp, &value, arena);
6418 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006419 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006420 } else {
6421 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
6422 return 1;
6423 }
6424 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6425 int res;
6426 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6427 if (tmp == NULL) goto failed;
6428 res = obj2ast_expr_context(tmp, &ctx, arena);
6429 if (res != 0) goto failed;
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 \"ctx\" missing from Starred");
6433 return 1;
6434 }
6435 *out = Starred(value, ctx, lineno, col_offset, arena);
6436 if (*out == NULL) goto failed;
6437 return 0;
6438 }
6439 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
6440 if (isinstance == -1) {
6441 return 1;
6442 }
6443 if (isinstance) {
6444 identifier id;
6445 expr_context_ty ctx;
6446
6447 if (_PyObject_HasAttrId(obj, &PyId_id)) {
6448 int res;
6449 tmp = _PyObject_GetAttrId(obj, &PyId_id);
6450 if (tmp == NULL) goto failed;
6451 res = obj2ast_identifier(tmp, &id, arena);
6452 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006453 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006454 } else {
6455 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
6456 return 1;
6457 }
6458 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6459 int res;
6460 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6461 if (tmp == NULL) goto failed;
6462 res = obj2ast_expr_context(tmp, &ctx, arena);
6463 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006464 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006465 } else {
6466 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
6467 return 1;
6468 }
6469 *out = Name(id, ctx, lineno, col_offset, arena);
6470 if (*out == NULL) goto failed;
6471 return 0;
6472 }
6473 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
6474 if (isinstance == -1) {
6475 return 1;
6476 }
6477 if (isinstance) {
6478 asdl_seq* elts;
6479 expr_context_ty ctx;
6480
6481 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6482 int res;
6483 Py_ssize_t len;
6484 Py_ssize_t i;
6485 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6486 if (tmp == NULL) goto failed;
6487 if (!PyList_Check(tmp)) {
6488 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6489 goto failed;
6490 }
6491 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006492 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006493 if (elts == NULL) goto failed;
6494 for (i = 0; i < len; i++) {
6495 expr_ty value;
6496 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6497 if (res != 0) goto failed;
6498 asdl_seq_SET(elts, i, value);
6499 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006500 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006501 } else {
6502 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
6503 return 1;
6504 }
6505 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6506 int res;
6507 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6508 if (tmp == NULL) goto failed;
6509 res = obj2ast_expr_context(tmp, &ctx, arena);
6510 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006511 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006512 } else {
6513 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
6514 return 1;
6515 }
6516 *out = List(elts, ctx, lineno, col_offset, arena);
6517 if (*out == NULL) goto failed;
6518 return 0;
6519 }
6520 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
6521 if (isinstance == -1) {
6522 return 1;
6523 }
6524 if (isinstance) {
6525 asdl_seq* elts;
6526 expr_context_ty ctx;
6527
6528 if (_PyObject_HasAttrId(obj, &PyId_elts)) {
6529 int res;
6530 Py_ssize_t len;
6531 Py_ssize_t i;
6532 tmp = _PyObject_GetAttrId(obj, &PyId_elts);
6533 if (tmp == NULL) goto failed;
6534 if (!PyList_Check(tmp)) {
6535 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6536 goto failed;
6537 }
6538 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006539 elts = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006540 if (elts == NULL) goto failed;
6541 for (i = 0; i < len; i++) {
6542 expr_ty value;
6543 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6544 if (res != 0) goto failed;
6545 asdl_seq_SET(elts, i, value);
6546 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006547 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006548 } else {
6549 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
6550 return 1;
6551 }
6552 if (_PyObject_HasAttrId(obj, &PyId_ctx)) {
6553 int res;
6554 tmp = _PyObject_GetAttrId(obj, &PyId_ctx);
6555 if (tmp == NULL) goto failed;
6556 res = obj2ast_expr_context(tmp, &ctx, arena);
6557 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006558 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006559 } else {
6560 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
6561 return 1;
6562 }
6563 *out = Tuple(elts, ctx, lineno, col_offset, arena);
6564 if (*out == NULL) goto failed;
6565 return 0;
6566 }
6567
6568 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
6569 failed:
6570 Py_XDECREF(tmp);
6571 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006572}
6573
6574int
6575obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
6576{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006577 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006578
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006579 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
6580 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006581 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006582 }
6583 if (isinstance) {
6584 *out = Load;
6585 return 0;
6586 }
6587 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
6588 if (isinstance == -1) {
6589 return 1;
6590 }
6591 if (isinstance) {
6592 *out = Store;
6593 return 0;
6594 }
6595 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
6596 if (isinstance == -1) {
6597 return 1;
6598 }
6599 if (isinstance) {
6600 *out = Del;
6601 return 0;
6602 }
6603 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
6604 if (isinstance == -1) {
6605 return 1;
6606 }
6607 if (isinstance) {
6608 *out = AugLoad;
6609 return 0;
6610 }
6611 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
6612 if (isinstance == -1) {
6613 return 1;
6614 }
6615 if (isinstance) {
6616 *out = AugStore;
6617 return 0;
6618 }
6619 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
6620 if (isinstance == -1) {
6621 return 1;
6622 }
6623 if (isinstance) {
6624 *out = Param;
6625 return 0;
6626 }
6627
6628 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
6629 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006630}
6631
6632int
6633obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
6634{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006635 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006636
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006637 PyObject *tmp = NULL;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006638
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006639 if (obj == Py_None) {
6640 *out = NULL;
6641 return 0;
6642 }
6643 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
6644 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006645 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006646 }
6647 if (isinstance) {
6648 expr_ty lower;
6649 expr_ty upper;
6650 expr_ty step;
6651
Victor Stinneree4b59c2013-07-27 00:01:35 +02006652 if (exists_not_none(obj, &PyId_lower)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006653 int res;
6654 tmp = _PyObject_GetAttrId(obj, &PyId_lower);
6655 if (tmp == NULL) goto failed;
6656 res = obj2ast_expr(tmp, &lower, arena);
6657 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006658 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006659 } else {
6660 lower = NULL;
6661 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006662 if (exists_not_none(obj, &PyId_upper)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006663 int res;
6664 tmp = _PyObject_GetAttrId(obj, &PyId_upper);
6665 if (tmp == NULL) goto failed;
6666 res = obj2ast_expr(tmp, &upper, arena);
6667 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006668 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006669 } else {
6670 upper = NULL;
6671 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02006672 if (exists_not_none(obj, &PyId_step)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006673 int res;
6674 tmp = _PyObject_GetAttrId(obj, &PyId_step);
6675 if (tmp == NULL) goto failed;
6676 res = obj2ast_expr(tmp, &step, arena);
6677 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006678 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006679 } else {
6680 step = NULL;
6681 }
6682 *out = Slice(lower, upper, step, arena);
6683 if (*out == NULL) goto failed;
6684 return 0;
6685 }
6686 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
6687 if (isinstance == -1) {
6688 return 1;
6689 }
6690 if (isinstance) {
6691 asdl_seq* dims;
6692
6693 if (_PyObject_HasAttrId(obj, &PyId_dims)) {
6694 int res;
6695 Py_ssize_t len;
6696 Py_ssize_t i;
6697 tmp = _PyObject_GetAttrId(obj, &PyId_dims);
6698 if (tmp == NULL) goto failed;
6699 if (!PyList_Check(tmp)) {
6700 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6701 goto failed;
6702 }
6703 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02006704 dims = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006705 if (dims == NULL) goto failed;
6706 for (i = 0; i < len; i++) {
6707 slice_ty value;
6708 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
6709 if (res != 0) goto failed;
6710 asdl_seq_SET(dims, i, value);
6711 }
Victor Stinner1acc1292013-07-27 00:03:47 +02006712 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006713 } else {
6714 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
6715 return 1;
6716 }
6717 *out = ExtSlice(dims, arena);
6718 if (*out == NULL) goto failed;
6719 return 0;
6720 }
6721 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
6722 if (isinstance == -1) {
6723 return 1;
6724 }
6725 if (isinstance) {
6726 expr_ty value;
6727
6728 if (_PyObject_HasAttrId(obj, &PyId_value)) {
6729 int res;
6730 tmp = _PyObject_GetAttrId(obj, &PyId_value);
6731 if (tmp == NULL) goto failed;
6732 res = obj2ast_expr(tmp, &value, arena);
6733 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02006734 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006735 } else {
6736 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
6737 return 1;
6738 }
6739 *out = Index(value, arena);
6740 if (*out == NULL) goto failed;
6741 return 0;
6742 }
6743
6744 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6745 failed:
6746 Py_XDECREF(tmp);
6747 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006748}
6749
6750int
6751obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
6752{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006753 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006754
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006755 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
6756 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006757 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006758 }
6759 if (isinstance) {
6760 *out = And;
6761 return 0;
6762 }
6763 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
6764 if (isinstance == -1) {
6765 return 1;
6766 }
6767 if (isinstance) {
6768 *out = Or;
6769 return 0;
6770 }
6771
6772 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
6773 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006774}
6775
6776int
6777obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
6778{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006779 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006780
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006781 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
6782 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006783 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006784 }
6785 if (isinstance) {
6786 *out = Add;
6787 return 0;
6788 }
6789 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6790 if (isinstance == -1) {
6791 return 1;
6792 }
6793 if (isinstance) {
6794 *out = Sub;
6795 return 0;
6796 }
6797 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6798 if (isinstance == -1) {
6799 return 1;
6800 }
6801 if (isinstance) {
6802 *out = Mult;
6803 return 0;
6804 }
Benjamin Petersond51374e2014-04-09 23:55:56 -04006805 isinstance = PyObject_IsInstance(obj, (PyObject *)MatMult_type);
6806 if (isinstance == -1) {
6807 return 1;
6808 }
6809 if (isinstance) {
6810 *out = MatMult;
6811 return 0;
6812 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006813 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6814 if (isinstance == -1) {
6815 return 1;
6816 }
6817 if (isinstance) {
6818 *out = Div;
6819 return 0;
6820 }
6821 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6822 if (isinstance == -1) {
6823 return 1;
6824 }
6825 if (isinstance) {
6826 *out = Mod;
6827 return 0;
6828 }
6829 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6830 if (isinstance == -1) {
6831 return 1;
6832 }
6833 if (isinstance) {
6834 *out = Pow;
6835 return 0;
6836 }
6837 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6838 if (isinstance == -1) {
6839 return 1;
6840 }
6841 if (isinstance) {
6842 *out = LShift;
6843 return 0;
6844 }
6845 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6846 if (isinstance == -1) {
6847 return 1;
6848 }
6849 if (isinstance) {
6850 *out = RShift;
6851 return 0;
6852 }
6853 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6854 if (isinstance == -1) {
6855 return 1;
6856 }
6857 if (isinstance) {
6858 *out = BitOr;
6859 return 0;
6860 }
6861 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6862 if (isinstance == -1) {
6863 return 1;
6864 }
6865 if (isinstance) {
6866 *out = BitXor;
6867 return 0;
6868 }
6869 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6870 if (isinstance == -1) {
6871 return 1;
6872 }
6873 if (isinstance) {
6874 *out = BitAnd;
6875 return 0;
6876 }
6877 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6878 if (isinstance == -1) {
6879 return 1;
6880 }
6881 if (isinstance) {
6882 *out = FloorDiv;
6883 return 0;
6884 }
6885
6886 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
6887 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006888}
6889
6890int
6891obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6892{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006893 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006894
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006895 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6896 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006897 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006898 }
6899 if (isinstance) {
6900 *out = Invert;
6901 return 0;
6902 }
6903 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6904 if (isinstance == -1) {
6905 return 1;
6906 }
6907 if (isinstance) {
6908 *out = Not;
6909 return 0;
6910 }
6911 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6912 if (isinstance == -1) {
6913 return 1;
6914 }
6915 if (isinstance) {
6916 *out = UAdd;
6917 return 0;
6918 }
6919 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6920 if (isinstance == -1) {
6921 return 1;
6922 }
6923 if (isinstance) {
6924 *out = USub;
6925 return 0;
6926 }
6927
6928 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
6929 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006930}
6931
6932int
6933obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6934{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006935 int isinstance;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006936
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006937 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6938 if (isinstance == -1) {
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006939 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02006940 }
6941 if (isinstance) {
6942 *out = Eq;
6943 return 0;
6944 }
6945 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6946 if (isinstance == -1) {
6947 return 1;
6948 }
6949 if (isinstance) {
6950 *out = NotEq;
6951 return 0;
6952 }
6953 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6954 if (isinstance == -1) {
6955 return 1;
6956 }
6957 if (isinstance) {
6958 *out = Lt;
6959 return 0;
6960 }
6961 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6962 if (isinstance == -1) {
6963 return 1;
6964 }
6965 if (isinstance) {
6966 *out = LtE;
6967 return 0;
6968 }
6969 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6970 if (isinstance == -1) {
6971 return 1;
6972 }
6973 if (isinstance) {
6974 *out = Gt;
6975 return 0;
6976 }
6977 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6978 if (isinstance == -1) {
6979 return 1;
6980 }
6981 if (isinstance) {
6982 *out = GtE;
6983 return 0;
6984 }
6985 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6986 if (isinstance == -1) {
6987 return 1;
6988 }
6989 if (isinstance) {
6990 *out = Is;
6991 return 0;
6992 }
6993 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6994 if (isinstance == -1) {
6995 return 1;
6996 }
6997 if (isinstance) {
6998 *out = IsNot;
6999 return 0;
7000 }
7001 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
7002 if (isinstance == -1) {
7003 return 1;
7004 }
7005 if (isinstance) {
7006 *out = In;
7007 return 0;
7008 }
7009 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
7010 if (isinstance == -1) {
7011 return 1;
7012 }
7013 if (isinstance) {
7014 *out = NotIn;
7015 return 0;
7016 }
7017
7018 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
7019 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007020}
7021
7022int
7023obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
7024{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007025 PyObject* tmp = NULL;
7026 expr_ty target;
7027 expr_ty iter;
7028 asdl_seq* ifs;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007029
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007030 if (_PyObject_HasAttrId(obj, &PyId_target)) {
7031 int res;
7032 tmp = _PyObject_GetAttrId(obj, &PyId_target);
7033 if (tmp == NULL) goto failed;
7034 res = obj2ast_expr(tmp, &target, arena);
7035 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007036 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007037 } else {
7038 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007039 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007040 }
7041 if (_PyObject_HasAttrId(obj, &PyId_iter)) {
7042 int res;
7043 tmp = _PyObject_GetAttrId(obj, &PyId_iter);
7044 if (tmp == NULL) goto failed;
7045 res = obj2ast_expr(tmp, &iter, arena);
7046 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007047 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007048 } else {
7049 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
7050 return 1;
7051 }
7052 if (_PyObject_HasAttrId(obj, &PyId_ifs)) {
7053 int res;
7054 Py_ssize_t len;
7055 Py_ssize_t i;
7056 tmp = _PyObject_GetAttrId(obj, &PyId_ifs);
7057 if (tmp == NULL) goto failed;
7058 if (!PyList_Check(tmp)) {
7059 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7060 goto failed;
7061 }
7062 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007063 ifs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007064 if (ifs == NULL) goto failed;
7065 for (i = 0; i < len; i++) {
7066 expr_ty value;
7067 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7068 if (res != 0) goto failed;
7069 asdl_seq_SET(ifs, i, value);
7070 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007071 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007072 } else {
7073 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
7074 return 1;
7075 }
7076 *out = comprehension(target, iter, ifs, arena);
7077 return 0;
7078failed:
7079 Py_XDECREF(tmp);
7080 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007081}
7082
7083int
7084obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
7085{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007086 int isinstance;
Neal Norwitzad74aa82008-03-31 05:14:30 +00007087
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007088 PyObject *tmp = NULL;
7089 int lineno;
7090 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007091
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007092 if (obj == Py_None) {
7093 *out = NULL;
7094 return 0;
7095 }
7096 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7097 int res;
7098 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7099 if (tmp == NULL) goto failed;
7100 res = obj2ast_int(tmp, &lineno, arena);
7101 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007102 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007103 } else {
7104 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007105 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007106 }
7107 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7108 int res;
7109 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7110 if (tmp == NULL) goto failed;
7111 res = obj2ast_int(tmp, &col_offset, arena);
7112 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007113 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007114 } else {
7115 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
7116 return 1;
7117 }
7118 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
7119 if (isinstance == -1) {
7120 return 1;
7121 }
7122 if (isinstance) {
7123 expr_ty type;
7124 identifier name;
7125 asdl_seq* body;
7126
Victor Stinneree4b59c2013-07-27 00:01:35 +02007127 if (exists_not_none(obj, &PyId_type)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007128 int res;
7129 tmp = _PyObject_GetAttrId(obj, &PyId_type);
7130 if (tmp == NULL) goto failed;
7131 res = obj2ast_expr(tmp, &type, arena);
7132 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007133 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007134 } else {
7135 type = NULL;
7136 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007137 if (exists_not_none(obj, &PyId_name)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007138 int res;
7139 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7140 if (tmp == NULL) goto failed;
7141 res = obj2ast_identifier(tmp, &name, arena);
7142 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007143 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007144 } else {
7145 name = NULL;
7146 }
7147 if (_PyObject_HasAttrId(obj, &PyId_body)) {
7148 int res;
7149 Py_ssize_t len;
7150 Py_ssize_t i;
7151 tmp = _PyObject_GetAttrId(obj, &PyId_body);
7152 if (tmp == NULL) goto failed;
7153 if (!PyList_Check(tmp)) {
7154 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7155 goto failed;
7156 }
7157 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007158 body = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007159 if (body == NULL) goto failed;
7160 for (i = 0; i < len; i++) {
7161 stmt_ty value;
7162 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
7163 if (res != 0) goto failed;
7164 asdl_seq_SET(body, i, value);
7165 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007166 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007167 } else {
7168 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
7169 return 1;
7170 }
7171 *out = ExceptHandler(type, name, body, lineno, col_offset, arena);
7172 if (*out == NULL) goto failed;
7173 return 0;
7174 }
7175
7176 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
7177 failed:
7178 Py_XDECREF(tmp);
7179 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007180}
7181
7182int
7183obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
7184{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007185 PyObject* tmp = NULL;
7186 asdl_seq* args;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007187 arg_ty vararg;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007188 asdl_seq* kwonlyargs;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007189 asdl_seq* kw_defaults;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007190 arg_ty kwarg;
7191 asdl_seq* defaults;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007192
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007193 if (_PyObject_HasAttrId(obj, &PyId_args)) {
7194 int res;
7195 Py_ssize_t len;
7196 Py_ssize_t i;
7197 tmp = _PyObject_GetAttrId(obj, &PyId_args);
7198 if (tmp == NULL) goto failed;
7199 if (!PyList_Check(tmp)) {
7200 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7201 goto failed;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007202 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007203 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007204 args = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007205 if (args == NULL) goto failed;
7206 for (i = 0; i < len; i++) {
7207 arg_ty value;
7208 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7209 if (res != 0) goto failed;
7210 asdl_seq_SET(args, i, value);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007211 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007212 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007213 } else {
7214 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007215 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007216 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007217 if (exists_not_none(obj, &PyId_vararg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007218 int res;
7219 tmp = _PyObject_GetAttrId(obj, &PyId_vararg);
7220 if (tmp == NULL) goto failed;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007221 res = obj2ast_arg(tmp, &vararg, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007222 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007223 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007224 } else {
7225 vararg = NULL;
7226 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007227 if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {
7228 int res;
7229 Py_ssize_t len;
7230 Py_ssize_t i;
7231 tmp = _PyObject_GetAttrId(obj, &PyId_kwonlyargs);
7232 if (tmp == NULL) goto failed;
7233 if (!PyList_Check(tmp)) {
7234 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7235 goto failed;
7236 }
7237 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007238 kwonlyargs = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007239 if (kwonlyargs == NULL) goto failed;
7240 for (i = 0; i < len; i++) {
7241 arg_ty value;
7242 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
7243 if (res != 0) goto failed;
7244 asdl_seq_SET(kwonlyargs, i, value);
7245 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007246 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007247 } else {
7248 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
7249 return 1;
7250 }
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007251 if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {
7252 int res;
7253 Py_ssize_t len;
7254 Py_ssize_t i;
7255 tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);
7256 if (tmp == NULL) goto failed;
7257 if (!PyList_Check(tmp)) {
7258 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7259 goto failed;
7260 }
7261 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007262 kw_defaults = _Py_asdl_seq_new(len, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007263 if (kw_defaults == NULL) goto failed;
7264 for (i = 0; i < len; i++) {
7265 expr_ty value;
7266 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7267 if (res != 0) goto failed;
7268 asdl_seq_SET(kw_defaults, i, value);
7269 }
Victor Stinner1acc1292013-07-27 00:03:47 +02007270 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007271 } else {
7272 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
7273 return 1;
7274 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007275 if (exists_not_none(obj, &PyId_kwarg)) {
7276 int res;
7277 tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);
7278 if (tmp == NULL) goto failed;
7279 res = obj2ast_arg(tmp, &kwarg, arena);
7280 if (res != 0) goto failed;
Victor Stinnerb3189902013-07-27 00:04:42 +02007281 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007282 } else {
7283 kwarg = NULL;
7284 }
7285 if (_PyObject_HasAttrId(obj, &PyId_defaults)) {
7286 int res;
7287 Py_ssize_t len;
7288 Py_ssize_t i;
7289 tmp = _PyObject_GetAttrId(obj, &PyId_defaults);
7290 if (tmp == NULL) goto failed;
7291 if (!PyList_Check(tmp)) {
7292 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
7293 goto failed;
7294 }
7295 len = PyList_GET_SIZE(tmp);
Antoine Pitroud01d396e2013-10-12 22:52:43 +02007296 defaults = _Py_asdl_seq_new(len, arena);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007297 if (defaults == NULL) goto failed;
7298 for (i = 0; i < len; i++) {
7299 expr_ty value;
7300 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
7301 if (res != 0) goto failed;
7302 asdl_seq_SET(defaults, i, value);
7303 }
Victor Stinnerb3189902013-07-27 00:04:42 +02007304 Py_CLEAR(tmp);
Victor Stinneree4b59c2013-07-27 00:01:35 +02007305 } else {
7306 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
7307 return 1;
7308 }
7309 *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults,
7310 arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007311 return 0;
7312failed:
7313 Py_XDECREF(tmp);
7314 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007315}
7316
7317int
7318obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
7319{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007320 PyObject* tmp = NULL;
7321 identifier arg;
7322 expr_ty annotation;
Victor Stinnerc106c682015-11-06 17:01:48 +01007323 int lineno;
7324 int col_offset;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007325
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007326 if (_PyObject_HasAttrId(obj, &PyId_arg)) {
7327 int res;
7328 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7329 if (tmp == NULL) goto failed;
7330 res = obj2ast_identifier(tmp, &arg, arena);
7331 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007332 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007333 } else {
7334 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007335 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007336 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007337 if (exists_not_none(obj, &PyId_annotation)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007338 int res;
7339 tmp = _PyObject_GetAttrId(obj, &PyId_annotation);
7340 if (tmp == NULL) goto failed;
7341 res = obj2ast_expr(tmp, &annotation, arena);
7342 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007343 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007344 } else {
7345 annotation = NULL;
7346 }
Victor Stinnerc106c682015-11-06 17:01:48 +01007347 if (_PyObject_HasAttrId(obj, &PyId_lineno)) {
7348 int res;
7349 tmp = _PyObject_GetAttrId(obj, &PyId_lineno);
7350 if (tmp == NULL) goto failed;
7351 res = obj2ast_int(tmp, &lineno, arena);
7352 if (res != 0) goto failed;
7353 Py_CLEAR(tmp);
7354 } else {
7355 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from arg");
7356 return 1;
7357 }
7358 if (_PyObject_HasAttrId(obj, &PyId_col_offset)) {
7359 int res;
7360 tmp = _PyObject_GetAttrId(obj, &PyId_col_offset);
7361 if (tmp == NULL) goto failed;
7362 res = obj2ast_int(tmp, &col_offset, arena);
7363 if (res != 0) goto failed;
7364 Py_CLEAR(tmp);
7365 } else {
7366 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from arg");
7367 return 1;
7368 }
7369 *out = arg(arg, annotation, lineno, col_offset, arena);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007370 return 0;
7371failed:
7372 Py_XDECREF(tmp);
7373 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007374}
7375
7376int
7377obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
7378{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007379 PyObject* tmp = NULL;
7380 identifier arg;
7381 expr_ty value;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007382
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007383 if (exists_not_none(obj, &PyId_arg)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007384 int res;
7385 tmp = _PyObject_GetAttrId(obj, &PyId_arg);
7386 if (tmp == NULL) goto failed;
7387 res = obj2ast_identifier(tmp, &arg, 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 {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04007391 arg = NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007392 }
7393 if (_PyObject_HasAttrId(obj, &PyId_value)) {
7394 int res;
7395 tmp = _PyObject_GetAttrId(obj, &PyId_value);
7396 if (tmp == NULL) goto failed;
7397 res = obj2ast_expr(tmp, &value, arena);
7398 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007399 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007400 } else {
7401 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
7402 return 1;
7403 }
7404 *out = keyword(arg, value, arena);
7405 return 0;
7406failed:
7407 Py_XDECREF(tmp);
7408 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007409}
7410
7411int
7412obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
7413{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007414 PyObject* tmp = NULL;
7415 identifier name;
7416 identifier asname;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007417
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007418 if (_PyObject_HasAttrId(obj, &PyId_name)) {
7419 int res;
7420 tmp = _PyObject_GetAttrId(obj, &PyId_name);
7421 if (tmp == NULL) goto failed;
7422 res = obj2ast_identifier(tmp, &name, arena);
7423 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007424 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007425 } else {
7426 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007427 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007428 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007429 if (exists_not_none(obj, &PyId_asname)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007430 int res;
7431 tmp = _PyObject_GetAttrId(obj, &PyId_asname);
7432 if (tmp == NULL) goto failed;
7433 res = obj2ast_identifier(tmp, &asname, arena);
7434 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007435 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007436 } else {
7437 asname = NULL;
7438 }
7439 *out = alias(name, asname, arena);
7440 return 0;
7441failed:
7442 Py_XDECREF(tmp);
7443 return 1;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007444}
7445
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007446int
7447obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)
7448{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007449 PyObject* tmp = NULL;
7450 expr_ty context_expr;
7451 expr_ty optional_vars;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007452
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007453 if (_PyObject_HasAttrId(obj, &PyId_context_expr)) {
7454 int res;
7455 tmp = _PyObject_GetAttrId(obj, &PyId_context_expr);
7456 if (tmp == NULL) goto failed;
7457 res = obj2ast_expr(tmp, &context_expr, arena);
7458 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007459 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007460 } else {
7461 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007462 return 1;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007463 }
Victor Stinneree4b59c2013-07-27 00:01:35 +02007464 if (exists_not_none(obj, &PyId_optional_vars)) {
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007465 int res;
7466 tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);
7467 if (tmp == NULL) goto failed;
7468 res = obj2ast_expr(tmp, &optional_vars, arena);
7469 if (res != 0) goto failed;
Victor Stinner1acc1292013-07-27 00:03:47 +02007470 Py_CLEAR(tmp);
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007471 } else {
7472 optional_vars = NULL;
7473 }
7474 *out = withitem(context_expr, optional_vars, arena);
7475 return 0;
7476failed:
7477 Py_XDECREF(tmp);
7478 return 1;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05007479}
7480
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007481
Martin v. Löwis1a214512008-06-11 05:26:20 +00007482static struct PyModuleDef _astmodule = {
7483 PyModuleDef_HEAD_INIT, "_ast"
7484};
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007485PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00007486PyInit__ast(void)
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007487{
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007488 PyObject *m, *d;
7489 if (!init_types()) return NULL;
7490 m = PyModule_Create(&_astmodule);
7491 if (!m) return NULL;
7492 d = PyModule_GetDict(m);
7493 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007494 if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007495 return NULL;
7496 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL;
7497 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return
7498 NULL;
7499 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type) <
7500 0) return NULL;
7501 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) < 0)
7502 return NULL;
7503 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return
7504 NULL;
7505 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return NULL;
7506 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type) <
7507 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007508 if (PyDict_SetItemString(d, "AsyncFunctionDef",
7509 (PyObject*)AsyncFunctionDef_type) < 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007510 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
7511 return NULL;
7512 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0) return
7513 NULL;
7514 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0) return
7515 NULL;
7516 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0) return
7517 NULL;
7518 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) < 0)
7519 return NULL;
7520 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007521 if (PyDict_SetItemString(d, "AsyncFor", (PyObject*)AsyncFor_type) < 0)
7522 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007523 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return
7524 NULL;
7525 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return NULL;
7526 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007527 if (PyDict_SetItemString(d, "AsyncWith", (PyObject*)AsyncWith_type) < 0)
7528 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007529 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return
7530 NULL;
7531 if (PyDict_SetItemString(d, "Try", (PyObject*)Try_type) < 0) return NULL;
7532 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0) return
7533 NULL;
7534 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0) return
7535 NULL;
7536 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) < 0)
7537 return NULL;
7538 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0) return
7539 NULL;
7540 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
7541 return NULL;
7542 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return NULL;
7543 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return NULL;
7544 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return
7545 NULL;
7546 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
7547 return NULL;
7548 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return NULL;
7549 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0) return
7550 NULL;
7551 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return
7552 NULL;
7553 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0) return
7554 NULL;
7555 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0) return
7556 NULL;
7557 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return
7558 NULL;
7559 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return NULL;
7560 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return NULL;
7561 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
7562 return NULL;
7563 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0) return
7564 NULL;
7565 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
7566 return NULL;
7567 if (PyDict_SetItemString(d, "GeneratorExp", (PyObject*)GeneratorExp_type) <
7568 0) return NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04007569 if (PyDict_SetItemString(d, "Await", (PyObject*)Await_type) < 0) return
7570 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007571 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return
7572 NULL;
7573 if (PyDict_SetItemString(d, "YieldFrom", (PyObject*)YieldFrom_type) < 0)
7574 return NULL;
7575 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0) return
7576 NULL;
7577 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL;
7578 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return NULL;
7579 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return NULL;
Eric V. Smith235a6f02015-09-19 14:51:32 -04007580 if (PyDict_SetItemString(d, "FormattedValue",
7581 (PyObject*)FormattedValue_type) < 0) return NULL;
7582 if (PyDict_SetItemString(d, "JoinedStr", (PyObject*)JoinedStr_type) < 0)
7583 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007584 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return
7585 NULL;
Victor Stinneree4b59c2013-07-27 00:01:35 +02007586 if (PyDict_SetItemString(d, "NameConstant", (PyObject*)NameConstant_type) <
7587 0) return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007588 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
7589 return NULL;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01007590 if (PyDict_SetItemString(d, "Constant", (PyObject*)Constant_type) < 0)
7591 return NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007592 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0)
7593 return NULL;
7594 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < 0)
7595 return NULL;
7596 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0) return
7597 NULL;
7598 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return NULL;
7599 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return NULL;
7600 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return
7601 NULL;
7602 if (PyDict_SetItemString(d, "expr_context", (PyObject*)expr_context_type) <
7603 0) return NULL;
7604 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return NULL;
7605 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return
7606 NULL;
7607 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return NULL;
7608 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0) return
7609 NULL;
7610 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
7611 return NULL;
7612 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return
7613 NULL;
7614 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return
7615 NULL;
7616 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return
7617 NULL;
7618 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
7619 return NULL;
7620 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return
7621 NULL;
7622 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0) return
7623 NULL;
7624 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return NULL;
7625 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return NULL;
7626 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
7627 return NULL;
7628 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return NULL;
7629 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return NULL;
7630 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return NULL;
Benjamin Petersond51374e2014-04-09 23:55:56 -04007631 if (PyDict_SetItemString(d, "MatMult", (PyObject*)MatMult_type) < 0) return
7632 NULL;
Victor Stinnerce72e1c2013-07-27 00:00:36 +02007633 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return NULL;
7634 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return NULL;
7635 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return NULL;
7636 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0) return
7637 NULL;
7638 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0) return
7639 NULL;
7640 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return
7641 NULL;
7642 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0) return
7643 NULL;
7644 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0) return
7645 NULL;
7646 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
7647 return NULL;
7648 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0) return
7649 NULL;
7650 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0) return
7651 NULL;
7652 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return NULL;
7653 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return NULL;
7654 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return NULL;
7655 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return
7656 NULL;
7657 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return NULL;
7658 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return
7659 NULL;
7660 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return NULL;
7661 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return NULL;
7662 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return NULL;
7663 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return NULL;
7664 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return NULL;
7665 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return
7666 NULL;
7667 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return NULL;
7668 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return
7669 NULL;
7670 if (PyDict_SetItemString(d, "comprehension", (PyObject*)comprehension_type)
7671 < 0) return NULL;
7672 if (PyDict_SetItemString(d, "excepthandler", (PyObject*)excepthandler_type)
7673 < 0) return NULL;
7674 if (PyDict_SetItemString(d, "ExceptHandler", (PyObject*)ExceptHandler_type)
7675 < 0) return NULL;
7676 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) < 0)
7677 return NULL;
7678 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return NULL;
7679 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0) return
7680 NULL;
7681 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return
7682 NULL;
7683 if (PyDict_SetItemString(d, "withitem", (PyObject*)withitem_type) < 0)
7684 return NULL;
7685 return m;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00007686}
7687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007688
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007689PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007690{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007691 if (!init_types())
7692 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00007693 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00007694}
Martin v. Löwis5b222132007-06-10 09:51:05 +00007695
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007696/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
7697mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007698{
7699 mod_ty res;
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007700 PyObject *req_type[3];
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007701 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007702 int isinstance;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007703
Benjamin Petersonc2f665e2014-02-10 22:19:02 -05007704 req_type[0] = (PyObject*)Module_type;
7705 req_type[1] = (PyObject*)Expression_type;
7706 req_type[2] = (PyObject*)Interactive_type;
Benjamin Peterson42ec0312014-02-10 22:41:40 -05007707
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007708 assert(0 <= mode && mode <= 2);
7709
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007710 if (!init_types())
7711 return NULL;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007712
Benjamin Peterson0496c9e2009-12-13 01:24:58 +00007713 isinstance = PyObject_IsInstance(ast, req_type[mode]);
7714 if (isinstance == -1)
7715 return NULL;
7716 if (!isinstance) {
Neal Norwitzdb4115f2008-03-31 04:20:05 +00007717 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
7718 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007719 return NULL;
7720 }
7721 if (obj2ast_mod(ast, &res, arena) != 0)
7722 return NULL;
7723 else
7724 return res;
7725}
7726
7727int PyAST_Check(PyObject* obj)
7728{
Victor Stinnerbdf630c2013-07-17 00:17:15 +02007729 if (!init_types())
7730 return -1;
Neal Norwitz207c9f32008-03-31 04:42:11 +00007731 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00007732}
7733
Martin v. Löwis5b222132007-06-10 09:51:05 +00007734