blob: e89c9d57e8c1a92ca7f9eee6ea68bf2368f1c2bd [file] [log] [blame]
Thomas Wouterscf297e42007-02-23 15:07:44 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Christian Heimesd3eb5a152008-02-24 00:38:49 +00005 __version__ 57783.
Thomas Wouterscf297e42007-02-23 15:07:44 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Neal Norwitz53d960c2006-02-28 22:47:29 +000015static PyTypeObject* AST_type;
16static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000045 "decorator_list",
Neal Norwitzc1505362006-12-28 06:47:50 +000046 "returns",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000047};
Neal Norwitz53d960c2006-02-28 22:47:29 +000048static PyTypeObject *ClassDef_type;
49static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000050 "name",
51 "bases",
Guido van Rossum52cc1d82007-03-18 15:41:51 +000052 "keywords",
53 "starargs",
54 "kwargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000055 "body",
Guido van Rossumd59da4b2007-05-22 18:11:13 +000056 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000057};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Return_type;
59static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "value",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Delete_type;
63static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65};
Neal Norwitz53d960c2006-02-28 22:47:29 +000066static PyTypeObject *Assign_type;
67static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000068 "targets",
69 "value",
70};
Neal Norwitz53d960c2006-02-28 22:47:29 +000071static PyTypeObject *AugAssign_type;
72static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000073 "target",
74 "op",
75 "value",
76};
Neal Norwitz53d960c2006-02-28 22:47:29 +000077static PyTypeObject *For_type;
78static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000079 "target",
80 "iter",
81 "body",
82 "orelse",
83};
Neal Norwitz53d960c2006-02-28 22:47:29 +000084static PyTypeObject *While_type;
85static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000086 "test",
87 "body",
88 "orelse",
89};
Neal Norwitz53d960c2006-02-28 22:47:29 +000090static PyTypeObject *If_type;
91static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000092 "test",
93 "body",
94 "orelse",
95};
Neal Norwitz53d960c2006-02-28 22:47:29 +000096static PyTypeObject *With_type;
97static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +000098 "context_expr",
99 "optional_vars",
100 "body",
101};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000102static PyTypeObject *Raise_type;
103static char *Raise_fields[]={
Collin Winter828f04a2007-08-31 00:04:24 +0000104 "exc",
105 "cause",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000107static PyTypeObject *TryExcept_type;
108static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000109 "body",
110 "handlers",
111 "orelse",
112};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000113static PyTypeObject *TryFinally_type;
114static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000115 "body",
116 "finalbody",
117};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000118static PyTypeObject *Assert_type;
119static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000120 "test",
121 "msg",
122};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000123static PyTypeObject *Import_type;
124static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000125 "names",
126};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000127static PyTypeObject *ImportFrom_type;
128static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000129 "module",
130 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000131 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000133static PyTypeObject *Global_type;
134static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135 "names",
136};
Jeremy Hylton81e95022007-02-27 06:50:52 +0000137static PyTypeObject *Nonlocal_type;
138static char *Nonlocal_fields[]={
139 "names",
140};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000141static PyTypeObject *Expr_type;
142static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000143 "value",
144};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000145static PyTypeObject *Pass_type;
146static PyTypeObject *Break_type;
147static PyTypeObject *Continue_type;
148static PyTypeObject *expr_type;
149static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000150 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000151 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000152};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000153static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000154static PyTypeObject *BoolOp_type;
155static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000156 "op",
157 "values",
158};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BinOp_type;
160static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "left",
162 "op",
163 "right",
164};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000165static PyTypeObject *UnaryOp_type;
166static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000167 "op",
168 "operand",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *Lambda_type;
171static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "args",
173 "body",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *IfExp_type;
176static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000177 "test",
178 "body",
179 "orelse",
180};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000181static PyTypeObject *Dict_type;
182static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000183 "keys",
184 "values",
185};
Guido van Rossum86e58e22006-08-28 15:27:34 +0000186static PyTypeObject *Set_type;
187static char *Set_fields[]={
188 "elts",
189};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000190static PyTypeObject *ListComp_type;
191static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000192 "elt",
193 "generators",
194};
Nick Coghlan650f0d02007-04-15 12:05:43 +0000195static PyTypeObject *SetComp_type;
196static char *SetComp_fields[]={
197 "elt",
198 "generators",
199};
Guido van Rossum992d4a32007-07-11 13:09:30 +0000200static PyTypeObject *DictComp_type;
201static char *DictComp_fields[]={
202 "key",
203 "value",
204 "generators",
205};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000206static PyTypeObject *GeneratorExp_type;
207static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000208 "elt",
209 "generators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Yield_type;
212static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "value",
214};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static PyTypeObject *Compare_type;
216static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217 "left",
218 "ops",
219 "comparators",
220};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000221static PyTypeObject *Call_type;
222static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000223 "func",
224 "args",
225 "keywords",
226 "starargs",
227 "kwargs",
228};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000229static PyTypeObject *Num_type;
230static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000231 "n",
232};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000233static PyTypeObject *Str_type;
234static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000235 "s",
236};
Thomas Wouters00e41de2007-02-23 19:56:57 +0000237static PyTypeObject *Bytes_type;
238static char *Bytes_fields[]={
239 "s",
240};
Georg Brandl52318d62006-09-06 07:06:08 +0000241static PyTypeObject *Ellipsis_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +0000242static PyTypeObject *Attribute_type;
243static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000244 "value",
245 "attr",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *Subscript_type;
249static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "value",
251 "slice",
252 "ctx",
253};
Guido van Rossum0368b722007-05-11 16:50:42 +0000254static PyTypeObject *Starred_type;
255static char *Starred_fields[]={
256 "value",
257 "ctx",
258};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000259static PyTypeObject *Name_type;
260static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000261 "id",
262 "ctx",
263};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000264static PyTypeObject *List_type;
265static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000266 "elts",
267 "ctx",
268};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000269static PyTypeObject *Tuple_type;
270static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000271 "elts",
272 "ctx",
273};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000274static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000275static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
276*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
277static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000278static PyTypeObject *Load_type;
279static PyTypeObject *Store_type;
280static PyTypeObject *Del_type;
281static PyTypeObject *AugLoad_type;
282static PyTypeObject *AugStore_type;
283static PyTypeObject *Param_type;
284static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000285static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000286static PyTypeObject *Slice_type;
287static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000288 "lower",
289 "upper",
290 "step",
291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *ExtSlice_type;
293static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000294 "dims",
295};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Index_type;
297static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298 "value",
299};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301static PyObject *And_singleton, *Or_singleton;
302static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000303static PyTypeObject *And_type;
304static PyTypeObject *Or_type;
305static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000306static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
307*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
308*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
309*FloorDiv_singleton;
310static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000311static PyTypeObject *Add_type;
312static PyTypeObject *Sub_type;
313static PyTypeObject *Mult_type;
314static PyTypeObject *Div_type;
315static PyTypeObject *Mod_type;
316static PyTypeObject *Pow_type;
317static PyTypeObject *LShift_type;
318static PyTypeObject *RShift_type;
319static PyTypeObject *BitOr_type;
320static PyTypeObject *BitXor_type;
321static PyTypeObject *BitAnd_type;
322static PyTypeObject *FloorDiv_type;
323static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000324static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
325*USub_singleton;
326static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Invert_type;
328static PyTypeObject *Not_type;
329static PyTypeObject *UAdd_type;
330static PyTypeObject *USub_type;
331static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
333*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
334*NotIn_singleton;
335static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000336static PyTypeObject *Eq_type;
337static PyTypeObject *NotEq_type;
338static PyTypeObject *Lt_type;
339static PyTypeObject *LtE_type;
340static PyTypeObject *Gt_type;
341static PyTypeObject *GtE_type;
342static PyTypeObject *Is_type;
343static PyTypeObject *IsNot_type;
344static PyTypeObject *In_type;
345static PyTypeObject *NotIn_type;
346static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000349 "target",
350 "iter",
351 "ifs",
352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *excepthandler_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000354static PyObject* ast2obj_excepthandler(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000355static char *excepthandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000356 "type",
357 "name",
358 "body",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 "lineno",
360 "col_offset",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000362static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000363static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365 "args",
366 "vararg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000367 "varargannotation",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000368 "kwonlyargs",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369 "kwarg",
Neal Norwitzc1505362006-12-28 06:47:50 +0000370 "kwargannotation",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000371 "defaults",
Guido van Rossum4f72a782006-10-27 23:31:49 +0000372 "kw_defaults",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000373};
Neal Norwitzc1505362006-12-28 06:47:50 +0000374static PyTypeObject *arg_type;
375static PyObject* ast2obj_arg(void*);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000376static char *arg_fields[]={
Neal Norwitzc1505362006-12-28 06:47:50 +0000377 "arg",
378 "annotation",
379};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000380static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000381static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000382static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000383 "arg",
384 "value",
385};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000386static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000387static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000388static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000389 "name",
390 "asname",
391};
392
393
394static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
395{
396 PyObject *fnames, *result;
397 int i;
398 if (num_fields) {
399 fnames = PyTuple_New(num_fields);
400 if (!fnames) return NULL;
401 } else {
402 fnames = Py_None;
403 Py_INCREF(Py_None);
404 }
405 for(i=0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000406 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000407 if (!field) {
408 Py_DECREF(fnames);
409 return NULL;
410 }
411 PyTuple_SET_ITEM(fnames, i, field);
412 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000413 result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000414 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000415 Py_DECREF(fnames);
416 return (PyTypeObject*)result;
417}
418
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000419static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
420{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000421 int i, result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000422 PyObject *s, *l = PyList_New(num_fields);
423 if (!l) return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 for(i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000425 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000426 if (!s) {
427 Py_DECREF(l);
428 return 0;
429 }
430 PyList_SET_ITEM(l, i, s);
431 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
433 Py_DECREF(l);
434 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000435}
436
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000437/* Conversion AST -> Python */
438
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000439static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
440{
441 int i, n = asdl_seq_LEN(seq);
442 PyObject *result = PyList_New(n);
443 PyObject *value;
444 if (!result)
445 return NULL;
446 for (i = 0; i < n; i++) {
447 value = func(asdl_seq_GET(seq, i));
448 if (!value) {
449 Py_DECREF(result);
450 return NULL;
451 }
452 PyList_SET_ITEM(result, i, value);
453 }
454 return result;
455}
456
457static PyObject* ast2obj_object(void *o)
458{
459 if (!o)
460 o = Py_None;
461 Py_INCREF((PyObject*)o);
462 return (PyObject*)o;
463}
464#define ast2obj_identifier ast2obj_object
465#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000466
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000467static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000468{
Christian Heimes217cfd12007-12-02 14:31:20 +0000469 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000470}
471
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000472/* Conversion Python -> AST */
473
474static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
475{
476 if (obj == Py_None)
477 obj = NULL;
478 if (obj)
479 PyArena_AddPyObject(arena, obj);
480 Py_XINCREF(obj);
481 *out = obj;
482 return 0;
483}
484
485#define obj2ast_identifier obj2ast_object
486#define obj2ast_string obj2ast_object
487
488static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
489{
490 int i;
491 if (!PyLong_Check(obj)) {
492 PyObject *s = PyObject_Repr(obj);
493 if (s == NULL) return 1;
494 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
495 PyString_AS_STRING(s));
496 Py_DECREF(s);
497 return 1;
498 }
499
500 i = (int)PyLong_AsLong(obj);
501 if (i == -1 && PyErr_Occurred())
502 return 1;
503 *out = i;
504 return 0;
505}
506
507
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000508static int init_types(void)
509{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000510 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000511 if (initialized) return 1;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000512 AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);
513 mod_type = make_type("mod", AST_type, NULL, 0);
514 if (!mod_type) return 0;
515 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000516 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000517 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000518 Interactive_type = make_type("Interactive", mod_type,
519 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000520 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000521 Expression_type = make_type("Expression", mod_type, Expression_fields,
522 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000523 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000524 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000525 if (!Suite_type) return 0;
526 stmt_type = make_type("stmt", AST_type, NULL, 0);
527 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000528 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000529 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000530 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000531 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000532 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000533 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000534 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000535 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000536 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000537 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000538 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000539 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000540 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000541 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000542 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000543 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000544 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000545 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000546 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000547 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000548 With_type = make_type("With", stmt_type, With_fields, 3);
549 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000550 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000551 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000552 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000553 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000554 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
555 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000556 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000557 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000558 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000559 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000560 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000561 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000562 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000563 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000564 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000565 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000566 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
567 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000568 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000569 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000570 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000571 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000572 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000573 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000574 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000575 if (!Continue_type) return 0;
576 expr_type = make_type("expr", AST_type, NULL, 0);
577 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000578 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000579 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000580 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000581 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000582 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000583 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000584 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000585 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000586 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000587 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000588 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000589 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000590 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000591 Set_type = make_type("Set", expr_type, Set_fields, 1);
592 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000593 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000594 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000595 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
596 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000597 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
598 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000599 GeneratorExp_type = make_type("GeneratorExp", expr_type,
600 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000601 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000602 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000603 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000604 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000605 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000606 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000607 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000608 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000609 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000610 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000611 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000612 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
613 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000614 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
615 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000616 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000617 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000618 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000619 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000620 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
621 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000622 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000623 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000624 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000625 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000626 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000627 if (!Tuple_type) return 0;
628 expr_context_type = make_type("expr_context", AST_type, NULL, 0);
629 if (!expr_context_type) return 0;
630 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000631 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000632 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000633 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000634 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000635 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000636 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000637 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000639 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000640 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000641 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000642 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000643 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000644 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000645 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000646 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000647 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000649 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000651 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!Param_singleton) return 0;
655 slice_type = make_type("slice", AST_type, NULL, 0);
656 if (!slice_type) return 0;
657 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000662 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Index_type) return 0;
664 boolop_type = make_type("boolop", AST_type, NULL, 0);
665 if (!boolop_type) return 0;
666 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000667 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000671 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000674 if (!Or_singleton) return 0;
675 operator_type = make_type("operator", AST_type, NULL, 0);
676 if (!operator_type) return 0;
677 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000678 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000682 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000686 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000690 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000694 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000698 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000702 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000706 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000710 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000714 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000716 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000718 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000722 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!FloorDiv_singleton) return 0;
726 unaryop_type = make_type("unaryop", AST_type, NULL, 0);
727 if (!unaryop_type) return 0;
728 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000729 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000733 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000737 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000741 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000743 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!USub_singleton) return 0;
745 cmpop_type = make_type("cmpop", AST_type, NULL, 0);
746 if (!cmpop_type) return 0;
747 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000748 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000752 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000754 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000756 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000760 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000764 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000768 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000770 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000772 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000776 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000778 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000780 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!NotIn_singleton) return 0;
788 comprehension_type = make_type("comprehension", AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000789 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!comprehension_type) return 0;
791 excepthandler_type = make_type("excepthandler", AST_type,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000792 excepthandler_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!excepthandler_type) return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +0000794 arguments_type = make_type("arguments", AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!arguments_type) return 0;
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000796 arg_type = make_type("arg", AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000797 if (!arg_type) return 0;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 keyword_type = make_type("keyword", AST_type, keyword_fields, 2);
799 if (!keyword_type) return 0;
800 alias_type = make_type("alias", AST_type, alias_fields, 2);
801 if (!alias_type) return 0;
802 initialized = 1;
803 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000805
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000806static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
807static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
808static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
809static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
810 arena);
811static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
812static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
813static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
814static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
815static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
816static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
817 arena);
818static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
819 arena);
820static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
821static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
822static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
823static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
824
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827{
828 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000829 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000830 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 p->kind = Module_kind;
833 p->v.Module.body = body;
834 return p;
835}
836
837mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000838Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839{
840 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000841 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000842 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 p->kind = Interactive_kind;
845 p->v.Interactive.body = body;
846 return p;
847}
848
849mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000850Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851{
852 mod_ty p;
853 if (!body) {
854 PyErr_SetString(PyExc_ValueError,
855 "field body is required for Expression");
856 return NULL;
857 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000858 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000859 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 p->kind = Expression_kind;
862 p->v.Expression.body = body;
863 return p;
864}
865
866mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000867Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868{
869 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000870 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000871 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 p->kind = Suite_kind;
874 p->v.Suite.body = body;
875 return p;
876}
877
878stmt_ty
879FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000880 decorator_list, expr_ty returns, int lineno, int col_offset,
881 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882{
883 stmt_ty p;
884 if (!name) {
885 PyErr_SetString(PyExc_ValueError,
886 "field name is required for FunctionDef");
887 return NULL;
888 }
889 if (!args) {
890 PyErr_SetString(PyExc_ValueError,
891 "field args is required for FunctionDef");
892 return NULL;
893 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000894 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000895 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897 p->kind = FunctionDef_kind;
898 p->v.FunctionDef.name = name;
899 p->v.FunctionDef.args = args;
900 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000901 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +0000902 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000904 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 return p;
906}
907
908stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000909ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000910 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
911 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912{
913 stmt_ty p;
914 if (!name) {
915 PyErr_SetString(PyExc_ValueError,
916 "field name is required for ClassDef");
917 return NULL;
918 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000919 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000920 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 p->kind = ClassDef_kind;
923 p->v.ClassDef.name = name;
924 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000925 p->v.ClassDef.keywords = keywords;
926 p->v.ClassDef.starargs = starargs;
927 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000929 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000931 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 return p;
933}
934
935stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000936Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937{
938 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000939 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000940 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 p->kind = Return_kind;
943 p->v.Return.value = value;
944 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000945 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 return p;
947}
948
949stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000950Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
952 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000953 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000954 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 p->kind = Delete_kind;
957 p->v.Delete.targets = targets;
958 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000959 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 return p;
961}
962
963stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000964Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
965 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 stmt_ty p;
968 if (!value) {
969 PyErr_SetString(PyExc_ValueError,
970 "field value is required for Assign");
971 return NULL;
972 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000974 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 p->kind = Assign_kind;
977 p->v.Assign.targets = targets;
978 p->v.Assign.value = value;
979 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000980 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 return p;
982}
983
984stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000985AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
986 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987{
988 stmt_ty p;
989 if (!target) {
990 PyErr_SetString(PyExc_ValueError,
991 "field target is required for AugAssign");
992 return NULL;
993 }
994 if (!op) {
995 PyErr_SetString(PyExc_ValueError,
996 "field op is required for AugAssign");
997 return NULL;
998 }
999 if (!value) {
1000 PyErr_SetString(PyExc_ValueError,
1001 "field value is required for AugAssign");
1002 return NULL;
1003 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001004 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001005 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 p->kind = AugAssign_kind;
1008 p->v.AugAssign.target = target;
1009 p->v.AugAssign.op = op;
1010 p->v.AugAssign.value = value;
1011 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001012 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return p;
1014}
1015
1016stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001018 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019{
1020 stmt_ty p;
1021 if (!target) {
1022 PyErr_SetString(PyExc_ValueError,
1023 "field target is required for For");
1024 return NULL;
1025 }
1026 if (!iter) {
1027 PyErr_SetString(PyExc_ValueError,
1028 "field iter is required for For");
1029 return NULL;
1030 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001031 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001032 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 p->kind = For_kind;
1035 p->v.For.target = target;
1036 p->v.For.iter = iter;
1037 p->v.For.body = body;
1038 p->v.For.orelse = orelse;
1039 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001040 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 return p;
1042}
1043
1044stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001045While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1046 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047{
1048 stmt_ty p;
1049 if (!test) {
1050 PyErr_SetString(PyExc_ValueError,
1051 "field test is required for While");
1052 return NULL;
1053 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001054 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001055 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 p->kind = While_kind;
1058 p->v.While.test = test;
1059 p->v.While.body = body;
1060 p->v.While.orelse = orelse;
1061 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001062 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 return p;
1064}
1065
1066stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001067If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1068 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069{
1070 stmt_ty p;
1071 if (!test) {
1072 PyErr_SetString(PyExc_ValueError,
1073 "field test is required for If");
1074 return NULL;
1075 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001076 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001077 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 p->kind = If_kind;
1080 p->v.If.test = test;
1081 p->v.If.body = body;
1082 p->v.If.orelse = orelse;
1083 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001084 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 return p;
1086}
1087
1088stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001089With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001090 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001091{
1092 stmt_ty p;
1093 if (!context_expr) {
1094 PyErr_SetString(PyExc_ValueError,
1095 "field context_expr is required for With");
1096 return NULL;
1097 }
1098 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001099 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001100 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001101 p->kind = With_kind;
1102 p->v.With.context_expr = context_expr;
1103 p->v.With.optional_vars = optional_vars;
1104 p->v.With.body = body;
1105 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001107 return p;
1108}
1109
1110stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001111Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112{
1113 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001114 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001115 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001118 p->v.Raise.exc = exc;
1119 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001121 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return p;
1123}
1124
1125stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001126TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001127 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128{
1129 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001130 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001131 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133 p->kind = TryExcept_kind;
1134 p->v.TryExcept.body = body;
1135 p->v.TryExcept.handlers = handlers;
1136 p->v.TryExcept.orelse = orelse;
1137 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001138 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return p;
1140}
1141
1142stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001143TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1144 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145{
1146 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001147 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001148 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 p->kind = TryFinally_kind;
1151 p->v.TryFinally.body = body;
1152 p->v.TryFinally.finalbody = finalbody;
1153 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001154 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 return p;
1156}
1157
1158stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001159Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160{
1161 stmt_ty p;
1162 if (!test) {
1163 PyErr_SetString(PyExc_ValueError,
1164 "field test is required for Assert");
1165 return NULL;
1166 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001167 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001168 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 p->kind = Assert_kind;
1171 p->v.Assert.test = test;
1172 p->v.Assert.msg = msg;
1173 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 return p;
1176}
1177
1178stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001179Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180{
1181 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001182 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001183 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 p->kind = Import_kind;
1186 p->v.Import.names = names;
1187 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001188 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return p;
1190}
1191
1192stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001193ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1194 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195{
1196 stmt_ty p;
1197 if (!module) {
1198 PyErr_SetString(PyExc_ValueError,
1199 "field module is required for ImportFrom");
1200 return NULL;
1201 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001202 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001203 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 p->kind = ImportFrom_kind;
1206 p->v.ImportFrom.module = module;
1207 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001208 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001210 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 return p;
1212}
1213
1214stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001215Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216{
1217 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001218 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001219 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 p->kind = Global_kind;
1222 p->v.Global.names = names;
1223 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001224 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return p;
1226}
1227
1228stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001229Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1230{
1231 stmt_ty p;
1232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1233 if (!p)
1234 return NULL;
1235 p->kind = Nonlocal_kind;
1236 p->v.Nonlocal.names = names;
1237 p->lineno = lineno;
1238 p->col_offset = col_offset;
1239 return p;
1240}
1241
1242stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001243Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244{
1245 stmt_ty p;
1246 if (!value) {
1247 PyErr_SetString(PyExc_ValueError,
1248 "field value is required for Expr");
1249 return NULL;
1250 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001251 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001252 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 p->kind = Expr_kind;
1255 p->v.Expr.value = value;
1256 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 return p;
1259}
1260
1261stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001262Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263{
1264 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001266 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 p->kind = Pass_kind;
1269 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001270 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 return p;
1272}
1273
1274stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001275Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276{
1277 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001279 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 p->kind = Break_kind;
1282 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001283 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 return p;
1285}
1286
1287stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001288Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289{
1290 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001291 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001292 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 p->kind = Continue_kind;
1295 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001296 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 return p;
1298}
1299
1300expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001301BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1302 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303{
1304 expr_ty p;
1305 if (!op) {
1306 PyErr_SetString(PyExc_ValueError,
1307 "field op is required for BoolOp");
1308 return NULL;
1309 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001310 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001311 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 p->kind = BoolOp_kind;
1314 p->v.BoolOp.op = op;
1315 p->v.BoolOp.values = values;
1316 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001317 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return p;
1319}
1320
1321expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001322BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1323 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324{
1325 expr_ty p;
1326 if (!left) {
1327 PyErr_SetString(PyExc_ValueError,
1328 "field left is required for BinOp");
1329 return NULL;
1330 }
1331 if (!op) {
1332 PyErr_SetString(PyExc_ValueError,
1333 "field op is required for BinOp");
1334 return NULL;
1335 }
1336 if (!right) {
1337 PyErr_SetString(PyExc_ValueError,
1338 "field right is required for BinOp");
1339 return NULL;
1340 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001341 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001342 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 p->kind = BinOp_kind;
1345 p->v.BinOp.left = left;
1346 p->v.BinOp.op = op;
1347 p->v.BinOp.right = right;
1348 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001349 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return p;
1351}
1352
1353expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001354UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1355 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
1357 expr_ty p;
1358 if (!op) {
1359 PyErr_SetString(PyExc_ValueError,
1360 "field op is required for UnaryOp");
1361 return NULL;
1362 }
1363 if (!operand) {
1364 PyErr_SetString(PyExc_ValueError,
1365 "field operand is required for UnaryOp");
1366 return NULL;
1367 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001368 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001369 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 p->kind = UnaryOp_kind;
1372 p->v.UnaryOp.op = op;
1373 p->v.UnaryOp.operand = operand;
1374 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001375 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 return p;
1377}
1378
1379expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001380Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1381 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382{
1383 expr_ty p;
1384 if (!args) {
1385 PyErr_SetString(PyExc_ValueError,
1386 "field args is required for Lambda");
1387 return NULL;
1388 }
1389 if (!body) {
1390 PyErr_SetString(PyExc_ValueError,
1391 "field body is required for Lambda");
1392 return NULL;
1393 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001394 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001395 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 p->kind = Lambda_kind;
1398 p->v.Lambda.args = args;
1399 p->v.Lambda.body = body;
1400 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001401 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return p;
1403}
1404
1405expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001406IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1407 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001408{
1409 expr_ty p;
1410 if (!test) {
1411 PyErr_SetString(PyExc_ValueError,
1412 "field test is required for IfExp");
1413 return NULL;
1414 }
1415 if (!body) {
1416 PyErr_SetString(PyExc_ValueError,
1417 "field body is required for IfExp");
1418 return NULL;
1419 }
1420 if (!orelse) {
1421 PyErr_SetString(PyExc_ValueError,
1422 "field orelse is required for IfExp");
1423 return NULL;
1424 }
1425 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001426 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001427 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001428 p->kind = IfExp_kind;
1429 p->v.IfExp.test = test;
1430 p->v.IfExp.body = body;
1431 p->v.IfExp.orelse = orelse;
1432 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001433 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001434 return p;
1435}
1436
1437expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001438Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1439 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440{
1441 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001443 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 p->kind = Dict_kind;
1446 p->v.Dict.keys = keys;
1447 p->v.Dict.values = values;
1448 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001449 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return p;
1451}
1452
1453expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001454Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1455{
1456 expr_ty p;
1457 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001458 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001459 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001460 p->kind = Set_kind;
1461 p->v.Set.elts = elts;
1462 p->lineno = lineno;
1463 p->col_offset = col_offset;
1464 return p;
1465}
1466
1467expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1469 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470{
1471 expr_ty p;
1472 if (!elt) {
1473 PyErr_SetString(PyExc_ValueError,
1474 "field elt is required for ListComp");
1475 return NULL;
1476 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001477 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001478 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 p->kind = ListComp_kind;
1481 p->v.ListComp.elt = elt;
1482 p->v.ListComp.generators = generators;
1483 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001484 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 return p;
1486}
1487
1488expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001489SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1490 *arena)
1491{
1492 expr_ty p;
1493 if (!elt) {
1494 PyErr_SetString(PyExc_ValueError,
1495 "field elt is required for SetComp");
1496 return NULL;
1497 }
1498 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1499 if (!p)
1500 return NULL;
1501 p->kind = SetComp_kind;
1502 p->v.SetComp.elt = elt;
1503 p->v.SetComp.generators = generators;
1504 p->lineno = lineno;
1505 p->col_offset = col_offset;
1506 return p;
1507}
1508
1509expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001510DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1511 col_offset, PyArena *arena)
1512{
1513 expr_ty p;
1514 if (!key) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field key is required for DictComp");
1517 return NULL;
1518 }
1519 if (!value) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field value is required for DictComp");
1522 return NULL;
1523 }
1524 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1525 if (!p)
1526 return NULL;
1527 p->kind = DictComp_kind;
1528 p->v.DictComp.key = key;
1529 p->v.DictComp.value = value;
1530 p->v.DictComp.generators = generators;
1531 p->lineno = lineno;
1532 p->col_offset = col_offset;
1533 return p;
1534}
1535
1536expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001537GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1538 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
1540 expr_ty p;
1541 if (!elt) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "field elt is required for GeneratorExp");
1544 return NULL;
1545 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001546 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001547 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 p->kind = GeneratorExp_kind;
1550 p->v.GeneratorExp.elt = elt;
1551 p->v.GeneratorExp.generators = generators;
1552 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 return p;
1555}
1556
1557expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559{
1560 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001561 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001562 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 p->kind = Yield_kind;
1565 p->v.Yield.value = value;
1566 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001567 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568 return p;
1569}
1570
1571expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001572Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1573 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574{
1575 expr_ty p;
1576 if (!left) {
1577 PyErr_SetString(PyExc_ValueError,
1578 "field left is required for Compare");
1579 return NULL;
1580 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001581 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001582 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 p->kind = Compare_kind;
1585 p->v.Compare.left = left;
1586 p->v.Compare.ops = ops;
1587 p->v.Compare.comparators = comparators;
1588 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001589 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 return p;
1591}
1592
1593expr_ty
1594Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001595 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596{
1597 expr_ty p;
1598 if (!func) {
1599 PyErr_SetString(PyExc_ValueError,
1600 "field func is required for Call");
1601 return NULL;
1602 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001603 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001604 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 p->kind = Call_kind;
1607 p->v.Call.func = func;
1608 p->v.Call.args = args;
1609 p->v.Call.keywords = keywords;
1610 p->v.Call.starargs = starargs;
1611 p->v.Call.kwargs = kwargs;
1612 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001613 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 return p;
1615}
1616
1617expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001618Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619{
1620 expr_ty p;
1621 if (!n) {
1622 PyErr_SetString(PyExc_ValueError,
1623 "field n is required for Num");
1624 return NULL;
1625 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001626 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001627 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 p->kind = Num_kind;
1630 p->v.Num.n = n;
1631 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001632 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001633 return p;
1634}
1635
1636expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001637Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638{
1639 expr_ty p;
1640 if (!s) {
1641 PyErr_SetString(PyExc_ValueError,
1642 "field s is required for Str");
1643 return NULL;
1644 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001645 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001646 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 p->kind = Str_kind;
1649 p->v.Str.s = s;
1650 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 return p;
1653}
1654
1655expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001656Bytes(string s, int lineno, int col_offset, PyArena *arena)
1657{
1658 expr_ty p;
1659 if (!s) {
1660 PyErr_SetString(PyExc_ValueError,
1661 "field s is required for Bytes");
1662 return NULL;
1663 }
1664 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001665 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001666 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001667 p->kind = Bytes_kind;
1668 p->v.Bytes.s = s;
1669 p->lineno = lineno;
1670 p->col_offset = col_offset;
1671 return p;
1672}
1673
1674expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001675Ellipsis(int lineno, int col_offset, PyArena *arena)
1676{
1677 expr_ty p;
1678 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001679 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001680 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001681 p->kind = Ellipsis_kind;
1682 p->lineno = lineno;
1683 p->col_offset = col_offset;
1684 return p;
1685}
1686
1687expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1689 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690{
1691 expr_ty p;
1692 if (!value) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "field value is required for Attribute");
1695 return NULL;
1696 }
1697 if (!attr) {
1698 PyErr_SetString(PyExc_ValueError,
1699 "field attr is required for Attribute");
1700 return NULL;
1701 }
1702 if (!ctx) {
1703 PyErr_SetString(PyExc_ValueError,
1704 "field ctx is required for Attribute");
1705 return NULL;
1706 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001707 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001708 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 p->kind = Attribute_kind;
1711 p->v.Attribute.value = value;
1712 p->v.Attribute.attr = attr;
1713 p->v.Attribute.ctx = ctx;
1714 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001715 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 return p;
1717}
1718
1719expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001720Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1721 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722{
1723 expr_ty p;
1724 if (!value) {
1725 PyErr_SetString(PyExc_ValueError,
1726 "field value is required for Subscript");
1727 return NULL;
1728 }
1729 if (!slice) {
1730 PyErr_SetString(PyExc_ValueError,
1731 "field slice is required for Subscript");
1732 return NULL;
1733 }
1734 if (!ctx) {
1735 PyErr_SetString(PyExc_ValueError,
1736 "field ctx is required for Subscript");
1737 return NULL;
1738 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001740 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 p->kind = Subscript_kind;
1743 p->v.Subscript.value = value;
1744 p->v.Subscript.slice = slice;
1745 p->v.Subscript.ctx = ctx;
1746 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001747 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 return p;
1749}
1750
1751expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001752Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1753 *arena)
1754{
1755 expr_ty p;
1756 if (!value) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field value is required for Starred");
1759 return NULL;
1760 }
1761 if (!ctx) {
1762 PyErr_SetString(PyExc_ValueError,
1763 "field ctx is required for Starred");
1764 return NULL;
1765 }
1766 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1767 if (!p)
1768 return NULL;
1769 p->kind = Starred_kind;
1770 p->v.Starred.value = value;
1771 p->v.Starred.ctx = ctx;
1772 p->lineno = lineno;
1773 p->col_offset = col_offset;
1774 return p;
1775}
1776
1777expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001778Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1779 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780{
1781 expr_ty p;
1782 if (!id) {
1783 PyErr_SetString(PyExc_ValueError,
1784 "field id is required for Name");
1785 return NULL;
1786 }
1787 if (!ctx) {
1788 PyErr_SetString(PyExc_ValueError,
1789 "field ctx is required for Name");
1790 return NULL;
1791 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001793 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 p->kind = Name_kind;
1796 p->v.Name.id = id;
1797 p->v.Name.ctx = ctx;
1798 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001799 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 return p;
1801}
1802
1803expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001804List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1805 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
1807 expr_ty p;
1808 if (!ctx) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field ctx is required for List");
1811 return NULL;
1812 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001813 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001814 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 p->kind = List_kind;
1817 p->v.List.elts = elts;
1818 p->v.List.ctx = ctx;
1819 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001820 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 return p;
1822}
1823
1824expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001825Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1826 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827{
1828 expr_ty p;
1829 if (!ctx) {
1830 PyErr_SetString(PyExc_ValueError,
1831 "field ctx is required for Tuple");
1832 return NULL;
1833 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001835 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 p->kind = Tuple_kind;
1838 p->v.Tuple.elts = elts;
1839 p->v.Tuple.ctx = ctx;
1840 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001841 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 return p;
1843}
1844
1845slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847{
1848 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001850 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 p->kind = Slice_kind;
1853 p->v.Slice.lower = lower;
1854 p->v.Slice.upper = upper;
1855 p->v.Slice.step = step;
1856 return p;
1857}
1858
1859slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861{
1862 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001864 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 p->kind = ExtSlice_kind;
1867 p->v.ExtSlice.dims = dims;
1868 return p;
1869}
1870
1871slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873{
1874 slice_ty p;
1875 if (!value) {
1876 PyErr_SetString(PyExc_ValueError,
1877 "field value is required for Index");
1878 return NULL;
1879 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001880 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001881 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 p->kind = Index_kind;
1884 p->v.Index.value = value;
1885 return p;
1886}
1887
1888comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890{
1891 comprehension_ty p;
1892 if (!target) {
1893 PyErr_SetString(PyExc_ValueError,
1894 "field target is required for comprehension");
1895 return NULL;
1896 }
1897 if (!iter) {
1898 PyErr_SetString(PyExc_ValueError,
1899 "field iter is required for comprehension");
1900 return NULL;
1901 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001902 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001903 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 p->target = target;
1906 p->iter = iter;
1907 p->ifs = ifs;
1908 return p;
1909}
1910
1911excepthandler_ty
Guido van Rossum16be03e2007-01-10 18:51:35 +00001912excepthandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001913 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914{
1915 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001917 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 p->type = type;
1920 p->name = name;
1921 p->body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001922 p->lineno = lineno;
1923 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 return p;
1925}
1926
1927arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00001928arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
1929 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
1930 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931{
1932 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001933 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001934 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 p->args = args;
1937 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001938 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001939 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001941 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001943 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 return p;
1945}
1946
Neal Norwitzc1505362006-12-28 06:47:50 +00001947arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001948arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00001949{
1950 arg_ty p;
1951 if (!arg) {
1952 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001953 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00001954 return NULL;
1955 }
1956 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001957 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00001958 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001959 p->arg = arg;
1960 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00001961 return p;
1962}
1963
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001965keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966{
1967 keyword_ty p;
1968 if (!arg) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field arg is required for keyword");
1971 return NULL;
1972 }
1973 if (!value) {
1974 PyErr_SetString(PyExc_ValueError,
1975 "field value is required for keyword");
1976 return NULL;
1977 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001978 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001979 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 p->arg = arg;
1982 p->value = value;
1983 return p;
1984}
1985
1986alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001987alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988{
1989 alias_ty p;
1990 if (!name) {
1991 PyErr_SetString(PyExc_ValueError,
1992 "field name is required for alias");
1993 return NULL;
1994 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001996 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 p->name = name;
1999 p->asname = asname;
2000 return p;
2001}
2002
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002003
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002004PyObject*
2005ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002006{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002007 mod_ty o = (mod_ty)_o;
2008 PyObject *result = NULL, *value = NULL;
2009 if (!o) {
2010 Py_INCREF(Py_None);
2011 return Py_None;
2012 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 switch (o->kind) {
2015 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002016 result = PyType_GenericNew(Module_type, NULL, NULL);
2017 if (!result) goto failed;
2018 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2019 if (!value) goto failed;
2020 if (PyObject_SetAttrString(result, "body", value) == -1)
2021 goto failed;
2022 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 break;
2024 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002025 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2026 if (!result) goto failed;
2027 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2028 if (!value) goto failed;
2029 if (PyObject_SetAttrString(result, "body", value) == -1)
2030 goto failed;
2031 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 break;
2033 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002034 result = PyType_GenericNew(Expression_type, NULL, NULL);
2035 if (!result) goto failed;
2036 value = ast2obj_expr(o->v.Expression.body);
2037 if (!value) goto failed;
2038 if (PyObject_SetAttrString(result, "body", value) == -1)
2039 goto failed;
2040 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 break;
2042 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002043 result = PyType_GenericNew(Suite_type, NULL, NULL);
2044 if (!result) goto failed;
2045 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2046 if (!value) goto failed;
2047 if (PyObject_SetAttrString(result, "body", value) == -1)
2048 goto failed;
2049 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 break;
2051 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002052 return result;
2053failed:
2054 Py_XDECREF(value);
2055 Py_XDECREF(result);
2056 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057}
2058
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002059PyObject*
2060ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002062 stmt_ty o = (stmt_ty)_o;
2063 PyObject *result = NULL, *value = NULL;
2064 if (!o) {
2065 Py_INCREF(Py_None);
2066 return Py_None;
2067 }
2068
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 switch (o->kind) {
2070 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002071 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2072 if (!result) goto failed;
2073 value = ast2obj_identifier(o->v.FunctionDef.name);
2074 if (!value) goto failed;
2075 if (PyObject_SetAttrString(result, "name", value) == -1)
2076 goto failed;
2077 Py_DECREF(value);
2078 value = ast2obj_arguments(o->v.FunctionDef.args);
2079 if (!value) goto failed;
2080 if (PyObject_SetAttrString(result, "args", value) == -1)
2081 goto failed;
2082 Py_DECREF(value);
2083 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2084 if (!value) goto failed;
2085 if (PyObject_SetAttrString(result, "body", value) == -1)
2086 goto failed;
2087 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002088 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2089 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002090 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002091 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2092 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002093 goto failed;
2094 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002095 value = ast2obj_expr(o->v.FunctionDef.returns);
2096 if (!value) goto failed;
2097 if (PyObject_SetAttrString(result, "returns", value) == -1)
2098 goto failed;
2099 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 break;
2101 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002102 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2103 if (!result) goto failed;
2104 value = ast2obj_identifier(o->v.ClassDef.name);
2105 if (!value) goto failed;
2106 if (PyObject_SetAttrString(result, "name", value) == -1)
2107 goto failed;
2108 Py_DECREF(value);
2109 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2110 if (!value) goto failed;
2111 if (PyObject_SetAttrString(result, "bases", value) == -1)
2112 goto failed;
2113 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002114 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2115 if (!value) goto failed;
2116 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2117 goto failed;
2118 Py_DECREF(value);
2119 value = ast2obj_expr(o->v.ClassDef.starargs);
2120 if (!value) goto failed;
2121 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2122 goto failed;
2123 Py_DECREF(value);
2124 value = ast2obj_expr(o->v.ClassDef.kwargs);
2125 if (!value) goto failed;
2126 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2127 goto failed;
2128 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002129 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2130 if (!value) goto failed;
2131 if (PyObject_SetAttrString(result, "body", value) == -1)
2132 goto failed;
2133 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002134 value = ast2obj_list(o->v.ClassDef.decorator_list,
2135 ast2obj_expr);
2136 if (!value) goto failed;
2137 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2138 -1)
2139 goto failed;
2140 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 break;
2142 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002143 result = PyType_GenericNew(Return_type, NULL, NULL);
2144 if (!result) goto failed;
2145 value = ast2obj_expr(o->v.Return.value);
2146 if (!value) goto failed;
2147 if (PyObject_SetAttrString(result, "value", value) == -1)
2148 goto failed;
2149 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 break;
2151 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002152 result = PyType_GenericNew(Delete_type, NULL, NULL);
2153 if (!result) goto failed;
2154 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2155 if (!value) goto failed;
2156 if (PyObject_SetAttrString(result, "targets", value) == -1)
2157 goto failed;
2158 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 break;
2160 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002161 result = PyType_GenericNew(Assign_type, NULL, NULL);
2162 if (!result) goto failed;
2163 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2164 if (!value) goto failed;
2165 if (PyObject_SetAttrString(result, "targets", value) == -1)
2166 goto failed;
2167 Py_DECREF(value);
2168 value = ast2obj_expr(o->v.Assign.value);
2169 if (!value) goto failed;
2170 if (PyObject_SetAttrString(result, "value", value) == -1)
2171 goto failed;
2172 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 break;
2174 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002175 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2176 if (!result) goto failed;
2177 value = ast2obj_expr(o->v.AugAssign.target);
2178 if (!value) goto failed;
2179 if (PyObject_SetAttrString(result, "target", value) == -1)
2180 goto failed;
2181 Py_DECREF(value);
2182 value = ast2obj_operator(o->v.AugAssign.op);
2183 if (!value) goto failed;
2184 if (PyObject_SetAttrString(result, "op", value) == -1)
2185 goto failed;
2186 Py_DECREF(value);
2187 value = ast2obj_expr(o->v.AugAssign.value);
2188 if (!value) goto failed;
2189 if (PyObject_SetAttrString(result, "value", value) == -1)
2190 goto failed;
2191 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002194 result = PyType_GenericNew(For_type, NULL, NULL);
2195 if (!result) goto failed;
2196 value = ast2obj_expr(o->v.For.target);
2197 if (!value) goto failed;
2198 if (PyObject_SetAttrString(result, "target", value) == -1)
2199 goto failed;
2200 Py_DECREF(value);
2201 value = ast2obj_expr(o->v.For.iter);
2202 if (!value) goto failed;
2203 if (PyObject_SetAttrString(result, "iter", value) == -1)
2204 goto failed;
2205 Py_DECREF(value);
2206 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2207 if (!value) goto failed;
2208 if (PyObject_SetAttrString(result, "body", value) == -1)
2209 goto failed;
2210 Py_DECREF(value);
2211 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2212 if (!value) goto failed;
2213 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2214 goto failed;
2215 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 break;
2217 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002218 result = PyType_GenericNew(While_type, NULL, NULL);
2219 if (!result) goto failed;
2220 value = ast2obj_expr(o->v.While.test);
2221 if (!value) goto failed;
2222 if (PyObject_SetAttrString(result, "test", value) == -1)
2223 goto failed;
2224 Py_DECREF(value);
2225 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2226 if (!value) goto failed;
2227 if (PyObject_SetAttrString(result, "body", value) == -1)
2228 goto failed;
2229 Py_DECREF(value);
2230 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2231 if (!value) goto failed;
2232 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2233 goto failed;
2234 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 break;
2236 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002237 result = PyType_GenericNew(If_type, NULL, NULL);
2238 if (!result) goto failed;
2239 value = ast2obj_expr(o->v.If.test);
2240 if (!value) goto failed;
2241 if (PyObject_SetAttrString(result, "test", value) == -1)
2242 goto failed;
2243 Py_DECREF(value);
2244 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2245 if (!value) goto failed;
2246 if (PyObject_SetAttrString(result, "body", value) == -1)
2247 goto failed;
2248 Py_DECREF(value);
2249 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2250 if (!value) goto failed;
2251 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2252 goto failed;
2253 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002255 case With_kind:
2256 result = PyType_GenericNew(With_type, NULL, NULL);
2257 if (!result) goto failed;
2258 value = ast2obj_expr(o->v.With.context_expr);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
2263 value = ast2obj_expr(o->v.With.optional_vars);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2266 -1)
2267 goto failed;
2268 Py_DECREF(value);
2269 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2270 if (!value) goto failed;
2271 if (PyObject_SetAttrString(result, "body", value) == -1)
2272 goto failed;
2273 Py_DECREF(value);
2274 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002276 result = PyType_GenericNew(Raise_type, NULL, NULL);
2277 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002278 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002279 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002280 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002281 goto failed;
2282 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002283 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002285 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002286 goto failed;
2287 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 break;
2289 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002290 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2291 if (!result) goto failed;
2292 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2293 if (!value) goto failed;
2294 if (PyObject_SetAttrString(result, "body", value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
2297 value = ast2obj_list(o->v.TryExcept.handlers,
2298 ast2obj_excepthandler);
2299 if (!value) goto failed;
2300 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2301 goto failed;
2302 Py_DECREF(value);
2303 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2304 if (!value) goto failed;
2305 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2306 goto failed;
2307 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 break;
2309 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002310 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2311 if (!result) goto failed;
2312 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2313 if (!value) goto failed;
2314 if (PyObject_SetAttrString(result, "body", value) == -1)
2315 goto failed;
2316 Py_DECREF(value);
2317 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2318 if (!value) goto failed;
2319 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2320 goto failed;
2321 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 break;
2323 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002324 result = PyType_GenericNew(Assert_type, NULL, NULL);
2325 if (!result) goto failed;
2326 value = ast2obj_expr(o->v.Assert.test);
2327 if (!value) goto failed;
2328 if (PyObject_SetAttrString(result, "test", value) == -1)
2329 goto failed;
2330 Py_DECREF(value);
2331 value = ast2obj_expr(o->v.Assert.msg);
2332 if (!value) goto failed;
2333 if (PyObject_SetAttrString(result, "msg", value) == -1)
2334 goto failed;
2335 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 break;
2337 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002338 result = PyType_GenericNew(Import_type, NULL, NULL);
2339 if (!result) goto failed;
2340 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2341 if (!value) goto failed;
2342 if (PyObject_SetAttrString(result, "names", value) == -1)
2343 goto failed;
2344 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 break;
2346 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002347 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2348 if (!result) goto failed;
2349 value = ast2obj_identifier(o->v.ImportFrom.module);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "module", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
2354 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2355 if (!value) goto failed;
2356 if (PyObject_SetAttrString(result, "names", value) == -1)
2357 goto failed;
2358 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002359 value = ast2obj_int(o->v.ImportFrom.level);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "level", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002366 result = PyType_GenericNew(Global_type, NULL, NULL);
2367 if (!result) goto failed;
2368 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "names", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002374 case Nonlocal_kind:
2375 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2376 if (!result) goto failed;
2377 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "names", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002384 result = PyType_GenericNew(Expr_type, NULL, NULL);
2385 if (!result) goto failed;
2386 value = ast2obj_expr(o->v.Expr.value);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "value", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 break;
2392 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002393 result = PyType_GenericNew(Pass_type, NULL, NULL);
2394 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 break;
2396 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002397 result = PyType_GenericNew(Break_type, NULL, NULL);
2398 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 break;
2400 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002401 result = PyType_GenericNew(Continue_type, NULL, NULL);
2402 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 break;
2404 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002405 value = ast2obj_int(o->lineno);
2406 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002407 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2408 goto failed;
2409 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002410 value = ast2obj_int(o->col_offset);
2411 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002412 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2413 goto failed;
2414 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002415 return result;
2416failed:
2417 Py_XDECREF(value);
2418 Py_XDECREF(result);
2419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420}
2421
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002422PyObject*
2423ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002425 expr_ty o = (expr_ty)_o;
2426 PyObject *result = NULL, *value = NULL;
2427 if (!o) {
2428 Py_INCREF(Py_None);
2429 return Py_None;
2430 }
2431
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 switch (o->kind) {
2433 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002434 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2435 if (!result) goto failed;
2436 value = ast2obj_boolop(o->v.BoolOp.op);
2437 if (!value) goto failed;
2438 if (PyObject_SetAttrString(result, "op", value) == -1)
2439 goto failed;
2440 Py_DECREF(value);
2441 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2442 if (!value) goto failed;
2443 if (PyObject_SetAttrString(result, "values", value) == -1)
2444 goto failed;
2445 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 break;
2447 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002448 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2449 if (!result) goto failed;
2450 value = ast2obj_expr(o->v.BinOp.left);
2451 if (!value) goto failed;
2452 if (PyObject_SetAttrString(result, "left", value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
2455 value = ast2obj_operator(o->v.BinOp.op);
2456 if (!value) goto failed;
2457 if (PyObject_SetAttrString(result, "op", value) == -1)
2458 goto failed;
2459 Py_DECREF(value);
2460 value = ast2obj_expr(o->v.BinOp.right);
2461 if (!value) goto failed;
2462 if (PyObject_SetAttrString(result, "right", value) == -1)
2463 goto failed;
2464 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 break;
2466 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002467 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2468 if (!result) goto failed;
2469 value = ast2obj_unaryop(o->v.UnaryOp.op);
2470 if (!value) goto failed;
2471 if (PyObject_SetAttrString(result, "op", value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
2474 value = ast2obj_expr(o->v.UnaryOp.operand);
2475 if (!value) goto failed;
2476 if (PyObject_SetAttrString(result, "operand", value) == -1)
2477 goto failed;
2478 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 break;
2480 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002481 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2482 if (!result) goto failed;
2483 value = ast2obj_arguments(o->v.Lambda.args);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "args", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
2488 value = ast2obj_expr(o->v.Lambda.body);
2489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "body", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002494 case IfExp_kind:
2495 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2496 if (!result) goto failed;
2497 value = ast2obj_expr(o->v.IfExp.test);
2498 if (!value) goto failed;
2499 if (PyObject_SetAttrString(result, "test", value) == -1)
2500 goto failed;
2501 Py_DECREF(value);
2502 value = ast2obj_expr(o->v.IfExp.body);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "body", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
2507 value = ast2obj_expr(o->v.IfExp.orelse);
2508 if (!value) goto failed;
2509 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2510 goto failed;
2511 Py_DECREF(value);
2512 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002514 result = PyType_GenericNew(Dict_type, NULL, NULL);
2515 if (!result) goto failed;
2516 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "keys", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
2521 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2522 if (!value) goto failed;
2523 if (PyObject_SetAttrString(result, "values", value) == -1)
2524 goto failed;
2525 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002527 case Set_kind:
2528 result = PyType_GenericNew(Set_type, NULL, NULL);
2529 if (!result) goto failed;
2530 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2531 if (!value) goto failed;
2532 if (PyObject_SetAttrString(result, "elts", value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
2535 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002537 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2538 if (!result) goto failed;
2539 value = ast2obj_expr(o->v.ListComp.elt);
2540 if (!value) goto failed;
2541 if (PyObject_SetAttrString(result, "elt", value) == -1)
2542 goto failed;
2543 Py_DECREF(value);
2544 value = ast2obj_list(o->v.ListComp.generators,
2545 ast2obj_comprehension);
2546 if (!value) goto failed;
2547 if (PyObject_SetAttrString(result, "generators", value) == -1)
2548 goto failed;
2549 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002551 case SetComp_kind:
2552 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2553 if (!result) goto failed;
2554 value = ast2obj_expr(o->v.SetComp.elt);
2555 if (!value) goto failed;
2556 if (PyObject_SetAttrString(result, "elt", value) == -1)
2557 goto failed;
2558 Py_DECREF(value);
2559 value = ast2obj_list(o->v.SetComp.generators,
2560 ast2obj_comprehension);
2561 if (!value) goto failed;
2562 if (PyObject_SetAttrString(result, "generators", value) == -1)
2563 goto failed;
2564 Py_DECREF(value);
2565 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002566 case DictComp_kind:
2567 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2568 if (!result) goto failed;
2569 value = ast2obj_expr(o->v.DictComp.key);
2570 if (!value) goto failed;
2571 if (PyObject_SetAttrString(result, "key", value) == -1)
2572 goto failed;
2573 Py_DECREF(value);
2574 value = ast2obj_expr(o->v.DictComp.value);
2575 if (!value) goto failed;
2576 if (PyObject_SetAttrString(result, "value", value) == -1)
2577 goto failed;
2578 Py_DECREF(value);
2579 value = ast2obj_list(o->v.DictComp.generators,
2580 ast2obj_comprehension);
2581 if (!value) goto failed;
2582 if (PyObject_SetAttrString(result, "generators", value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
2585 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002587 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2588 if (!result) goto failed;
2589 value = ast2obj_expr(o->v.GeneratorExp.elt);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "elt", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
2594 value = ast2obj_list(o->v.GeneratorExp.generators,
2595 ast2obj_comprehension);
2596 if (!value) goto failed;
2597 if (PyObject_SetAttrString(result, "generators", value) == -1)
2598 goto failed;
2599 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 break;
2601 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002602 result = PyType_GenericNew(Yield_type, NULL, NULL);
2603 if (!result) goto failed;
2604 value = ast2obj_expr(o->v.Yield.value);
2605 if (!value) goto failed;
2606 if (PyObject_SetAttrString(result, "value", value) == -1)
2607 goto failed;
2608 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 break;
2610 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002611 result = PyType_GenericNew(Compare_type, NULL, NULL);
2612 if (!result) goto failed;
2613 value = ast2obj_expr(o->v.Compare.left);
2614 if (!value) goto failed;
2615 if (PyObject_SetAttrString(result, "left", value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002618 {
2619 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2620 value = PyList_New(n);
2621 if (!value) goto failed;
2622 for(i = 0; i < n; i++)
2623 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2624 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002625 if (!value) goto failed;
2626 if (PyObject_SetAttrString(result, "ops", value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
2629 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2630 if (!value) goto failed;
2631 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 break;
2635 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002636 result = PyType_GenericNew(Call_type, NULL, NULL);
2637 if (!result) goto failed;
2638 value = ast2obj_expr(o->v.Call.func);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "func", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
2643 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2644 if (!value) goto failed;
2645 if (PyObject_SetAttrString(result, "args", value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
2648 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2649 if (!value) goto failed;
2650 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2651 goto failed;
2652 Py_DECREF(value);
2653 value = ast2obj_expr(o->v.Call.starargs);
2654 if (!value) goto failed;
2655 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2656 goto failed;
2657 Py_DECREF(value);
2658 value = ast2obj_expr(o->v.Call.kwargs);
2659 if (!value) goto failed;
2660 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2661 goto failed;
2662 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002665 result = PyType_GenericNew(Num_type, NULL, NULL);
2666 if (!result) goto failed;
2667 value = ast2obj_object(o->v.Num.n);
2668 if (!value) goto failed;
2669 if (PyObject_SetAttrString(result, "n", value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 break;
2673 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002674 result = PyType_GenericNew(Str_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_string(o->v.Str.s);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "s", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002682 case Bytes_kind:
2683 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2684 if (!result) goto failed;
2685 value = ast2obj_string(o->v.Bytes.s);
2686 if (!value) goto failed;
2687 if (PyObject_SetAttrString(result, "s", value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002691 case Ellipsis_kind:
2692 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2693 if (!result) goto failed;
2694 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002696 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2697 if (!result) goto failed;
2698 value = ast2obj_expr(o->v.Attribute.value);
2699 if (!value) goto failed;
2700 if (PyObject_SetAttrString(result, "value", value) == -1)
2701 goto failed;
2702 Py_DECREF(value);
2703 value = ast2obj_identifier(o->v.Attribute.attr);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "attr", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 value = ast2obj_expr_context(o->v.Attribute.ctx);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 break;
2714 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002715 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2716 if (!result) goto failed;
2717 value = ast2obj_expr(o->v.Subscript.value);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "value", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_slice(o->v.Subscript.slice);
2723 if (!value) goto failed;
2724 if (PyObject_SetAttrString(result, "slice", value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
2727 value = ast2obj_expr_context(o->v.Subscript.ctx);
2728 if (!value) goto failed;
2729 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2730 goto failed;
2731 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002733 case Starred_kind:
2734 result = PyType_GenericNew(Starred_type, NULL, NULL);
2735 if (!result) goto failed;
2736 value = ast2obj_expr(o->v.Starred.value);
2737 if (!value) goto failed;
2738 if (PyObject_SetAttrString(result, "value", value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
2741 value = ast2obj_expr_context(o->v.Starred.ctx);
2742 if (!value) goto failed;
2743 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002748 result = PyType_GenericNew(Name_type, NULL, NULL);
2749 if (!result) goto failed;
2750 value = ast2obj_identifier(o->v.Name.id);
2751 if (!value) goto failed;
2752 if (PyObject_SetAttrString(result, "id", value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
2755 value = ast2obj_expr_context(o->v.Name.ctx);
2756 if (!value) goto failed;
2757 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 break;
2761 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002762 result = PyType_GenericNew(List_type, NULL, NULL);
2763 if (!result) goto failed;
2764 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2765 if (!value) goto failed;
2766 if (PyObject_SetAttrString(result, "elts", value) == -1)
2767 goto failed;
2768 Py_DECREF(value);
2769 value = ast2obj_expr_context(o->v.List.ctx);
2770 if (!value) goto failed;
2771 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 break;
2775 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002776 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2777 if (!result) goto failed;
2778 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2779 if (!value) goto failed;
2780 if (PyObject_SetAttrString(result, "elts", value) == -1)
2781 goto failed;
2782 Py_DECREF(value);
2783 value = ast2obj_expr_context(o->v.Tuple.ctx);
2784 if (!value) goto failed;
2785 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2786 goto failed;
2787 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 break;
2789 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002790 value = ast2obj_int(o->lineno);
2791 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002792 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2793 goto failed;
2794 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002795 value = ast2obj_int(o->col_offset);
2796 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002797 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2798 goto failed;
2799 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002800 return result;
2801failed:
2802 Py_XDECREF(value);
2803 Py_XDECREF(result);
2804 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805}
2806
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002807PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002809 switch(o) {
2810 case Load:
2811 Py_INCREF(Load_singleton);
2812 return Load_singleton;
2813 case Store:
2814 Py_INCREF(Store_singleton);
2815 return Store_singleton;
2816 case Del:
2817 Py_INCREF(Del_singleton);
2818 return Del_singleton;
2819 case AugLoad:
2820 Py_INCREF(AugLoad_singleton);
2821 return AugLoad_singleton;
2822 case AugStore:
2823 Py_INCREF(AugStore_singleton);
2824 return AugStore_singleton;
2825 case Param:
2826 Py_INCREF(Param_singleton);
2827 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002828 default:
2829 /* should never happen, but just in case ... */
2830 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2831 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002834PyObject*
2835ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002837 slice_ty o = (slice_ty)_o;
2838 PyObject *result = NULL, *value = NULL;
2839 if (!o) {
2840 Py_INCREF(Py_None);
2841 return Py_None;
2842 }
2843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002846 result = PyType_GenericNew(Slice_type, NULL, NULL);
2847 if (!result) goto failed;
2848 value = ast2obj_expr(o->v.Slice.lower);
2849 if (!value) goto failed;
2850 if (PyObject_SetAttrString(result, "lower", value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
2853 value = ast2obj_expr(o->v.Slice.upper);
2854 if (!value) goto failed;
2855 if (PyObject_SetAttrString(result, "upper", value) == -1)
2856 goto failed;
2857 Py_DECREF(value);
2858 value = ast2obj_expr(o->v.Slice.step);
2859 if (!value) goto failed;
2860 if (PyObject_SetAttrString(result, "step", value) == -1)
2861 goto failed;
2862 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863 break;
2864 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002865 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2866 if (!result) goto failed;
2867 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2868 if (!value) goto failed;
2869 if (PyObject_SetAttrString(result, "dims", value) == -1)
2870 goto failed;
2871 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872 break;
2873 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002874 result = PyType_GenericNew(Index_type, NULL, NULL);
2875 if (!result) goto failed;
2876 value = ast2obj_expr(o->v.Index.value);
2877 if (!value) goto failed;
2878 if (PyObject_SetAttrString(result, "value", value) == -1)
2879 goto failed;
2880 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 break;
2882 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002883 return result;
2884failed:
2885 Py_XDECREF(value);
2886 Py_XDECREF(result);
2887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888}
2889
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002890PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002892 switch(o) {
2893 case And:
2894 Py_INCREF(And_singleton);
2895 return And_singleton;
2896 case Or:
2897 Py_INCREF(Or_singleton);
2898 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002899 default:
2900 /* should never happen, but just in case ... */
2901 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2902 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002904}
2905PyObject* ast2obj_operator(operator_ty o)
2906{
2907 switch(o) {
2908 case Add:
2909 Py_INCREF(Add_singleton);
2910 return Add_singleton;
2911 case Sub:
2912 Py_INCREF(Sub_singleton);
2913 return Sub_singleton;
2914 case Mult:
2915 Py_INCREF(Mult_singleton);
2916 return Mult_singleton;
2917 case Div:
2918 Py_INCREF(Div_singleton);
2919 return Div_singleton;
2920 case Mod:
2921 Py_INCREF(Mod_singleton);
2922 return Mod_singleton;
2923 case Pow:
2924 Py_INCREF(Pow_singleton);
2925 return Pow_singleton;
2926 case LShift:
2927 Py_INCREF(LShift_singleton);
2928 return LShift_singleton;
2929 case RShift:
2930 Py_INCREF(RShift_singleton);
2931 return RShift_singleton;
2932 case BitOr:
2933 Py_INCREF(BitOr_singleton);
2934 return BitOr_singleton;
2935 case BitXor:
2936 Py_INCREF(BitXor_singleton);
2937 return BitXor_singleton;
2938 case BitAnd:
2939 Py_INCREF(BitAnd_singleton);
2940 return BitAnd_singleton;
2941 case FloorDiv:
2942 Py_INCREF(FloorDiv_singleton);
2943 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002944 default:
2945 /* should never happen, but just in case ... */
2946 PyErr_Format(PyExc_SystemError, "unknown operator found");
2947 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002948 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002949}
2950PyObject* ast2obj_unaryop(unaryop_ty o)
2951{
2952 switch(o) {
2953 case Invert:
2954 Py_INCREF(Invert_singleton);
2955 return Invert_singleton;
2956 case Not:
2957 Py_INCREF(Not_singleton);
2958 return Not_singleton;
2959 case UAdd:
2960 Py_INCREF(UAdd_singleton);
2961 return UAdd_singleton;
2962 case USub:
2963 Py_INCREF(USub_singleton);
2964 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002965 default:
2966 /* should never happen, but just in case ... */
2967 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2968 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002969 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002970}
2971PyObject* ast2obj_cmpop(cmpop_ty o)
2972{
2973 switch(o) {
2974 case Eq:
2975 Py_INCREF(Eq_singleton);
2976 return Eq_singleton;
2977 case NotEq:
2978 Py_INCREF(NotEq_singleton);
2979 return NotEq_singleton;
2980 case Lt:
2981 Py_INCREF(Lt_singleton);
2982 return Lt_singleton;
2983 case LtE:
2984 Py_INCREF(LtE_singleton);
2985 return LtE_singleton;
2986 case Gt:
2987 Py_INCREF(Gt_singleton);
2988 return Gt_singleton;
2989 case GtE:
2990 Py_INCREF(GtE_singleton);
2991 return GtE_singleton;
2992 case Is:
2993 Py_INCREF(Is_singleton);
2994 return Is_singleton;
2995 case IsNot:
2996 Py_INCREF(IsNot_singleton);
2997 return IsNot_singleton;
2998 case In:
2999 Py_INCREF(In_singleton);
3000 return In_singleton;
3001 case NotIn:
3002 Py_INCREF(NotIn_singleton);
3003 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003004 default:
3005 /* should never happen, but just in case ... */
3006 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3007 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003008 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003009}
3010PyObject*
3011ast2obj_comprehension(void* _o)
3012{
3013 comprehension_ty o = (comprehension_ty)_o;
3014 PyObject *result = NULL, *value = NULL;
3015 if (!o) {
3016 Py_INCREF(Py_None);
3017 return Py_None;
3018 }
3019
3020 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3021 if (!result) return NULL;
3022 value = ast2obj_expr(o->target);
3023 if (!value) goto failed;
3024 if (PyObject_SetAttrString(result, "target", value) == -1)
3025 goto failed;
3026 Py_DECREF(value);
3027 value = ast2obj_expr(o->iter);
3028 if (!value) goto failed;
3029 if (PyObject_SetAttrString(result, "iter", value) == -1)
3030 goto failed;
3031 Py_DECREF(value);
3032 value = ast2obj_list(o->ifs, ast2obj_expr);
3033 if (!value) goto failed;
3034 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3035 goto failed;
3036 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003037 return result;
3038failed:
3039 Py_XDECREF(value);
3040 Py_XDECREF(result);
3041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042}
3043
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003044PyObject*
3045ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003047 excepthandler_ty o = (excepthandler_ty)_o;
3048 PyObject *result = NULL, *value = NULL;
3049 if (!o) {
3050 Py_INCREF(Py_None);
3051 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003052 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003053
3054 result = PyType_GenericNew(excepthandler_type, NULL, NULL);
3055 if (!result) return NULL;
3056 value = ast2obj_expr(o->type);
3057 if (!value) goto failed;
3058 if (PyObject_SetAttrString(result, "type", value) == -1)
3059 goto failed;
3060 Py_DECREF(value);
Guido van Rossum16be03e2007-01-10 18:51:35 +00003061 value = ast2obj_identifier(o->name);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003062 if (!value) goto failed;
3063 if (PyObject_SetAttrString(result, "name", value) == -1)
3064 goto failed;
3065 Py_DECREF(value);
3066 value = ast2obj_list(o->body, ast2obj_stmt);
3067 if (!value) goto failed;
3068 if (PyObject_SetAttrString(result, "body", value) == -1)
3069 goto failed;
3070 Py_DECREF(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003071 value = ast2obj_int(o->lineno);
3072 if (!value) goto failed;
3073 if (PyObject_SetAttrString(result, "lineno", value) == -1)
3074 goto failed;
3075 Py_DECREF(value);
3076 value = ast2obj_int(o->col_offset);
3077 if (!value) goto failed;
3078 if (PyObject_SetAttrString(result, "col_offset", value) == -1)
3079 goto failed;
3080 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003081 return result;
3082failed:
3083 Py_XDECREF(value);
3084 Py_XDECREF(result);
3085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086}
3087
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003088PyObject*
3089ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003091 arguments_ty o = (arguments_ty)_o;
3092 PyObject *result = NULL, *value = NULL;
3093 if (!o) {
3094 Py_INCREF(Py_None);
3095 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003097
3098 result = PyType_GenericNew(arguments_type, NULL, NULL);
3099 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003100 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003101 if (!value) goto failed;
3102 if (PyObject_SetAttrString(result, "args", value) == -1)
3103 goto failed;
3104 Py_DECREF(value);
3105 value = ast2obj_identifier(o->vararg);
3106 if (!value) goto failed;
3107 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3108 goto failed;
3109 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003110 value = ast2obj_expr(o->varargannotation);
3111 if (!value) goto failed;
3112 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3113 goto failed;
3114 Py_DECREF(value);
3115 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003116 if (!value) goto failed;
3117 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3118 goto failed;
3119 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003120 value = ast2obj_identifier(o->kwarg);
3121 if (!value) goto failed;
3122 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3123 goto failed;
3124 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003125 value = ast2obj_expr(o->kwargannotation);
3126 if (!value) goto failed;
3127 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003130 value = ast2obj_list(o->defaults, ast2obj_expr);
3131 if (!value) goto failed;
3132 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3133 goto failed;
3134 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003135 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3136 if (!value) goto failed;
3137 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3138 goto failed;
3139 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003140 return result;
3141failed:
3142 Py_XDECREF(value);
3143 Py_XDECREF(result);
3144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145}
3146
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003147PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003148ast2obj_arg(void* _o)
3149{
3150 arg_ty o = (arg_ty)_o;
3151 PyObject *result = NULL, *value = NULL;
3152 if (!o) {
3153 Py_INCREF(Py_None);
3154 return Py_None;
3155 }
3156
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003157 result = PyType_GenericNew(arg_type, NULL, NULL);
3158 if (!result) return NULL;
3159 value = ast2obj_identifier(o->arg);
3160 if (!value) goto failed;
3161 if (PyObject_SetAttrString(result, "arg", value) == -1)
3162 goto failed;
3163 Py_DECREF(value);
3164 value = ast2obj_expr(o->annotation);
3165 if (!value) goto failed;
3166 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003169 return result;
3170failed:
3171 Py_XDECREF(value);
3172 Py_XDECREF(result);
3173 return NULL;
3174}
3175
3176PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003177ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003179 keyword_ty o = (keyword_ty)_o;
3180 PyObject *result = NULL, *value = NULL;
3181 if (!o) {
3182 Py_INCREF(Py_None);
3183 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003185
3186 result = PyType_GenericNew(keyword_type, NULL, NULL);
3187 if (!result) return NULL;
3188 value = ast2obj_identifier(o->arg);
3189 if (!value) goto failed;
3190 if (PyObject_SetAttrString(result, "arg", value) == -1)
3191 goto failed;
3192 Py_DECREF(value);
3193 value = ast2obj_expr(o->value);
3194 if (!value) goto failed;
3195 if (PyObject_SetAttrString(result, "value", value) == -1)
3196 goto failed;
3197 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003198 return result;
3199failed:
3200 Py_XDECREF(value);
3201 Py_XDECREF(result);
3202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203}
3204
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003205PyObject*
3206ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003208 alias_ty o = (alias_ty)_o;
3209 PyObject *result = NULL, *value = NULL;
3210 if (!o) {
3211 Py_INCREF(Py_None);
3212 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003214
3215 result = PyType_GenericNew(alias_type, NULL, NULL);
3216 if (!result) return NULL;
3217 value = ast2obj_identifier(o->name);
3218 if (!value) goto failed;
3219 if (PyObject_SetAttrString(result, "name", value) == -1)
3220 goto failed;
3221 Py_DECREF(value);
3222 value = ast2obj_identifier(o->asname);
3223 if (!value) goto failed;
3224 if (PyObject_SetAttrString(result, "asname", value) == -1)
3225 goto failed;
3226 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003227 return result;
3228failed:
3229 Py_XDECREF(value);
3230 Py_XDECREF(result);
3231 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232}
3233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003235int
3236obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3237{
3238 PyObject* tmp = NULL;
3239
3240
3241 if (obj == Py_None) {
3242 *out = NULL;
3243 return 0;
3244 }
3245 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3246 asdl_seq* body;
3247
3248 if (PyObject_HasAttrString(obj, "body")) {
3249 int res;
3250 Py_ssize_t len;
3251 Py_ssize_t i;
3252 tmp = PyObject_GetAttrString(obj, "body");
3253 if (tmp == NULL) goto failed;
3254 if (!PyList_Check(tmp)) {
3255 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3256 goto failed;
3257 }
3258 len = PyList_GET_SIZE(tmp);
3259 body = asdl_seq_new(len, arena);
3260 if (body == NULL) goto failed;
3261 for (i = 0; i < len; i++) {
3262 stmt_ty value;
3263 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3264 if (res != 0) goto failed;
3265 asdl_seq_SET(body, i, value);
3266 }
3267 Py_XDECREF(tmp);
3268 tmp = NULL;
3269 } else {
3270 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3271 return 1;
3272 }
3273 *out = Module(body, arena);
3274 if (*out == NULL) goto failed;
3275 return 0;
3276 }
3277 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3278 asdl_seq* body;
3279
3280 if (PyObject_HasAttrString(obj, "body")) {
3281 int res;
3282 Py_ssize_t len;
3283 Py_ssize_t i;
3284 tmp = PyObject_GetAttrString(obj, "body");
3285 if (tmp == NULL) goto failed;
3286 if (!PyList_Check(tmp)) {
3287 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3288 goto failed;
3289 }
3290 len = PyList_GET_SIZE(tmp);
3291 body = asdl_seq_new(len, arena);
3292 if (body == NULL) goto failed;
3293 for (i = 0; i < len; i++) {
3294 stmt_ty value;
3295 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3296 if (res != 0) goto failed;
3297 asdl_seq_SET(body, i, value);
3298 }
3299 Py_XDECREF(tmp);
3300 tmp = NULL;
3301 } else {
3302 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3303 return 1;
3304 }
3305 *out = Interactive(body, arena);
3306 if (*out == NULL) goto failed;
3307 return 0;
3308 }
3309 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3310 expr_ty body;
3311
3312 if (PyObject_HasAttrString(obj, "body")) {
3313 int res;
3314 tmp = PyObject_GetAttrString(obj, "body");
3315 if (tmp == NULL) goto failed;
3316 res = obj2ast_expr(tmp, &body, arena);
3317 if (res != 0) goto failed;
3318 Py_XDECREF(tmp);
3319 tmp = NULL;
3320 } else {
3321 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3322 return 1;
3323 }
3324 *out = Expression(body, arena);
3325 if (*out == NULL) goto failed;
3326 return 0;
3327 }
3328 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3329 asdl_seq* body;
3330
3331 if (PyObject_HasAttrString(obj, "body")) {
3332 int res;
3333 Py_ssize_t len;
3334 Py_ssize_t i;
3335 tmp = PyObject_GetAttrString(obj, "body");
3336 if (tmp == NULL) goto failed;
3337 if (!PyList_Check(tmp)) {
3338 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3339 goto failed;
3340 }
3341 len = PyList_GET_SIZE(tmp);
3342 body = asdl_seq_new(len, arena);
3343 if (body == NULL) goto failed;
3344 for (i = 0; i < len; i++) {
3345 stmt_ty value;
3346 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3347 if (res != 0) goto failed;
3348 asdl_seq_SET(body, i, value);
3349 }
3350 Py_XDECREF(tmp);
3351 tmp = NULL;
3352 } else {
3353 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3354 return 1;
3355 }
3356 *out = Suite(body, arena);
3357 if (*out == NULL) goto failed;
3358 return 0;
3359 }
3360
3361 tmp = PyObject_Repr(obj);
3362 if (tmp == NULL) goto failed;
3363 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3364failed:
3365 Py_XDECREF(tmp);
3366 return 1;
3367}
3368
3369int
3370obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3371{
3372 PyObject* tmp = NULL;
3373
3374 int lineno;
3375 int col_offset;
3376
3377 if (obj == Py_None) {
3378 *out = NULL;
3379 return 0;
3380 }
3381 if (PyObject_HasAttrString(obj, "lineno")) {
3382 int res;
3383 tmp = PyObject_GetAttrString(obj, "lineno");
3384 if (tmp == NULL) goto failed;
3385 res = obj2ast_int(tmp, &lineno, arena);
3386 if (res != 0) goto failed;
3387 Py_XDECREF(tmp);
3388 tmp = NULL;
3389 } else {
3390 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3391 return 1;
3392 }
3393 if (PyObject_HasAttrString(obj, "col_offset")) {
3394 int res;
3395 tmp = PyObject_GetAttrString(obj, "col_offset");
3396 if (tmp == NULL) goto failed;
3397 res = obj2ast_int(tmp, &col_offset, arena);
3398 if (res != 0) goto failed;
3399 Py_XDECREF(tmp);
3400 tmp = NULL;
3401 } else {
3402 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3403 return 1;
3404 }
3405 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3406 identifier name;
3407 arguments_ty args;
3408 asdl_seq* body;
3409 asdl_seq* decorator_list;
3410 expr_ty returns;
3411
3412 if (PyObject_HasAttrString(obj, "name")) {
3413 int res;
3414 tmp = PyObject_GetAttrString(obj, "name");
3415 if (tmp == NULL) goto failed;
3416 res = obj2ast_identifier(tmp, &name, arena);
3417 if (res != 0) goto failed;
3418 Py_XDECREF(tmp);
3419 tmp = NULL;
3420 } else {
3421 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3422 return 1;
3423 }
3424 if (PyObject_HasAttrString(obj, "args")) {
3425 int res;
3426 tmp = PyObject_GetAttrString(obj, "args");
3427 if (tmp == NULL) goto failed;
3428 res = obj2ast_arguments(tmp, &args, arena);
3429 if (res != 0) goto failed;
3430 Py_XDECREF(tmp);
3431 tmp = NULL;
3432 } else {
3433 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3434 return 1;
3435 }
3436 if (PyObject_HasAttrString(obj, "body")) {
3437 int res;
3438 Py_ssize_t len;
3439 Py_ssize_t i;
3440 tmp = PyObject_GetAttrString(obj, "body");
3441 if (tmp == NULL) goto failed;
3442 if (!PyList_Check(tmp)) {
3443 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3444 goto failed;
3445 }
3446 len = PyList_GET_SIZE(tmp);
3447 body = asdl_seq_new(len, arena);
3448 if (body == NULL) goto failed;
3449 for (i = 0; i < len; i++) {
3450 stmt_ty value;
3451 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3452 if (res != 0) goto failed;
3453 asdl_seq_SET(body, i, value);
3454 }
3455 Py_XDECREF(tmp);
3456 tmp = NULL;
3457 } else {
3458 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3459 return 1;
3460 }
3461 if (PyObject_HasAttrString(obj, "decorator_list")) {
3462 int res;
3463 Py_ssize_t len;
3464 Py_ssize_t i;
3465 tmp = PyObject_GetAttrString(obj, "decorator_list");
3466 if (tmp == NULL) goto failed;
3467 if (!PyList_Check(tmp)) {
3468 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3469 goto failed;
3470 }
3471 len = PyList_GET_SIZE(tmp);
3472 decorator_list = asdl_seq_new(len, arena);
3473 if (decorator_list == NULL) goto failed;
3474 for (i = 0; i < len; i++) {
3475 expr_ty value;
3476 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3477 if (res != 0) goto failed;
3478 asdl_seq_SET(decorator_list, i, value);
3479 }
3480 Py_XDECREF(tmp);
3481 tmp = NULL;
3482 } else {
3483 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3484 return 1;
3485 }
3486 if (PyObject_HasAttrString(obj, "returns")) {
3487 int res;
3488 tmp = PyObject_GetAttrString(obj, "returns");
3489 if (tmp == NULL) goto failed;
3490 res = obj2ast_expr(tmp, &returns, arena);
3491 if (res != 0) goto failed;
3492 Py_XDECREF(tmp);
3493 tmp = NULL;
3494 } else {
3495 returns = NULL;
3496 }
3497 *out = FunctionDef(name, args, body, decorator_list, returns,
3498 lineno, col_offset, arena);
3499 if (*out == NULL) goto failed;
3500 return 0;
3501 }
3502 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3503 identifier name;
3504 asdl_seq* bases;
3505 asdl_seq* keywords;
3506 expr_ty starargs;
3507 expr_ty kwargs;
3508 asdl_seq* body;
3509 asdl_seq* decorator_list;
3510
3511 if (PyObject_HasAttrString(obj, "name")) {
3512 int res;
3513 tmp = PyObject_GetAttrString(obj, "name");
3514 if (tmp == NULL) goto failed;
3515 res = obj2ast_identifier(tmp, &name, arena);
3516 if (res != 0) goto failed;
3517 Py_XDECREF(tmp);
3518 tmp = NULL;
3519 } else {
3520 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3521 return 1;
3522 }
3523 if (PyObject_HasAttrString(obj, "bases")) {
3524 int res;
3525 Py_ssize_t len;
3526 Py_ssize_t i;
3527 tmp = PyObject_GetAttrString(obj, "bases");
3528 if (tmp == NULL) goto failed;
3529 if (!PyList_Check(tmp)) {
3530 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3531 goto failed;
3532 }
3533 len = PyList_GET_SIZE(tmp);
3534 bases = asdl_seq_new(len, arena);
3535 if (bases == NULL) goto failed;
3536 for (i = 0; i < len; i++) {
3537 expr_ty value;
3538 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3539 if (res != 0) goto failed;
3540 asdl_seq_SET(bases, i, value);
3541 }
3542 Py_XDECREF(tmp);
3543 tmp = NULL;
3544 } else {
3545 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3546 return 1;
3547 }
3548 if (PyObject_HasAttrString(obj, "keywords")) {
3549 int res;
3550 Py_ssize_t len;
3551 Py_ssize_t i;
3552 tmp = PyObject_GetAttrString(obj, "keywords");
3553 if (tmp == NULL) goto failed;
3554 if (!PyList_Check(tmp)) {
3555 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3556 goto failed;
3557 }
3558 len = PyList_GET_SIZE(tmp);
3559 keywords = asdl_seq_new(len, arena);
3560 if (keywords == NULL) goto failed;
3561 for (i = 0; i < len; i++) {
3562 keyword_ty value;
3563 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3564 if (res != 0) goto failed;
3565 asdl_seq_SET(keywords, i, value);
3566 }
3567 Py_XDECREF(tmp);
3568 tmp = NULL;
3569 } else {
3570 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3571 return 1;
3572 }
3573 if (PyObject_HasAttrString(obj, "starargs")) {
3574 int res;
3575 tmp = PyObject_GetAttrString(obj, "starargs");
3576 if (tmp == NULL) goto failed;
3577 res = obj2ast_expr(tmp, &starargs, arena);
3578 if (res != 0) goto failed;
3579 Py_XDECREF(tmp);
3580 tmp = NULL;
3581 } else {
3582 starargs = NULL;
3583 }
3584 if (PyObject_HasAttrString(obj, "kwargs")) {
3585 int res;
3586 tmp = PyObject_GetAttrString(obj, "kwargs");
3587 if (tmp == NULL) goto failed;
3588 res = obj2ast_expr(tmp, &kwargs, arena);
3589 if (res != 0) goto failed;
3590 Py_XDECREF(tmp);
3591 tmp = NULL;
3592 } else {
3593 kwargs = NULL;
3594 }
3595 if (PyObject_HasAttrString(obj, "body")) {
3596 int res;
3597 Py_ssize_t len;
3598 Py_ssize_t i;
3599 tmp = PyObject_GetAttrString(obj, "body");
3600 if (tmp == NULL) goto failed;
3601 if (!PyList_Check(tmp)) {
3602 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3603 goto failed;
3604 }
3605 len = PyList_GET_SIZE(tmp);
3606 body = asdl_seq_new(len, arena);
3607 if (body == NULL) goto failed;
3608 for (i = 0; i < len; i++) {
3609 stmt_ty value;
3610 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3611 if (res != 0) goto failed;
3612 asdl_seq_SET(body, i, value);
3613 }
3614 Py_XDECREF(tmp);
3615 tmp = NULL;
3616 } else {
3617 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3618 return 1;
3619 }
3620 if (PyObject_HasAttrString(obj, "decorator_list")) {
3621 int res;
3622 Py_ssize_t len;
3623 Py_ssize_t i;
3624 tmp = PyObject_GetAttrString(obj, "decorator_list");
3625 if (tmp == NULL) goto failed;
3626 if (!PyList_Check(tmp)) {
3627 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3628 goto failed;
3629 }
3630 len = PyList_GET_SIZE(tmp);
3631 decorator_list = asdl_seq_new(len, arena);
3632 if (decorator_list == NULL) goto failed;
3633 for (i = 0; i < len; i++) {
3634 expr_ty value;
3635 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3636 if (res != 0) goto failed;
3637 asdl_seq_SET(decorator_list, i, value);
3638 }
3639 Py_XDECREF(tmp);
3640 tmp = NULL;
3641 } else {
3642 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3643 return 1;
3644 }
3645 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3646 decorator_list, lineno, col_offset, arena);
3647 if (*out == NULL) goto failed;
3648 return 0;
3649 }
3650 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3651 expr_ty value;
3652
3653 if (PyObject_HasAttrString(obj, "value")) {
3654 int res;
3655 tmp = PyObject_GetAttrString(obj, "value");
3656 if (tmp == NULL) goto failed;
3657 res = obj2ast_expr(tmp, &value, arena);
3658 if (res != 0) goto failed;
3659 Py_XDECREF(tmp);
3660 tmp = NULL;
3661 } else {
3662 value = NULL;
3663 }
3664 *out = Return(value, lineno, col_offset, arena);
3665 if (*out == NULL) goto failed;
3666 return 0;
3667 }
3668 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3669 asdl_seq* targets;
3670
3671 if (PyObject_HasAttrString(obj, "targets")) {
3672 int res;
3673 Py_ssize_t len;
3674 Py_ssize_t i;
3675 tmp = PyObject_GetAttrString(obj, "targets");
3676 if (tmp == NULL) goto failed;
3677 if (!PyList_Check(tmp)) {
3678 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3679 goto failed;
3680 }
3681 len = PyList_GET_SIZE(tmp);
3682 targets = asdl_seq_new(len, arena);
3683 if (targets == NULL) goto failed;
3684 for (i = 0; i < len; i++) {
3685 expr_ty value;
3686 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3687 if (res != 0) goto failed;
3688 asdl_seq_SET(targets, i, value);
3689 }
3690 Py_XDECREF(tmp);
3691 tmp = NULL;
3692 } else {
3693 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3694 return 1;
3695 }
3696 *out = Delete(targets, lineno, col_offset, arena);
3697 if (*out == NULL) goto failed;
3698 return 0;
3699 }
3700 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3701 asdl_seq* targets;
3702 expr_ty value;
3703
3704 if (PyObject_HasAttrString(obj, "targets")) {
3705 int res;
3706 Py_ssize_t len;
3707 Py_ssize_t i;
3708 tmp = PyObject_GetAttrString(obj, "targets");
3709 if (tmp == NULL) goto failed;
3710 if (!PyList_Check(tmp)) {
3711 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3712 goto failed;
3713 }
3714 len = PyList_GET_SIZE(tmp);
3715 targets = asdl_seq_new(len, arena);
3716 if (targets == NULL) goto failed;
3717 for (i = 0; i < len; i++) {
3718 expr_ty value;
3719 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3720 if (res != 0) goto failed;
3721 asdl_seq_SET(targets, i, value);
3722 }
3723 Py_XDECREF(tmp);
3724 tmp = NULL;
3725 } else {
3726 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3727 return 1;
3728 }
3729 if (PyObject_HasAttrString(obj, "value")) {
3730 int res;
3731 tmp = PyObject_GetAttrString(obj, "value");
3732 if (tmp == NULL) goto failed;
3733 res = obj2ast_expr(tmp, &value, arena);
3734 if (res != 0) goto failed;
3735 Py_XDECREF(tmp);
3736 tmp = NULL;
3737 } else {
3738 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3739 return 1;
3740 }
3741 *out = Assign(targets, value, lineno, col_offset, arena);
3742 if (*out == NULL) goto failed;
3743 return 0;
3744 }
3745 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3746 expr_ty target;
3747 operator_ty op;
3748 expr_ty value;
3749
3750 if (PyObject_HasAttrString(obj, "target")) {
3751 int res;
3752 tmp = PyObject_GetAttrString(obj, "target");
3753 if (tmp == NULL) goto failed;
3754 res = obj2ast_expr(tmp, &target, arena);
3755 if (res != 0) goto failed;
3756 Py_XDECREF(tmp);
3757 tmp = NULL;
3758 } else {
3759 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3760 return 1;
3761 }
3762 if (PyObject_HasAttrString(obj, "op")) {
3763 int res;
3764 tmp = PyObject_GetAttrString(obj, "op");
3765 if (tmp == NULL) goto failed;
3766 res = obj2ast_operator(tmp, &op, arena);
3767 if (res != 0) goto failed;
3768 Py_XDECREF(tmp);
3769 tmp = NULL;
3770 } else {
3771 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3772 return 1;
3773 }
3774 if (PyObject_HasAttrString(obj, "value")) {
3775 int res;
3776 tmp = PyObject_GetAttrString(obj, "value");
3777 if (tmp == NULL) goto failed;
3778 res = obj2ast_expr(tmp, &value, arena);
3779 if (res != 0) goto failed;
3780 Py_XDECREF(tmp);
3781 tmp = NULL;
3782 } else {
3783 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3784 return 1;
3785 }
3786 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3787 if (*out == NULL) goto failed;
3788 return 0;
3789 }
3790 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3791 expr_ty target;
3792 expr_ty iter;
3793 asdl_seq* body;
3794 asdl_seq* orelse;
3795
3796 if (PyObject_HasAttrString(obj, "target")) {
3797 int res;
3798 tmp = PyObject_GetAttrString(obj, "target");
3799 if (tmp == NULL) goto failed;
3800 res = obj2ast_expr(tmp, &target, arena);
3801 if (res != 0) goto failed;
3802 Py_XDECREF(tmp);
3803 tmp = NULL;
3804 } else {
3805 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3806 return 1;
3807 }
3808 if (PyObject_HasAttrString(obj, "iter")) {
3809 int res;
3810 tmp = PyObject_GetAttrString(obj, "iter");
3811 if (tmp == NULL) goto failed;
3812 res = obj2ast_expr(tmp, &iter, arena);
3813 if (res != 0) goto failed;
3814 Py_XDECREF(tmp);
3815 tmp = NULL;
3816 } else {
3817 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3818 return 1;
3819 }
3820 if (PyObject_HasAttrString(obj, "body")) {
3821 int res;
3822 Py_ssize_t len;
3823 Py_ssize_t i;
3824 tmp = PyObject_GetAttrString(obj, "body");
3825 if (tmp == NULL) goto failed;
3826 if (!PyList_Check(tmp)) {
3827 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3828 goto failed;
3829 }
3830 len = PyList_GET_SIZE(tmp);
3831 body = asdl_seq_new(len, arena);
3832 if (body == NULL) goto failed;
3833 for (i = 0; i < len; i++) {
3834 stmt_ty value;
3835 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3836 if (res != 0) goto failed;
3837 asdl_seq_SET(body, i, value);
3838 }
3839 Py_XDECREF(tmp);
3840 tmp = NULL;
3841 } else {
3842 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3843 return 1;
3844 }
3845 if (PyObject_HasAttrString(obj, "orelse")) {
3846 int res;
3847 Py_ssize_t len;
3848 Py_ssize_t i;
3849 tmp = PyObject_GetAttrString(obj, "orelse");
3850 if (tmp == NULL) goto failed;
3851 if (!PyList_Check(tmp)) {
3852 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3853 goto failed;
3854 }
3855 len = PyList_GET_SIZE(tmp);
3856 orelse = asdl_seq_new(len, arena);
3857 if (orelse == NULL) goto failed;
3858 for (i = 0; i < len; i++) {
3859 stmt_ty value;
3860 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3861 if (res != 0) goto failed;
3862 asdl_seq_SET(orelse, i, value);
3863 }
3864 Py_XDECREF(tmp);
3865 tmp = NULL;
3866 } else {
3867 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3868 return 1;
3869 }
3870 *out = For(target, iter, body, orelse, lineno, col_offset,
3871 arena);
3872 if (*out == NULL) goto failed;
3873 return 0;
3874 }
3875 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3876 expr_ty test;
3877 asdl_seq* body;
3878 asdl_seq* orelse;
3879
3880 if (PyObject_HasAttrString(obj, "test")) {
3881 int res;
3882 tmp = PyObject_GetAttrString(obj, "test");
3883 if (tmp == NULL) goto failed;
3884 res = obj2ast_expr(tmp, &test, arena);
3885 if (res != 0) goto failed;
3886 Py_XDECREF(tmp);
3887 tmp = NULL;
3888 } else {
3889 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3890 return 1;
3891 }
3892 if (PyObject_HasAttrString(obj, "body")) {
3893 int res;
3894 Py_ssize_t len;
3895 Py_ssize_t i;
3896 tmp = PyObject_GetAttrString(obj, "body");
3897 if (tmp == NULL) goto failed;
3898 if (!PyList_Check(tmp)) {
3899 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3900 goto failed;
3901 }
3902 len = PyList_GET_SIZE(tmp);
3903 body = asdl_seq_new(len, arena);
3904 if (body == NULL) goto failed;
3905 for (i = 0; i < len; i++) {
3906 stmt_ty value;
3907 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3908 if (res != 0) goto failed;
3909 asdl_seq_SET(body, i, value);
3910 }
3911 Py_XDECREF(tmp);
3912 tmp = NULL;
3913 } else {
3914 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3915 return 1;
3916 }
3917 if (PyObject_HasAttrString(obj, "orelse")) {
3918 int res;
3919 Py_ssize_t len;
3920 Py_ssize_t i;
3921 tmp = PyObject_GetAttrString(obj, "orelse");
3922 if (tmp == NULL) goto failed;
3923 if (!PyList_Check(tmp)) {
3924 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3925 goto failed;
3926 }
3927 len = PyList_GET_SIZE(tmp);
3928 orelse = asdl_seq_new(len, arena);
3929 if (orelse == NULL) goto failed;
3930 for (i = 0; i < len; i++) {
3931 stmt_ty value;
3932 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3933 if (res != 0) goto failed;
3934 asdl_seq_SET(orelse, i, value);
3935 }
3936 Py_XDECREF(tmp);
3937 tmp = NULL;
3938 } else {
3939 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3940 return 1;
3941 }
3942 *out = While(test, body, orelse, lineno, col_offset, arena);
3943 if (*out == NULL) goto failed;
3944 return 0;
3945 }
3946 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3947 expr_ty test;
3948 asdl_seq* body;
3949 asdl_seq* orelse;
3950
3951 if (PyObject_HasAttrString(obj, "test")) {
3952 int res;
3953 tmp = PyObject_GetAttrString(obj, "test");
3954 if (tmp == NULL) goto failed;
3955 res = obj2ast_expr(tmp, &test, arena);
3956 if (res != 0) goto failed;
3957 Py_XDECREF(tmp);
3958 tmp = NULL;
3959 } else {
3960 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3961 return 1;
3962 }
3963 if (PyObject_HasAttrString(obj, "body")) {
3964 int res;
3965 Py_ssize_t len;
3966 Py_ssize_t i;
3967 tmp = PyObject_GetAttrString(obj, "body");
3968 if (tmp == NULL) goto failed;
3969 if (!PyList_Check(tmp)) {
3970 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3971 goto failed;
3972 }
3973 len = PyList_GET_SIZE(tmp);
3974 body = asdl_seq_new(len, arena);
3975 if (body == NULL) goto failed;
3976 for (i = 0; i < len; i++) {
3977 stmt_ty value;
3978 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3979 if (res != 0) goto failed;
3980 asdl_seq_SET(body, i, value);
3981 }
3982 Py_XDECREF(tmp);
3983 tmp = NULL;
3984 } else {
3985 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3986 return 1;
3987 }
3988 if (PyObject_HasAttrString(obj, "orelse")) {
3989 int res;
3990 Py_ssize_t len;
3991 Py_ssize_t i;
3992 tmp = PyObject_GetAttrString(obj, "orelse");
3993 if (tmp == NULL) goto failed;
3994 if (!PyList_Check(tmp)) {
3995 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3996 goto failed;
3997 }
3998 len = PyList_GET_SIZE(tmp);
3999 orelse = asdl_seq_new(len, arena);
4000 if (orelse == NULL) goto failed;
4001 for (i = 0; i < len; i++) {
4002 stmt_ty value;
4003 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4004 if (res != 0) goto failed;
4005 asdl_seq_SET(orelse, i, value);
4006 }
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4011 return 1;
4012 }
4013 *out = If(test, body, orelse, lineno, col_offset, arena);
4014 if (*out == NULL) goto failed;
4015 return 0;
4016 }
4017 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
4018 expr_ty context_expr;
4019 expr_ty optional_vars;
4020 asdl_seq* body;
4021
4022 if (PyObject_HasAttrString(obj, "context_expr")) {
4023 int res;
4024 tmp = PyObject_GetAttrString(obj, "context_expr");
4025 if (tmp == NULL) goto failed;
4026 res = obj2ast_expr(tmp, &context_expr, arena);
4027 if (res != 0) goto failed;
4028 Py_XDECREF(tmp);
4029 tmp = NULL;
4030 } else {
4031 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4032 return 1;
4033 }
4034 if (PyObject_HasAttrString(obj, "optional_vars")) {
4035 int res;
4036 tmp = PyObject_GetAttrString(obj, "optional_vars");
4037 if (tmp == NULL) goto failed;
4038 res = obj2ast_expr(tmp, &optional_vars, arena);
4039 if (res != 0) goto failed;
4040 Py_XDECREF(tmp);
4041 tmp = NULL;
4042 } else {
4043 optional_vars = NULL;
4044 }
4045 if (PyObject_HasAttrString(obj, "body")) {
4046 int res;
4047 Py_ssize_t len;
4048 Py_ssize_t i;
4049 tmp = PyObject_GetAttrString(obj, "body");
4050 if (tmp == NULL) goto failed;
4051 if (!PyList_Check(tmp)) {
4052 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4053 goto failed;
4054 }
4055 len = PyList_GET_SIZE(tmp);
4056 body = asdl_seq_new(len, arena);
4057 if (body == NULL) goto failed;
4058 for (i = 0; i < len; i++) {
4059 stmt_ty value;
4060 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4061 if (res != 0) goto failed;
4062 asdl_seq_SET(body, i, value);
4063 }
4064 Py_XDECREF(tmp);
4065 tmp = NULL;
4066 } else {
4067 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4068 return 1;
4069 }
4070 *out = With(context_expr, optional_vars, body, lineno,
4071 col_offset, arena);
4072 if (*out == NULL) goto failed;
4073 return 0;
4074 }
4075 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4076 expr_ty exc;
4077 expr_ty cause;
4078
4079 if (PyObject_HasAttrString(obj, "exc")) {
4080 int res;
4081 tmp = PyObject_GetAttrString(obj, "exc");
4082 if (tmp == NULL) goto failed;
4083 res = obj2ast_expr(tmp, &exc, arena);
4084 if (res != 0) goto failed;
4085 Py_XDECREF(tmp);
4086 tmp = NULL;
4087 } else {
4088 exc = NULL;
4089 }
4090 if (PyObject_HasAttrString(obj, "cause")) {
4091 int res;
4092 tmp = PyObject_GetAttrString(obj, "cause");
4093 if (tmp == NULL) goto failed;
4094 res = obj2ast_expr(tmp, &cause, arena);
4095 if (res != 0) goto failed;
4096 Py_XDECREF(tmp);
4097 tmp = NULL;
4098 } else {
4099 cause = NULL;
4100 }
4101 *out = Raise(exc, cause, lineno, col_offset, arena);
4102 if (*out == NULL) goto failed;
4103 return 0;
4104 }
4105 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4106 asdl_seq* body;
4107 asdl_seq* handlers;
4108 asdl_seq* orelse;
4109
4110 if (PyObject_HasAttrString(obj, "body")) {
4111 int res;
4112 Py_ssize_t len;
4113 Py_ssize_t i;
4114 tmp = PyObject_GetAttrString(obj, "body");
4115 if (tmp == NULL) goto failed;
4116 if (!PyList_Check(tmp)) {
4117 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4118 goto failed;
4119 }
4120 len = PyList_GET_SIZE(tmp);
4121 body = asdl_seq_new(len, arena);
4122 if (body == NULL) goto failed;
4123 for (i = 0; i < len; i++) {
4124 stmt_ty value;
4125 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4126 if (res != 0) goto failed;
4127 asdl_seq_SET(body, i, value);
4128 }
4129 Py_XDECREF(tmp);
4130 tmp = NULL;
4131 } else {
4132 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4133 return 1;
4134 }
4135 if (PyObject_HasAttrString(obj, "handlers")) {
4136 int res;
4137 Py_ssize_t len;
4138 Py_ssize_t i;
4139 tmp = PyObject_GetAttrString(obj, "handlers");
4140 if (tmp == NULL) goto failed;
4141 if (!PyList_Check(tmp)) {
4142 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4143 goto failed;
4144 }
4145 len = PyList_GET_SIZE(tmp);
4146 handlers = asdl_seq_new(len, arena);
4147 if (handlers == NULL) goto failed;
4148 for (i = 0; i < len; i++) {
4149 excepthandler_ty value;
4150 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4151 if (res != 0) goto failed;
4152 asdl_seq_SET(handlers, i, value);
4153 }
4154 Py_XDECREF(tmp);
4155 tmp = NULL;
4156 } else {
4157 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4158 return 1;
4159 }
4160 if (PyObject_HasAttrString(obj, "orelse")) {
4161 int res;
4162 Py_ssize_t len;
4163 Py_ssize_t i;
4164 tmp = PyObject_GetAttrString(obj, "orelse");
4165 if (tmp == NULL) goto failed;
4166 if (!PyList_Check(tmp)) {
4167 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4168 goto failed;
4169 }
4170 len = PyList_GET_SIZE(tmp);
4171 orelse = asdl_seq_new(len, arena);
4172 if (orelse == NULL) goto failed;
4173 for (i = 0; i < len; i++) {
4174 stmt_ty value;
4175 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4176 if (res != 0) goto failed;
4177 asdl_seq_SET(orelse, i, value);
4178 }
4179 Py_XDECREF(tmp);
4180 tmp = NULL;
4181 } else {
4182 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4183 return 1;
4184 }
4185 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4186 arena);
4187 if (*out == NULL) goto failed;
4188 return 0;
4189 }
4190 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4191 asdl_seq* body;
4192 asdl_seq* finalbody;
4193
4194 if (PyObject_HasAttrString(obj, "body")) {
4195 int res;
4196 Py_ssize_t len;
4197 Py_ssize_t i;
4198 tmp = PyObject_GetAttrString(obj, "body");
4199 if (tmp == NULL) goto failed;
4200 if (!PyList_Check(tmp)) {
4201 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4202 goto failed;
4203 }
4204 len = PyList_GET_SIZE(tmp);
4205 body = asdl_seq_new(len, arena);
4206 if (body == NULL) goto failed;
4207 for (i = 0; i < len; i++) {
4208 stmt_ty value;
4209 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4210 if (res != 0) goto failed;
4211 asdl_seq_SET(body, i, value);
4212 }
4213 Py_XDECREF(tmp);
4214 tmp = NULL;
4215 } else {
4216 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4217 return 1;
4218 }
4219 if (PyObject_HasAttrString(obj, "finalbody")) {
4220 int res;
4221 Py_ssize_t len;
4222 Py_ssize_t i;
4223 tmp = PyObject_GetAttrString(obj, "finalbody");
4224 if (tmp == NULL) goto failed;
4225 if (!PyList_Check(tmp)) {
4226 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4227 goto failed;
4228 }
4229 len = PyList_GET_SIZE(tmp);
4230 finalbody = asdl_seq_new(len, arena);
4231 if (finalbody == NULL) goto failed;
4232 for (i = 0; i < len; i++) {
4233 stmt_ty value;
4234 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4235 if (res != 0) goto failed;
4236 asdl_seq_SET(finalbody, i, value);
4237 }
4238 Py_XDECREF(tmp);
4239 tmp = NULL;
4240 } else {
4241 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4242 return 1;
4243 }
4244 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4245 if (*out == NULL) goto failed;
4246 return 0;
4247 }
4248 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4249 expr_ty test;
4250 expr_ty msg;
4251
4252 if (PyObject_HasAttrString(obj, "test")) {
4253 int res;
4254 tmp = PyObject_GetAttrString(obj, "test");
4255 if (tmp == NULL) goto failed;
4256 res = obj2ast_expr(tmp, &test, arena);
4257 if (res != 0) goto failed;
4258 Py_XDECREF(tmp);
4259 tmp = NULL;
4260 } else {
4261 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4262 return 1;
4263 }
4264 if (PyObject_HasAttrString(obj, "msg")) {
4265 int res;
4266 tmp = PyObject_GetAttrString(obj, "msg");
4267 if (tmp == NULL) goto failed;
4268 res = obj2ast_expr(tmp, &msg, arena);
4269 if (res != 0) goto failed;
4270 Py_XDECREF(tmp);
4271 tmp = NULL;
4272 } else {
4273 msg = NULL;
4274 }
4275 *out = Assert(test, msg, lineno, col_offset, arena);
4276 if (*out == NULL) goto failed;
4277 return 0;
4278 }
4279 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4280 asdl_seq* names;
4281
4282 if (PyObject_HasAttrString(obj, "names")) {
4283 int res;
4284 Py_ssize_t len;
4285 Py_ssize_t i;
4286 tmp = PyObject_GetAttrString(obj, "names");
4287 if (tmp == NULL) goto failed;
4288 if (!PyList_Check(tmp)) {
4289 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4290 goto failed;
4291 }
4292 len = PyList_GET_SIZE(tmp);
4293 names = asdl_seq_new(len, arena);
4294 if (names == NULL) goto failed;
4295 for (i = 0; i < len; i++) {
4296 alias_ty value;
4297 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4298 if (res != 0) goto failed;
4299 asdl_seq_SET(names, i, value);
4300 }
4301 Py_XDECREF(tmp);
4302 tmp = NULL;
4303 } else {
4304 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4305 return 1;
4306 }
4307 *out = Import(names, lineno, col_offset, arena);
4308 if (*out == NULL) goto failed;
4309 return 0;
4310 }
4311 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4312 identifier module;
4313 asdl_seq* names;
4314 int level;
4315
4316 if (PyObject_HasAttrString(obj, "module")) {
4317 int res;
4318 tmp = PyObject_GetAttrString(obj, "module");
4319 if (tmp == NULL) goto failed;
4320 res = obj2ast_identifier(tmp, &module, arena);
4321 if (res != 0) goto failed;
4322 Py_XDECREF(tmp);
4323 tmp = NULL;
4324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4326 return 1;
4327 }
4328 if (PyObject_HasAttrString(obj, "names")) {
4329 int res;
4330 Py_ssize_t len;
4331 Py_ssize_t i;
4332 tmp = PyObject_GetAttrString(obj, "names");
4333 if (tmp == NULL) goto failed;
4334 if (!PyList_Check(tmp)) {
4335 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4336 goto failed;
4337 }
4338 len = PyList_GET_SIZE(tmp);
4339 names = asdl_seq_new(len, arena);
4340 if (names == NULL) goto failed;
4341 for (i = 0; i < len; i++) {
4342 alias_ty value;
4343 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4344 if (res != 0) goto failed;
4345 asdl_seq_SET(names, i, value);
4346 }
4347 Py_XDECREF(tmp);
4348 tmp = NULL;
4349 } else {
4350 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4351 return 1;
4352 }
4353 if (PyObject_HasAttrString(obj, "level")) {
4354 int res;
4355 tmp = PyObject_GetAttrString(obj, "level");
4356 if (tmp == NULL) goto failed;
4357 res = obj2ast_int(tmp, &level, arena);
4358 if (res != 0) goto failed;
4359 Py_XDECREF(tmp);
4360 tmp = NULL;
4361 } else {
4362 level = 0;
4363 }
4364 *out = ImportFrom(module, names, level, lineno, col_offset,
4365 arena);
4366 if (*out == NULL) goto failed;
4367 return 0;
4368 }
4369 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4370 asdl_seq* names;
4371
4372 if (PyObject_HasAttrString(obj, "names")) {
4373 int res;
4374 Py_ssize_t len;
4375 Py_ssize_t i;
4376 tmp = PyObject_GetAttrString(obj, "names");
4377 if (tmp == NULL) goto failed;
4378 if (!PyList_Check(tmp)) {
4379 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4380 goto failed;
4381 }
4382 len = PyList_GET_SIZE(tmp);
4383 names = asdl_seq_new(len, arena);
4384 if (names == NULL) goto failed;
4385 for (i = 0; i < len; i++) {
4386 identifier value;
4387 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4388 if (res != 0) goto failed;
4389 asdl_seq_SET(names, i, value);
4390 }
4391 Py_XDECREF(tmp);
4392 tmp = NULL;
4393 } else {
4394 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4395 return 1;
4396 }
4397 *out = Global(names, lineno, col_offset, arena);
4398 if (*out == NULL) goto failed;
4399 return 0;
4400 }
4401 if (PyObject_IsInstance(obj, (PyObject*)Nonlocal_type)) {
4402 asdl_seq* names;
4403
4404 if (PyObject_HasAttrString(obj, "names")) {
4405 int res;
4406 Py_ssize_t len;
4407 Py_ssize_t i;
4408 tmp = PyObject_GetAttrString(obj, "names");
4409 if (tmp == NULL) goto failed;
4410 if (!PyList_Check(tmp)) {
4411 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4412 goto failed;
4413 }
4414 len = PyList_GET_SIZE(tmp);
4415 names = asdl_seq_new(len, arena);
4416 if (names == NULL) goto failed;
4417 for (i = 0; i < len; i++) {
4418 identifier value;
4419 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4420 if (res != 0) goto failed;
4421 asdl_seq_SET(names, i, value);
4422 }
4423 Py_XDECREF(tmp);
4424 tmp = NULL;
4425 } else {
4426 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4427 return 1;
4428 }
4429 *out = Nonlocal(names, lineno, col_offset, arena);
4430 if (*out == NULL) goto failed;
4431 return 0;
4432 }
4433 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4434 expr_ty value;
4435
4436 if (PyObject_HasAttrString(obj, "value")) {
4437 int res;
4438 tmp = PyObject_GetAttrString(obj, "value");
4439 if (tmp == NULL) goto failed;
4440 res = obj2ast_expr(tmp, &value, arena);
4441 if (res != 0) goto failed;
4442 Py_XDECREF(tmp);
4443 tmp = NULL;
4444 } else {
4445 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4446 return 1;
4447 }
4448 *out = Expr(value, lineno, col_offset, arena);
4449 if (*out == NULL) goto failed;
4450 return 0;
4451 }
4452 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4453
4454 *out = Pass(lineno, col_offset, arena);
4455 if (*out == NULL) goto failed;
4456 return 0;
4457 }
4458 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4459
4460 *out = Break(lineno, col_offset, arena);
4461 if (*out == NULL) goto failed;
4462 return 0;
4463 }
4464 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4465
4466 *out = Continue(lineno, col_offset, arena);
4467 if (*out == NULL) goto failed;
4468 return 0;
4469 }
4470
4471 tmp = PyObject_Repr(obj);
4472 if (tmp == NULL) goto failed;
4473 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4474failed:
4475 Py_XDECREF(tmp);
4476 return 1;
4477}
4478
4479int
4480obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4481{
4482 PyObject* tmp = NULL;
4483
4484 int lineno;
4485 int col_offset;
4486
4487 if (obj == Py_None) {
4488 *out = NULL;
4489 return 0;
4490 }
4491 if (PyObject_HasAttrString(obj, "lineno")) {
4492 int res;
4493 tmp = PyObject_GetAttrString(obj, "lineno");
4494 if (tmp == NULL) goto failed;
4495 res = obj2ast_int(tmp, &lineno, arena);
4496 if (res != 0) goto failed;
4497 Py_XDECREF(tmp);
4498 tmp = NULL;
4499 } else {
4500 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4501 return 1;
4502 }
4503 if (PyObject_HasAttrString(obj, "col_offset")) {
4504 int res;
4505 tmp = PyObject_GetAttrString(obj, "col_offset");
4506 if (tmp == NULL) goto failed;
4507 res = obj2ast_int(tmp, &col_offset, arena);
4508 if (res != 0) goto failed;
4509 Py_XDECREF(tmp);
4510 tmp = NULL;
4511 } else {
4512 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4513 return 1;
4514 }
4515 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4516 boolop_ty op;
4517 asdl_seq* values;
4518
4519 if (PyObject_HasAttrString(obj, "op")) {
4520 int res;
4521 tmp = PyObject_GetAttrString(obj, "op");
4522 if (tmp == NULL) goto failed;
4523 res = obj2ast_boolop(tmp, &op, arena);
4524 if (res != 0) goto failed;
4525 Py_XDECREF(tmp);
4526 tmp = NULL;
4527 } else {
4528 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4529 return 1;
4530 }
4531 if (PyObject_HasAttrString(obj, "values")) {
4532 int res;
4533 Py_ssize_t len;
4534 Py_ssize_t i;
4535 tmp = PyObject_GetAttrString(obj, "values");
4536 if (tmp == NULL) goto failed;
4537 if (!PyList_Check(tmp)) {
4538 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4539 goto failed;
4540 }
4541 len = PyList_GET_SIZE(tmp);
4542 values = asdl_seq_new(len, arena);
4543 if (values == NULL) goto failed;
4544 for (i = 0; i < len; i++) {
4545 expr_ty value;
4546 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4547 if (res != 0) goto failed;
4548 asdl_seq_SET(values, i, value);
4549 }
4550 Py_XDECREF(tmp);
4551 tmp = NULL;
4552 } else {
4553 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4554 return 1;
4555 }
4556 *out = BoolOp(op, values, lineno, col_offset, arena);
4557 if (*out == NULL) goto failed;
4558 return 0;
4559 }
4560 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4561 expr_ty left;
4562 operator_ty op;
4563 expr_ty right;
4564
4565 if (PyObject_HasAttrString(obj, "left")) {
4566 int res;
4567 tmp = PyObject_GetAttrString(obj, "left");
4568 if (tmp == NULL) goto failed;
4569 res = obj2ast_expr(tmp, &left, arena);
4570 if (res != 0) goto failed;
4571 Py_XDECREF(tmp);
4572 tmp = NULL;
4573 } else {
4574 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4575 return 1;
4576 }
4577 if (PyObject_HasAttrString(obj, "op")) {
4578 int res;
4579 tmp = PyObject_GetAttrString(obj, "op");
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_operator(tmp, &op, arena);
4582 if (res != 0) goto failed;
4583 Py_XDECREF(tmp);
4584 tmp = NULL;
4585 } else {
4586 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4587 return 1;
4588 }
4589 if (PyObject_HasAttrString(obj, "right")) {
4590 int res;
4591 tmp = PyObject_GetAttrString(obj, "right");
4592 if (tmp == NULL) goto failed;
4593 res = obj2ast_expr(tmp, &right, arena);
4594 if (res != 0) goto failed;
4595 Py_XDECREF(tmp);
4596 tmp = NULL;
4597 } else {
4598 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4599 return 1;
4600 }
4601 *out = BinOp(left, op, right, lineno, col_offset, arena);
4602 if (*out == NULL) goto failed;
4603 return 0;
4604 }
4605 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4606 unaryop_ty op;
4607 expr_ty operand;
4608
4609 if (PyObject_HasAttrString(obj, "op")) {
4610 int res;
4611 tmp = PyObject_GetAttrString(obj, "op");
4612 if (tmp == NULL) goto failed;
4613 res = obj2ast_unaryop(tmp, &op, arena);
4614 if (res != 0) goto failed;
4615 Py_XDECREF(tmp);
4616 tmp = NULL;
4617 } else {
4618 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4619 return 1;
4620 }
4621 if (PyObject_HasAttrString(obj, "operand")) {
4622 int res;
4623 tmp = PyObject_GetAttrString(obj, "operand");
4624 if (tmp == NULL) goto failed;
4625 res = obj2ast_expr(tmp, &operand, arena);
4626 if (res != 0) goto failed;
4627 Py_XDECREF(tmp);
4628 tmp = NULL;
4629 } else {
4630 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4631 return 1;
4632 }
4633 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4634 if (*out == NULL) goto failed;
4635 return 0;
4636 }
4637 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4638 arguments_ty args;
4639 expr_ty body;
4640
4641 if (PyObject_HasAttrString(obj, "args")) {
4642 int res;
4643 tmp = PyObject_GetAttrString(obj, "args");
4644 if (tmp == NULL) goto failed;
4645 res = obj2ast_arguments(tmp, &args, arena);
4646 if (res != 0) goto failed;
4647 Py_XDECREF(tmp);
4648 tmp = NULL;
4649 } else {
4650 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4651 return 1;
4652 }
4653 if (PyObject_HasAttrString(obj, "body")) {
4654 int res;
4655 tmp = PyObject_GetAttrString(obj, "body");
4656 if (tmp == NULL) goto failed;
4657 res = obj2ast_expr(tmp, &body, arena);
4658 if (res != 0) goto failed;
4659 Py_XDECREF(tmp);
4660 tmp = NULL;
4661 } else {
4662 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4663 return 1;
4664 }
4665 *out = Lambda(args, body, lineno, col_offset, arena);
4666 if (*out == NULL) goto failed;
4667 return 0;
4668 }
4669 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4670 expr_ty test;
4671 expr_ty body;
4672 expr_ty orelse;
4673
4674 if (PyObject_HasAttrString(obj, "test")) {
4675 int res;
4676 tmp = PyObject_GetAttrString(obj, "test");
4677 if (tmp == NULL) goto failed;
4678 res = obj2ast_expr(tmp, &test, arena);
4679 if (res != 0) goto failed;
4680 Py_XDECREF(tmp);
4681 tmp = NULL;
4682 } else {
4683 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4684 return 1;
4685 }
4686 if (PyObject_HasAttrString(obj, "body")) {
4687 int res;
4688 tmp = PyObject_GetAttrString(obj, "body");
4689 if (tmp == NULL) goto failed;
4690 res = obj2ast_expr(tmp, &body, arena);
4691 if (res != 0) goto failed;
4692 Py_XDECREF(tmp);
4693 tmp = NULL;
4694 } else {
4695 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4696 return 1;
4697 }
4698 if (PyObject_HasAttrString(obj, "orelse")) {
4699 int res;
4700 tmp = PyObject_GetAttrString(obj, "orelse");
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_expr(tmp, &orelse, arena);
4703 if (res != 0) goto failed;
4704 Py_XDECREF(tmp);
4705 tmp = NULL;
4706 } else {
4707 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4708 return 1;
4709 }
4710 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4711 if (*out == NULL) goto failed;
4712 return 0;
4713 }
4714 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4715 asdl_seq* keys;
4716 asdl_seq* values;
4717
4718 if (PyObject_HasAttrString(obj, "keys")) {
4719 int res;
4720 Py_ssize_t len;
4721 Py_ssize_t i;
4722 tmp = PyObject_GetAttrString(obj, "keys");
4723 if (tmp == NULL) goto failed;
4724 if (!PyList_Check(tmp)) {
4725 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4726 goto failed;
4727 }
4728 len = PyList_GET_SIZE(tmp);
4729 keys = asdl_seq_new(len, arena);
4730 if (keys == NULL) goto failed;
4731 for (i = 0; i < len; i++) {
4732 expr_ty value;
4733 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4734 if (res != 0) goto failed;
4735 asdl_seq_SET(keys, i, value);
4736 }
4737 Py_XDECREF(tmp);
4738 tmp = NULL;
4739 } else {
4740 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4741 return 1;
4742 }
4743 if (PyObject_HasAttrString(obj, "values")) {
4744 int res;
4745 Py_ssize_t len;
4746 Py_ssize_t i;
4747 tmp = PyObject_GetAttrString(obj, "values");
4748 if (tmp == NULL) goto failed;
4749 if (!PyList_Check(tmp)) {
4750 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4751 goto failed;
4752 }
4753 len = PyList_GET_SIZE(tmp);
4754 values = asdl_seq_new(len, arena);
4755 if (values == NULL) goto failed;
4756 for (i = 0; i < len; i++) {
4757 expr_ty value;
4758 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4759 if (res != 0) goto failed;
4760 asdl_seq_SET(values, i, value);
4761 }
4762 Py_XDECREF(tmp);
4763 tmp = NULL;
4764 } else {
4765 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4766 return 1;
4767 }
4768 *out = Dict(keys, values, lineno, col_offset, arena);
4769 if (*out == NULL) goto failed;
4770 return 0;
4771 }
4772 if (PyObject_IsInstance(obj, (PyObject*)Set_type)) {
4773 asdl_seq* elts;
4774
4775 if (PyObject_HasAttrString(obj, "elts")) {
4776 int res;
4777 Py_ssize_t len;
4778 Py_ssize_t i;
4779 tmp = PyObject_GetAttrString(obj, "elts");
4780 if (tmp == NULL) goto failed;
4781 if (!PyList_Check(tmp)) {
4782 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4783 goto failed;
4784 }
4785 len = PyList_GET_SIZE(tmp);
4786 elts = asdl_seq_new(len, arena);
4787 if (elts == NULL) goto failed;
4788 for (i = 0; i < len; i++) {
4789 expr_ty value;
4790 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4791 if (res != 0) goto failed;
4792 asdl_seq_SET(elts, i, value);
4793 }
4794 Py_XDECREF(tmp);
4795 tmp = NULL;
4796 } else {
4797 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4798 return 1;
4799 }
4800 *out = Set(elts, lineno, col_offset, arena);
4801 if (*out == NULL) goto failed;
4802 return 0;
4803 }
4804 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4805 expr_ty elt;
4806 asdl_seq* generators;
4807
4808 if (PyObject_HasAttrString(obj, "elt")) {
4809 int res;
4810 tmp = PyObject_GetAttrString(obj, "elt");
4811 if (tmp == NULL) goto failed;
4812 res = obj2ast_expr(tmp, &elt, arena);
4813 if (res != 0) goto failed;
4814 Py_XDECREF(tmp);
4815 tmp = NULL;
4816 } else {
4817 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4818 return 1;
4819 }
4820 if (PyObject_HasAttrString(obj, "generators")) {
4821 int res;
4822 Py_ssize_t len;
4823 Py_ssize_t i;
4824 tmp = PyObject_GetAttrString(obj, "generators");
4825 if (tmp == NULL) goto failed;
4826 if (!PyList_Check(tmp)) {
4827 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4828 goto failed;
4829 }
4830 len = PyList_GET_SIZE(tmp);
4831 generators = asdl_seq_new(len, arena);
4832 if (generators == NULL) goto failed;
4833 for (i = 0; i < len; i++) {
4834 comprehension_ty value;
4835 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4836 if (res != 0) goto failed;
4837 asdl_seq_SET(generators, i, value);
4838 }
4839 Py_XDECREF(tmp);
4840 tmp = NULL;
4841 } else {
4842 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4843 return 1;
4844 }
4845 *out = ListComp(elt, generators, lineno, col_offset, arena);
4846 if (*out == NULL) goto failed;
4847 return 0;
4848 }
4849 if (PyObject_IsInstance(obj, (PyObject*)SetComp_type)) {
4850 expr_ty elt;
4851 asdl_seq* generators;
4852
4853 if (PyObject_HasAttrString(obj, "elt")) {
4854 int res;
4855 tmp = PyObject_GetAttrString(obj, "elt");
4856 if (tmp == NULL) goto failed;
4857 res = obj2ast_expr(tmp, &elt, arena);
4858 if (res != 0) goto failed;
4859 Py_XDECREF(tmp);
4860 tmp = NULL;
4861 } else {
4862 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
4863 return 1;
4864 }
4865 if (PyObject_HasAttrString(obj, "generators")) {
4866 int res;
4867 Py_ssize_t len;
4868 Py_ssize_t i;
4869 tmp = PyObject_GetAttrString(obj, "generators");
4870 if (tmp == NULL) goto failed;
4871 if (!PyList_Check(tmp)) {
4872 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4873 goto failed;
4874 }
4875 len = PyList_GET_SIZE(tmp);
4876 generators = asdl_seq_new(len, arena);
4877 if (generators == NULL) goto failed;
4878 for (i = 0; i < len; i++) {
4879 comprehension_ty value;
4880 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4881 if (res != 0) goto failed;
4882 asdl_seq_SET(generators, i, value);
4883 }
4884 Py_XDECREF(tmp);
4885 tmp = NULL;
4886 } else {
4887 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
4888 return 1;
4889 }
4890 *out = SetComp(elt, generators, lineno, col_offset, arena);
4891 if (*out == NULL) goto failed;
4892 return 0;
4893 }
4894 if (PyObject_IsInstance(obj, (PyObject*)DictComp_type)) {
4895 expr_ty key;
4896 expr_ty value;
4897 asdl_seq* generators;
4898
4899 if (PyObject_HasAttrString(obj, "key")) {
4900 int res;
4901 tmp = PyObject_GetAttrString(obj, "key");
4902 if (tmp == NULL) goto failed;
4903 res = obj2ast_expr(tmp, &key, arena);
4904 if (res != 0) goto failed;
4905 Py_XDECREF(tmp);
4906 tmp = NULL;
4907 } else {
4908 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
4909 return 1;
4910 }
4911 if (PyObject_HasAttrString(obj, "value")) {
4912 int res;
4913 tmp = PyObject_GetAttrString(obj, "value");
4914 if (tmp == NULL) goto failed;
4915 res = obj2ast_expr(tmp, &value, arena);
4916 if (res != 0) goto failed;
4917 Py_XDECREF(tmp);
4918 tmp = NULL;
4919 } else {
4920 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
4921 return 1;
4922 }
4923 if (PyObject_HasAttrString(obj, "generators")) {
4924 int res;
4925 Py_ssize_t len;
4926 Py_ssize_t i;
4927 tmp = PyObject_GetAttrString(obj, "generators");
4928 if (tmp == NULL) goto failed;
4929 if (!PyList_Check(tmp)) {
4930 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4931 goto failed;
4932 }
4933 len = PyList_GET_SIZE(tmp);
4934 generators = asdl_seq_new(len, arena);
4935 if (generators == NULL) goto failed;
4936 for (i = 0; i < len; i++) {
4937 comprehension_ty value;
4938 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4939 if (res != 0) goto failed;
4940 asdl_seq_SET(generators, i, value);
4941 }
4942 Py_XDECREF(tmp);
4943 tmp = NULL;
4944 } else {
4945 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
4946 return 1;
4947 }
4948 *out = DictComp(key, value, generators, lineno, col_offset,
4949 arena);
4950 if (*out == NULL) goto failed;
4951 return 0;
4952 }
4953 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4954 expr_ty elt;
4955 asdl_seq* generators;
4956
4957 if (PyObject_HasAttrString(obj, "elt")) {
4958 int res;
4959 tmp = PyObject_GetAttrString(obj, "elt");
4960 if (tmp == NULL) goto failed;
4961 res = obj2ast_expr(tmp, &elt, arena);
4962 if (res != 0) goto failed;
4963 Py_XDECREF(tmp);
4964 tmp = NULL;
4965 } else {
4966 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4967 return 1;
4968 }
4969 if (PyObject_HasAttrString(obj, "generators")) {
4970 int res;
4971 Py_ssize_t len;
4972 Py_ssize_t i;
4973 tmp = PyObject_GetAttrString(obj, "generators");
4974 if (tmp == NULL) goto failed;
4975 if (!PyList_Check(tmp)) {
4976 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4977 goto failed;
4978 }
4979 len = PyList_GET_SIZE(tmp);
4980 generators = asdl_seq_new(len, arena);
4981 if (generators == NULL) goto failed;
4982 for (i = 0; i < len; i++) {
4983 comprehension_ty value;
4984 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4985 if (res != 0) goto failed;
4986 asdl_seq_SET(generators, i, value);
4987 }
4988 Py_XDECREF(tmp);
4989 tmp = NULL;
4990 } else {
4991 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4992 return 1;
4993 }
4994 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4995 if (*out == NULL) goto failed;
4996 return 0;
4997 }
4998 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4999 expr_ty value;
5000
5001 if (PyObject_HasAttrString(obj, "value")) {
5002 int res;
5003 tmp = PyObject_GetAttrString(obj, "value");
5004 if (tmp == NULL) goto failed;
5005 res = obj2ast_expr(tmp, &value, arena);
5006 if (res != 0) goto failed;
5007 Py_XDECREF(tmp);
5008 tmp = NULL;
5009 } else {
5010 value = NULL;
5011 }
5012 *out = Yield(value, lineno, col_offset, arena);
5013 if (*out == NULL) goto failed;
5014 return 0;
5015 }
5016 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
5017 expr_ty left;
5018 asdl_int_seq* ops;
5019 asdl_seq* comparators;
5020
5021 if (PyObject_HasAttrString(obj, "left")) {
5022 int res;
5023 tmp = PyObject_GetAttrString(obj, "left");
5024 if (tmp == NULL) goto failed;
5025 res = obj2ast_expr(tmp, &left, arena);
5026 if (res != 0) goto failed;
5027 Py_XDECREF(tmp);
5028 tmp = NULL;
5029 } else {
5030 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5031 return 1;
5032 }
5033 if (PyObject_HasAttrString(obj, "ops")) {
5034 int res;
5035 Py_ssize_t len;
5036 Py_ssize_t i;
5037 tmp = PyObject_GetAttrString(obj, "ops");
5038 if (tmp == NULL) goto failed;
5039 if (!PyList_Check(tmp)) {
5040 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5041 goto failed;
5042 }
5043 len = PyList_GET_SIZE(tmp);
5044 ops = asdl_int_seq_new(len, arena);
5045 if (ops == NULL) goto failed;
5046 for (i = 0; i < len; i++) {
5047 cmpop_ty value;
5048 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5049 if (res != 0) goto failed;
5050 asdl_seq_SET(ops, i, value);
5051 }
5052 Py_XDECREF(tmp);
5053 tmp = NULL;
5054 } else {
5055 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5056 return 1;
5057 }
5058 if (PyObject_HasAttrString(obj, "comparators")) {
5059 int res;
5060 Py_ssize_t len;
5061 Py_ssize_t i;
5062 tmp = PyObject_GetAttrString(obj, "comparators");
5063 if (tmp == NULL) goto failed;
5064 if (!PyList_Check(tmp)) {
5065 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5066 goto failed;
5067 }
5068 len = PyList_GET_SIZE(tmp);
5069 comparators = asdl_seq_new(len, arena);
5070 if (comparators == NULL) goto failed;
5071 for (i = 0; i < len; i++) {
5072 expr_ty value;
5073 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5074 if (res != 0) goto failed;
5075 asdl_seq_SET(comparators, i, value);
5076 }
5077 Py_XDECREF(tmp);
5078 tmp = NULL;
5079 } else {
5080 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5081 return 1;
5082 }
5083 *out = Compare(left, ops, comparators, lineno, col_offset,
5084 arena);
5085 if (*out == NULL) goto failed;
5086 return 0;
5087 }
5088 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
5089 expr_ty func;
5090 asdl_seq* args;
5091 asdl_seq* keywords;
5092 expr_ty starargs;
5093 expr_ty kwargs;
5094
5095 if (PyObject_HasAttrString(obj, "func")) {
5096 int res;
5097 tmp = PyObject_GetAttrString(obj, "func");
5098 if (tmp == NULL) goto failed;
5099 res = obj2ast_expr(tmp, &func, arena);
5100 if (res != 0) goto failed;
5101 Py_XDECREF(tmp);
5102 tmp = NULL;
5103 } else {
5104 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5105 return 1;
5106 }
5107 if (PyObject_HasAttrString(obj, "args")) {
5108 int res;
5109 Py_ssize_t len;
5110 Py_ssize_t i;
5111 tmp = PyObject_GetAttrString(obj, "args");
5112 if (tmp == NULL) goto failed;
5113 if (!PyList_Check(tmp)) {
5114 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5115 goto failed;
5116 }
5117 len = PyList_GET_SIZE(tmp);
5118 args = asdl_seq_new(len, arena);
5119 if (args == NULL) goto failed;
5120 for (i = 0; i < len; i++) {
5121 expr_ty value;
5122 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5123 if (res != 0) goto failed;
5124 asdl_seq_SET(args, i, value);
5125 }
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5130 return 1;
5131 }
5132 if (PyObject_HasAttrString(obj, "keywords")) {
5133 int res;
5134 Py_ssize_t len;
5135 Py_ssize_t i;
5136 tmp = PyObject_GetAttrString(obj, "keywords");
5137 if (tmp == NULL) goto failed;
5138 if (!PyList_Check(tmp)) {
5139 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5140 goto failed;
5141 }
5142 len = PyList_GET_SIZE(tmp);
5143 keywords = asdl_seq_new(len, arena);
5144 if (keywords == NULL) goto failed;
5145 for (i = 0; i < len; i++) {
5146 keyword_ty value;
5147 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5148 if (res != 0) goto failed;
5149 asdl_seq_SET(keywords, i, value);
5150 }
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "starargs")) {
5158 int res;
5159 tmp = PyObject_GetAttrString(obj, "starargs");
5160 if (tmp == NULL) goto failed;
5161 res = obj2ast_expr(tmp, &starargs, arena);
5162 if (res != 0) goto failed;
5163 Py_XDECREF(tmp);
5164 tmp = NULL;
5165 } else {
5166 starargs = NULL;
5167 }
5168 if (PyObject_HasAttrString(obj, "kwargs")) {
5169 int res;
5170 tmp = PyObject_GetAttrString(obj, "kwargs");
5171 if (tmp == NULL) goto failed;
5172 res = obj2ast_expr(tmp, &kwargs, arena);
5173 if (res != 0) goto failed;
5174 Py_XDECREF(tmp);
5175 tmp = NULL;
5176 } else {
5177 kwargs = NULL;
5178 }
5179 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5180 col_offset, arena);
5181 if (*out == NULL) goto failed;
5182 return 0;
5183 }
5184 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5185 object n;
5186
5187 if (PyObject_HasAttrString(obj, "n")) {
5188 int res;
5189 tmp = PyObject_GetAttrString(obj, "n");
5190 if (tmp == NULL) goto failed;
5191 res = obj2ast_object(tmp, &n, arena);
5192 if (res != 0) goto failed;
5193 Py_XDECREF(tmp);
5194 tmp = NULL;
5195 } else {
5196 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5197 return 1;
5198 }
5199 *out = Num(n, lineno, col_offset, arena);
5200 if (*out == NULL) goto failed;
5201 return 0;
5202 }
5203 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5204 string s;
5205
5206 if (PyObject_HasAttrString(obj, "s")) {
5207 int res;
5208 tmp = PyObject_GetAttrString(obj, "s");
5209 if (tmp == NULL) goto failed;
5210 res = obj2ast_string(tmp, &s, arena);
5211 if (res != 0) goto failed;
5212 Py_XDECREF(tmp);
5213 tmp = NULL;
5214 } else {
5215 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5216 return 1;
5217 }
5218 *out = Str(s, lineno, col_offset, arena);
5219 if (*out == NULL) goto failed;
5220 return 0;
5221 }
5222 if (PyObject_IsInstance(obj, (PyObject*)Bytes_type)) {
5223 string s;
5224
5225 if (PyObject_HasAttrString(obj, "s")) {
5226 int res;
5227 tmp = PyObject_GetAttrString(obj, "s");
5228 if (tmp == NULL) goto failed;
5229 res = obj2ast_string(tmp, &s, arena);
5230 if (res != 0) goto failed;
5231 Py_XDECREF(tmp);
5232 tmp = NULL;
5233 } else {
5234 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5235 return 1;
5236 }
5237 *out = Bytes(s, lineno, col_offset, arena);
5238 if (*out == NULL) goto failed;
5239 return 0;
5240 }
5241 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5242
5243 *out = Ellipsis(lineno, col_offset, arena);
5244 if (*out == NULL) goto failed;
5245 return 0;
5246 }
5247 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5248 expr_ty value;
5249 identifier attr;
5250 expr_context_ty ctx;
5251
5252 if (PyObject_HasAttrString(obj, "value")) {
5253 int res;
5254 tmp = PyObject_GetAttrString(obj, "value");
5255 if (tmp == NULL) goto failed;
5256 res = obj2ast_expr(tmp, &value, arena);
5257 if (res != 0) goto failed;
5258 Py_XDECREF(tmp);
5259 tmp = NULL;
5260 } else {
5261 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5262 return 1;
5263 }
5264 if (PyObject_HasAttrString(obj, "attr")) {
5265 int res;
5266 tmp = PyObject_GetAttrString(obj, "attr");
5267 if (tmp == NULL) goto failed;
5268 res = obj2ast_identifier(tmp, &attr, arena);
5269 if (res != 0) goto failed;
5270 Py_XDECREF(tmp);
5271 tmp = NULL;
5272 } else {
5273 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5274 return 1;
5275 }
5276 if (PyObject_HasAttrString(obj, "ctx")) {
5277 int res;
5278 tmp = PyObject_GetAttrString(obj, "ctx");
5279 if (tmp == NULL) goto failed;
5280 res = obj2ast_expr_context(tmp, &ctx, arena);
5281 if (res != 0) goto failed;
5282 Py_XDECREF(tmp);
5283 tmp = NULL;
5284 } else {
5285 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5286 return 1;
5287 }
5288 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5289 if (*out == NULL) goto failed;
5290 return 0;
5291 }
5292 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5293 expr_ty value;
5294 slice_ty slice;
5295 expr_context_ty ctx;
5296
5297 if (PyObject_HasAttrString(obj, "value")) {
5298 int res;
5299 tmp = PyObject_GetAttrString(obj, "value");
5300 if (tmp == NULL) goto failed;
5301 res = obj2ast_expr(tmp, &value, arena);
5302 if (res != 0) goto failed;
5303 Py_XDECREF(tmp);
5304 tmp = NULL;
5305 } else {
5306 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5307 return 1;
5308 }
5309 if (PyObject_HasAttrString(obj, "slice")) {
5310 int res;
5311 tmp = PyObject_GetAttrString(obj, "slice");
5312 if (tmp == NULL) goto failed;
5313 res = obj2ast_slice(tmp, &slice, arena);
5314 if (res != 0) goto failed;
5315 Py_XDECREF(tmp);
5316 tmp = NULL;
5317 } else {
5318 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5319 return 1;
5320 }
5321 if (PyObject_HasAttrString(obj, "ctx")) {
5322 int res;
5323 tmp = PyObject_GetAttrString(obj, "ctx");
5324 if (tmp == NULL) goto failed;
5325 res = obj2ast_expr_context(tmp, &ctx, arena);
5326 if (res != 0) goto failed;
5327 Py_XDECREF(tmp);
5328 tmp = NULL;
5329 } else {
5330 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5331 return 1;
5332 }
5333 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5334 if (*out == NULL) goto failed;
5335 return 0;
5336 }
5337 if (PyObject_IsInstance(obj, (PyObject*)Starred_type)) {
5338 expr_ty value;
5339 expr_context_ty ctx;
5340
5341 if (PyObject_HasAttrString(obj, "value")) {
5342 int res;
5343 tmp = PyObject_GetAttrString(obj, "value");
5344 if (tmp == NULL) goto failed;
5345 res = obj2ast_expr(tmp, &value, arena);
5346 if (res != 0) goto failed;
5347 Py_XDECREF(tmp);
5348 tmp = NULL;
5349 } else {
5350 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5351 return 1;
5352 }
5353 if (PyObject_HasAttrString(obj, "ctx")) {
5354 int res;
5355 tmp = PyObject_GetAttrString(obj, "ctx");
5356 if (tmp == NULL) goto failed;
5357 res = obj2ast_expr_context(tmp, &ctx, arena);
5358 if (res != 0) goto failed;
5359 Py_XDECREF(tmp);
5360 tmp = NULL;
5361 } else {
5362 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5363 return 1;
5364 }
5365 *out = Starred(value, ctx, lineno, col_offset, arena);
5366 if (*out == NULL) goto failed;
5367 return 0;
5368 }
5369 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5370 identifier id;
5371 expr_context_ty ctx;
5372
5373 if (PyObject_HasAttrString(obj, "id")) {
5374 int res;
5375 tmp = PyObject_GetAttrString(obj, "id");
5376 if (tmp == NULL) goto failed;
5377 res = obj2ast_identifier(tmp, &id, arena);
5378 if (res != 0) goto failed;
5379 Py_XDECREF(tmp);
5380 tmp = NULL;
5381 } else {
5382 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5383 return 1;
5384 }
5385 if (PyObject_HasAttrString(obj, "ctx")) {
5386 int res;
5387 tmp = PyObject_GetAttrString(obj, "ctx");
5388 if (tmp == NULL) goto failed;
5389 res = obj2ast_expr_context(tmp, &ctx, arena);
5390 if (res != 0) goto failed;
5391 Py_XDECREF(tmp);
5392 tmp = NULL;
5393 } else {
5394 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5395 return 1;
5396 }
5397 *out = Name(id, ctx, lineno, col_offset, arena);
5398 if (*out == NULL) goto failed;
5399 return 0;
5400 }
5401 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5402 asdl_seq* elts;
5403 expr_context_ty ctx;
5404
5405 if (PyObject_HasAttrString(obj, "elts")) {
5406 int res;
5407 Py_ssize_t len;
5408 Py_ssize_t i;
5409 tmp = PyObject_GetAttrString(obj, "elts");
5410 if (tmp == NULL) goto failed;
5411 if (!PyList_Check(tmp)) {
5412 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5413 goto failed;
5414 }
5415 len = PyList_GET_SIZE(tmp);
5416 elts = asdl_seq_new(len, arena);
5417 if (elts == NULL) goto failed;
5418 for (i = 0; i < len; i++) {
5419 expr_ty value;
5420 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5421 if (res != 0) goto failed;
5422 asdl_seq_SET(elts, i, value);
5423 }
5424 Py_XDECREF(tmp);
5425 tmp = NULL;
5426 } else {
5427 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5428 return 1;
5429 }
5430 if (PyObject_HasAttrString(obj, "ctx")) {
5431 int res;
5432 tmp = PyObject_GetAttrString(obj, "ctx");
5433 if (tmp == NULL) goto failed;
5434 res = obj2ast_expr_context(tmp, &ctx, arena);
5435 if (res != 0) goto failed;
5436 Py_XDECREF(tmp);
5437 tmp = NULL;
5438 } else {
5439 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5440 return 1;
5441 }
5442 *out = List(elts, ctx, lineno, col_offset, arena);
5443 if (*out == NULL) goto failed;
5444 return 0;
5445 }
5446 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5447 asdl_seq* elts;
5448 expr_context_ty ctx;
5449
5450 if (PyObject_HasAttrString(obj, "elts")) {
5451 int res;
5452 Py_ssize_t len;
5453 Py_ssize_t i;
5454 tmp = PyObject_GetAttrString(obj, "elts");
5455 if (tmp == NULL) goto failed;
5456 if (!PyList_Check(tmp)) {
5457 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5458 goto failed;
5459 }
5460 len = PyList_GET_SIZE(tmp);
5461 elts = asdl_seq_new(len, arena);
5462 if (elts == NULL) goto failed;
5463 for (i = 0; i < len; i++) {
5464 expr_ty value;
5465 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5466 if (res != 0) goto failed;
5467 asdl_seq_SET(elts, i, value);
5468 }
5469 Py_XDECREF(tmp);
5470 tmp = NULL;
5471 } else {
5472 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5473 return 1;
5474 }
5475 if (PyObject_HasAttrString(obj, "ctx")) {
5476 int res;
5477 tmp = PyObject_GetAttrString(obj, "ctx");
5478 if (tmp == NULL) goto failed;
5479 res = obj2ast_expr_context(tmp, &ctx, arena);
5480 if (res != 0) goto failed;
5481 Py_XDECREF(tmp);
5482 tmp = NULL;
5483 } else {
5484 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5485 return 1;
5486 }
5487 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5488 if (*out == NULL) goto failed;
5489 return 0;
5490 }
5491
5492 tmp = PyObject_Repr(obj);
5493 if (tmp == NULL) goto failed;
5494 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5495failed:
5496 Py_XDECREF(tmp);
5497 return 1;
5498}
5499
5500int
5501obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5502{
5503 PyObject* tmp = NULL;
5504
5505 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5506 *out = Load;
5507 return 0;
5508 }
5509 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5510 *out = Store;
5511 return 0;
5512 }
5513 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5514 *out = Del;
5515 return 0;
5516 }
5517 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5518 *out = AugLoad;
5519 return 0;
5520 }
5521 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5522 *out = AugStore;
5523 return 0;
5524 }
5525 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5526 *out = Param;
5527 return 0;
5528 }
5529
5530 tmp = PyObject_Repr(obj);
5531 if (tmp == NULL) goto failed;
5532 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5533failed:
5534 Py_XDECREF(tmp);
5535 return 1;
5536}
5537
5538int
5539obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5540{
5541 PyObject* tmp = NULL;
5542
5543
5544 if (obj == Py_None) {
5545 *out = NULL;
5546 return 0;
5547 }
5548 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5549 expr_ty lower;
5550 expr_ty upper;
5551 expr_ty step;
5552
5553 if (PyObject_HasAttrString(obj, "lower")) {
5554 int res;
5555 tmp = PyObject_GetAttrString(obj, "lower");
5556 if (tmp == NULL) goto failed;
5557 res = obj2ast_expr(tmp, &lower, arena);
5558 if (res != 0) goto failed;
5559 Py_XDECREF(tmp);
5560 tmp = NULL;
5561 } else {
5562 lower = NULL;
5563 }
5564 if (PyObject_HasAttrString(obj, "upper")) {
5565 int res;
5566 tmp = PyObject_GetAttrString(obj, "upper");
5567 if (tmp == NULL) goto failed;
5568 res = obj2ast_expr(tmp, &upper, arena);
5569 if (res != 0) goto failed;
5570 Py_XDECREF(tmp);
5571 tmp = NULL;
5572 } else {
5573 upper = NULL;
5574 }
5575 if (PyObject_HasAttrString(obj, "step")) {
5576 int res;
5577 tmp = PyObject_GetAttrString(obj, "step");
5578 if (tmp == NULL) goto failed;
5579 res = obj2ast_expr(tmp, &step, arena);
5580 if (res != 0) goto failed;
5581 Py_XDECREF(tmp);
5582 tmp = NULL;
5583 } else {
5584 step = NULL;
5585 }
5586 *out = Slice(lower, upper, step, arena);
5587 if (*out == NULL) goto failed;
5588 return 0;
5589 }
5590 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5591 asdl_seq* dims;
5592
5593 if (PyObject_HasAttrString(obj, "dims")) {
5594 int res;
5595 Py_ssize_t len;
5596 Py_ssize_t i;
5597 tmp = PyObject_GetAttrString(obj, "dims");
5598 if (tmp == NULL) goto failed;
5599 if (!PyList_Check(tmp)) {
5600 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5601 goto failed;
5602 }
5603 len = PyList_GET_SIZE(tmp);
5604 dims = asdl_seq_new(len, arena);
5605 if (dims == NULL) goto failed;
5606 for (i = 0; i < len; i++) {
5607 slice_ty value;
5608 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5609 if (res != 0) goto failed;
5610 asdl_seq_SET(dims, i, value);
5611 }
5612 Py_XDECREF(tmp);
5613 tmp = NULL;
5614 } else {
5615 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5616 return 1;
5617 }
5618 *out = ExtSlice(dims, arena);
5619 if (*out == NULL) goto failed;
5620 return 0;
5621 }
5622 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5623 expr_ty value;
5624
5625 if (PyObject_HasAttrString(obj, "value")) {
5626 int res;
5627 tmp = PyObject_GetAttrString(obj, "value");
5628 if (tmp == NULL) goto failed;
5629 res = obj2ast_expr(tmp, &value, arena);
5630 if (res != 0) goto failed;
5631 Py_XDECREF(tmp);
5632 tmp = NULL;
5633 } else {
5634 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5635 return 1;
5636 }
5637 *out = Index(value, arena);
5638 if (*out == NULL) goto failed;
5639 return 0;
5640 }
5641
5642 tmp = PyObject_Repr(obj);
5643 if (tmp == NULL) goto failed;
5644 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5645failed:
5646 Py_XDECREF(tmp);
5647 return 1;
5648}
5649
5650int
5651obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5652{
5653 PyObject* tmp = NULL;
5654
5655 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5656 *out = And;
5657 return 0;
5658 }
5659 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5660 *out = Or;
5661 return 0;
5662 }
5663
5664 tmp = PyObject_Repr(obj);
5665 if (tmp == NULL) goto failed;
5666 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5667failed:
5668 Py_XDECREF(tmp);
5669 return 1;
5670}
5671
5672int
5673obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5674{
5675 PyObject* tmp = NULL;
5676
5677 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5678 *out = Add;
5679 return 0;
5680 }
5681 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5682 *out = Sub;
5683 return 0;
5684 }
5685 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5686 *out = Mult;
5687 return 0;
5688 }
5689 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5690 *out = Div;
5691 return 0;
5692 }
5693 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5694 *out = Mod;
5695 return 0;
5696 }
5697 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5698 *out = Pow;
5699 return 0;
5700 }
5701 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5702 *out = LShift;
5703 return 0;
5704 }
5705 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5706 *out = RShift;
5707 return 0;
5708 }
5709 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5710 *out = BitOr;
5711 return 0;
5712 }
5713 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5714 *out = BitXor;
5715 return 0;
5716 }
5717 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5718 *out = BitAnd;
5719 return 0;
5720 }
5721 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5722 *out = FloorDiv;
5723 return 0;
5724 }
5725
5726 tmp = PyObject_Repr(obj);
5727 if (tmp == NULL) goto failed;
5728 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5729failed:
5730 Py_XDECREF(tmp);
5731 return 1;
5732}
5733
5734int
5735obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5736{
5737 PyObject* tmp = NULL;
5738
5739 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5740 *out = Invert;
5741 return 0;
5742 }
5743 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5744 *out = Not;
5745 return 0;
5746 }
5747 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5748 *out = UAdd;
5749 return 0;
5750 }
5751 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5752 *out = USub;
5753 return 0;
5754 }
5755
5756 tmp = PyObject_Repr(obj);
5757 if (tmp == NULL) goto failed;
5758 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5759failed:
5760 Py_XDECREF(tmp);
5761 return 1;
5762}
5763
5764int
5765obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5766{
5767 PyObject* tmp = NULL;
5768
5769 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5770 *out = Eq;
5771 return 0;
5772 }
5773 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5774 *out = NotEq;
5775 return 0;
5776 }
5777 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5778 *out = Lt;
5779 return 0;
5780 }
5781 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5782 *out = LtE;
5783 return 0;
5784 }
5785 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5786 *out = Gt;
5787 return 0;
5788 }
5789 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5790 *out = GtE;
5791 return 0;
5792 }
5793 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5794 *out = Is;
5795 return 0;
5796 }
5797 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5798 *out = IsNot;
5799 return 0;
5800 }
5801 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5802 *out = In;
5803 return 0;
5804 }
5805 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5806 *out = NotIn;
5807 return 0;
5808 }
5809
5810 tmp = PyObject_Repr(obj);
5811 if (tmp == NULL) goto failed;
5812 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5813failed:
5814 Py_XDECREF(tmp);
5815 return 1;
5816}
5817
5818int
5819obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5820{
5821 PyObject* tmp = NULL;
5822 expr_ty target;
5823 expr_ty iter;
5824 asdl_seq* ifs;
5825
5826 if (PyObject_HasAttrString(obj, "target")) {
5827 int res;
5828 tmp = PyObject_GetAttrString(obj, "target");
5829 if (tmp == NULL) goto failed;
5830 res = obj2ast_expr(tmp, &target, arena);
5831 if (res != 0) goto failed;
5832 Py_XDECREF(tmp);
5833 tmp = NULL;
5834 } else {
5835 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5836 return 1;
5837 }
5838 if (PyObject_HasAttrString(obj, "iter")) {
5839 int res;
5840 tmp = PyObject_GetAttrString(obj, "iter");
5841 if (tmp == NULL) goto failed;
5842 res = obj2ast_expr(tmp, &iter, arena);
5843 if (res != 0) goto failed;
5844 Py_XDECREF(tmp);
5845 tmp = NULL;
5846 } else {
5847 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5848 return 1;
5849 }
5850 if (PyObject_HasAttrString(obj, "ifs")) {
5851 int res;
5852 Py_ssize_t len;
5853 Py_ssize_t i;
5854 tmp = PyObject_GetAttrString(obj, "ifs");
5855 if (tmp == NULL) goto failed;
5856 if (!PyList_Check(tmp)) {
5857 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5858 goto failed;
5859 }
5860 len = PyList_GET_SIZE(tmp);
5861 ifs = asdl_seq_new(len, arena);
5862 if (ifs == NULL) goto failed;
5863 for (i = 0; i < len; i++) {
5864 expr_ty value;
5865 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5866 if (res != 0) goto failed;
5867 asdl_seq_SET(ifs, i, value);
5868 }
5869 Py_XDECREF(tmp);
5870 tmp = NULL;
5871 } else {
5872 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5873 return 1;
5874 }
5875 *out = comprehension(target, iter, ifs, arena);
5876 return 0;
5877failed:
5878 Py_XDECREF(tmp);
5879 return 1;
5880}
5881
5882int
5883obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5884{
5885 PyObject* tmp = NULL;
5886 expr_ty type;
5887 identifier name;
5888 asdl_seq* body;
5889 int lineno;
5890 int col_offset;
5891
5892 if (PyObject_HasAttrString(obj, "type")) {
5893 int res;
5894 tmp = PyObject_GetAttrString(obj, "type");
5895 if (tmp == NULL) goto failed;
5896 res = obj2ast_expr(tmp, &type, arena);
5897 if (res != 0) goto failed;
5898 Py_XDECREF(tmp);
5899 tmp = NULL;
5900 } else {
5901 type = NULL;
5902 }
5903 if (PyObject_HasAttrString(obj, "name")) {
5904 int res;
5905 tmp = PyObject_GetAttrString(obj, "name");
5906 if (tmp == NULL) goto failed;
5907 res = obj2ast_identifier(tmp, &name, arena);
5908 if (res != 0) goto failed;
5909 Py_XDECREF(tmp);
5910 tmp = NULL;
5911 } else {
5912 name = NULL;
5913 }
5914 if (PyObject_HasAttrString(obj, "body")) {
5915 int res;
5916 Py_ssize_t len;
5917 Py_ssize_t i;
5918 tmp = PyObject_GetAttrString(obj, "body");
5919 if (tmp == NULL) goto failed;
5920 if (!PyList_Check(tmp)) {
5921 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5922 goto failed;
5923 }
5924 len = PyList_GET_SIZE(tmp);
5925 body = asdl_seq_new(len, arena);
5926 if (body == NULL) goto failed;
5927 for (i = 0; i < len; i++) {
5928 stmt_ty value;
5929 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5930 if (res != 0) goto failed;
5931 asdl_seq_SET(body, i, value);
5932 }
5933 Py_XDECREF(tmp);
5934 tmp = NULL;
5935 } else {
5936 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
5937 return 1;
5938 }
5939 if (PyObject_HasAttrString(obj, "lineno")) {
5940 int res;
5941 tmp = PyObject_GetAttrString(obj, "lineno");
5942 if (tmp == NULL) goto failed;
5943 res = obj2ast_int(tmp, &lineno, arena);
5944 if (res != 0) goto failed;
5945 Py_XDECREF(tmp);
5946 tmp = NULL;
5947 } else {
5948 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5949 return 1;
5950 }
5951 if (PyObject_HasAttrString(obj, "col_offset")) {
5952 int res;
5953 tmp = PyObject_GetAttrString(obj, "col_offset");
5954 if (tmp == NULL) goto failed;
5955 res = obj2ast_int(tmp, &col_offset, arena);
5956 if (res != 0) goto failed;
5957 Py_XDECREF(tmp);
5958 tmp = NULL;
5959 } else {
5960 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5961 return 1;
5962 }
5963 *out = excepthandler(type, name, body, lineno, col_offset, arena);
5964 return 0;
5965failed:
5966 Py_XDECREF(tmp);
5967 return 1;
5968}
5969
5970int
5971obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5972{
5973 PyObject* tmp = NULL;
5974 asdl_seq* args;
5975 identifier vararg;
5976 expr_ty varargannotation;
5977 asdl_seq* kwonlyargs;
5978 identifier kwarg;
5979 expr_ty kwargannotation;
5980 asdl_seq* defaults;
5981 asdl_seq* kw_defaults;
5982
5983 if (PyObject_HasAttrString(obj, "args")) {
5984 int res;
5985 Py_ssize_t len;
5986 Py_ssize_t i;
5987 tmp = PyObject_GetAttrString(obj, "args");
5988 if (tmp == NULL) goto failed;
5989 if (!PyList_Check(tmp)) {
5990 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5991 goto failed;
5992 }
5993 len = PyList_GET_SIZE(tmp);
5994 args = asdl_seq_new(len, arena);
5995 if (args == NULL) goto failed;
5996 for (i = 0; i < len; i++) {
5997 arg_ty value;
5998 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
5999 if (res != 0) goto failed;
6000 asdl_seq_SET(args, i, value);
6001 }
6002 Py_XDECREF(tmp);
6003 tmp = NULL;
6004 } else {
6005 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6006 return 1;
6007 }
6008 if (PyObject_HasAttrString(obj, "vararg")) {
6009 int res;
6010 tmp = PyObject_GetAttrString(obj, "vararg");
6011 if (tmp == NULL) goto failed;
6012 res = obj2ast_identifier(tmp, &vararg, arena);
6013 if (res != 0) goto failed;
6014 Py_XDECREF(tmp);
6015 tmp = NULL;
6016 } else {
6017 vararg = NULL;
6018 }
6019 if (PyObject_HasAttrString(obj, "varargannotation")) {
6020 int res;
6021 tmp = PyObject_GetAttrString(obj, "varargannotation");
6022 if (tmp == NULL) goto failed;
6023 res = obj2ast_expr(tmp, &varargannotation, arena);
6024 if (res != 0) goto failed;
6025 Py_XDECREF(tmp);
6026 tmp = NULL;
6027 } else {
6028 varargannotation = NULL;
6029 }
6030 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6031 int res;
6032 Py_ssize_t len;
6033 Py_ssize_t i;
6034 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6035 if (tmp == NULL) goto failed;
6036 if (!PyList_Check(tmp)) {
6037 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6038 goto failed;
6039 }
6040 len = PyList_GET_SIZE(tmp);
6041 kwonlyargs = asdl_seq_new(len, arena);
6042 if (kwonlyargs == NULL) goto failed;
6043 for (i = 0; i < len; i++) {
6044 arg_ty value;
6045 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6046 if (res != 0) goto failed;
6047 asdl_seq_SET(kwonlyargs, i, value);
6048 }
6049 Py_XDECREF(tmp);
6050 tmp = NULL;
6051 } else {
6052 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6053 return 1;
6054 }
6055 if (PyObject_HasAttrString(obj, "kwarg")) {
6056 int res;
6057 tmp = PyObject_GetAttrString(obj, "kwarg");
6058 if (tmp == NULL) goto failed;
6059 res = obj2ast_identifier(tmp, &kwarg, arena);
6060 if (res != 0) goto failed;
6061 Py_XDECREF(tmp);
6062 tmp = NULL;
6063 } else {
6064 kwarg = NULL;
6065 }
6066 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6067 int res;
6068 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6069 if (tmp == NULL) goto failed;
6070 res = obj2ast_expr(tmp, &kwargannotation, arena);
6071 if (res != 0) goto failed;
6072 Py_XDECREF(tmp);
6073 tmp = NULL;
6074 } else {
6075 kwargannotation = NULL;
6076 }
6077 if (PyObject_HasAttrString(obj, "defaults")) {
6078 int res;
6079 Py_ssize_t len;
6080 Py_ssize_t i;
6081 tmp = PyObject_GetAttrString(obj, "defaults");
6082 if (tmp == NULL) goto failed;
6083 if (!PyList_Check(tmp)) {
6084 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6085 goto failed;
6086 }
6087 len = PyList_GET_SIZE(tmp);
6088 defaults = asdl_seq_new(len, arena);
6089 if (defaults == NULL) goto failed;
6090 for (i = 0; i < len; i++) {
6091 expr_ty value;
6092 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6093 if (res != 0) goto failed;
6094 asdl_seq_SET(defaults, i, value);
6095 }
6096 Py_XDECREF(tmp);
6097 tmp = NULL;
6098 } else {
6099 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6100 return 1;
6101 }
6102 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6103 int res;
6104 Py_ssize_t len;
6105 Py_ssize_t i;
6106 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6107 if (tmp == NULL) goto failed;
6108 if (!PyList_Check(tmp)) {
6109 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6110 goto failed;
6111 }
6112 len = PyList_GET_SIZE(tmp);
6113 kw_defaults = asdl_seq_new(len, arena);
6114 if (kw_defaults == NULL) goto failed;
6115 for (i = 0; i < len; i++) {
6116 expr_ty value;
6117 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6118 if (res != 0) goto failed;
6119 asdl_seq_SET(kw_defaults, i, value);
6120 }
6121 Py_XDECREF(tmp);
6122 tmp = NULL;
6123 } else {
6124 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6125 return 1;
6126 }
6127 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6128 kwargannotation, defaults, kw_defaults, arena);
6129 return 0;
6130failed:
6131 Py_XDECREF(tmp);
6132 return 1;
6133}
6134
6135int
6136obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6137{
6138 PyObject* tmp = NULL;
6139 identifier arg;
6140 expr_ty annotation;
6141
6142 if (PyObject_HasAttrString(obj, "arg")) {
6143 int res;
6144 tmp = PyObject_GetAttrString(obj, "arg");
6145 if (tmp == NULL) goto failed;
6146 res = obj2ast_identifier(tmp, &arg, arena);
6147 if (res != 0) goto failed;
6148 Py_XDECREF(tmp);
6149 tmp = NULL;
6150 } else {
6151 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6152 return 1;
6153 }
6154 if (PyObject_HasAttrString(obj, "annotation")) {
6155 int res;
6156 tmp = PyObject_GetAttrString(obj, "annotation");
6157 if (tmp == NULL) goto failed;
6158 res = obj2ast_expr(tmp, &annotation, arena);
6159 if (res != 0) goto failed;
6160 Py_XDECREF(tmp);
6161 tmp = NULL;
6162 } else {
6163 annotation = NULL;
6164 }
6165 *out = arg(arg, annotation, arena);
6166 return 0;
6167failed:
6168 Py_XDECREF(tmp);
6169 return 1;
6170}
6171
6172int
6173obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6174{
6175 PyObject* tmp = NULL;
6176 identifier arg;
6177 expr_ty value;
6178
6179 if (PyObject_HasAttrString(obj, "arg")) {
6180 int res;
6181 tmp = PyObject_GetAttrString(obj, "arg");
6182 if (tmp == NULL) goto failed;
6183 res = obj2ast_identifier(tmp, &arg, arena);
6184 if (res != 0) goto failed;
6185 Py_XDECREF(tmp);
6186 tmp = NULL;
6187 } else {
6188 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6189 return 1;
6190 }
6191 if (PyObject_HasAttrString(obj, "value")) {
6192 int res;
6193 tmp = PyObject_GetAttrString(obj, "value");
6194 if (tmp == NULL) goto failed;
6195 res = obj2ast_expr(tmp, &value, arena);
6196 if (res != 0) goto failed;
6197 Py_XDECREF(tmp);
6198 tmp = NULL;
6199 } else {
6200 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6201 return 1;
6202 }
6203 *out = keyword(arg, value, arena);
6204 return 0;
6205failed:
6206 Py_XDECREF(tmp);
6207 return 1;
6208}
6209
6210int
6211obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6212{
6213 PyObject* tmp = NULL;
6214 identifier name;
6215 identifier asname;
6216
6217 if (PyObject_HasAttrString(obj, "name")) {
6218 int res;
6219 tmp = PyObject_GetAttrString(obj, "name");
6220 if (tmp == NULL) goto failed;
6221 res = obj2ast_identifier(tmp, &name, arena);
6222 if (res != 0) goto failed;
6223 Py_XDECREF(tmp);
6224 tmp = NULL;
6225 } else {
6226 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6227 return 1;
6228 }
6229 if (PyObject_HasAttrString(obj, "asname")) {
6230 int res;
6231 tmp = PyObject_GetAttrString(obj, "asname");
6232 if (tmp == NULL) goto failed;
6233 res = obj2ast_identifier(tmp, &asname, arena);
6234 if (res != 0) goto failed;
6235 Py_XDECREF(tmp);
6236 tmp = NULL;
6237 } else {
6238 asname = NULL;
6239 }
6240 *out = alias(name, asname, arena);
6241 return 0;
6242failed:
6243 Py_XDECREF(tmp);
6244 return 1;
6245}
6246
6247
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006248PyMODINIT_FUNC
6249init_ast(void)
6250{
6251 PyObject *m, *d;
6252 if (!init_types()) return;
6253 m = Py_InitModule3("_ast", NULL, NULL);
6254 if (!m) return;
6255 d = PyModule_GetDict(m);
6256 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;
6257 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6258 return;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00006259 if (PyModule_AddStringConstant(m, "__version__", "57783") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006260 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006261 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6262 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6263 return;
6264 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6265 < 0) return;
6266 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6267 0) return;
6268 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6269 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6270 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6271 < 0) return;
6272 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6273 return;
6274 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6275 return;
6276 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6277 return;
6278 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6279 return;
6280 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6281 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006282 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6283 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6284 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6285 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6286 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6287 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6288 0) return;
6289 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6290 0) return;
6291 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6292 return;
6293 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6294 return;
6295 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6296 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006297 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6298 return;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006299 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6300 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006301 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6302 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6303 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6304 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6305 return;
6306 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6307 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6308 return;
6309 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6310 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6311 return;
6312 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6313 return;
6314 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6315 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Guido van Rossum86e58e22006-08-28 15:27:34 +00006316 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006317 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6318 return;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006319 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6320 return;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006321 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6322 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006323 if (PyDict_SetItemString(d, "GeneratorExp",
6324 (PyObject*)GeneratorExp_type) < 0) return;
6325 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6326 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6327 return;
6328 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006329 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6330 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
Thomas Wouters00e41de2007-02-23 19:56:57 +00006331 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return;
Georg Brandl52318d62006-09-06 07:06:08 +00006332 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6333 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006334 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6335 0) return;
6336 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6337 0) return;
Guido van Rossum0368b722007-05-11 16:50:42 +00006338 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
6339 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006340 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6341 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6342 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6343 if (PyDict_SetItemString(d, "expr_context",
6344 (PyObject*)expr_context_type) < 0) return;
6345 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6346 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6347 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6348 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6349 return;
6350 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6351 return;
6352 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6353 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006354 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6355 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6356 return;
6357 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6358 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6359 return;
6360 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6361 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6362 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6363 return;
6364 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6365 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6366 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6367 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6368 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6369 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6370 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6371 return;
6372 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6373 return;
6374 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6375 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6376 return;
6377 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6378 return;
6379 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6380 return;
6381 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6382 return;
6383 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6384 return;
6385 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6386 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6387 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6388 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6389 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6390 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6391 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6392 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6393 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6394 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6395 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6396 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6397 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6398 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6399 if (PyDict_SetItemString(d, "comprehension",
6400 (PyObject*)comprehension_type) < 0) return;
6401 if (PyDict_SetItemString(d, "excepthandler",
6402 (PyObject*)excepthandler_type) < 0) return;
6403 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6404 0) return;
Neal Norwitzc1505362006-12-28 06:47:50 +00006405 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006406 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6407 return;
6408 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006409}
6410
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006411
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006412PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006413{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006414 init_types();
6415 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006416}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006417
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006418mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena)
6419{
6420 mod_ty res;
6421 init_types();
6422 if (!PyObject_IsInstance(ast, mod_type)) {
6423 PyErr_SetString(PyExc_TypeError, "expected either Module, Interactive "
6424 "or Expression node");
6425 return NULL;
6426 }
6427 if (obj2ast_mod(ast, &res, arena) != 0)
6428 return NULL;
6429 else
6430 return res;
6431}
6432
6433int PyAST_Check(PyObject* obj)
6434{
6435 init_types();
6436 return PyObject_IsInstance(obj, (PyObject*)AST_type);
6437}
6438
Martin v. Löwis5b222132007-06-10 09:51:05 +00006439