blob: b88b310af537ebda813b7f0fafdc5994ddc3d03c [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 Norwitz207c9f32008-03-31 04:42:11 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static 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
Neal Norwitz207c9f32008-03-31 04:42:11 +0000394static int
395ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
396{
397 Py_ssize_t i, numfields = 0;
398 int res = -1;
399 PyObject *key, *value, *fields;
400 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
401 if (!fields)
402 PyErr_Clear();
403 if (fields) {
404 numfields = PySequence_Size(fields);
405 if (numfields == -1)
406 goto cleanup;
407 }
408 res = 0; /* if no error occurs, this stays 0 to the end */
409 if (PyTuple_GET_SIZE(args) > 0) {
410 if (numfields != PyTuple_GET_SIZE(args)) {
411 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
412 "%" PY_FORMAT_SIZE_T "d positional argument%s",
413 Py_TYPE(self)->tp_name,
414 numfields == 0 ? "" : "either 0 or ",
415 numfields, numfields == 1 ? "" : "s");
416 res = -1;
417 goto cleanup;
418 }
419 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
420 /* cannot be reached when fields is NULL */
421 PyObject *name = PySequence_GetItem(fields, i);
422 if (!name) {
423 res = -1;
424 goto cleanup;
425 }
426 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
427 Py_DECREF(name);
428 if (res < 0)
429 goto cleanup;
430 }
431 }
432 if (kw) {
433 i = 0; /* needed by PyDict_Next */
434 while (PyDict_Next(kw, &i, &key, &value)) {
435 res = PyObject_SetAttr(self, key, value);
436 if (res < 0)
437 goto cleanup;
438 }
439 }
440 cleanup:
441 Py_XDECREF(fields);
442 return res;
443}
444
445static PyTypeObject AST_type = {
446 PyVarObject_HEAD_INIT(&PyType_Type, 0)
447 "AST",
448 sizeof(PyObject),
449 0,
450 0, /* tp_dealloc */
451 0, /* tp_print */
452 0, /* tp_getattr */
453 0, /* tp_setattr */
454 0, /* tp_compare */
455 0, /* tp_repr */
456 0, /* tp_as_number */
457 0, /* tp_as_sequence */
458 0, /* tp_as_mapping */
459 0, /* tp_hash */
460 0, /* tp_call */
461 0, /* tp_str */
462 PyObject_GenericGetAttr, /* tp_getattro */
463 PyObject_GenericSetAttr, /* tp_setattro */
464 0, /* tp_as_buffer */
465 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
466 0, /* tp_doc */
467 0, /* tp_traverse */
468 0, /* tp_clear */
469 0, /* tp_richcompare */
470 0, /* tp_weaklistoffset */
471 0, /* tp_iter */
472 0, /* tp_iternext */
473 0, /* tp_methods */
474 0, /* tp_members */
475 0, /* tp_getset */
476 0, /* tp_base */
477 0, /* tp_dict */
478 0, /* tp_descr_get */
479 0, /* tp_descr_set */
480 0, /* tp_dictoffset */
481 (initproc)ast_type_init, /* tp_init */
482 PyType_GenericAlloc, /* tp_alloc */
483 PyType_GenericNew, /* tp_new */
484 PyObject_Del, /* tp_free */
485};
486
487
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000488static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
489{
490 PyObject *fnames, *result;
491 int i;
492 if (num_fields) {
493 fnames = PyTuple_New(num_fields);
494 if (!fnames) return NULL;
495 } else {
496 fnames = Py_None;
497 Py_INCREF(Py_None);
498 }
499 for(i=0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000500 PyObject *field = PyUnicode_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000501 if (!field) {
502 Py_DECREF(fnames);
503 return NULL;
504 }
505 PyTuple_SET_ITEM(fnames, i, field);
506 }
Guido van Rossume845c0f2007-11-02 23:07:07 +0000507 result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000508 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000509 Py_DECREF(fnames);
510 return (PyTypeObject*)result;
511}
512
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000513static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
514{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 int i, result;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000516 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000517 if (!l) return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000518 for(i = 0; i < num_fields; i++) {
Neal Norwitze4dc3242007-08-25 01:33:49 +0000519 s = PyUnicode_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000520 if (!s) {
521 Py_DECREF(l);
522 return 0;
523 }
Neal Norwitz207c9f32008-03-31 04:42:11 +0000524 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000525 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
527 Py_DECREF(l);
528 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000529}
530
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000531/* Conversion AST -> Python */
532
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000533static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
534{
535 int i, n = asdl_seq_LEN(seq);
536 PyObject *result = PyList_New(n);
537 PyObject *value;
538 if (!result)
539 return NULL;
540 for (i = 0; i < n; i++) {
541 value = func(asdl_seq_GET(seq, i));
542 if (!value) {
543 Py_DECREF(result);
544 return NULL;
545 }
546 PyList_SET_ITEM(result, i, value);
547 }
548 return result;
549}
550
551static PyObject* ast2obj_object(void *o)
552{
553 if (!o)
554 o = Py_None;
555 Py_INCREF((PyObject*)o);
556 return (PyObject*)o;
557}
558#define ast2obj_identifier ast2obj_object
559#define ast2obj_string ast2obj_object
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000560
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000561static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000562{
Christian Heimes217cfd12007-12-02 14:31:20 +0000563 return PyLong_FromLong(b);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000564}
565
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000566/* Conversion Python -> AST */
567
568static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
569{
570 if (obj == Py_None)
571 obj = NULL;
572 if (obj)
573 PyArena_AddPyObject(arena, obj);
574 Py_XINCREF(obj);
575 *out = obj;
576 return 0;
577}
578
579#define obj2ast_identifier obj2ast_object
580#define obj2ast_string obj2ast_object
581
582static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
583{
584 int i;
585 if (!PyLong_Check(obj)) {
586 PyObject *s = PyObject_Repr(obj);
587 if (s == NULL) return 1;
588 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
589 PyString_AS_STRING(s));
590 Py_DECREF(s);
591 return 1;
592 }
593
594 i = (int)PyLong_AsLong(obj);
595 if (i == -1 && PyErr_Occurred())
596 return 1;
597 *out = i;
598 return 0;
599}
600
601
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000602static int init_types(void)
603{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000605 if (initialized) return 1;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000606 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000607 if (!mod_type) return 0;
608 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000609 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000610 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000611 Interactive_type = make_type("Interactive", mod_type,
612 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000613 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000614 Expression_type = make_type("Expression", mod_type, Expression_fields,
615 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000616 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000617 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000618 if (!Suite_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000619 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000620 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000621 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000622 FunctionDef_type = make_type("FunctionDef", stmt_type,
Neal Norwitzc1505362006-12-28 06:47:50 +0000623 FunctionDef_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 if (!FunctionDef_type) return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000625 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000626 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000627 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000629 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000630 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000631 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000632 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000633 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000634 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000635 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000636 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000637 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000640 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000641 With_type = make_type("With", stmt_type, With_fields, 3);
642 if (!With_type) return 0;
Collin Winter828f04a2007-08-31 00:04:24 +0000643 Raise_type = make_type("Raise", stmt_type, Raise_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000644 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000645 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000646 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000647 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
648 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000649 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000653 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000655 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000657 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!Global_type) return 0;
Jeremy Hylton81e95022007-02-27 06:50:52 +0000659 Nonlocal_type = make_type("Nonlocal", stmt_type, Nonlocal_fields, 1);
660 if (!Nonlocal_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000663 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000665 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000667 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668 if (!Continue_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000669 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000671 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000680 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!Dict_type) return 0;
Guido van Rossum86e58e22006-08-28 15:27:34 +0000684 Set_type = make_type("Set", expr_type, Set_fields, 1);
685 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!ListComp_type) return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000688 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
689 if (!SetComp_type) return 0;
Guido van Rossum992d4a32007-07-11 13:09:30 +0000690 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
691 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 GeneratorExp_type = make_type("GeneratorExp", expr_type,
693 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Str_type) return 0;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000705 Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);
706 if (!Bytes_type) return 0;
Georg Brandl52318d62006-09-06 07:06:08 +0000707 Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);
708 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!Subscript_type) return 0;
Guido van Rossum0368b722007-05-11 16:50:42 +0000713 Starred_type = make_type("Starred", expr_type, Starred_fields, 2);
714 if (!Starred_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Tuple_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000721 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!expr_context_type) return 0;
723 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000724 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000728 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000732 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000736 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000740 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000744 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Param_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000748 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!slice_type) return 0;
750 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000753 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Index_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000757 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!boolop_type) return 0;
759 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000760 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000764 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Or_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000768 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!operator_type) return 0;
770 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000775 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000779 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000781 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000783 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000785 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000787 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000789 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000791 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000793 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000795 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000799 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000803 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000805 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000807 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000809 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000810 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000811 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000813 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000815 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000816 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000817 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!FloorDiv_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000819 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!unaryop_type) return 0;
821 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!USub_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000838 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!cmpop_type) return 0;
840 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000841 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000845 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000847 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000849 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000851 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000853 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000855 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000865 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000867 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000869 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000871 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000873 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000875 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000877 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000879 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!NotIn_singleton) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000881 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!comprehension_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000884 excepthandler_type = make_type("excepthandler", &AST_type,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000885 excepthandler_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!excepthandler_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000887 arguments_type = make_type("arguments", &AST_type, arguments_fields, 8);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!arguments_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000889 arg_type = make_type("arg", &AST_type, arg_fields, 2);
Neal Norwitzc1505362006-12-28 06:47:50 +0000890 if (!arg_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000891 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!keyword_type) return 0;
Neal Norwitz207c9f32008-03-31 04:42:11 +0000893 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!alias_type) return 0;
895 initialized = 1;
896 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000897}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000898
Martin v. Löwis618dc5e2008-03-30 20:03:44 +0000899static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
900static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
901static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
902static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
903 arena);
904static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
905static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
906static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
907static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
908static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
909static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
910 arena);
911static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
912 arena);
913static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
914static int obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena);
915static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
916static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
917
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000919Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920{
921 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000922 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000923 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 p->kind = Module_kind;
926 p->v.Module.body = body;
927 return p;
928}
929
930mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000931Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932{
933 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000934 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000935 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 p->kind = Interactive_kind;
938 p->v.Interactive.body = body;
939 return p;
940}
941
942mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000943Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944{
945 mod_ty p;
946 if (!body) {
947 PyErr_SetString(PyExc_ValueError,
948 "field body is required for Expression");
949 return NULL;
950 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000951 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000952 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 p->kind = Expression_kind;
955 p->v.Expression.body = body;
956 return p;
957}
958
959mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000960Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961{
962 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000963 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000964 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 p->kind = Suite_kind;
967 p->v.Suite.body = body;
968 return p;
969}
970
971stmt_ty
972FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000973 decorator_list, expr_ty returns, int lineno, int col_offset,
974 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975{
976 stmt_ty p;
977 if (!name) {
978 PyErr_SetString(PyExc_ValueError,
979 "field name is required for FunctionDef");
980 return NULL;
981 }
982 if (!args) {
983 PyErr_SetString(PyExc_ValueError,
984 "field args is required for FunctionDef");
985 return NULL;
986 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +0000988 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 p->kind = FunctionDef_kind;
991 p->v.FunctionDef.name = name;
992 p->v.FunctionDef.args = args;
993 p->v.FunctionDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000994 p->v.FunctionDef.decorator_list = decorator_list;
Neal Norwitzc1505362006-12-28 06:47:50 +0000995 p->v.FunctionDef.returns = returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000997 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return p;
999}
1000
1001stmt_ty
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001002ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001003 starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list,
1004 int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005{
1006 stmt_ty p;
1007 if (!name) {
1008 PyErr_SetString(PyExc_ValueError,
1009 "field name is required for ClassDef");
1010 return NULL;
1011 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001013 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 p->kind = ClassDef_kind;
1016 p->v.ClassDef.name = name;
1017 p->v.ClassDef.bases = bases;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001018 p->v.ClassDef.keywords = keywords;
1019 p->v.ClassDef.starargs = starargs;
1020 p->v.ClassDef.kwargs = kwargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 p->v.ClassDef.body = body;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001022 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001024 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 return p;
1026}
1027
1028stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001029Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030{
1031 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001032 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001033 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 p->kind = Return_kind;
1036 p->v.Return.value = value;
1037 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001038 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 return p;
1040}
1041
1042stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001043Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044{
1045 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001046 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001047 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 p->kind = Delete_kind;
1050 p->v.Delete.targets = targets;
1051 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001052 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 return p;
1054}
1055
1056stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001057Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1058 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059{
1060 stmt_ty p;
1061 if (!value) {
1062 PyErr_SetString(PyExc_ValueError,
1063 "field value is required for Assign");
1064 return NULL;
1065 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001066 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001067 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 p->kind = Assign_kind;
1070 p->v.Assign.targets = targets;
1071 p->v.Assign.value = value;
1072 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001073 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 return p;
1075}
1076
1077stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001078AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1079 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080{
1081 stmt_ty p;
1082 if (!target) {
1083 PyErr_SetString(PyExc_ValueError,
1084 "field target is required for AugAssign");
1085 return NULL;
1086 }
1087 if (!op) {
1088 PyErr_SetString(PyExc_ValueError,
1089 "field op is required for AugAssign");
1090 return NULL;
1091 }
1092 if (!value) {
1093 PyErr_SetString(PyExc_ValueError,
1094 "field value is required for AugAssign");
1095 return NULL;
1096 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001097 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001098 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 p->kind = AugAssign_kind;
1101 p->v.AugAssign.target = target;
1102 p->v.AugAssign.op = op;
1103 p->v.AugAssign.value = value;
1104 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001105 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 return p;
1107}
1108
1109stmt_ty
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001111 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112{
1113 stmt_ty p;
1114 if (!target) {
1115 PyErr_SetString(PyExc_ValueError,
1116 "field target is required for For");
1117 return NULL;
1118 }
1119 if (!iter) {
1120 PyErr_SetString(PyExc_ValueError,
1121 "field iter is required for For");
1122 return NULL;
1123 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001124 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001125 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 p->kind = For_kind;
1128 p->v.For.target = target;
1129 p->v.For.iter = iter;
1130 p->v.For.body = body;
1131 p->v.For.orelse = orelse;
1132 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001133 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 return p;
1135}
1136
1137stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001138While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1139 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140{
1141 stmt_ty p;
1142 if (!test) {
1143 PyErr_SetString(PyExc_ValueError,
1144 "field test is required for While");
1145 return NULL;
1146 }
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 = While_kind;
1151 p->v.While.test = test;
1152 p->v.While.body = body;
1153 p->v.While.orelse = orelse;
1154 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001155 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 return p;
1157}
1158
1159stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001160If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1161 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162{
1163 stmt_ty p;
1164 if (!test) {
1165 PyErr_SetString(PyExc_ValueError,
1166 "field test is required for If");
1167 return NULL;
1168 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001169 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001170 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 p->kind = If_kind;
1173 p->v.If.test = test;
1174 p->v.If.body = body;
1175 p->v.If.orelse = orelse;
1176 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001177 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178 return p;
1179}
1180
1181stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001182With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001183 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001184{
1185 stmt_ty p;
1186 if (!context_expr) {
1187 PyErr_SetString(PyExc_ValueError,
1188 "field context_expr is required for With");
1189 return NULL;
1190 }
1191 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001192 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001193 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001194 p->kind = With_kind;
1195 p->v.With.context_expr = context_expr;
1196 p->v.With.optional_vars = optional_vars;
1197 p->v.With.body = body;
1198 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001199 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001200 return p;
1201}
1202
1203stmt_ty
Collin Winter828f04a2007-08-31 00:04:24 +00001204Raise(expr_ty exc, expr_ty cause, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205{
1206 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001207 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001208 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 p->kind = Raise_kind;
Collin Winter828f04a2007-08-31 00:04:24 +00001211 p->v.Raise.exc = exc;
1212 p->v.Raise.cause = cause;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001214 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 return p;
1216}
1217
1218stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001219TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001220 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221{
1222 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001224 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 p->kind = TryExcept_kind;
1227 p->v.TryExcept.body = body;
1228 p->v.TryExcept.handlers = handlers;
1229 p->v.TryExcept.orelse = orelse;
1230 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001231 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 return p;
1233}
1234
1235stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001236TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1237 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
1239 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001240 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001241 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 p->kind = TryFinally_kind;
1244 p->v.TryFinally.body = body;
1245 p->v.TryFinally.finalbody = finalbody;
1246 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001247 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return p;
1249}
1250
1251stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001252Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253{
1254 stmt_ty p;
1255 if (!test) {
1256 PyErr_SetString(PyExc_ValueError,
1257 "field test is required for Assert");
1258 return NULL;
1259 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001261 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 p->kind = Assert_kind;
1264 p->v.Assert.test = test;
1265 p->v.Assert.msg = msg;
1266 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001267 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 return p;
1269}
1270
1271stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001272Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273{
1274 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001276 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 p->kind = Import_kind;
1279 p->v.Import.names = names;
1280 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 return p;
1283}
1284
1285stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001286ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1287 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288{
1289 stmt_ty p;
1290 if (!module) {
1291 PyErr_SetString(PyExc_ValueError,
1292 "field module is required for ImportFrom");
1293 return NULL;
1294 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001295 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001296 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 p->kind = ImportFrom_kind;
1299 p->v.ImportFrom.module = module;
1300 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001301 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001303 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return p;
1305}
1306
1307stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001308Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309{
1310 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001311 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001312 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 p->kind = Global_kind;
1315 p->v.Global.names = names;
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
1321stmt_ty
Jeremy Hylton81e95022007-02-27 06:50:52 +00001322Nonlocal(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
1323{
1324 stmt_ty p;
1325 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
1326 if (!p)
1327 return NULL;
1328 p->kind = Nonlocal_kind;
1329 p->v.Nonlocal.names = names;
1330 p->lineno = lineno;
1331 p->col_offset = col_offset;
1332 return p;
1333}
1334
1335stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001336Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337{
1338 stmt_ty p;
1339 if (!value) {
1340 PyErr_SetString(PyExc_ValueError,
1341 "field value is required for Expr");
1342 return NULL;
1343 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001344 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001345 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 p->kind = Expr_kind;
1348 p->v.Expr.value = value;
1349 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001350 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return p;
1352}
1353
1354stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001355Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
1357 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001358 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001359 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 p->kind = Pass_kind;
1362 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 return p;
1365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369{
1370 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001372 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 p->kind = Break_kind;
1375 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001376 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 return p;
1378}
1379
1380stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001381Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382{
1383 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001384 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001385 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 p->kind = Continue_kind;
1388 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001389 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 return p;
1391}
1392
1393expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001394BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1395 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396{
1397 expr_ty p;
1398 if (!op) {
1399 PyErr_SetString(PyExc_ValueError,
1400 "field op is required for BoolOp");
1401 return NULL;
1402 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001403 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001404 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 p->kind = BoolOp_kind;
1407 p->v.BoolOp.op = op;
1408 p->v.BoolOp.values = values;
1409 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001410 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 return p;
1412}
1413
1414expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001415BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1416 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417{
1418 expr_ty p;
1419 if (!left) {
1420 PyErr_SetString(PyExc_ValueError,
1421 "field left is required for BinOp");
1422 return NULL;
1423 }
1424 if (!op) {
1425 PyErr_SetString(PyExc_ValueError,
1426 "field op is required for BinOp");
1427 return NULL;
1428 }
1429 if (!right) {
1430 PyErr_SetString(PyExc_ValueError,
1431 "field right is required for BinOp");
1432 return NULL;
1433 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001434 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001435 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 p->kind = BinOp_kind;
1438 p->v.BinOp.left = left;
1439 p->v.BinOp.op = op;
1440 p->v.BinOp.right = right;
1441 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001442 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 return p;
1444}
1445
1446expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001447UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1448 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449{
1450 expr_ty p;
1451 if (!op) {
1452 PyErr_SetString(PyExc_ValueError,
1453 "field op is required for UnaryOp");
1454 return NULL;
1455 }
1456 if (!operand) {
1457 PyErr_SetString(PyExc_ValueError,
1458 "field operand is required for UnaryOp");
1459 return NULL;
1460 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001461 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001462 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 p->kind = UnaryOp_kind;
1465 p->v.UnaryOp.op = op;
1466 p->v.UnaryOp.operand = operand;
1467 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001468 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 return p;
1470}
1471
1472expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001473Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1474 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475{
1476 expr_ty p;
1477 if (!args) {
1478 PyErr_SetString(PyExc_ValueError,
1479 "field args is required for Lambda");
1480 return NULL;
1481 }
1482 if (!body) {
1483 PyErr_SetString(PyExc_ValueError,
1484 "field body is required for Lambda");
1485 return NULL;
1486 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001487 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001488 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 p->kind = Lambda_kind;
1491 p->v.Lambda.args = args;
1492 p->v.Lambda.body = body;
1493 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001494 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 return p;
1496}
1497
1498expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001499IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1500 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001501{
1502 expr_ty p;
1503 if (!test) {
1504 PyErr_SetString(PyExc_ValueError,
1505 "field test is required for IfExp");
1506 return NULL;
1507 }
1508 if (!body) {
1509 PyErr_SetString(PyExc_ValueError,
1510 "field body is required for IfExp");
1511 return NULL;
1512 }
1513 if (!orelse) {
1514 PyErr_SetString(PyExc_ValueError,
1515 "field orelse is required for IfExp");
1516 return NULL;
1517 }
1518 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001519 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001520 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001521 p->kind = IfExp_kind;
1522 p->v.IfExp.test = test;
1523 p->v.IfExp.body = body;
1524 p->v.IfExp.orelse = orelse;
1525 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001526 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001527 return p;
1528}
1529
1530expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001531Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1532 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533{
1534 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001535 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001536 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 p->kind = Dict_kind;
1539 p->v.Dict.keys = keys;
1540 p->v.Dict.values = values;
1541 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001542 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 return p;
1544}
1545
1546expr_ty
Guido van Rossum86e58e22006-08-28 15:27:34 +00001547Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1548{
1549 expr_ty p;
1550 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001551 if (!p)
Guido van Rossum86e58e22006-08-28 15:27:34 +00001552 return NULL;
Guido van Rossum86e58e22006-08-28 15:27:34 +00001553 p->kind = Set_kind;
1554 p->v.Set.elts = elts;
1555 p->lineno = lineno;
1556 p->col_offset = col_offset;
1557 return p;
1558}
1559
1560expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001561ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1562 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563{
1564 expr_ty p;
1565 if (!elt) {
1566 PyErr_SetString(PyExc_ValueError,
1567 "field elt is required for ListComp");
1568 return NULL;
1569 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001570 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001571 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 p->kind = ListComp_kind;
1574 p->v.ListComp.elt = elt;
1575 p->v.ListComp.generators = generators;
1576 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001577 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578 return p;
1579}
1580
1581expr_ty
Nick Coghlan650f0d02007-04-15 12:05:43 +00001582SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1583 *arena)
1584{
1585 expr_ty p;
1586 if (!elt) {
1587 PyErr_SetString(PyExc_ValueError,
1588 "field elt is required for SetComp");
1589 return NULL;
1590 }
1591 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1592 if (!p)
1593 return NULL;
1594 p->kind = SetComp_kind;
1595 p->v.SetComp.elt = elt;
1596 p->v.SetComp.generators = generators;
1597 p->lineno = lineno;
1598 p->col_offset = col_offset;
1599 return p;
1600}
1601
1602expr_ty
Guido van Rossum992d4a32007-07-11 13:09:30 +00001603DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1604 col_offset, PyArena *arena)
1605{
1606 expr_ty p;
1607 if (!key) {
1608 PyErr_SetString(PyExc_ValueError,
1609 "field key is required for DictComp");
1610 return NULL;
1611 }
1612 if (!value) {
1613 PyErr_SetString(PyExc_ValueError,
1614 "field value is required for DictComp");
1615 return NULL;
1616 }
1617 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1618 if (!p)
1619 return NULL;
1620 p->kind = DictComp_kind;
1621 p->v.DictComp.key = key;
1622 p->v.DictComp.value = value;
1623 p->v.DictComp.generators = generators;
1624 p->lineno = lineno;
1625 p->col_offset = col_offset;
1626 return p;
1627}
1628
1629expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001630GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1631 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632{
1633 expr_ty p;
1634 if (!elt) {
1635 PyErr_SetString(PyExc_ValueError,
1636 "field elt is required for GeneratorExp");
1637 return NULL;
1638 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001639 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001640 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 p->kind = GeneratorExp_kind;
1643 p->v.GeneratorExp.elt = elt;
1644 p->v.GeneratorExp.generators = generators;
1645 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return p;
1648}
1649
1650expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652{
1653 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001654 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001655 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 p->kind = Yield_kind;
1658 p->v.Yield.value = value;
1659 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001660 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 return p;
1662}
1663
1664expr_ty
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001665Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1666 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667{
1668 expr_ty p;
1669 if (!left) {
1670 PyErr_SetString(PyExc_ValueError,
1671 "field left is required for Compare");
1672 return NULL;
1673 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001674 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001675 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 p->kind = Compare_kind;
1678 p->v.Compare.left = left;
1679 p->v.Compare.ops = ops;
1680 p->v.Compare.comparators = comparators;
1681 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001682 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 return p;
1684}
1685
1686expr_ty
1687Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001688 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689{
1690 expr_ty p;
1691 if (!func) {
1692 PyErr_SetString(PyExc_ValueError,
1693 "field func is required for Call");
1694 return NULL;
1695 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001696 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001697 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 p->kind = Call_kind;
1700 p->v.Call.func = func;
1701 p->v.Call.args = args;
1702 p->v.Call.keywords = keywords;
1703 p->v.Call.starargs = starargs;
1704 p->v.Call.kwargs = kwargs;
1705 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001706 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707 return p;
1708}
1709
1710expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001711Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712{
1713 expr_ty p;
1714 if (!n) {
1715 PyErr_SetString(PyExc_ValueError,
1716 "field n is required for Num");
1717 return NULL;
1718 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001719 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001720 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 p->kind = Num_kind;
1723 p->v.Num.n = n;
1724 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001725 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return p;
1727}
1728
1729expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001730Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731{
1732 expr_ty p;
1733 if (!s) {
1734 PyErr_SetString(PyExc_ValueError,
1735 "field s is required for Str");
1736 return NULL;
1737 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001738 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001739 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 p->kind = Str_kind;
1742 p->v.Str.s = s;
1743 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001744 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return p;
1746}
1747
1748expr_ty
Thomas Wouters00e41de2007-02-23 19:56:57 +00001749Bytes(string s, int lineno, int col_offset, PyArena *arena)
1750{
1751 expr_ty p;
1752 if (!s) {
1753 PyErr_SetString(PyExc_ValueError,
1754 "field s is required for Bytes");
1755 return NULL;
1756 }
1757 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001758 if (!p)
Thomas Wouters00e41de2007-02-23 19:56:57 +00001759 return NULL;
Thomas Wouters00e41de2007-02-23 19:56:57 +00001760 p->kind = Bytes_kind;
1761 p->v.Bytes.s = s;
1762 p->lineno = lineno;
1763 p->col_offset = col_offset;
1764 return p;
1765}
1766
1767expr_ty
Georg Brandl52318d62006-09-06 07:06:08 +00001768Ellipsis(int lineno, int col_offset, PyArena *arena)
1769{
1770 expr_ty p;
1771 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001772 if (!p)
Georg Brandl52318d62006-09-06 07:06:08 +00001773 return NULL;
Georg Brandl52318d62006-09-06 07:06:08 +00001774 p->kind = Ellipsis_kind;
1775 p->lineno = lineno;
1776 p->col_offset = col_offset;
1777 return p;
1778}
1779
1780expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001781Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1782 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783{
1784 expr_ty p;
1785 if (!value) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field value is required for Attribute");
1788 return NULL;
1789 }
1790 if (!attr) {
1791 PyErr_SetString(PyExc_ValueError,
1792 "field attr is required for Attribute");
1793 return NULL;
1794 }
1795 if (!ctx) {
1796 PyErr_SetString(PyExc_ValueError,
1797 "field ctx is required for Attribute");
1798 return NULL;
1799 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001800 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001801 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 p->kind = Attribute_kind;
1804 p->v.Attribute.value = value;
1805 p->v.Attribute.attr = attr;
1806 p->v.Attribute.ctx = ctx;
1807 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001808 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 return p;
1810}
1811
1812expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001813Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1814 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815{
1816 expr_ty p;
1817 if (!value) {
1818 PyErr_SetString(PyExc_ValueError,
1819 "field value is required for Subscript");
1820 return NULL;
1821 }
1822 if (!slice) {
1823 PyErr_SetString(PyExc_ValueError,
1824 "field slice is required for Subscript");
1825 return NULL;
1826 }
1827 if (!ctx) {
1828 PyErr_SetString(PyExc_ValueError,
1829 "field ctx is required for Subscript");
1830 return NULL;
1831 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001833 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 p->kind = Subscript_kind;
1836 p->v.Subscript.value = value;
1837 p->v.Subscript.slice = slice;
1838 p->v.Subscript.ctx = ctx;
1839 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001840 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 return p;
1842}
1843
1844expr_ty
Guido van Rossum0368b722007-05-11 16:50:42 +00001845Starred(expr_ty value, expr_context_ty ctx, int lineno, int col_offset, PyArena
1846 *arena)
1847{
1848 expr_ty p;
1849 if (!value) {
1850 PyErr_SetString(PyExc_ValueError,
1851 "field value is required for Starred");
1852 return NULL;
1853 }
1854 if (!ctx) {
1855 PyErr_SetString(PyExc_ValueError,
1856 "field ctx is required for Starred");
1857 return NULL;
1858 }
1859 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1860 if (!p)
1861 return NULL;
1862 p->kind = Starred_kind;
1863 p->v.Starred.value = value;
1864 p->v.Starred.ctx = ctx;
1865 p->lineno = lineno;
1866 p->col_offset = col_offset;
1867 return p;
1868}
1869
1870expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001871Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1872 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873{
1874 expr_ty p;
1875 if (!id) {
1876 PyErr_SetString(PyExc_ValueError,
1877 "field id is required for Name");
1878 return NULL;
1879 }
1880 if (!ctx) {
1881 PyErr_SetString(PyExc_ValueError,
1882 "field ctx is required for Name");
1883 return NULL;
1884 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001885 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001886 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 p->kind = Name_kind;
1889 p->v.Name.id = id;
1890 p->v.Name.ctx = ctx;
1891 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001892 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return p;
1894}
1895
1896expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001897List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1898 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899{
1900 expr_ty p;
1901 if (!ctx) {
1902 PyErr_SetString(PyExc_ValueError,
1903 "field ctx is required for List");
1904 return NULL;
1905 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001906 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001907 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 p->kind = List_kind;
1910 p->v.List.elts = elts;
1911 p->v.List.ctx = ctx;
1912 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001913 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 return p;
1915}
1916
1917expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001918Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1919 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920{
1921 expr_ty p;
1922 if (!ctx) {
1923 PyErr_SetString(PyExc_ValueError,
1924 "field ctx is required for Tuple");
1925 return NULL;
1926 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001928 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 p->kind = Tuple_kind;
1931 p->v.Tuple.elts = elts;
1932 p->v.Tuple.ctx = ctx;
1933 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001934 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 return p;
1936}
1937
1938slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001939Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940{
1941 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001942 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001943 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 p->kind = Slice_kind;
1946 p->v.Slice.lower = lower;
1947 p->v.Slice.upper = upper;
1948 p->v.Slice.step = step;
1949 return p;
1950}
1951
1952slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001953ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954{
1955 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001956 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001957 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 p->kind = ExtSlice_kind;
1960 p->v.ExtSlice.dims = dims;
1961 return p;
1962}
1963
1964slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001965Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966{
1967 slice_ty p;
1968 if (!value) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field value is required for Index");
1971 return NULL;
1972 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001973 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00001974 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 p->kind = Index_kind;
1977 p->v.Index.value = value;
1978 return p;
1979}
1980
1981comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983{
1984 comprehension_ty p;
1985 if (!target) {
1986 PyErr_SetString(PyExc_ValueError,
1987 "field target is required for comprehension");
1988 return NULL;
1989 }
1990 if (!iter) {
1991 PyErr_SetString(PyExc_ValueError,
1992 "field iter is required for comprehension");
1993 return NULL;
1994 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 p = (comprehension_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->target = target;
1999 p->iter = iter;
2000 p->ifs = ifs;
2001 return p;
2002}
2003
2004excepthandler_ty
Guido van Rossum16be03e2007-01-10 18:51:35 +00002005excepthandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002006 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007{
2008 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002009 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002010 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 p->type = type;
2013 p->name = name;
2014 p->body = body;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002015 p->lineno = lineno;
2016 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 return p;
2018}
2019
2020arguments_ty
Neal Norwitzc1505362006-12-28 06:47:50 +00002021arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,
2022 asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation,
2023 asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024{
2025 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002026 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002027 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 p->args = args;
2030 p->vararg = vararg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002031 p->varargannotation = varargannotation;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002032 p->kwonlyargs = kwonlyargs;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 p->kwarg = kwarg;
Neal Norwitzc1505362006-12-28 06:47:50 +00002034 p->kwargannotation = kwargannotation;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 p->defaults = defaults;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002036 p->kw_defaults = kw_defaults;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 return p;
2038}
2039
Neal Norwitzc1505362006-12-28 06:47:50 +00002040arg_ty
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002041arg(identifier arg, expr_ty annotation, PyArena *arena)
Neal Norwitzc1505362006-12-28 06:47:50 +00002042{
2043 arg_ty p;
2044 if (!arg) {
2045 PyErr_SetString(PyExc_ValueError,
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002046 "field arg is required for arg");
Neal Norwitzc1505362006-12-28 06:47:50 +00002047 return NULL;
2048 }
2049 p = (arg_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002050 if (!p)
Neal Norwitzc1505362006-12-28 06:47:50 +00002051 return NULL;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00002052 p->arg = arg;
2053 p->annotation = annotation;
Neal Norwitzc1505362006-12-28 06:47:50 +00002054 return p;
2055}
2056
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002058keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059{
2060 keyword_ty p;
2061 if (!arg) {
2062 PyErr_SetString(PyExc_ValueError,
2063 "field arg is required for keyword");
2064 return NULL;
2065 }
2066 if (!value) {
2067 PyErr_SetString(PyExc_ValueError,
2068 "field value is required for keyword");
2069 return NULL;
2070 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002071 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002072 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 p->arg = arg;
2075 p->value = value;
2076 return p;
2077}
2078
2079alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081{
2082 alias_ty p;
2083 if (!name) {
2084 PyErr_SetString(PyExc_ValueError,
2085 "field name is required for alias");
2086 return NULL;
2087 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002088 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Thomas Woutersa44f3a32007-02-26 18:20:15 +00002089 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002090 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 p->name = name;
2092 p->asname = asname;
2093 return p;
2094}
2095
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002096
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002097PyObject*
2098ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002099{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002100 mod_ty o = (mod_ty)_o;
2101 PyObject *result = NULL, *value = NULL;
2102 if (!o) {
2103 Py_INCREF(Py_None);
2104 return Py_None;
2105 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 switch (o->kind) {
2108 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002109 result = PyType_GenericNew(Module_type, NULL, NULL);
2110 if (!result) goto failed;
2111 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2112 if (!value) goto failed;
2113 if (PyObject_SetAttrString(result, "body", value) == -1)
2114 goto failed;
2115 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 break;
2117 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002118 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2119 if (!result) goto failed;
2120 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2121 if (!value) goto failed;
2122 if (PyObject_SetAttrString(result, "body", value) == -1)
2123 goto failed;
2124 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 break;
2126 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002127 result = PyType_GenericNew(Expression_type, NULL, NULL);
2128 if (!result) goto failed;
2129 value = ast2obj_expr(o->v.Expression.body);
2130 if (!value) goto failed;
2131 if (PyObject_SetAttrString(result, "body", value) == -1)
2132 goto failed;
2133 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 break;
2135 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002136 result = PyType_GenericNew(Suite_type, NULL, NULL);
2137 if (!result) goto failed;
2138 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2139 if (!value) goto failed;
2140 if (PyObject_SetAttrString(result, "body", value) == -1)
2141 goto failed;
2142 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 break;
2144 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 return result;
2146failed:
2147 Py_XDECREF(value);
2148 Py_XDECREF(result);
2149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150}
2151
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002152PyObject*
2153ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002155 stmt_ty o = (stmt_ty)_o;
2156 PyObject *result = NULL, *value = NULL;
2157 if (!o) {
2158 Py_INCREF(Py_None);
2159 return Py_None;
2160 }
2161
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 switch (o->kind) {
2163 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002164 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2165 if (!result) goto failed;
2166 value = ast2obj_identifier(o->v.FunctionDef.name);
2167 if (!value) goto failed;
2168 if (PyObject_SetAttrString(result, "name", value) == -1)
2169 goto failed;
2170 Py_DECREF(value);
2171 value = ast2obj_arguments(o->v.FunctionDef.args);
2172 if (!value) goto failed;
2173 if (PyObject_SetAttrString(result, "args", value) == -1)
2174 goto failed;
2175 Py_DECREF(value);
2176 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2177 if (!value) goto failed;
2178 if (PyObject_SetAttrString(result, "body", value) == -1)
2179 goto failed;
2180 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002181 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2182 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002183 if (!value) goto failed;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002184 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2185 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002186 goto failed;
2187 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00002188 value = ast2obj_expr(o->v.FunctionDef.returns);
2189 if (!value) goto failed;
2190 if (PyObject_SetAttrString(result, "returns", value) == -1)
2191 goto failed;
2192 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 break;
2194 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002195 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2196 if (!result) goto failed;
2197 value = ast2obj_identifier(o->v.ClassDef.name);
2198 if (!value) goto failed;
2199 if (PyObject_SetAttrString(result, "name", value) == -1)
2200 goto failed;
2201 Py_DECREF(value);
2202 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2203 if (!value) goto failed;
2204 if (PyObject_SetAttrString(result, "bases", value) == -1)
2205 goto failed;
2206 Py_DECREF(value);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002207 value = ast2obj_list(o->v.ClassDef.keywords, ast2obj_keyword);
2208 if (!value) goto failed;
2209 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2210 goto failed;
2211 Py_DECREF(value);
2212 value = ast2obj_expr(o->v.ClassDef.starargs);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
2217 value = ast2obj_expr(o->v.ClassDef.kwargs);
2218 if (!value) goto failed;
2219 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2220 goto failed;
2221 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002222 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2223 if (!value) goto failed;
2224 if (PyObject_SetAttrString(result, "body", value) == -1)
2225 goto failed;
2226 Py_DECREF(value);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002227 value = ast2obj_list(o->v.ClassDef.decorator_list,
2228 ast2obj_expr);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2231 -1)
2232 goto failed;
2233 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 break;
2235 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002236 result = PyType_GenericNew(Return_type, NULL, NULL);
2237 if (!result) goto failed;
2238 value = ast2obj_expr(o->v.Return.value);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "value", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 break;
2244 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002245 result = PyType_GenericNew(Delete_type, NULL, NULL);
2246 if (!result) goto failed;
2247 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "targets", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 break;
2253 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002254 result = PyType_GenericNew(Assign_type, NULL, NULL);
2255 if (!result) goto failed;
2256 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2257 if (!value) goto failed;
2258 if (PyObject_SetAttrString(result, "targets", value) == -1)
2259 goto failed;
2260 Py_DECREF(value);
2261 value = ast2obj_expr(o->v.Assign.value);
2262 if (!value) goto failed;
2263 if (PyObject_SetAttrString(result, "value", value) == -1)
2264 goto failed;
2265 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 break;
2267 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002268 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2269 if (!result) goto failed;
2270 value = ast2obj_expr(o->v.AugAssign.target);
2271 if (!value) goto failed;
2272 if (PyObject_SetAttrString(result, "target", value) == -1)
2273 goto failed;
2274 Py_DECREF(value);
2275 value = ast2obj_operator(o->v.AugAssign.op);
2276 if (!value) goto failed;
2277 if (PyObject_SetAttrString(result, "op", value) == -1)
2278 goto failed;
2279 Py_DECREF(value);
2280 value = ast2obj_expr(o->v.AugAssign.value);
2281 if (!value) goto failed;
2282 if (PyObject_SetAttrString(result, "value", value) == -1)
2283 goto failed;
2284 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002287 result = PyType_GenericNew(For_type, NULL, NULL);
2288 if (!result) goto failed;
2289 value = ast2obj_expr(o->v.For.target);
2290 if (!value) goto failed;
2291 if (PyObject_SetAttrString(result, "target", value) == -1)
2292 goto failed;
2293 Py_DECREF(value);
2294 value = ast2obj_expr(o->v.For.iter);
2295 if (!value) goto failed;
2296 if (PyObject_SetAttrString(result, "iter", value) == -1)
2297 goto failed;
2298 Py_DECREF(value);
2299 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2300 if (!value) goto failed;
2301 if (PyObject_SetAttrString(result, "body", value) == -1)
2302 goto failed;
2303 Py_DECREF(value);
2304 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2305 if (!value) goto failed;
2306 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2307 goto failed;
2308 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 break;
2310 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002311 result = PyType_GenericNew(While_type, NULL, NULL);
2312 if (!result) goto failed;
2313 value = ast2obj_expr(o->v.While.test);
2314 if (!value) goto failed;
2315 if (PyObject_SetAttrString(result, "test", value) == -1)
2316 goto failed;
2317 Py_DECREF(value);
2318 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2319 if (!value) goto failed;
2320 if (PyObject_SetAttrString(result, "body", value) == -1)
2321 goto failed;
2322 Py_DECREF(value);
2323 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2324 if (!value) goto failed;
2325 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2326 goto failed;
2327 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 break;
2329 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002330 result = PyType_GenericNew(If_type, NULL, NULL);
2331 if (!result) goto failed;
2332 value = ast2obj_expr(o->v.If.test);
2333 if (!value) goto failed;
2334 if (PyObject_SetAttrString(result, "test", value) == -1)
2335 goto failed;
2336 Py_DECREF(value);
2337 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2338 if (!value) goto failed;
2339 if (PyObject_SetAttrString(result, "body", value) == -1)
2340 goto failed;
2341 Py_DECREF(value);
2342 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2343 if (!value) goto failed;
2344 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2345 goto failed;
2346 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002348 case With_kind:
2349 result = PyType_GenericNew(With_type, NULL, NULL);
2350 if (!result) goto failed;
2351 value = ast2obj_expr(o->v.With.context_expr);
2352 if (!value) goto failed;
2353 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2354 goto failed;
2355 Py_DECREF(value);
2356 value = ast2obj_expr(o->v.With.optional_vars);
2357 if (!value) goto failed;
2358 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2359 -1)
2360 goto failed;
2361 Py_DECREF(value);
2362 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2363 if (!value) goto failed;
2364 if (PyObject_SetAttrString(result, "body", value) == -1)
2365 goto failed;
2366 Py_DECREF(value);
2367 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002369 result = PyType_GenericNew(Raise_type, NULL, NULL);
2370 if (!result) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002371 value = ast2obj_expr(o->v.Raise.exc);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002372 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002373 if (PyObject_SetAttrString(result, "exc", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002374 goto failed;
2375 Py_DECREF(value);
Collin Winter828f04a2007-08-31 00:04:24 +00002376 value = ast2obj_expr(o->v.Raise.cause);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002377 if (!value) goto failed;
Collin Winter828f04a2007-08-31 00:04:24 +00002378 if (PyObject_SetAttrString(result, "cause", value) == -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002379 goto failed;
2380 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 break;
2382 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002383 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2384 if (!result) goto failed;
2385 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2386 if (!value) goto failed;
2387 if (PyObject_SetAttrString(result, "body", value) == -1)
2388 goto failed;
2389 Py_DECREF(value);
2390 value = ast2obj_list(o->v.TryExcept.handlers,
2391 ast2obj_excepthandler);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2397 if (!value) goto failed;
2398 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 break;
2402 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002403 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2404 if (!result) goto failed;
2405 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2406 if (!value) goto failed;
2407 if (PyObject_SetAttrString(result, "body", value) == -1)
2408 goto failed;
2409 Py_DECREF(value);
2410 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 break;
2416 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002417 result = PyType_GenericNew(Assert_type, NULL, NULL);
2418 if (!result) goto failed;
2419 value = ast2obj_expr(o->v.Assert.test);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "test", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
2424 value = ast2obj_expr(o->v.Assert.msg);
2425 if (!value) goto failed;
2426 if (PyObject_SetAttrString(result, "msg", value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 break;
2430 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002431 result = PyType_GenericNew(Import_type, NULL, NULL);
2432 if (!result) goto failed;
2433 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2434 if (!value) goto failed;
2435 if (PyObject_SetAttrString(result, "names", value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 break;
2439 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2441 if (!result) goto failed;
2442 value = ast2obj_identifier(o->v.ImportFrom.module);
2443 if (!value) goto failed;
2444 if (PyObject_SetAttrString(result, "module", value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
2447 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2448 if (!value) goto failed;
2449 if (PyObject_SetAttrString(result, "names", value) == -1)
2450 goto failed;
2451 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002452 value = ast2obj_int(o->v.ImportFrom.level);
2453 if (!value) goto failed;
2454 if (PyObject_SetAttrString(result, "level", value) == -1)
2455 goto failed;
2456 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002459 result = PyType_GenericNew(Global_type, NULL, NULL);
2460 if (!result) goto failed;
2461 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2462 if (!value) goto failed;
2463 if (PyObject_SetAttrString(result, "names", value) == -1)
2464 goto failed;
2465 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 break;
Jeremy Hylton81e95022007-02-27 06:50:52 +00002467 case Nonlocal_kind:
2468 result = PyType_GenericNew(Nonlocal_type, NULL, NULL);
2469 if (!result) goto failed;
2470 value = ast2obj_list(o->v.Nonlocal.names, ast2obj_identifier);
2471 if (!value) goto failed;
2472 if (PyObject_SetAttrString(result, "names", value) == -1)
2473 goto failed;
2474 Py_DECREF(value);
2475 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002477 result = PyType_GenericNew(Expr_type, NULL, NULL);
2478 if (!result) goto failed;
2479 value = ast2obj_expr(o->v.Expr.value);
2480 if (!value) goto failed;
2481 if (PyObject_SetAttrString(result, "value", value) == -1)
2482 goto failed;
2483 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 break;
2485 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002486 result = PyType_GenericNew(Pass_type, NULL, NULL);
2487 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 break;
2489 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002490 result = PyType_GenericNew(Break_type, NULL, NULL);
2491 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 break;
2493 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002494 result = PyType_GenericNew(Continue_type, NULL, NULL);
2495 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 break;
2497 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002498 value = ast2obj_int(o->lineno);
2499 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002500 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2501 goto failed;
2502 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002503 value = ast2obj_int(o->col_offset);
2504 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002505 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2506 goto failed;
2507 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002508 return result;
2509failed:
2510 Py_XDECREF(value);
2511 Py_XDECREF(result);
2512 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513}
2514
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002515PyObject*
2516ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 expr_ty o = (expr_ty)_o;
2519 PyObject *result = NULL, *value = NULL;
2520 if (!o) {
2521 Py_INCREF(Py_None);
2522 return Py_None;
2523 }
2524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 switch (o->kind) {
2526 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002527 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2528 if (!result) goto failed;
2529 value = ast2obj_boolop(o->v.BoolOp.op);
2530 if (!value) goto failed;
2531 if (PyObject_SetAttrString(result, "op", value) == -1)
2532 goto failed;
2533 Py_DECREF(value);
2534 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2535 if (!value) goto failed;
2536 if (PyObject_SetAttrString(result, "values", value) == -1)
2537 goto failed;
2538 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 break;
2540 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002541 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2542 if (!result) goto failed;
2543 value = ast2obj_expr(o->v.BinOp.left);
2544 if (!value) goto failed;
2545 if (PyObject_SetAttrString(result, "left", value) == -1)
2546 goto failed;
2547 Py_DECREF(value);
2548 value = ast2obj_operator(o->v.BinOp.op);
2549 if (!value) goto failed;
2550 if (PyObject_SetAttrString(result, "op", value) == -1)
2551 goto failed;
2552 Py_DECREF(value);
2553 value = ast2obj_expr(o->v.BinOp.right);
2554 if (!value) goto failed;
2555 if (PyObject_SetAttrString(result, "right", value) == -1)
2556 goto failed;
2557 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 break;
2559 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002560 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2561 if (!result) goto failed;
2562 value = ast2obj_unaryop(o->v.UnaryOp.op);
2563 if (!value) goto failed;
2564 if (PyObject_SetAttrString(result, "op", value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 value = ast2obj_expr(o->v.UnaryOp.operand);
2568 if (!value) goto failed;
2569 if (PyObject_SetAttrString(result, "operand", value) == -1)
2570 goto failed;
2571 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 break;
2573 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002574 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2575 if (!result) goto failed;
2576 value = ast2obj_arguments(o->v.Lambda.args);
2577 if (!value) goto failed;
2578 if (PyObject_SetAttrString(result, "args", value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 value = ast2obj_expr(o->v.Lambda.body);
2582 if (!value) goto failed;
2583 if (PyObject_SetAttrString(result, "body", value) == -1)
2584 goto failed;
2585 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002587 case IfExp_kind:
2588 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2589 if (!result) goto failed;
2590 value = ast2obj_expr(o->v.IfExp.test);
2591 if (!value) goto failed;
2592 if (PyObject_SetAttrString(result, "test", value) == -1)
2593 goto failed;
2594 Py_DECREF(value);
2595 value = ast2obj_expr(o->v.IfExp.body);
2596 if (!value) goto failed;
2597 if (PyObject_SetAttrString(result, "body", value) == -1)
2598 goto failed;
2599 Py_DECREF(value);
2600 value = ast2obj_expr(o->v.IfExp.orelse);
2601 if (!value) goto failed;
2602 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2603 goto failed;
2604 Py_DECREF(value);
2605 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002607 result = PyType_GenericNew(Dict_type, NULL, NULL);
2608 if (!result) goto failed;
2609 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2610 if (!value) goto failed;
2611 if (PyObject_SetAttrString(result, "keys", value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2615 if (!value) goto failed;
2616 if (PyObject_SetAttrString(result, "values", value) == -1)
2617 goto failed;
2618 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002620 case Set_kind:
2621 result = PyType_GenericNew(Set_type, NULL, NULL);
2622 if (!result) goto failed;
2623 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2624 if (!value) goto failed;
2625 if (PyObject_SetAttrString(result, "elts", value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002630 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2631 if (!result) goto failed;
2632 value = ast2obj_expr(o->v.ListComp.elt);
2633 if (!value) goto failed;
2634 if (PyObject_SetAttrString(result, "elt", value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 value = ast2obj_list(o->v.ListComp.generators,
2638 ast2obj_comprehension);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "generators", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002644 case SetComp_kind:
2645 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2646 if (!result) goto failed;
2647 value = ast2obj_expr(o->v.SetComp.elt);
2648 if (!value) goto failed;
2649 if (PyObject_SetAttrString(result, "elt", value) == -1)
2650 goto failed;
2651 Py_DECREF(value);
2652 value = ast2obj_list(o->v.SetComp.generators,
2653 ast2obj_comprehension);
2654 if (!value) goto failed;
2655 if (PyObject_SetAttrString(result, "generators", value) == -1)
2656 goto failed;
2657 Py_DECREF(value);
2658 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002659 case DictComp_kind:
2660 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2661 if (!result) goto failed;
2662 value = ast2obj_expr(o->v.DictComp.key);
2663 if (!value) goto failed;
2664 if (PyObject_SetAttrString(result, "key", value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 value = ast2obj_expr(o->v.DictComp.value);
2668 if (!value) goto failed;
2669 if (PyObject_SetAttrString(result, "value", value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
2672 value = ast2obj_list(o->v.DictComp.generators,
2673 ast2obj_comprehension);
2674 if (!value) goto failed;
2675 if (PyObject_SetAttrString(result, "generators", value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
2678 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002680 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2681 if (!result) goto failed;
2682 value = ast2obj_expr(o->v.GeneratorExp.elt);
2683 if (!value) goto failed;
2684 if (PyObject_SetAttrString(result, "elt", value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 value = ast2obj_list(o->v.GeneratorExp.generators,
2688 ast2obj_comprehension);
2689 if (!value) goto failed;
2690 if (PyObject_SetAttrString(result, "generators", value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 break;
2694 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002695 result = PyType_GenericNew(Yield_type, NULL, NULL);
2696 if (!result) goto failed;
2697 value = ast2obj_expr(o->v.Yield.value);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "value", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 break;
2703 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002704 result = PyType_GenericNew(Compare_type, NULL, NULL);
2705 if (!result) goto failed;
2706 value = ast2obj_expr(o->v.Compare.left);
2707 if (!value) goto failed;
2708 if (PyObject_SetAttrString(result, "left", value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002711 {
2712 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2713 value = PyList_New(n);
2714 if (!value) goto failed;
2715 for(i = 0; i < n; i++)
2716 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
2717 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "ops", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2723 if (!value) goto failed;
2724 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 break;
2728 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002729 result = PyType_GenericNew(Call_type, NULL, NULL);
2730 if (!result) goto failed;
2731 value = ast2obj_expr(o->v.Call.func);
2732 if (!value) goto failed;
2733 if (PyObject_SetAttrString(result, "func", value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2737 if (!value) goto failed;
2738 if (PyObject_SetAttrString(result, "args", value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
2741 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2742 if (!value) goto failed;
2743 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2744 goto failed;
2745 Py_DECREF(value);
2746 value = ast2obj_expr(o->v.Call.starargs);
2747 if (!value) goto failed;
2748 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2749 goto failed;
2750 Py_DECREF(value);
2751 value = ast2obj_expr(o->v.Call.kwargs);
2752 if (!value) goto failed;
2753 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2754 goto failed;
2755 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002758 result = PyType_GenericNew(Num_type, NULL, NULL);
2759 if (!result) goto failed;
2760 value = ast2obj_object(o->v.Num.n);
2761 if (!value) goto failed;
2762 if (PyObject_SetAttrString(result, "n", value) == -1)
2763 goto failed;
2764 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 break;
2766 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002767 result = PyType_GenericNew(Str_type, NULL, NULL);
2768 if (!result) goto failed;
2769 value = ast2obj_string(o->v.Str.s);
2770 if (!value) goto failed;
2771 if (PyObject_SetAttrString(result, "s", value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00002775 case Bytes_kind:
2776 result = PyType_GenericNew(Bytes_type, NULL, NULL);
2777 if (!result) goto failed;
2778 value = ast2obj_string(o->v.Bytes.s);
2779 if (!value) goto failed;
2780 if (PyObject_SetAttrString(result, "s", value) == -1)
2781 goto failed;
2782 Py_DECREF(value);
2783 break;
Georg Brandl52318d62006-09-06 07:06:08 +00002784 case Ellipsis_kind:
2785 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2786 if (!result) goto failed;
2787 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002789 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2790 if (!result) goto failed;
2791 value = ast2obj_expr(o->v.Attribute.value);
2792 if (!value) goto failed;
2793 if (PyObject_SetAttrString(result, "value", value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 value = ast2obj_identifier(o->v.Attribute.attr);
2797 if (!value) goto failed;
2798 if (PyObject_SetAttrString(result, "attr", value) == -1)
2799 goto failed;
2800 Py_DECREF(value);
2801 value = ast2obj_expr_context(o->v.Attribute.ctx);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 break;
2807 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002808 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2809 if (!result) goto failed;
2810 value = ast2obj_expr(o->v.Subscript.value);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "value", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
2815 value = ast2obj_slice(o->v.Subscript.slice);
2816 if (!value) goto failed;
2817 if (PyObject_SetAttrString(result, "slice", value) == -1)
2818 goto failed;
2819 Py_DECREF(value);
2820 value = ast2obj_expr_context(o->v.Subscript.ctx);
2821 if (!value) goto failed;
2822 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00002826 case Starred_kind:
2827 result = PyType_GenericNew(Starred_type, NULL, NULL);
2828 if (!result) goto failed;
2829 value = ast2obj_expr(o->v.Starred.value);
2830 if (!value) goto failed;
2831 if (PyObject_SetAttrString(result, "value", value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
2834 value = ast2obj_expr_context(o->v.Starred.ctx);
2835 if (!value) goto failed;
2836 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2837 goto failed;
2838 Py_DECREF(value);
2839 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002841 result = PyType_GenericNew(Name_type, NULL, NULL);
2842 if (!result) goto failed;
2843 value = ast2obj_identifier(o->v.Name.id);
2844 if (!value) goto failed;
2845 if (PyObject_SetAttrString(result, "id", value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
2848 value = ast2obj_expr_context(o->v.Name.ctx);
2849 if (!value) goto failed;
2850 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 break;
2854 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002855 result = PyType_GenericNew(List_type, NULL, NULL);
2856 if (!result) goto failed;
2857 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2858 if (!value) goto failed;
2859 if (PyObject_SetAttrString(result, "elts", value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 value = ast2obj_expr_context(o->v.List.ctx);
2863 if (!value) goto failed;
2864 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2865 goto failed;
2866 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 break;
2868 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002869 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2870 if (!result) goto failed;
2871 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2872 if (!value) goto failed;
2873 if (PyObject_SetAttrString(result, "elts", value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
2876 value = ast2obj_expr_context(o->v.Tuple.ctx);
2877 if (!value) goto failed;
2878 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2879 goto failed;
2880 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 break;
2882 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002883 value = ast2obj_int(o->lineno);
2884 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002885 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2886 goto failed;
2887 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002888 value = ast2obj_int(o->col_offset);
2889 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002890 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2891 goto failed;
2892 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002893 return result;
2894failed:
2895 Py_XDECREF(value);
2896 Py_XDECREF(result);
2897 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898}
2899
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002900PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002902 switch(o) {
2903 case Load:
2904 Py_INCREF(Load_singleton);
2905 return Load_singleton;
2906 case Store:
2907 Py_INCREF(Store_singleton);
2908 return Store_singleton;
2909 case Del:
2910 Py_INCREF(Del_singleton);
2911 return Del_singleton;
2912 case AugLoad:
2913 Py_INCREF(AugLoad_singleton);
2914 return AugLoad_singleton;
2915 case AugStore:
2916 Py_INCREF(AugStore_singleton);
2917 return AugStore_singleton;
2918 case Param:
2919 Py_INCREF(Param_singleton);
2920 return Param_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002921 default:
2922 /* should never happen, but just in case ... */
2923 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2924 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002927PyObject*
2928ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002930 slice_ty o = (slice_ty)_o;
2931 PyObject *result = NULL, *value = NULL;
2932 if (!o) {
2933 Py_INCREF(Py_None);
2934 return Py_None;
2935 }
2936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002937 switch (o->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002938 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002939 result = PyType_GenericNew(Slice_type, NULL, NULL);
2940 if (!result) goto failed;
2941 value = ast2obj_expr(o->v.Slice.lower);
2942 if (!value) goto failed;
2943 if (PyObject_SetAttrString(result, "lower", value) == -1)
2944 goto failed;
2945 Py_DECREF(value);
2946 value = ast2obj_expr(o->v.Slice.upper);
2947 if (!value) goto failed;
2948 if (PyObject_SetAttrString(result, "upper", value) == -1)
2949 goto failed;
2950 Py_DECREF(value);
2951 value = ast2obj_expr(o->v.Slice.step);
2952 if (!value) goto failed;
2953 if (PyObject_SetAttrString(result, "step", value) == -1)
2954 goto failed;
2955 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 break;
2957 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002958 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2959 if (!result) goto failed;
2960 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2961 if (!value) goto failed;
2962 if (PyObject_SetAttrString(result, "dims", value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 break;
2966 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002967 result = PyType_GenericNew(Index_type, NULL, NULL);
2968 if (!result) goto failed;
2969 value = ast2obj_expr(o->v.Index.value);
2970 if (!value) goto failed;
2971 if (PyObject_SetAttrString(result, "value", value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 break;
2975 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002976 return result;
2977failed:
2978 Py_XDECREF(value);
2979 Py_XDECREF(result);
2980 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981}
2982
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002983PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002985 switch(o) {
2986 case And:
2987 Py_INCREF(And_singleton);
2988 return And_singleton;
2989 case Or:
2990 Py_INCREF(Or_singleton);
2991 return Or_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002992 default:
2993 /* should never happen, but just in case ... */
2994 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002997}
2998PyObject* ast2obj_operator(operator_ty o)
2999{
3000 switch(o) {
3001 case Add:
3002 Py_INCREF(Add_singleton);
3003 return Add_singleton;
3004 case Sub:
3005 Py_INCREF(Sub_singleton);
3006 return Sub_singleton;
3007 case Mult:
3008 Py_INCREF(Mult_singleton);
3009 return Mult_singleton;
3010 case Div:
3011 Py_INCREF(Div_singleton);
3012 return Div_singleton;
3013 case Mod:
3014 Py_INCREF(Mod_singleton);
3015 return Mod_singleton;
3016 case Pow:
3017 Py_INCREF(Pow_singleton);
3018 return Pow_singleton;
3019 case LShift:
3020 Py_INCREF(LShift_singleton);
3021 return LShift_singleton;
3022 case RShift:
3023 Py_INCREF(RShift_singleton);
3024 return RShift_singleton;
3025 case BitOr:
3026 Py_INCREF(BitOr_singleton);
3027 return BitOr_singleton;
3028 case BitXor:
3029 Py_INCREF(BitXor_singleton);
3030 return BitXor_singleton;
3031 case BitAnd:
3032 Py_INCREF(BitAnd_singleton);
3033 return BitAnd_singleton;
3034 case FloorDiv:
3035 Py_INCREF(FloorDiv_singleton);
3036 return FloorDiv_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003037 default:
3038 /* should never happen, but just in case ... */
3039 PyErr_Format(PyExc_SystemError, "unknown operator found");
3040 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003041 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003042}
3043PyObject* ast2obj_unaryop(unaryop_ty o)
3044{
3045 switch(o) {
3046 case Invert:
3047 Py_INCREF(Invert_singleton);
3048 return Invert_singleton;
3049 case Not:
3050 Py_INCREF(Not_singleton);
3051 return Not_singleton;
3052 case UAdd:
3053 Py_INCREF(UAdd_singleton);
3054 return UAdd_singleton;
3055 case USub:
3056 Py_INCREF(USub_singleton);
3057 return USub_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003058 default:
3059 /* should never happen, but just in case ... */
3060 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3061 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003062 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003063}
3064PyObject* ast2obj_cmpop(cmpop_ty o)
3065{
3066 switch(o) {
3067 case Eq:
3068 Py_INCREF(Eq_singleton);
3069 return Eq_singleton;
3070 case NotEq:
3071 Py_INCREF(NotEq_singleton);
3072 return NotEq_singleton;
3073 case Lt:
3074 Py_INCREF(Lt_singleton);
3075 return Lt_singleton;
3076 case LtE:
3077 Py_INCREF(LtE_singleton);
3078 return LtE_singleton;
3079 case Gt:
3080 Py_INCREF(Gt_singleton);
3081 return Gt_singleton;
3082 case GtE:
3083 Py_INCREF(GtE_singleton);
3084 return GtE_singleton;
3085 case Is:
3086 Py_INCREF(Is_singleton);
3087 return Is_singleton;
3088 case IsNot:
3089 Py_INCREF(IsNot_singleton);
3090 return IsNot_singleton;
3091 case In:
3092 Py_INCREF(In_singleton);
3093 return In_singleton;
3094 case NotIn:
3095 Py_INCREF(NotIn_singleton);
3096 return NotIn_singleton;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003097 default:
3098 /* should never happen, but just in case ... */
3099 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3100 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003101 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003102}
3103PyObject*
3104ast2obj_comprehension(void* _o)
3105{
3106 comprehension_ty o = (comprehension_ty)_o;
3107 PyObject *result = NULL, *value = NULL;
3108 if (!o) {
3109 Py_INCREF(Py_None);
3110 return Py_None;
3111 }
3112
3113 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3114 if (!result) return NULL;
3115 value = ast2obj_expr(o->target);
3116 if (!value) goto failed;
3117 if (PyObject_SetAttrString(result, "target", value) == -1)
3118 goto failed;
3119 Py_DECREF(value);
3120 value = ast2obj_expr(o->iter);
3121 if (!value) goto failed;
3122 if (PyObject_SetAttrString(result, "iter", value) == -1)
3123 goto failed;
3124 Py_DECREF(value);
3125 value = ast2obj_list(o->ifs, ast2obj_expr);
3126 if (!value) goto failed;
3127 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3128 goto failed;
3129 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003130 return result;
3131failed:
3132 Py_XDECREF(value);
3133 Py_XDECREF(result);
3134 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135}
3136
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003137PyObject*
3138ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003140 excepthandler_ty o = (excepthandler_ty)_o;
3141 PyObject *result = NULL, *value = NULL;
3142 if (!o) {
3143 Py_INCREF(Py_None);
3144 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003146
3147 result = PyType_GenericNew(excepthandler_type, NULL, NULL);
3148 if (!result) return NULL;
3149 value = ast2obj_expr(o->type);
3150 if (!value) goto failed;
3151 if (PyObject_SetAttrString(result, "type", value) == -1)
3152 goto failed;
3153 Py_DECREF(value);
Guido van Rossum16be03e2007-01-10 18:51:35 +00003154 value = ast2obj_identifier(o->name);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003155 if (!value) goto failed;
3156 if (PyObject_SetAttrString(result, "name", value) == -1)
3157 goto failed;
3158 Py_DECREF(value);
3159 value = ast2obj_list(o->body, ast2obj_stmt);
3160 if (!value) goto failed;
3161 if (PyObject_SetAttrString(result, "body", value) == -1)
3162 goto failed;
3163 Py_DECREF(value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003164 value = ast2obj_int(o->lineno);
3165 if (!value) goto failed;
3166 if (PyObject_SetAttrString(result, "lineno", value) == -1)
3167 goto failed;
3168 Py_DECREF(value);
3169 value = ast2obj_int(o->col_offset);
3170 if (!value) goto failed;
3171 if (PyObject_SetAttrString(result, "col_offset", value) == -1)
3172 goto failed;
3173 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003174 return result;
3175failed:
3176 Py_XDECREF(value);
3177 Py_XDECREF(result);
3178 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179}
3180
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003181PyObject*
3182ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003184 arguments_ty o = (arguments_ty)_o;
3185 PyObject *result = NULL, *value = NULL;
3186 if (!o) {
3187 Py_INCREF(Py_None);
3188 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003190
3191 result = PyType_GenericNew(arguments_type, NULL, NULL);
3192 if (!result) return NULL;
Neal Norwitzc1505362006-12-28 06:47:50 +00003193 value = ast2obj_list(o->args, ast2obj_arg);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003194 if (!value) goto failed;
3195 if (PyObject_SetAttrString(result, "args", value) == -1)
3196 goto failed;
3197 Py_DECREF(value);
3198 value = ast2obj_identifier(o->vararg);
3199 if (!value) goto failed;
3200 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3201 goto failed;
3202 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003203 value = ast2obj_expr(o->varargannotation);
3204 if (!value) goto failed;
3205 if (PyObject_SetAttrString(result, "varargannotation", value) == -1)
3206 goto failed;
3207 Py_DECREF(value);
3208 value = ast2obj_list(o->kwonlyargs, ast2obj_arg);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003209 if (!value) goto failed;
3210 if (PyObject_SetAttrString(result, "kwonlyargs", value) == -1)
3211 goto failed;
3212 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003213 value = ast2obj_identifier(o->kwarg);
3214 if (!value) goto failed;
3215 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3216 goto failed;
3217 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003218 value = ast2obj_expr(o->kwargannotation);
3219 if (!value) goto failed;
3220 if (PyObject_SetAttrString(result, "kwargannotation", value) == -1)
3221 goto failed;
3222 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003223 value = ast2obj_list(o->defaults, ast2obj_expr);
3224 if (!value) goto failed;
3225 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3226 goto failed;
3227 Py_DECREF(value);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003228 value = ast2obj_list(o->kw_defaults, ast2obj_expr);
3229 if (!value) goto failed;
3230 if (PyObject_SetAttrString(result, "kw_defaults", value) == -1)
3231 goto failed;
3232 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003233 return result;
3234failed:
3235 Py_XDECREF(value);
3236 Py_XDECREF(result);
3237 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238}
3239
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003240PyObject*
Neal Norwitzc1505362006-12-28 06:47:50 +00003241ast2obj_arg(void* _o)
3242{
3243 arg_ty o = (arg_ty)_o;
3244 PyObject *result = NULL, *value = NULL;
3245 if (!o) {
3246 Py_INCREF(Py_None);
3247 return Py_None;
3248 }
3249
Guido van Rossum1bc535d2007-05-15 18:46:22 +00003250 result = PyType_GenericNew(arg_type, NULL, NULL);
3251 if (!result) return NULL;
3252 value = ast2obj_identifier(o->arg);
3253 if (!value) goto failed;
3254 if (PyObject_SetAttrString(result, "arg", value) == -1)
3255 goto failed;
3256 Py_DECREF(value);
3257 value = ast2obj_expr(o->annotation);
3258 if (!value) goto failed;
3259 if (PyObject_SetAttrString(result, "annotation", value) == -1)
3260 goto failed;
3261 Py_DECREF(value);
Neal Norwitzc1505362006-12-28 06:47:50 +00003262 return result;
3263failed:
3264 Py_XDECREF(value);
3265 Py_XDECREF(result);
3266 return NULL;
3267}
3268
3269PyObject*
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003270ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003272 keyword_ty o = (keyword_ty)_o;
3273 PyObject *result = NULL, *value = NULL;
3274 if (!o) {
3275 Py_INCREF(Py_None);
3276 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003278
3279 result = PyType_GenericNew(keyword_type, NULL, NULL);
3280 if (!result) return NULL;
3281 value = ast2obj_identifier(o->arg);
3282 if (!value) goto failed;
3283 if (PyObject_SetAttrString(result, "arg", value) == -1)
3284 goto failed;
3285 Py_DECREF(value);
3286 value = ast2obj_expr(o->value);
3287 if (!value) goto failed;
3288 if (PyObject_SetAttrString(result, "value", value) == -1)
3289 goto failed;
3290 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003291 return result;
3292failed:
3293 Py_XDECREF(value);
3294 Py_XDECREF(result);
3295 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296}
3297
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003298PyObject*
3299ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003301 alias_ty o = (alias_ty)_o;
3302 PyObject *result = NULL, *value = NULL;
3303 if (!o) {
3304 Py_INCREF(Py_None);
3305 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003307
3308 result = PyType_GenericNew(alias_type, NULL, NULL);
3309 if (!result) return NULL;
3310 value = ast2obj_identifier(o->name);
3311 if (!value) goto failed;
3312 if (PyObject_SetAttrString(result, "name", value) == -1)
3313 goto failed;
3314 Py_DECREF(value);
3315 value = ast2obj_identifier(o->asname);
3316 if (!value) goto failed;
3317 if (PyObject_SetAttrString(result, "asname", value) == -1)
3318 goto failed;
3319 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003320 return result;
3321failed:
3322 Py_XDECREF(value);
3323 Py_XDECREF(result);
3324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325}
3326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00003328int
3329obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3330{
3331 PyObject* tmp = NULL;
3332
3333
3334 if (obj == Py_None) {
3335 *out = NULL;
3336 return 0;
3337 }
3338 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3339 asdl_seq* body;
3340
3341 if (PyObject_HasAttrString(obj, "body")) {
3342 int res;
3343 Py_ssize_t len;
3344 Py_ssize_t i;
3345 tmp = PyObject_GetAttrString(obj, "body");
3346 if (tmp == NULL) goto failed;
3347 if (!PyList_Check(tmp)) {
3348 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3349 goto failed;
3350 }
3351 len = PyList_GET_SIZE(tmp);
3352 body = asdl_seq_new(len, arena);
3353 if (body == NULL) goto failed;
3354 for (i = 0; i < len; i++) {
3355 stmt_ty value;
3356 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3357 if (res != 0) goto failed;
3358 asdl_seq_SET(body, i, value);
3359 }
3360 Py_XDECREF(tmp);
3361 tmp = NULL;
3362 } else {
3363 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3364 return 1;
3365 }
3366 *out = Module(body, arena);
3367 if (*out == NULL) goto failed;
3368 return 0;
3369 }
3370 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3371 asdl_seq* body;
3372
3373 if (PyObject_HasAttrString(obj, "body")) {
3374 int res;
3375 Py_ssize_t len;
3376 Py_ssize_t i;
3377 tmp = PyObject_GetAttrString(obj, "body");
3378 if (tmp == NULL) goto failed;
3379 if (!PyList_Check(tmp)) {
3380 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3381 goto failed;
3382 }
3383 len = PyList_GET_SIZE(tmp);
3384 body = asdl_seq_new(len, arena);
3385 if (body == NULL) goto failed;
3386 for (i = 0; i < len; i++) {
3387 stmt_ty value;
3388 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3389 if (res != 0) goto failed;
3390 asdl_seq_SET(body, i, value);
3391 }
3392 Py_XDECREF(tmp);
3393 tmp = NULL;
3394 } else {
3395 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3396 return 1;
3397 }
3398 *out = Interactive(body, arena);
3399 if (*out == NULL) goto failed;
3400 return 0;
3401 }
3402 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3403 expr_ty body;
3404
3405 if (PyObject_HasAttrString(obj, "body")) {
3406 int res;
3407 tmp = PyObject_GetAttrString(obj, "body");
3408 if (tmp == NULL) goto failed;
3409 res = obj2ast_expr(tmp, &body, arena);
3410 if (res != 0) goto failed;
3411 Py_XDECREF(tmp);
3412 tmp = NULL;
3413 } else {
3414 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3415 return 1;
3416 }
3417 *out = Expression(body, arena);
3418 if (*out == NULL) goto failed;
3419 return 0;
3420 }
3421 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3422 asdl_seq* body;
3423
3424 if (PyObject_HasAttrString(obj, "body")) {
3425 int res;
3426 Py_ssize_t len;
3427 Py_ssize_t i;
3428 tmp = PyObject_GetAttrString(obj, "body");
3429 if (tmp == NULL) goto failed;
3430 if (!PyList_Check(tmp)) {
3431 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3432 goto failed;
3433 }
3434 len = PyList_GET_SIZE(tmp);
3435 body = asdl_seq_new(len, arena);
3436 if (body == NULL) goto failed;
3437 for (i = 0; i < len; i++) {
3438 stmt_ty value;
3439 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3440 if (res != 0) goto failed;
3441 asdl_seq_SET(body, i, value);
3442 }
3443 Py_XDECREF(tmp);
3444 tmp = NULL;
3445 } else {
3446 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3447 return 1;
3448 }
3449 *out = Suite(body, arena);
3450 if (*out == NULL) goto failed;
3451 return 0;
3452 }
3453
3454 tmp = PyObject_Repr(obj);
3455 if (tmp == NULL) goto failed;
3456 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3457failed:
3458 Py_XDECREF(tmp);
3459 return 1;
3460}
3461
3462int
3463obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3464{
3465 PyObject* tmp = NULL;
3466
3467 int lineno;
3468 int col_offset;
3469
3470 if (obj == Py_None) {
3471 *out = NULL;
3472 return 0;
3473 }
3474 if (PyObject_HasAttrString(obj, "lineno")) {
3475 int res;
3476 tmp = PyObject_GetAttrString(obj, "lineno");
3477 if (tmp == NULL) goto failed;
3478 res = obj2ast_int(tmp, &lineno, arena);
3479 if (res != 0) goto failed;
3480 Py_XDECREF(tmp);
3481 tmp = NULL;
3482 } else {
3483 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3484 return 1;
3485 }
3486 if (PyObject_HasAttrString(obj, "col_offset")) {
3487 int res;
3488 tmp = PyObject_GetAttrString(obj, "col_offset");
3489 if (tmp == NULL) goto failed;
3490 res = obj2ast_int(tmp, &col_offset, arena);
3491 if (res != 0) goto failed;
3492 Py_XDECREF(tmp);
3493 tmp = NULL;
3494 } else {
3495 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3496 return 1;
3497 }
3498 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3499 identifier name;
3500 arguments_ty args;
3501 asdl_seq* body;
3502 asdl_seq* decorator_list;
3503 expr_ty returns;
3504
3505 if (PyObject_HasAttrString(obj, "name")) {
3506 int res;
3507 tmp = PyObject_GetAttrString(obj, "name");
3508 if (tmp == NULL) goto failed;
3509 res = obj2ast_identifier(tmp, &name, arena);
3510 if (res != 0) goto failed;
3511 Py_XDECREF(tmp);
3512 tmp = NULL;
3513 } else {
3514 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3515 return 1;
3516 }
3517 if (PyObject_HasAttrString(obj, "args")) {
3518 int res;
3519 tmp = PyObject_GetAttrString(obj, "args");
3520 if (tmp == NULL) goto failed;
3521 res = obj2ast_arguments(tmp, &args, arena);
3522 if (res != 0) goto failed;
3523 Py_XDECREF(tmp);
3524 tmp = NULL;
3525 } else {
3526 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3527 return 1;
3528 }
3529 if (PyObject_HasAttrString(obj, "body")) {
3530 int res;
3531 Py_ssize_t len;
3532 Py_ssize_t i;
3533 tmp = PyObject_GetAttrString(obj, "body");
3534 if (tmp == NULL) goto failed;
3535 if (!PyList_Check(tmp)) {
3536 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3537 goto failed;
3538 }
3539 len = PyList_GET_SIZE(tmp);
3540 body = asdl_seq_new(len, arena);
3541 if (body == NULL) goto failed;
3542 for (i = 0; i < len; i++) {
3543 stmt_ty value;
3544 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3545 if (res != 0) goto failed;
3546 asdl_seq_SET(body, i, value);
3547 }
3548 Py_XDECREF(tmp);
3549 tmp = NULL;
3550 } else {
3551 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3552 return 1;
3553 }
3554 if (PyObject_HasAttrString(obj, "decorator_list")) {
3555 int res;
3556 Py_ssize_t len;
3557 Py_ssize_t i;
3558 tmp = PyObject_GetAttrString(obj, "decorator_list");
3559 if (tmp == NULL) goto failed;
3560 if (!PyList_Check(tmp)) {
3561 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3562 goto failed;
3563 }
3564 len = PyList_GET_SIZE(tmp);
3565 decorator_list = asdl_seq_new(len, arena);
3566 if (decorator_list == NULL) goto failed;
3567 for (i = 0; i < len; i++) {
3568 expr_ty value;
3569 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3570 if (res != 0) goto failed;
3571 asdl_seq_SET(decorator_list, i, value);
3572 }
3573 Py_XDECREF(tmp);
3574 tmp = NULL;
3575 } else {
3576 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3577 return 1;
3578 }
3579 if (PyObject_HasAttrString(obj, "returns")) {
3580 int res;
3581 tmp = PyObject_GetAttrString(obj, "returns");
3582 if (tmp == NULL) goto failed;
3583 res = obj2ast_expr(tmp, &returns, arena);
3584 if (res != 0) goto failed;
3585 Py_XDECREF(tmp);
3586 tmp = NULL;
3587 } else {
3588 returns = NULL;
3589 }
3590 *out = FunctionDef(name, args, body, decorator_list, returns,
3591 lineno, col_offset, arena);
3592 if (*out == NULL) goto failed;
3593 return 0;
3594 }
3595 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3596 identifier name;
3597 asdl_seq* bases;
3598 asdl_seq* keywords;
3599 expr_ty starargs;
3600 expr_ty kwargs;
3601 asdl_seq* body;
3602 asdl_seq* decorator_list;
3603
3604 if (PyObject_HasAttrString(obj, "name")) {
3605 int res;
3606 tmp = PyObject_GetAttrString(obj, "name");
3607 if (tmp == NULL) goto failed;
3608 res = obj2ast_identifier(tmp, &name, arena);
3609 if (res != 0) goto failed;
3610 Py_XDECREF(tmp);
3611 tmp = NULL;
3612 } else {
3613 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3614 return 1;
3615 }
3616 if (PyObject_HasAttrString(obj, "bases")) {
3617 int res;
3618 Py_ssize_t len;
3619 Py_ssize_t i;
3620 tmp = PyObject_GetAttrString(obj, "bases");
3621 if (tmp == NULL) goto failed;
3622 if (!PyList_Check(tmp)) {
3623 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3624 goto failed;
3625 }
3626 len = PyList_GET_SIZE(tmp);
3627 bases = asdl_seq_new(len, arena);
3628 if (bases == NULL) goto failed;
3629 for (i = 0; i < len; i++) {
3630 expr_ty value;
3631 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3632 if (res != 0) goto failed;
3633 asdl_seq_SET(bases, i, value);
3634 }
3635 Py_XDECREF(tmp);
3636 tmp = NULL;
3637 } else {
3638 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3639 return 1;
3640 }
3641 if (PyObject_HasAttrString(obj, "keywords")) {
3642 int res;
3643 Py_ssize_t len;
3644 Py_ssize_t i;
3645 tmp = PyObject_GetAttrString(obj, "keywords");
3646 if (tmp == NULL) goto failed;
3647 if (!PyList_Check(tmp)) {
3648 PyErr_Format(PyExc_TypeError, "ClassDef field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3649 goto failed;
3650 }
3651 len = PyList_GET_SIZE(tmp);
3652 keywords = asdl_seq_new(len, arena);
3653 if (keywords == NULL) goto failed;
3654 for (i = 0; i < len; i++) {
3655 keyword_ty value;
3656 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
3657 if (res != 0) goto failed;
3658 asdl_seq_SET(keywords, i, value);
3659 }
3660 Py_XDECREF(tmp);
3661 tmp = NULL;
3662 } else {
3663 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");
3664 return 1;
3665 }
3666 if (PyObject_HasAttrString(obj, "starargs")) {
3667 int res;
3668 tmp = PyObject_GetAttrString(obj, "starargs");
3669 if (tmp == NULL) goto failed;
3670 res = obj2ast_expr(tmp, &starargs, arena);
3671 if (res != 0) goto failed;
3672 Py_XDECREF(tmp);
3673 tmp = NULL;
3674 } else {
3675 starargs = NULL;
3676 }
3677 if (PyObject_HasAttrString(obj, "kwargs")) {
3678 int res;
3679 tmp = PyObject_GetAttrString(obj, "kwargs");
3680 if (tmp == NULL) goto failed;
3681 res = obj2ast_expr(tmp, &kwargs, arena);
3682 if (res != 0) goto failed;
3683 Py_XDECREF(tmp);
3684 tmp = NULL;
3685 } else {
3686 kwargs = NULL;
3687 }
3688 if (PyObject_HasAttrString(obj, "body")) {
3689 int res;
3690 Py_ssize_t len;
3691 Py_ssize_t i;
3692 tmp = PyObject_GetAttrString(obj, "body");
3693 if (tmp == NULL) goto failed;
3694 if (!PyList_Check(tmp)) {
3695 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3696 goto failed;
3697 }
3698 len = PyList_GET_SIZE(tmp);
3699 body = asdl_seq_new(len, arena);
3700 if (body == NULL) goto failed;
3701 for (i = 0; i < len; i++) {
3702 stmt_ty value;
3703 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3704 if (res != 0) goto failed;
3705 asdl_seq_SET(body, i, value);
3706 }
3707 Py_XDECREF(tmp);
3708 tmp = NULL;
3709 } else {
3710 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3711 return 1;
3712 }
3713 if (PyObject_HasAttrString(obj, "decorator_list")) {
3714 int res;
3715 Py_ssize_t len;
3716 Py_ssize_t i;
3717 tmp = PyObject_GetAttrString(obj, "decorator_list");
3718 if (tmp == NULL) goto failed;
3719 if (!PyList_Check(tmp)) {
3720 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3721 goto failed;
3722 }
3723 len = PyList_GET_SIZE(tmp);
3724 decorator_list = asdl_seq_new(len, arena);
3725 if (decorator_list == NULL) goto failed;
3726 for (i = 0; i < len; i++) {
3727 expr_ty value;
3728 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3729 if (res != 0) goto failed;
3730 asdl_seq_SET(decorator_list, i, value);
3731 }
3732 Py_XDECREF(tmp);
3733 tmp = NULL;
3734 } else {
3735 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3736 return 1;
3737 }
3738 *out = ClassDef(name, bases, keywords, starargs, kwargs, body,
3739 decorator_list, lineno, col_offset, arena);
3740 if (*out == NULL) goto failed;
3741 return 0;
3742 }
3743 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3744 expr_ty value;
3745
3746 if (PyObject_HasAttrString(obj, "value")) {
3747 int res;
3748 tmp = PyObject_GetAttrString(obj, "value");
3749 if (tmp == NULL) goto failed;
3750 res = obj2ast_expr(tmp, &value, arena);
3751 if (res != 0) goto failed;
3752 Py_XDECREF(tmp);
3753 tmp = NULL;
3754 } else {
3755 value = NULL;
3756 }
3757 *out = Return(value, lineno, col_offset, arena);
3758 if (*out == NULL) goto failed;
3759 return 0;
3760 }
3761 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3762 asdl_seq* targets;
3763
3764 if (PyObject_HasAttrString(obj, "targets")) {
3765 int res;
3766 Py_ssize_t len;
3767 Py_ssize_t i;
3768 tmp = PyObject_GetAttrString(obj, "targets");
3769 if (tmp == NULL) goto failed;
3770 if (!PyList_Check(tmp)) {
3771 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3772 goto failed;
3773 }
3774 len = PyList_GET_SIZE(tmp);
3775 targets = asdl_seq_new(len, arena);
3776 if (targets == NULL) goto failed;
3777 for (i = 0; i < len; i++) {
3778 expr_ty value;
3779 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3780 if (res != 0) goto failed;
3781 asdl_seq_SET(targets, i, value);
3782 }
3783 Py_XDECREF(tmp);
3784 tmp = NULL;
3785 } else {
3786 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3787 return 1;
3788 }
3789 *out = Delete(targets, lineno, col_offset, arena);
3790 if (*out == NULL) goto failed;
3791 return 0;
3792 }
3793 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3794 asdl_seq* targets;
3795 expr_ty value;
3796
3797 if (PyObject_HasAttrString(obj, "targets")) {
3798 int res;
3799 Py_ssize_t len;
3800 Py_ssize_t i;
3801 tmp = PyObject_GetAttrString(obj, "targets");
3802 if (tmp == NULL) goto failed;
3803 if (!PyList_Check(tmp)) {
3804 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3805 goto failed;
3806 }
3807 len = PyList_GET_SIZE(tmp);
3808 targets = asdl_seq_new(len, arena);
3809 if (targets == NULL) goto failed;
3810 for (i = 0; i < len; i++) {
3811 expr_ty value;
3812 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3813 if (res != 0) goto failed;
3814 asdl_seq_SET(targets, i, value);
3815 }
3816 Py_XDECREF(tmp);
3817 tmp = NULL;
3818 } else {
3819 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3820 return 1;
3821 }
3822 if (PyObject_HasAttrString(obj, "value")) {
3823 int res;
3824 tmp = PyObject_GetAttrString(obj, "value");
3825 if (tmp == NULL) goto failed;
3826 res = obj2ast_expr(tmp, &value, arena);
3827 if (res != 0) goto failed;
3828 Py_XDECREF(tmp);
3829 tmp = NULL;
3830 } else {
3831 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3832 return 1;
3833 }
3834 *out = Assign(targets, value, lineno, col_offset, arena);
3835 if (*out == NULL) goto failed;
3836 return 0;
3837 }
3838 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3839 expr_ty target;
3840 operator_ty op;
3841 expr_ty value;
3842
3843 if (PyObject_HasAttrString(obj, "target")) {
3844 int res;
3845 tmp = PyObject_GetAttrString(obj, "target");
3846 if (tmp == NULL) goto failed;
3847 res = obj2ast_expr(tmp, &target, arena);
3848 if (res != 0) goto failed;
3849 Py_XDECREF(tmp);
3850 tmp = NULL;
3851 } else {
3852 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3853 return 1;
3854 }
3855 if (PyObject_HasAttrString(obj, "op")) {
3856 int res;
3857 tmp = PyObject_GetAttrString(obj, "op");
3858 if (tmp == NULL) goto failed;
3859 res = obj2ast_operator(tmp, &op, arena);
3860 if (res != 0) goto failed;
3861 Py_XDECREF(tmp);
3862 tmp = NULL;
3863 } else {
3864 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3865 return 1;
3866 }
3867 if (PyObject_HasAttrString(obj, "value")) {
3868 int res;
3869 tmp = PyObject_GetAttrString(obj, "value");
3870 if (tmp == NULL) goto failed;
3871 res = obj2ast_expr(tmp, &value, arena);
3872 if (res != 0) goto failed;
3873 Py_XDECREF(tmp);
3874 tmp = NULL;
3875 } else {
3876 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3877 return 1;
3878 }
3879 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3880 if (*out == NULL) goto failed;
3881 return 0;
3882 }
3883 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3884 expr_ty target;
3885 expr_ty iter;
3886 asdl_seq* body;
3887 asdl_seq* orelse;
3888
3889 if (PyObject_HasAttrString(obj, "target")) {
3890 int res;
3891 tmp = PyObject_GetAttrString(obj, "target");
3892 if (tmp == NULL) goto failed;
3893 res = obj2ast_expr(tmp, &target, arena);
3894 if (res != 0) goto failed;
3895 Py_XDECREF(tmp);
3896 tmp = NULL;
3897 } else {
3898 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3899 return 1;
3900 }
3901 if (PyObject_HasAttrString(obj, "iter")) {
3902 int res;
3903 tmp = PyObject_GetAttrString(obj, "iter");
3904 if (tmp == NULL) goto failed;
3905 res = obj2ast_expr(tmp, &iter, arena);
3906 if (res != 0) goto failed;
3907 Py_XDECREF(tmp);
3908 tmp = NULL;
3909 } else {
3910 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3911 return 1;
3912 }
3913 if (PyObject_HasAttrString(obj, "body")) {
3914 int res;
3915 Py_ssize_t len;
3916 Py_ssize_t i;
3917 tmp = PyObject_GetAttrString(obj, "body");
3918 if (tmp == NULL) goto failed;
3919 if (!PyList_Check(tmp)) {
3920 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3921 goto failed;
3922 }
3923 len = PyList_GET_SIZE(tmp);
3924 body = asdl_seq_new(len, arena);
3925 if (body == NULL) goto failed;
3926 for (i = 0; i < len; i++) {
3927 stmt_ty value;
3928 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3929 if (res != 0) goto failed;
3930 asdl_seq_SET(body, i, value);
3931 }
3932 Py_XDECREF(tmp);
3933 tmp = NULL;
3934 } else {
3935 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3936 return 1;
3937 }
3938 if (PyObject_HasAttrString(obj, "orelse")) {
3939 int res;
3940 Py_ssize_t len;
3941 Py_ssize_t i;
3942 tmp = PyObject_GetAttrString(obj, "orelse");
3943 if (tmp == NULL) goto failed;
3944 if (!PyList_Check(tmp)) {
3945 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3946 goto failed;
3947 }
3948 len = PyList_GET_SIZE(tmp);
3949 orelse = asdl_seq_new(len, arena);
3950 if (orelse == NULL) goto failed;
3951 for (i = 0; i < len; i++) {
3952 stmt_ty value;
3953 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3954 if (res != 0) goto failed;
3955 asdl_seq_SET(orelse, i, value);
3956 }
3957 Py_XDECREF(tmp);
3958 tmp = NULL;
3959 } else {
3960 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3961 return 1;
3962 }
3963 *out = For(target, iter, body, orelse, lineno, col_offset,
3964 arena);
3965 if (*out == NULL) goto failed;
3966 return 0;
3967 }
3968 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3969 expr_ty test;
3970 asdl_seq* body;
3971 asdl_seq* orelse;
3972
3973 if (PyObject_HasAttrString(obj, "test")) {
3974 int res;
3975 tmp = PyObject_GetAttrString(obj, "test");
3976 if (tmp == NULL) goto failed;
3977 res = obj2ast_expr(tmp, &test, arena);
3978 if (res != 0) goto failed;
3979 Py_XDECREF(tmp);
3980 tmp = NULL;
3981 } else {
3982 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3983 return 1;
3984 }
3985 if (PyObject_HasAttrString(obj, "body")) {
3986 int res;
3987 Py_ssize_t len;
3988 Py_ssize_t i;
3989 tmp = PyObject_GetAttrString(obj, "body");
3990 if (tmp == NULL) goto failed;
3991 if (!PyList_Check(tmp)) {
3992 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3993 goto failed;
3994 }
3995 len = PyList_GET_SIZE(tmp);
3996 body = asdl_seq_new(len, arena);
3997 if (body == NULL) goto failed;
3998 for (i = 0; i < len; i++) {
3999 stmt_ty value;
4000 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4001 if (res != 0) goto failed;
4002 asdl_seq_SET(body, i, value);
4003 }
4004 Py_XDECREF(tmp);
4005 tmp = NULL;
4006 } else {
4007 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4008 return 1;
4009 }
4010 if (PyObject_HasAttrString(obj, "orelse")) {
4011 int res;
4012 Py_ssize_t len;
4013 Py_ssize_t i;
4014 tmp = PyObject_GetAttrString(obj, "orelse");
4015 if (tmp == NULL) goto failed;
4016 if (!PyList_Check(tmp)) {
4017 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4018 goto failed;
4019 }
4020 len = PyList_GET_SIZE(tmp);
4021 orelse = asdl_seq_new(len, arena);
4022 if (orelse == NULL) goto failed;
4023 for (i = 0; i < len; i++) {
4024 stmt_ty value;
4025 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4026 if (res != 0) goto failed;
4027 asdl_seq_SET(orelse, i, value);
4028 }
4029 Py_XDECREF(tmp);
4030 tmp = NULL;
4031 } else {
4032 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4033 return 1;
4034 }
4035 *out = While(test, body, orelse, lineno, col_offset, arena);
4036 if (*out == NULL) goto failed;
4037 return 0;
4038 }
4039 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
4040 expr_ty test;
4041 asdl_seq* body;
4042 asdl_seq* orelse;
4043
4044 if (PyObject_HasAttrString(obj, "test")) {
4045 int res;
4046 tmp = PyObject_GetAttrString(obj, "test");
4047 if (tmp == NULL) goto failed;
4048 res = obj2ast_expr(tmp, &test, arena);
4049 if (res != 0) goto failed;
4050 Py_XDECREF(tmp);
4051 tmp = NULL;
4052 } else {
4053 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4054 return 1;
4055 }
4056 if (PyObject_HasAttrString(obj, "body")) {
4057 int res;
4058 Py_ssize_t len;
4059 Py_ssize_t i;
4060 tmp = PyObject_GetAttrString(obj, "body");
4061 if (tmp == NULL) goto failed;
4062 if (!PyList_Check(tmp)) {
4063 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4064 goto failed;
4065 }
4066 len = PyList_GET_SIZE(tmp);
4067 body = asdl_seq_new(len, arena);
4068 if (body == NULL) goto failed;
4069 for (i = 0; i < len; i++) {
4070 stmt_ty value;
4071 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4072 if (res != 0) goto failed;
4073 asdl_seq_SET(body, i, value);
4074 }
4075 Py_XDECREF(tmp);
4076 tmp = NULL;
4077 } else {
4078 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4079 return 1;
4080 }
4081 if (PyObject_HasAttrString(obj, "orelse")) {
4082 int res;
4083 Py_ssize_t len;
4084 Py_ssize_t i;
4085 tmp = PyObject_GetAttrString(obj, "orelse");
4086 if (tmp == NULL) goto failed;
4087 if (!PyList_Check(tmp)) {
4088 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4089 goto failed;
4090 }
4091 len = PyList_GET_SIZE(tmp);
4092 orelse = asdl_seq_new(len, arena);
4093 if (orelse == NULL) goto failed;
4094 for (i = 0; i < len; i++) {
4095 stmt_ty value;
4096 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4097 if (res != 0) goto failed;
4098 asdl_seq_SET(orelse, i, value);
4099 }
4100 Py_XDECREF(tmp);
4101 tmp = NULL;
4102 } else {
4103 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4104 return 1;
4105 }
4106 *out = If(test, body, orelse, lineno, col_offset, arena);
4107 if (*out == NULL) goto failed;
4108 return 0;
4109 }
4110 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
4111 expr_ty context_expr;
4112 expr_ty optional_vars;
4113 asdl_seq* body;
4114
4115 if (PyObject_HasAttrString(obj, "context_expr")) {
4116 int res;
4117 tmp = PyObject_GetAttrString(obj, "context_expr");
4118 if (tmp == NULL) goto failed;
4119 res = obj2ast_expr(tmp, &context_expr, arena);
4120 if (res != 0) goto failed;
4121 Py_XDECREF(tmp);
4122 tmp = NULL;
4123 } else {
4124 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4125 return 1;
4126 }
4127 if (PyObject_HasAttrString(obj, "optional_vars")) {
4128 int res;
4129 tmp = PyObject_GetAttrString(obj, "optional_vars");
4130 if (tmp == NULL) goto failed;
4131 res = obj2ast_expr(tmp, &optional_vars, arena);
4132 if (res != 0) goto failed;
4133 Py_XDECREF(tmp);
4134 tmp = NULL;
4135 } else {
4136 optional_vars = NULL;
4137 }
4138 if (PyObject_HasAttrString(obj, "body")) {
4139 int res;
4140 Py_ssize_t len;
4141 Py_ssize_t i;
4142 tmp = PyObject_GetAttrString(obj, "body");
4143 if (tmp == NULL) goto failed;
4144 if (!PyList_Check(tmp)) {
4145 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4146 goto failed;
4147 }
4148 len = PyList_GET_SIZE(tmp);
4149 body = asdl_seq_new(len, arena);
4150 if (body == NULL) goto failed;
4151 for (i = 0; i < len; i++) {
4152 stmt_ty value;
4153 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4154 if (res != 0) goto failed;
4155 asdl_seq_SET(body, i, value);
4156 }
4157 Py_XDECREF(tmp);
4158 tmp = NULL;
4159 } else {
4160 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4161 return 1;
4162 }
4163 *out = With(context_expr, optional_vars, body, lineno,
4164 col_offset, arena);
4165 if (*out == NULL) goto failed;
4166 return 0;
4167 }
4168 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
4169 expr_ty exc;
4170 expr_ty cause;
4171
4172 if (PyObject_HasAttrString(obj, "exc")) {
4173 int res;
4174 tmp = PyObject_GetAttrString(obj, "exc");
4175 if (tmp == NULL) goto failed;
4176 res = obj2ast_expr(tmp, &exc, arena);
4177 if (res != 0) goto failed;
4178 Py_XDECREF(tmp);
4179 tmp = NULL;
4180 } else {
4181 exc = NULL;
4182 }
4183 if (PyObject_HasAttrString(obj, "cause")) {
4184 int res;
4185 tmp = PyObject_GetAttrString(obj, "cause");
4186 if (tmp == NULL) goto failed;
4187 res = obj2ast_expr(tmp, &cause, arena);
4188 if (res != 0) goto failed;
4189 Py_XDECREF(tmp);
4190 tmp = NULL;
4191 } else {
4192 cause = NULL;
4193 }
4194 *out = Raise(exc, cause, lineno, col_offset, arena);
4195 if (*out == NULL) goto failed;
4196 return 0;
4197 }
4198 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4199 asdl_seq* body;
4200 asdl_seq* handlers;
4201 asdl_seq* orelse;
4202
4203 if (PyObject_HasAttrString(obj, "body")) {
4204 int res;
4205 Py_ssize_t len;
4206 Py_ssize_t i;
4207 tmp = PyObject_GetAttrString(obj, "body");
4208 if (tmp == NULL) goto failed;
4209 if (!PyList_Check(tmp)) {
4210 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4211 goto failed;
4212 }
4213 len = PyList_GET_SIZE(tmp);
4214 body = asdl_seq_new(len, arena);
4215 if (body == NULL) goto failed;
4216 for (i = 0; i < len; i++) {
4217 stmt_ty value;
4218 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4219 if (res != 0) goto failed;
4220 asdl_seq_SET(body, i, value);
4221 }
4222 Py_XDECREF(tmp);
4223 tmp = NULL;
4224 } else {
4225 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4226 return 1;
4227 }
4228 if (PyObject_HasAttrString(obj, "handlers")) {
4229 int res;
4230 Py_ssize_t len;
4231 Py_ssize_t i;
4232 tmp = PyObject_GetAttrString(obj, "handlers");
4233 if (tmp == NULL) goto failed;
4234 if (!PyList_Check(tmp)) {
4235 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4236 goto failed;
4237 }
4238 len = PyList_GET_SIZE(tmp);
4239 handlers = asdl_seq_new(len, arena);
4240 if (handlers == NULL) goto failed;
4241 for (i = 0; i < len; i++) {
4242 excepthandler_ty value;
4243 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4244 if (res != 0) goto failed;
4245 asdl_seq_SET(handlers, i, value);
4246 }
4247 Py_XDECREF(tmp);
4248 tmp = NULL;
4249 } else {
4250 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4251 return 1;
4252 }
4253 if (PyObject_HasAttrString(obj, "orelse")) {
4254 int res;
4255 Py_ssize_t len;
4256 Py_ssize_t i;
4257 tmp = PyObject_GetAttrString(obj, "orelse");
4258 if (tmp == NULL) goto failed;
4259 if (!PyList_Check(tmp)) {
4260 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4261 goto failed;
4262 }
4263 len = PyList_GET_SIZE(tmp);
4264 orelse = asdl_seq_new(len, arena);
4265 if (orelse == NULL) goto failed;
4266 for (i = 0; i < len; i++) {
4267 stmt_ty value;
4268 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4269 if (res != 0) goto failed;
4270 asdl_seq_SET(orelse, i, value);
4271 }
4272 Py_XDECREF(tmp);
4273 tmp = NULL;
4274 } else {
4275 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4276 return 1;
4277 }
4278 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4279 arena);
4280 if (*out == NULL) goto failed;
4281 return 0;
4282 }
4283 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4284 asdl_seq* body;
4285 asdl_seq* finalbody;
4286
4287 if (PyObject_HasAttrString(obj, "body")) {
4288 int res;
4289 Py_ssize_t len;
4290 Py_ssize_t i;
4291 tmp = PyObject_GetAttrString(obj, "body");
4292 if (tmp == NULL) goto failed;
4293 if (!PyList_Check(tmp)) {
4294 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4295 goto failed;
4296 }
4297 len = PyList_GET_SIZE(tmp);
4298 body = asdl_seq_new(len, arena);
4299 if (body == NULL) goto failed;
4300 for (i = 0; i < len; i++) {
4301 stmt_ty value;
4302 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4303 if (res != 0) goto failed;
4304 asdl_seq_SET(body, i, value);
4305 }
4306 Py_XDECREF(tmp);
4307 tmp = NULL;
4308 } else {
4309 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4310 return 1;
4311 }
4312 if (PyObject_HasAttrString(obj, "finalbody")) {
4313 int res;
4314 Py_ssize_t len;
4315 Py_ssize_t i;
4316 tmp = PyObject_GetAttrString(obj, "finalbody");
4317 if (tmp == NULL) goto failed;
4318 if (!PyList_Check(tmp)) {
4319 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4320 goto failed;
4321 }
4322 len = PyList_GET_SIZE(tmp);
4323 finalbody = asdl_seq_new(len, arena);
4324 if (finalbody == NULL) goto failed;
4325 for (i = 0; i < len; i++) {
4326 stmt_ty value;
4327 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4328 if (res != 0) goto failed;
4329 asdl_seq_SET(finalbody, i, value);
4330 }
4331 Py_XDECREF(tmp);
4332 tmp = NULL;
4333 } else {
4334 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4335 return 1;
4336 }
4337 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4338 if (*out == NULL) goto failed;
4339 return 0;
4340 }
4341 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4342 expr_ty test;
4343 expr_ty msg;
4344
4345 if (PyObject_HasAttrString(obj, "test")) {
4346 int res;
4347 tmp = PyObject_GetAttrString(obj, "test");
4348 if (tmp == NULL) goto failed;
4349 res = obj2ast_expr(tmp, &test, arena);
4350 if (res != 0) goto failed;
4351 Py_XDECREF(tmp);
4352 tmp = NULL;
4353 } else {
4354 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4355 return 1;
4356 }
4357 if (PyObject_HasAttrString(obj, "msg")) {
4358 int res;
4359 tmp = PyObject_GetAttrString(obj, "msg");
4360 if (tmp == NULL) goto failed;
4361 res = obj2ast_expr(tmp, &msg, arena);
4362 if (res != 0) goto failed;
4363 Py_XDECREF(tmp);
4364 tmp = NULL;
4365 } else {
4366 msg = NULL;
4367 }
4368 *out = Assert(test, msg, lineno, col_offset, arena);
4369 if (*out == NULL) goto failed;
4370 return 0;
4371 }
4372 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4373 asdl_seq* names;
4374
4375 if (PyObject_HasAttrString(obj, "names")) {
4376 int res;
4377 Py_ssize_t len;
4378 Py_ssize_t i;
4379 tmp = PyObject_GetAttrString(obj, "names");
4380 if (tmp == NULL) goto failed;
4381 if (!PyList_Check(tmp)) {
4382 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4383 goto failed;
4384 }
4385 len = PyList_GET_SIZE(tmp);
4386 names = asdl_seq_new(len, arena);
4387 if (names == NULL) goto failed;
4388 for (i = 0; i < len; i++) {
4389 alias_ty value;
4390 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4391 if (res != 0) goto failed;
4392 asdl_seq_SET(names, i, value);
4393 }
4394 Py_XDECREF(tmp);
4395 tmp = NULL;
4396 } else {
4397 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4398 return 1;
4399 }
4400 *out = Import(names, lineno, col_offset, arena);
4401 if (*out == NULL) goto failed;
4402 return 0;
4403 }
4404 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4405 identifier module;
4406 asdl_seq* names;
4407 int level;
4408
4409 if (PyObject_HasAttrString(obj, "module")) {
4410 int res;
4411 tmp = PyObject_GetAttrString(obj, "module");
4412 if (tmp == NULL) goto failed;
4413 res = obj2ast_identifier(tmp, &module, arena);
4414 if (res != 0) goto failed;
4415 Py_XDECREF(tmp);
4416 tmp = NULL;
4417 } else {
4418 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4419 return 1;
4420 }
4421 if (PyObject_HasAttrString(obj, "names")) {
4422 int res;
4423 Py_ssize_t len;
4424 Py_ssize_t i;
4425 tmp = PyObject_GetAttrString(obj, "names");
4426 if (tmp == NULL) goto failed;
4427 if (!PyList_Check(tmp)) {
4428 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4429 goto failed;
4430 }
4431 len = PyList_GET_SIZE(tmp);
4432 names = asdl_seq_new(len, arena);
4433 if (names == NULL) goto failed;
4434 for (i = 0; i < len; i++) {
4435 alias_ty value;
4436 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4437 if (res != 0) goto failed;
4438 asdl_seq_SET(names, i, value);
4439 }
4440 Py_XDECREF(tmp);
4441 tmp = NULL;
4442 } else {
4443 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4444 return 1;
4445 }
4446 if (PyObject_HasAttrString(obj, "level")) {
4447 int res;
4448 tmp = PyObject_GetAttrString(obj, "level");
4449 if (tmp == NULL) goto failed;
4450 res = obj2ast_int(tmp, &level, arena);
4451 if (res != 0) goto failed;
4452 Py_XDECREF(tmp);
4453 tmp = NULL;
4454 } else {
4455 level = 0;
4456 }
4457 *out = ImportFrom(module, names, level, lineno, col_offset,
4458 arena);
4459 if (*out == NULL) goto failed;
4460 return 0;
4461 }
4462 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4463 asdl_seq* names;
4464
4465 if (PyObject_HasAttrString(obj, "names")) {
4466 int res;
4467 Py_ssize_t len;
4468 Py_ssize_t i;
4469 tmp = PyObject_GetAttrString(obj, "names");
4470 if (tmp == NULL) goto failed;
4471 if (!PyList_Check(tmp)) {
4472 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4473 goto failed;
4474 }
4475 len = PyList_GET_SIZE(tmp);
4476 names = asdl_seq_new(len, arena);
4477 if (names == NULL) goto failed;
4478 for (i = 0; i < len; i++) {
4479 identifier value;
4480 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4481 if (res != 0) goto failed;
4482 asdl_seq_SET(names, i, value);
4483 }
4484 Py_XDECREF(tmp);
4485 tmp = NULL;
4486 } else {
4487 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4488 return 1;
4489 }
4490 *out = Global(names, lineno, col_offset, arena);
4491 if (*out == NULL) goto failed;
4492 return 0;
4493 }
4494 if (PyObject_IsInstance(obj, (PyObject*)Nonlocal_type)) {
4495 asdl_seq* names;
4496
4497 if (PyObject_HasAttrString(obj, "names")) {
4498 int res;
4499 Py_ssize_t len;
4500 Py_ssize_t i;
4501 tmp = PyObject_GetAttrString(obj, "names");
4502 if (tmp == NULL) goto failed;
4503 if (!PyList_Check(tmp)) {
4504 PyErr_Format(PyExc_TypeError, "Nonlocal field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4505 goto failed;
4506 }
4507 len = PyList_GET_SIZE(tmp);
4508 names = asdl_seq_new(len, arena);
4509 if (names == NULL) goto failed;
4510 for (i = 0; i < len; i++) {
4511 identifier value;
4512 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4513 if (res != 0) goto failed;
4514 asdl_seq_SET(names, i, value);
4515 }
4516 Py_XDECREF(tmp);
4517 tmp = NULL;
4518 } else {
4519 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Nonlocal");
4520 return 1;
4521 }
4522 *out = Nonlocal(names, lineno, col_offset, arena);
4523 if (*out == NULL) goto failed;
4524 return 0;
4525 }
4526 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4527 expr_ty value;
4528
4529 if (PyObject_HasAttrString(obj, "value")) {
4530 int res;
4531 tmp = PyObject_GetAttrString(obj, "value");
4532 if (tmp == NULL) goto failed;
4533 res = obj2ast_expr(tmp, &value, arena);
4534 if (res != 0) goto failed;
4535 Py_XDECREF(tmp);
4536 tmp = NULL;
4537 } else {
4538 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4539 return 1;
4540 }
4541 *out = Expr(value, lineno, col_offset, arena);
4542 if (*out == NULL) goto failed;
4543 return 0;
4544 }
4545 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4546
4547 *out = Pass(lineno, col_offset, arena);
4548 if (*out == NULL) goto failed;
4549 return 0;
4550 }
4551 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4552
4553 *out = Break(lineno, col_offset, arena);
4554 if (*out == NULL) goto failed;
4555 return 0;
4556 }
4557 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4558
4559 *out = Continue(lineno, col_offset, arena);
4560 if (*out == NULL) goto failed;
4561 return 0;
4562 }
4563
4564 tmp = PyObject_Repr(obj);
4565 if (tmp == NULL) goto failed;
4566 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4567failed:
4568 Py_XDECREF(tmp);
4569 return 1;
4570}
4571
4572int
4573obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4574{
4575 PyObject* tmp = NULL;
4576
4577 int lineno;
4578 int col_offset;
4579
4580 if (obj == Py_None) {
4581 *out = NULL;
4582 return 0;
4583 }
4584 if (PyObject_HasAttrString(obj, "lineno")) {
4585 int res;
4586 tmp = PyObject_GetAttrString(obj, "lineno");
4587 if (tmp == NULL) goto failed;
4588 res = obj2ast_int(tmp, &lineno, arena);
4589 if (res != 0) goto failed;
4590 Py_XDECREF(tmp);
4591 tmp = NULL;
4592 } else {
4593 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4594 return 1;
4595 }
4596 if (PyObject_HasAttrString(obj, "col_offset")) {
4597 int res;
4598 tmp = PyObject_GetAttrString(obj, "col_offset");
4599 if (tmp == NULL) goto failed;
4600 res = obj2ast_int(tmp, &col_offset, arena);
4601 if (res != 0) goto failed;
4602 Py_XDECREF(tmp);
4603 tmp = NULL;
4604 } else {
4605 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4606 return 1;
4607 }
4608 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4609 boolop_ty op;
4610 asdl_seq* values;
4611
4612 if (PyObject_HasAttrString(obj, "op")) {
4613 int res;
4614 tmp = PyObject_GetAttrString(obj, "op");
4615 if (tmp == NULL) goto failed;
4616 res = obj2ast_boolop(tmp, &op, arena);
4617 if (res != 0) goto failed;
4618 Py_XDECREF(tmp);
4619 tmp = NULL;
4620 } else {
4621 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4622 return 1;
4623 }
4624 if (PyObject_HasAttrString(obj, "values")) {
4625 int res;
4626 Py_ssize_t len;
4627 Py_ssize_t i;
4628 tmp = PyObject_GetAttrString(obj, "values");
4629 if (tmp == NULL) goto failed;
4630 if (!PyList_Check(tmp)) {
4631 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4632 goto failed;
4633 }
4634 len = PyList_GET_SIZE(tmp);
4635 values = asdl_seq_new(len, arena);
4636 if (values == NULL) goto failed;
4637 for (i = 0; i < len; i++) {
4638 expr_ty value;
4639 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4640 if (res != 0) goto failed;
4641 asdl_seq_SET(values, i, value);
4642 }
4643 Py_XDECREF(tmp);
4644 tmp = NULL;
4645 } else {
4646 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4647 return 1;
4648 }
4649 *out = BoolOp(op, values, lineno, col_offset, arena);
4650 if (*out == NULL) goto failed;
4651 return 0;
4652 }
4653 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4654 expr_ty left;
4655 operator_ty op;
4656 expr_ty right;
4657
4658 if (PyObject_HasAttrString(obj, "left")) {
4659 int res;
4660 tmp = PyObject_GetAttrString(obj, "left");
4661 if (tmp == NULL) goto failed;
4662 res = obj2ast_expr(tmp, &left, arena);
4663 if (res != 0) goto failed;
4664 Py_XDECREF(tmp);
4665 tmp = NULL;
4666 } else {
4667 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4668 return 1;
4669 }
4670 if (PyObject_HasAttrString(obj, "op")) {
4671 int res;
4672 tmp = PyObject_GetAttrString(obj, "op");
4673 if (tmp == NULL) goto failed;
4674 res = obj2ast_operator(tmp, &op, arena);
4675 if (res != 0) goto failed;
4676 Py_XDECREF(tmp);
4677 tmp = NULL;
4678 } else {
4679 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4680 return 1;
4681 }
4682 if (PyObject_HasAttrString(obj, "right")) {
4683 int res;
4684 tmp = PyObject_GetAttrString(obj, "right");
4685 if (tmp == NULL) goto failed;
4686 res = obj2ast_expr(tmp, &right, arena);
4687 if (res != 0) goto failed;
4688 Py_XDECREF(tmp);
4689 tmp = NULL;
4690 } else {
4691 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4692 return 1;
4693 }
4694 *out = BinOp(left, op, right, lineno, col_offset, arena);
4695 if (*out == NULL) goto failed;
4696 return 0;
4697 }
4698 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4699 unaryop_ty op;
4700 expr_ty operand;
4701
4702 if (PyObject_HasAttrString(obj, "op")) {
4703 int res;
4704 tmp = PyObject_GetAttrString(obj, "op");
4705 if (tmp == NULL) goto failed;
4706 res = obj2ast_unaryop(tmp, &op, arena);
4707 if (res != 0) goto failed;
4708 Py_XDECREF(tmp);
4709 tmp = NULL;
4710 } else {
4711 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4712 return 1;
4713 }
4714 if (PyObject_HasAttrString(obj, "operand")) {
4715 int res;
4716 tmp = PyObject_GetAttrString(obj, "operand");
4717 if (tmp == NULL) goto failed;
4718 res = obj2ast_expr(tmp, &operand, arena);
4719 if (res != 0) goto failed;
4720 Py_XDECREF(tmp);
4721 tmp = NULL;
4722 } else {
4723 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4724 return 1;
4725 }
4726 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4727 if (*out == NULL) goto failed;
4728 return 0;
4729 }
4730 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4731 arguments_ty args;
4732 expr_ty body;
4733
4734 if (PyObject_HasAttrString(obj, "args")) {
4735 int res;
4736 tmp = PyObject_GetAttrString(obj, "args");
4737 if (tmp == NULL) goto failed;
4738 res = obj2ast_arguments(tmp, &args, arena);
4739 if (res != 0) goto failed;
4740 Py_XDECREF(tmp);
4741 tmp = NULL;
4742 } else {
4743 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4744 return 1;
4745 }
4746 if (PyObject_HasAttrString(obj, "body")) {
4747 int res;
4748 tmp = PyObject_GetAttrString(obj, "body");
4749 if (tmp == NULL) goto failed;
4750 res = obj2ast_expr(tmp, &body, arena);
4751 if (res != 0) goto failed;
4752 Py_XDECREF(tmp);
4753 tmp = NULL;
4754 } else {
4755 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4756 return 1;
4757 }
4758 *out = Lambda(args, body, lineno, col_offset, arena);
4759 if (*out == NULL) goto failed;
4760 return 0;
4761 }
4762 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4763 expr_ty test;
4764 expr_ty body;
4765 expr_ty orelse;
4766
4767 if (PyObject_HasAttrString(obj, "test")) {
4768 int res;
4769 tmp = PyObject_GetAttrString(obj, "test");
4770 if (tmp == NULL) goto failed;
4771 res = obj2ast_expr(tmp, &test, arena);
4772 if (res != 0) goto failed;
4773 Py_XDECREF(tmp);
4774 tmp = NULL;
4775 } else {
4776 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4777 return 1;
4778 }
4779 if (PyObject_HasAttrString(obj, "body")) {
4780 int res;
4781 tmp = PyObject_GetAttrString(obj, "body");
4782 if (tmp == NULL) goto failed;
4783 res = obj2ast_expr(tmp, &body, arena);
4784 if (res != 0) goto failed;
4785 Py_XDECREF(tmp);
4786 tmp = NULL;
4787 } else {
4788 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4789 return 1;
4790 }
4791 if (PyObject_HasAttrString(obj, "orelse")) {
4792 int res;
4793 tmp = PyObject_GetAttrString(obj, "orelse");
4794 if (tmp == NULL) goto failed;
4795 res = obj2ast_expr(tmp, &orelse, arena);
4796 if (res != 0) goto failed;
4797 Py_XDECREF(tmp);
4798 tmp = NULL;
4799 } else {
4800 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4801 return 1;
4802 }
4803 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4804 if (*out == NULL) goto failed;
4805 return 0;
4806 }
4807 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4808 asdl_seq* keys;
4809 asdl_seq* values;
4810
4811 if (PyObject_HasAttrString(obj, "keys")) {
4812 int res;
4813 Py_ssize_t len;
4814 Py_ssize_t i;
4815 tmp = PyObject_GetAttrString(obj, "keys");
4816 if (tmp == NULL) goto failed;
4817 if (!PyList_Check(tmp)) {
4818 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4819 goto failed;
4820 }
4821 len = PyList_GET_SIZE(tmp);
4822 keys = asdl_seq_new(len, arena);
4823 if (keys == NULL) goto failed;
4824 for (i = 0; i < len; i++) {
4825 expr_ty value;
4826 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4827 if (res != 0) goto failed;
4828 asdl_seq_SET(keys, i, value);
4829 }
4830 Py_XDECREF(tmp);
4831 tmp = NULL;
4832 } else {
4833 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4834 return 1;
4835 }
4836 if (PyObject_HasAttrString(obj, "values")) {
4837 int res;
4838 Py_ssize_t len;
4839 Py_ssize_t i;
4840 tmp = PyObject_GetAttrString(obj, "values");
4841 if (tmp == NULL) goto failed;
4842 if (!PyList_Check(tmp)) {
4843 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4844 goto failed;
4845 }
4846 len = PyList_GET_SIZE(tmp);
4847 values = asdl_seq_new(len, arena);
4848 if (values == NULL) goto failed;
4849 for (i = 0; i < len; i++) {
4850 expr_ty value;
4851 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4852 if (res != 0) goto failed;
4853 asdl_seq_SET(values, i, value);
4854 }
4855 Py_XDECREF(tmp);
4856 tmp = NULL;
4857 } else {
4858 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4859 return 1;
4860 }
4861 *out = Dict(keys, values, lineno, col_offset, arena);
4862 if (*out == NULL) goto failed;
4863 return 0;
4864 }
4865 if (PyObject_IsInstance(obj, (PyObject*)Set_type)) {
4866 asdl_seq* elts;
4867
4868 if (PyObject_HasAttrString(obj, "elts")) {
4869 int res;
4870 Py_ssize_t len;
4871 Py_ssize_t i;
4872 tmp = PyObject_GetAttrString(obj, "elts");
4873 if (tmp == NULL) goto failed;
4874 if (!PyList_Check(tmp)) {
4875 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4876 goto failed;
4877 }
4878 len = PyList_GET_SIZE(tmp);
4879 elts = asdl_seq_new(len, arena);
4880 if (elts == NULL) goto failed;
4881 for (i = 0; i < len; i++) {
4882 expr_ty value;
4883 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4884 if (res != 0) goto failed;
4885 asdl_seq_SET(elts, i, value);
4886 }
4887 Py_XDECREF(tmp);
4888 tmp = NULL;
4889 } else {
4890 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4891 return 1;
4892 }
4893 *out = Set(elts, lineno, col_offset, arena);
4894 if (*out == NULL) goto failed;
4895 return 0;
4896 }
4897 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4898 expr_ty elt;
4899 asdl_seq* generators;
4900
4901 if (PyObject_HasAttrString(obj, "elt")) {
4902 int res;
4903 tmp = PyObject_GetAttrString(obj, "elt");
4904 if (tmp == NULL) goto failed;
4905 res = obj2ast_expr(tmp, &elt, arena);
4906 if (res != 0) goto failed;
4907 Py_XDECREF(tmp);
4908 tmp = NULL;
4909 } else {
4910 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4911 return 1;
4912 }
4913 if (PyObject_HasAttrString(obj, "generators")) {
4914 int res;
4915 Py_ssize_t len;
4916 Py_ssize_t i;
4917 tmp = PyObject_GetAttrString(obj, "generators");
4918 if (tmp == NULL) goto failed;
4919 if (!PyList_Check(tmp)) {
4920 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4921 goto failed;
4922 }
4923 len = PyList_GET_SIZE(tmp);
4924 generators = asdl_seq_new(len, arena);
4925 if (generators == NULL) goto failed;
4926 for (i = 0; i < len; i++) {
4927 comprehension_ty value;
4928 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4929 if (res != 0) goto failed;
4930 asdl_seq_SET(generators, i, value);
4931 }
4932 Py_XDECREF(tmp);
4933 tmp = NULL;
4934 } else {
4935 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4936 return 1;
4937 }
4938 *out = ListComp(elt, generators, lineno, col_offset, arena);
4939 if (*out == NULL) goto failed;
4940 return 0;
4941 }
4942 if (PyObject_IsInstance(obj, (PyObject*)SetComp_type)) {
4943 expr_ty elt;
4944 asdl_seq* generators;
4945
4946 if (PyObject_HasAttrString(obj, "elt")) {
4947 int res;
4948 tmp = PyObject_GetAttrString(obj, "elt");
4949 if (tmp == NULL) goto failed;
4950 res = obj2ast_expr(tmp, &elt, arena);
4951 if (res != 0) goto failed;
4952 Py_XDECREF(tmp);
4953 tmp = NULL;
4954 } else {
4955 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
4956 return 1;
4957 }
4958 if (PyObject_HasAttrString(obj, "generators")) {
4959 int res;
4960 Py_ssize_t len;
4961 Py_ssize_t i;
4962 tmp = PyObject_GetAttrString(obj, "generators");
4963 if (tmp == NULL) goto failed;
4964 if (!PyList_Check(tmp)) {
4965 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4966 goto failed;
4967 }
4968 len = PyList_GET_SIZE(tmp);
4969 generators = asdl_seq_new(len, arena);
4970 if (generators == NULL) goto failed;
4971 for (i = 0; i < len; i++) {
4972 comprehension_ty value;
4973 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4974 if (res != 0) goto failed;
4975 asdl_seq_SET(generators, i, value);
4976 }
4977 Py_XDECREF(tmp);
4978 tmp = NULL;
4979 } else {
4980 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
4981 return 1;
4982 }
4983 *out = SetComp(elt, generators, lineno, col_offset, arena);
4984 if (*out == NULL) goto failed;
4985 return 0;
4986 }
4987 if (PyObject_IsInstance(obj, (PyObject*)DictComp_type)) {
4988 expr_ty key;
4989 expr_ty value;
4990 asdl_seq* generators;
4991
4992 if (PyObject_HasAttrString(obj, "key")) {
4993 int res;
4994 tmp = PyObject_GetAttrString(obj, "key");
4995 if (tmp == NULL) goto failed;
4996 res = obj2ast_expr(tmp, &key, arena);
4997 if (res != 0) goto failed;
4998 Py_XDECREF(tmp);
4999 tmp = NULL;
5000 } else {
5001 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5002 return 1;
5003 }
5004 if (PyObject_HasAttrString(obj, "value")) {
5005 int res;
5006 tmp = PyObject_GetAttrString(obj, "value");
5007 if (tmp == NULL) goto failed;
5008 res = obj2ast_expr(tmp, &value, arena);
5009 if (res != 0) goto failed;
5010 Py_XDECREF(tmp);
5011 tmp = NULL;
5012 } else {
5013 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5014 return 1;
5015 }
5016 if (PyObject_HasAttrString(obj, "generators")) {
5017 int res;
5018 Py_ssize_t len;
5019 Py_ssize_t i;
5020 tmp = PyObject_GetAttrString(obj, "generators");
5021 if (tmp == NULL) goto failed;
5022 if (!PyList_Check(tmp)) {
5023 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5024 goto failed;
5025 }
5026 len = PyList_GET_SIZE(tmp);
5027 generators = asdl_seq_new(len, arena);
5028 if (generators == NULL) goto failed;
5029 for (i = 0; i < len; i++) {
5030 comprehension_ty value;
5031 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5032 if (res != 0) goto failed;
5033 asdl_seq_SET(generators, i, value);
5034 }
5035 Py_XDECREF(tmp);
5036 tmp = NULL;
5037 } else {
5038 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5039 return 1;
5040 }
5041 *out = DictComp(key, value, generators, lineno, col_offset,
5042 arena);
5043 if (*out == NULL) goto failed;
5044 return 0;
5045 }
5046 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
5047 expr_ty elt;
5048 asdl_seq* generators;
5049
5050 if (PyObject_HasAttrString(obj, "elt")) {
5051 int res;
5052 tmp = PyObject_GetAttrString(obj, "elt");
5053 if (tmp == NULL) goto failed;
5054 res = obj2ast_expr(tmp, &elt, arena);
5055 if (res != 0) goto failed;
5056 Py_XDECREF(tmp);
5057 tmp = NULL;
5058 } else {
5059 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5060 return 1;
5061 }
5062 if (PyObject_HasAttrString(obj, "generators")) {
5063 int res;
5064 Py_ssize_t len;
5065 Py_ssize_t i;
5066 tmp = PyObject_GetAttrString(obj, "generators");
5067 if (tmp == NULL) goto failed;
5068 if (!PyList_Check(tmp)) {
5069 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5070 goto failed;
5071 }
5072 len = PyList_GET_SIZE(tmp);
5073 generators = asdl_seq_new(len, arena);
5074 if (generators == NULL) goto failed;
5075 for (i = 0; i < len; i++) {
5076 comprehension_ty value;
5077 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5078 if (res != 0) goto failed;
5079 asdl_seq_SET(generators, i, value);
5080 }
5081 Py_XDECREF(tmp);
5082 tmp = NULL;
5083 } else {
5084 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5085 return 1;
5086 }
5087 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5088 if (*out == NULL) goto failed;
5089 return 0;
5090 }
5091 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
5092 expr_ty value;
5093
5094 if (PyObject_HasAttrString(obj, "value")) {
5095 int res;
5096 tmp = PyObject_GetAttrString(obj, "value");
5097 if (tmp == NULL) goto failed;
5098 res = obj2ast_expr(tmp, &value, arena);
5099 if (res != 0) goto failed;
5100 Py_XDECREF(tmp);
5101 tmp = NULL;
5102 } else {
5103 value = NULL;
5104 }
5105 *out = Yield(value, lineno, col_offset, arena);
5106 if (*out == NULL) goto failed;
5107 return 0;
5108 }
5109 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
5110 expr_ty left;
5111 asdl_int_seq* ops;
5112 asdl_seq* comparators;
5113
5114 if (PyObject_HasAttrString(obj, "left")) {
5115 int res;
5116 tmp = PyObject_GetAttrString(obj, "left");
5117 if (tmp == NULL) goto failed;
5118 res = obj2ast_expr(tmp, &left, arena);
5119 if (res != 0) goto failed;
5120 Py_XDECREF(tmp);
5121 tmp = NULL;
5122 } else {
5123 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5124 return 1;
5125 }
5126 if (PyObject_HasAttrString(obj, "ops")) {
5127 int res;
5128 Py_ssize_t len;
5129 Py_ssize_t i;
5130 tmp = PyObject_GetAttrString(obj, "ops");
5131 if (tmp == NULL) goto failed;
5132 if (!PyList_Check(tmp)) {
5133 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5134 goto failed;
5135 }
5136 len = PyList_GET_SIZE(tmp);
5137 ops = asdl_int_seq_new(len, arena);
5138 if (ops == NULL) goto failed;
5139 for (i = 0; i < len; i++) {
5140 cmpop_ty value;
5141 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5142 if (res != 0) goto failed;
5143 asdl_seq_SET(ops, i, value);
5144 }
5145 Py_XDECREF(tmp);
5146 tmp = NULL;
5147 } else {
5148 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5149 return 1;
5150 }
5151 if (PyObject_HasAttrString(obj, "comparators")) {
5152 int res;
5153 Py_ssize_t len;
5154 Py_ssize_t i;
5155 tmp = PyObject_GetAttrString(obj, "comparators");
5156 if (tmp == NULL) goto failed;
5157 if (!PyList_Check(tmp)) {
5158 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5159 goto failed;
5160 }
5161 len = PyList_GET_SIZE(tmp);
5162 comparators = asdl_seq_new(len, arena);
5163 if (comparators == NULL) goto failed;
5164 for (i = 0; i < len; i++) {
5165 expr_ty value;
5166 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5167 if (res != 0) goto failed;
5168 asdl_seq_SET(comparators, i, value);
5169 }
5170 Py_XDECREF(tmp);
5171 tmp = NULL;
5172 } else {
5173 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5174 return 1;
5175 }
5176 *out = Compare(left, ops, comparators, lineno, col_offset,
5177 arena);
5178 if (*out == NULL) goto failed;
5179 return 0;
5180 }
5181 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
5182 expr_ty func;
5183 asdl_seq* args;
5184 asdl_seq* keywords;
5185 expr_ty starargs;
5186 expr_ty kwargs;
5187
5188 if (PyObject_HasAttrString(obj, "func")) {
5189 int res;
5190 tmp = PyObject_GetAttrString(obj, "func");
5191 if (tmp == NULL) goto failed;
5192 res = obj2ast_expr(tmp, &func, arena);
5193 if (res != 0) goto failed;
5194 Py_XDECREF(tmp);
5195 tmp = NULL;
5196 } else {
5197 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5198 return 1;
5199 }
5200 if (PyObject_HasAttrString(obj, "args")) {
5201 int res;
5202 Py_ssize_t len;
5203 Py_ssize_t i;
5204 tmp = PyObject_GetAttrString(obj, "args");
5205 if (tmp == NULL) goto failed;
5206 if (!PyList_Check(tmp)) {
5207 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5208 goto failed;
5209 }
5210 len = PyList_GET_SIZE(tmp);
5211 args = asdl_seq_new(len, arena);
5212 if (args == NULL) goto failed;
5213 for (i = 0; i < len; i++) {
5214 expr_ty value;
5215 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5216 if (res != 0) goto failed;
5217 asdl_seq_SET(args, i, value);
5218 }
5219 Py_XDECREF(tmp);
5220 tmp = NULL;
5221 } else {
5222 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5223 return 1;
5224 }
5225 if (PyObject_HasAttrString(obj, "keywords")) {
5226 int res;
5227 Py_ssize_t len;
5228 Py_ssize_t i;
5229 tmp = PyObject_GetAttrString(obj, "keywords");
5230 if (tmp == NULL) goto failed;
5231 if (!PyList_Check(tmp)) {
5232 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5233 goto failed;
5234 }
5235 len = PyList_GET_SIZE(tmp);
5236 keywords = asdl_seq_new(len, arena);
5237 if (keywords == NULL) goto failed;
5238 for (i = 0; i < len; i++) {
5239 keyword_ty value;
5240 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5241 if (res != 0) goto failed;
5242 asdl_seq_SET(keywords, i, value);
5243 }
5244 Py_XDECREF(tmp);
5245 tmp = NULL;
5246 } else {
5247 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5248 return 1;
5249 }
5250 if (PyObject_HasAttrString(obj, "starargs")) {
5251 int res;
5252 tmp = PyObject_GetAttrString(obj, "starargs");
5253 if (tmp == NULL) goto failed;
5254 res = obj2ast_expr(tmp, &starargs, arena);
5255 if (res != 0) goto failed;
5256 Py_XDECREF(tmp);
5257 tmp = NULL;
5258 } else {
5259 starargs = NULL;
5260 }
5261 if (PyObject_HasAttrString(obj, "kwargs")) {
5262 int res;
5263 tmp = PyObject_GetAttrString(obj, "kwargs");
5264 if (tmp == NULL) goto failed;
5265 res = obj2ast_expr(tmp, &kwargs, arena);
5266 if (res != 0) goto failed;
5267 Py_XDECREF(tmp);
5268 tmp = NULL;
5269 } else {
5270 kwargs = NULL;
5271 }
5272 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5273 col_offset, arena);
5274 if (*out == NULL) goto failed;
5275 return 0;
5276 }
5277 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5278 object n;
5279
5280 if (PyObject_HasAttrString(obj, "n")) {
5281 int res;
5282 tmp = PyObject_GetAttrString(obj, "n");
5283 if (tmp == NULL) goto failed;
5284 res = obj2ast_object(tmp, &n, arena);
5285 if (res != 0) goto failed;
5286 Py_XDECREF(tmp);
5287 tmp = NULL;
5288 } else {
5289 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5290 return 1;
5291 }
5292 *out = Num(n, lineno, col_offset, arena);
5293 if (*out == NULL) goto failed;
5294 return 0;
5295 }
5296 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5297 string s;
5298
5299 if (PyObject_HasAttrString(obj, "s")) {
5300 int res;
5301 tmp = PyObject_GetAttrString(obj, "s");
5302 if (tmp == NULL) goto failed;
5303 res = obj2ast_string(tmp, &s, arena);
5304 if (res != 0) goto failed;
5305 Py_XDECREF(tmp);
5306 tmp = NULL;
5307 } else {
5308 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5309 return 1;
5310 }
5311 *out = Str(s, lineno, col_offset, arena);
5312 if (*out == NULL) goto failed;
5313 return 0;
5314 }
5315 if (PyObject_IsInstance(obj, (PyObject*)Bytes_type)) {
5316 string s;
5317
5318 if (PyObject_HasAttrString(obj, "s")) {
5319 int res;
5320 tmp = PyObject_GetAttrString(obj, "s");
5321 if (tmp == NULL) goto failed;
5322 res = obj2ast_string(tmp, &s, arena);
5323 if (res != 0) goto failed;
5324 Py_XDECREF(tmp);
5325 tmp = NULL;
5326 } else {
5327 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes");
5328 return 1;
5329 }
5330 *out = Bytes(s, lineno, col_offset, arena);
5331 if (*out == NULL) goto failed;
5332 return 0;
5333 }
5334 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5335
5336 *out = Ellipsis(lineno, col_offset, arena);
5337 if (*out == NULL) goto failed;
5338 return 0;
5339 }
5340 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5341 expr_ty value;
5342 identifier attr;
5343 expr_context_ty ctx;
5344
5345 if (PyObject_HasAttrString(obj, "value")) {
5346 int res;
5347 tmp = PyObject_GetAttrString(obj, "value");
5348 if (tmp == NULL) goto failed;
5349 res = obj2ast_expr(tmp, &value, arena);
5350 if (res != 0) goto failed;
5351 Py_XDECREF(tmp);
5352 tmp = NULL;
5353 } else {
5354 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5355 return 1;
5356 }
5357 if (PyObject_HasAttrString(obj, "attr")) {
5358 int res;
5359 tmp = PyObject_GetAttrString(obj, "attr");
5360 if (tmp == NULL) goto failed;
5361 res = obj2ast_identifier(tmp, &attr, arena);
5362 if (res != 0) goto failed;
5363 Py_XDECREF(tmp);
5364 tmp = NULL;
5365 } else {
5366 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5367 return 1;
5368 }
5369 if (PyObject_HasAttrString(obj, "ctx")) {
5370 int res;
5371 tmp = PyObject_GetAttrString(obj, "ctx");
5372 if (tmp == NULL) goto failed;
5373 res = obj2ast_expr_context(tmp, &ctx, arena);
5374 if (res != 0) goto failed;
5375 Py_XDECREF(tmp);
5376 tmp = NULL;
5377 } else {
5378 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5379 return 1;
5380 }
5381 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5382 if (*out == NULL) goto failed;
5383 return 0;
5384 }
5385 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5386 expr_ty value;
5387 slice_ty slice;
5388 expr_context_ty ctx;
5389
5390 if (PyObject_HasAttrString(obj, "value")) {
5391 int res;
5392 tmp = PyObject_GetAttrString(obj, "value");
5393 if (tmp == NULL) goto failed;
5394 res = obj2ast_expr(tmp, &value, arena);
5395 if (res != 0) goto failed;
5396 Py_XDECREF(tmp);
5397 tmp = NULL;
5398 } else {
5399 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5400 return 1;
5401 }
5402 if (PyObject_HasAttrString(obj, "slice")) {
5403 int res;
5404 tmp = PyObject_GetAttrString(obj, "slice");
5405 if (tmp == NULL) goto failed;
5406 res = obj2ast_slice(tmp, &slice, arena);
5407 if (res != 0) goto failed;
5408 Py_XDECREF(tmp);
5409 tmp = NULL;
5410 } else {
5411 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5412 return 1;
5413 }
5414 if (PyObject_HasAttrString(obj, "ctx")) {
5415 int res;
5416 tmp = PyObject_GetAttrString(obj, "ctx");
5417 if (tmp == NULL) goto failed;
5418 res = obj2ast_expr_context(tmp, &ctx, arena);
5419 if (res != 0) goto failed;
5420 Py_XDECREF(tmp);
5421 tmp = NULL;
5422 } else {
5423 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5424 return 1;
5425 }
5426 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5427 if (*out == NULL) goto failed;
5428 return 0;
5429 }
5430 if (PyObject_IsInstance(obj, (PyObject*)Starred_type)) {
5431 expr_ty value;
5432 expr_context_ty ctx;
5433
5434 if (PyObject_HasAttrString(obj, "value")) {
5435 int res;
5436 tmp = PyObject_GetAttrString(obj, "value");
5437 if (tmp == NULL) goto failed;
5438 res = obj2ast_expr(tmp, &value, arena);
5439 if (res != 0) goto failed;
5440 Py_XDECREF(tmp);
5441 tmp = NULL;
5442 } else {
5443 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Starred");
5444 return 1;
5445 }
5446 if (PyObject_HasAttrString(obj, "ctx")) {
5447 int res;
5448 tmp = PyObject_GetAttrString(obj, "ctx");
5449 if (tmp == NULL) goto failed;
5450 res = obj2ast_expr_context(tmp, &ctx, arena);
5451 if (res != 0) goto failed;
5452 Py_XDECREF(tmp);
5453 tmp = NULL;
5454 } else {
5455 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Starred");
5456 return 1;
5457 }
5458 *out = Starred(value, ctx, lineno, col_offset, arena);
5459 if (*out == NULL) goto failed;
5460 return 0;
5461 }
5462 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5463 identifier id;
5464 expr_context_ty ctx;
5465
5466 if (PyObject_HasAttrString(obj, "id")) {
5467 int res;
5468 tmp = PyObject_GetAttrString(obj, "id");
5469 if (tmp == NULL) goto failed;
5470 res = obj2ast_identifier(tmp, &id, arena);
5471 if (res != 0) goto failed;
5472 Py_XDECREF(tmp);
5473 tmp = NULL;
5474 } else {
5475 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5476 return 1;
5477 }
5478 if (PyObject_HasAttrString(obj, "ctx")) {
5479 int res;
5480 tmp = PyObject_GetAttrString(obj, "ctx");
5481 if (tmp == NULL) goto failed;
5482 res = obj2ast_expr_context(tmp, &ctx, arena);
5483 if (res != 0) goto failed;
5484 Py_XDECREF(tmp);
5485 tmp = NULL;
5486 } else {
5487 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5488 return 1;
5489 }
5490 *out = Name(id, ctx, lineno, col_offset, arena);
5491 if (*out == NULL) goto failed;
5492 return 0;
5493 }
5494 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5495 asdl_seq* elts;
5496 expr_context_ty ctx;
5497
5498 if (PyObject_HasAttrString(obj, "elts")) {
5499 int res;
5500 Py_ssize_t len;
5501 Py_ssize_t i;
5502 tmp = PyObject_GetAttrString(obj, "elts");
5503 if (tmp == NULL) goto failed;
5504 if (!PyList_Check(tmp)) {
5505 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5506 goto failed;
5507 }
5508 len = PyList_GET_SIZE(tmp);
5509 elts = asdl_seq_new(len, arena);
5510 if (elts == NULL) goto failed;
5511 for (i = 0; i < len; i++) {
5512 expr_ty value;
5513 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5514 if (res != 0) goto failed;
5515 asdl_seq_SET(elts, i, value);
5516 }
5517 Py_XDECREF(tmp);
5518 tmp = NULL;
5519 } else {
5520 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5521 return 1;
5522 }
5523 if (PyObject_HasAttrString(obj, "ctx")) {
5524 int res;
5525 tmp = PyObject_GetAttrString(obj, "ctx");
5526 if (tmp == NULL) goto failed;
5527 res = obj2ast_expr_context(tmp, &ctx, arena);
5528 if (res != 0) goto failed;
5529 Py_XDECREF(tmp);
5530 tmp = NULL;
5531 } else {
5532 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5533 return 1;
5534 }
5535 *out = List(elts, ctx, lineno, col_offset, arena);
5536 if (*out == NULL) goto failed;
5537 return 0;
5538 }
5539 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5540 asdl_seq* elts;
5541 expr_context_ty ctx;
5542
5543 if (PyObject_HasAttrString(obj, "elts")) {
5544 int res;
5545 Py_ssize_t len;
5546 Py_ssize_t i;
5547 tmp = PyObject_GetAttrString(obj, "elts");
5548 if (tmp == NULL) goto failed;
5549 if (!PyList_Check(tmp)) {
5550 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5551 goto failed;
5552 }
5553 len = PyList_GET_SIZE(tmp);
5554 elts = asdl_seq_new(len, arena);
5555 if (elts == NULL) goto failed;
5556 for (i = 0; i < len; i++) {
5557 expr_ty value;
5558 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5559 if (res != 0) goto failed;
5560 asdl_seq_SET(elts, i, value);
5561 }
5562 Py_XDECREF(tmp);
5563 tmp = NULL;
5564 } else {
5565 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5566 return 1;
5567 }
5568 if (PyObject_HasAttrString(obj, "ctx")) {
5569 int res;
5570 tmp = PyObject_GetAttrString(obj, "ctx");
5571 if (tmp == NULL) goto failed;
5572 res = obj2ast_expr_context(tmp, &ctx, arena);
5573 if (res != 0) goto failed;
5574 Py_XDECREF(tmp);
5575 tmp = NULL;
5576 } else {
5577 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5578 return 1;
5579 }
5580 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5581 if (*out == NULL) goto failed;
5582 return 0;
5583 }
5584
5585 tmp = PyObject_Repr(obj);
5586 if (tmp == NULL) goto failed;
5587 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5588failed:
5589 Py_XDECREF(tmp);
5590 return 1;
5591}
5592
5593int
5594obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5595{
5596 PyObject* tmp = NULL;
5597
5598 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5599 *out = Load;
5600 return 0;
5601 }
5602 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5603 *out = Store;
5604 return 0;
5605 }
5606 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5607 *out = Del;
5608 return 0;
5609 }
5610 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5611 *out = AugLoad;
5612 return 0;
5613 }
5614 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5615 *out = AugStore;
5616 return 0;
5617 }
5618 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5619 *out = Param;
5620 return 0;
5621 }
5622
5623 tmp = PyObject_Repr(obj);
5624 if (tmp == NULL) goto failed;
5625 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5626failed:
5627 Py_XDECREF(tmp);
5628 return 1;
5629}
5630
5631int
5632obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5633{
5634 PyObject* tmp = NULL;
5635
5636
5637 if (obj == Py_None) {
5638 *out = NULL;
5639 return 0;
5640 }
5641 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5642 expr_ty lower;
5643 expr_ty upper;
5644 expr_ty step;
5645
5646 if (PyObject_HasAttrString(obj, "lower")) {
5647 int res;
5648 tmp = PyObject_GetAttrString(obj, "lower");
5649 if (tmp == NULL) goto failed;
5650 res = obj2ast_expr(tmp, &lower, arena);
5651 if (res != 0) goto failed;
5652 Py_XDECREF(tmp);
5653 tmp = NULL;
5654 } else {
5655 lower = NULL;
5656 }
5657 if (PyObject_HasAttrString(obj, "upper")) {
5658 int res;
5659 tmp = PyObject_GetAttrString(obj, "upper");
5660 if (tmp == NULL) goto failed;
5661 res = obj2ast_expr(tmp, &upper, arena);
5662 if (res != 0) goto failed;
5663 Py_XDECREF(tmp);
5664 tmp = NULL;
5665 } else {
5666 upper = NULL;
5667 }
5668 if (PyObject_HasAttrString(obj, "step")) {
5669 int res;
5670 tmp = PyObject_GetAttrString(obj, "step");
5671 if (tmp == NULL) goto failed;
5672 res = obj2ast_expr(tmp, &step, arena);
5673 if (res != 0) goto failed;
5674 Py_XDECREF(tmp);
5675 tmp = NULL;
5676 } else {
5677 step = NULL;
5678 }
5679 *out = Slice(lower, upper, step, arena);
5680 if (*out == NULL) goto failed;
5681 return 0;
5682 }
5683 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5684 asdl_seq* dims;
5685
5686 if (PyObject_HasAttrString(obj, "dims")) {
5687 int res;
5688 Py_ssize_t len;
5689 Py_ssize_t i;
5690 tmp = PyObject_GetAttrString(obj, "dims");
5691 if (tmp == NULL) goto failed;
5692 if (!PyList_Check(tmp)) {
5693 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5694 goto failed;
5695 }
5696 len = PyList_GET_SIZE(tmp);
5697 dims = asdl_seq_new(len, arena);
5698 if (dims == NULL) goto failed;
5699 for (i = 0; i < len; i++) {
5700 slice_ty value;
5701 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5702 if (res != 0) goto failed;
5703 asdl_seq_SET(dims, i, value);
5704 }
5705 Py_XDECREF(tmp);
5706 tmp = NULL;
5707 } else {
5708 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5709 return 1;
5710 }
5711 *out = ExtSlice(dims, arena);
5712 if (*out == NULL) goto failed;
5713 return 0;
5714 }
5715 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5716 expr_ty value;
5717
5718 if (PyObject_HasAttrString(obj, "value")) {
5719 int res;
5720 tmp = PyObject_GetAttrString(obj, "value");
5721 if (tmp == NULL) goto failed;
5722 res = obj2ast_expr(tmp, &value, arena);
5723 if (res != 0) goto failed;
5724 Py_XDECREF(tmp);
5725 tmp = NULL;
5726 } else {
5727 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5728 return 1;
5729 }
5730 *out = Index(value, arena);
5731 if (*out == NULL) goto failed;
5732 return 0;
5733 }
5734
5735 tmp = PyObject_Repr(obj);
5736 if (tmp == NULL) goto failed;
5737 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5738failed:
5739 Py_XDECREF(tmp);
5740 return 1;
5741}
5742
5743int
5744obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5745{
5746 PyObject* tmp = NULL;
5747
5748 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5749 *out = And;
5750 return 0;
5751 }
5752 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5753 *out = Or;
5754 return 0;
5755 }
5756
5757 tmp = PyObject_Repr(obj);
5758 if (tmp == NULL) goto failed;
5759 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5760failed:
5761 Py_XDECREF(tmp);
5762 return 1;
5763}
5764
5765int
5766obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5767{
5768 PyObject* tmp = NULL;
5769
5770 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5771 *out = Add;
5772 return 0;
5773 }
5774 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5775 *out = Sub;
5776 return 0;
5777 }
5778 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5779 *out = Mult;
5780 return 0;
5781 }
5782 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5783 *out = Div;
5784 return 0;
5785 }
5786 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5787 *out = Mod;
5788 return 0;
5789 }
5790 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5791 *out = Pow;
5792 return 0;
5793 }
5794 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5795 *out = LShift;
5796 return 0;
5797 }
5798 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5799 *out = RShift;
5800 return 0;
5801 }
5802 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5803 *out = BitOr;
5804 return 0;
5805 }
5806 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5807 *out = BitXor;
5808 return 0;
5809 }
5810 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5811 *out = BitAnd;
5812 return 0;
5813 }
5814 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5815 *out = FloorDiv;
5816 return 0;
5817 }
5818
5819 tmp = PyObject_Repr(obj);
5820 if (tmp == NULL) goto failed;
5821 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5822failed:
5823 Py_XDECREF(tmp);
5824 return 1;
5825}
5826
5827int
5828obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5829{
5830 PyObject* tmp = NULL;
5831
5832 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5833 *out = Invert;
5834 return 0;
5835 }
5836 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5837 *out = Not;
5838 return 0;
5839 }
5840 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5841 *out = UAdd;
5842 return 0;
5843 }
5844 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5845 *out = USub;
5846 return 0;
5847 }
5848
5849 tmp = PyObject_Repr(obj);
5850 if (tmp == NULL) goto failed;
5851 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5852failed:
5853 Py_XDECREF(tmp);
5854 return 1;
5855}
5856
5857int
5858obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5859{
5860 PyObject* tmp = NULL;
5861
5862 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5863 *out = Eq;
5864 return 0;
5865 }
5866 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5867 *out = NotEq;
5868 return 0;
5869 }
5870 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5871 *out = Lt;
5872 return 0;
5873 }
5874 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5875 *out = LtE;
5876 return 0;
5877 }
5878 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5879 *out = Gt;
5880 return 0;
5881 }
5882 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5883 *out = GtE;
5884 return 0;
5885 }
5886 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5887 *out = Is;
5888 return 0;
5889 }
5890 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5891 *out = IsNot;
5892 return 0;
5893 }
5894 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5895 *out = In;
5896 return 0;
5897 }
5898 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5899 *out = NotIn;
5900 return 0;
5901 }
5902
5903 tmp = PyObject_Repr(obj);
5904 if (tmp == NULL) goto failed;
5905 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5906failed:
5907 Py_XDECREF(tmp);
5908 return 1;
5909}
5910
5911int
5912obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5913{
5914 PyObject* tmp = NULL;
5915 expr_ty target;
5916 expr_ty iter;
5917 asdl_seq* ifs;
5918
5919 if (PyObject_HasAttrString(obj, "target")) {
5920 int res;
5921 tmp = PyObject_GetAttrString(obj, "target");
5922 if (tmp == NULL) goto failed;
5923 res = obj2ast_expr(tmp, &target, arena);
5924 if (res != 0) goto failed;
5925 Py_XDECREF(tmp);
5926 tmp = NULL;
5927 } else {
5928 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5929 return 1;
5930 }
5931 if (PyObject_HasAttrString(obj, "iter")) {
5932 int res;
5933 tmp = PyObject_GetAttrString(obj, "iter");
5934 if (tmp == NULL) goto failed;
5935 res = obj2ast_expr(tmp, &iter, arena);
5936 if (res != 0) goto failed;
5937 Py_XDECREF(tmp);
5938 tmp = NULL;
5939 } else {
5940 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5941 return 1;
5942 }
5943 if (PyObject_HasAttrString(obj, "ifs")) {
5944 int res;
5945 Py_ssize_t len;
5946 Py_ssize_t i;
5947 tmp = PyObject_GetAttrString(obj, "ifs");
5948 if (tmp == NULL) goto failed;
5949 if (!PyList_Check(tmp)) {
5950 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5951 goto failed;
5952 }
5953 len = PyList_GET_SIZE(tmp);
5954 ifs = asdl_seq_new(len, arena);
5955 if (ifs == NULL) goto failed;
5956 for (i = 0; i < len; i++) {
5957 expr_ty value;
5958 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5959 if (res != 0) goto failed;
5960 asdl_seq_SET(ifs, i, value);
5961 }
5962 Py_XDECREF(tmp);
5963 tmp = NULL;
5964 } else {
5965 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5966 return 1;
5967 }
5968 *out = comprehension(target, iter, ifs, arena);
5969 return 0;
5970failed:
5971 Py_XDECREF(tmp);
5972 return 1;
5973}
5974
5975int
5976obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5977{
5978 PyObject* tmp = NULL;
5979 expr_ty type;
5980 identifier name;
5981 asdl_seq* body;
5982 int lineno;
5983 int col_offset;
5984
5985 if (PyObject_HasAttrString(obj, "type")) {
5986 int res;
5987 tmp = PyObject_GetAttrString(obj, "type");
5988 if (tmp == NULL) goto failed;
5989 res = obj2ast_expr(tmp, &type, arena);
5990 if (res != 0) goto failed;
5991 Py_XDECREF(tmp);
5992 tmp = NULL;
5993 } else {
5994 type = NULL;
5995 }
5996 if (PyObject_HasAttrString(obj, "name")) {
5997 int res;
5998 tmp = PyObject_GetAttrString(obj, "name");
5999 if (tmp == NULL) goto failed;
6000 res = obj2ast_identifier(tmp, &name, arena);
6001 if (res != 0) goto failed;
6002 Py_XDECREF(tmp);
6003 tmp = NULL;
6004 } else {
6005 name = NULL;
6006 }
6007 if (PyObject_HasAttrString(obj, "body")) {
6008 int res;
6009 Py_ssize_t len;
6010 Py_ssize_t i;
6011 tmp = PyObject_GetAttrString(obj, "body");
6012 if (tmp == NULL) goto failed;
6013 if (!PyList_Check(tmp)) {
6014 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6015 goto failed;
6016 }
6017 len = PyList_GET_SIZE(tmp);
6018 body = asdl_seq_new(len, arena);
6019 if (body == NULL) goto failed;
6020 for (i = 0; i < len; i++) {
6021 stmt_ty value;
6022 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6023 if (res != 0) goto failed;
6024 asdl_seq_SET(body, i, value);
6025 }
6026 Py_XDECREF(tmp);
6027 tmp = NULL;
6028 } else {
6029 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
6030 return 1;
6031 }
6032 if (PyObject_HasAttrString(obj, "lineno")) {
6033 int res;
6034 tmp = PyObject_GetAttrString(obj, "lineno");
6035 if (tmp == NULL) goto failed;
6036 res = obj2ast_int(tmp, &lineno, arena);
6037 if (res != 0) goto failed;
6038 Py_XDECREF(tmp);
6039 tmp = NULL;
6040 } else {
6041 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6042 return 1;
6043 }
6044 if (PyObject_HasAttrString(obj, "col_offset")) {
6045 int res;
6046 tmp = PyObject_GetAttrString(obj, "col_offset");
6047 if (tmp == NULL) goto failed;
6048 res = obj2ast_int(tmp, &col_offset, arena);
6049 if (res != 0) goto failed;
6050 Py_XDECREF(tmp);
6051 tmp = NULL;
6052 } else {
6053 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6054 return 1;
6055 }
6056 *out = excepthandler(type, name, body, lineno, col_offset, arena);
6057 return 0;
6058failed:
6059 Py_XDECREF(tmp);
6060 return 1;
6061}
6062
6063int
6064obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6065{
6066 PyObject* tmp = NULL;
6067 asdl_seq* args;
6068 identifier vararg;
6069 expr_ty varargannotation;
6070 asdl_seq* kwonlyargs;
6071 identifier kwarg;
6072 expr_ty kwargannotation;
6073 asdl_seq* defaults;
6074 asdl_seq* kw_defaults;
6075
6076 if (PyObject_HasAttrString(obj, "args")) {
6077 int res;
6078 Py_ssize_t len;
6079 Py_ssize_t i;
6080 tmp = PyObject_GetAttrString(obj, "args");
6081 if (tmp == NULL) goto failed;
6082 if (!PyList_Check(tmp)) {
6083 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6084 goto failed;
6085 }
6086 len = PyList_GET_SIZE(tmp);
6087 args = asdl_seq_new(len, arena);
6088 if (args == NULL) goto failed;
6089 for (i = 0; i < len; i++) {
6090 arg_ty value;
6091 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6092 if (res != 0) goto failed;
6093 asdl_seq_SET(args, i, value);
6094 }
6095 Py_XDECREF(tmp);
6096 tmp = NULL;
6097 } else {
6098 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6099 return 1;
6100 }
6101 if (PyObject_HasAttrString(obj, "vararg")) {
6102 int res;
6103 tmp = PyObject_GetAttrString(obj, "vararg");
6104 if (tmp == NULL) goto failed;
6105 res = obj2ast_identifier(tmp, &vararg, arena);
6106 if (res != 0) goto failed;
6107 Py_XDECREF(tmp);
6108 tmp = NULL;
6109 } else {
6110 vararg = NULL;
6111 }
6112 if (PyObject_HasAttrString(obj, "varargannotation")) {
6113 int res;
6114 tmp = PyObject_GetAttrString(obj, "varargannotation");
6115 if (tmp == NULL) goto failed;
6116 res = obj2ast_expr(tmp, &varargannotation, arena);
6117 if (res != 0) goto failed;
6118 Py_XDECREF(tmp);
6119 tmp = NULL;
6120 } else {
6121 varargannotation = NULL;
6122 }
6123 if (PyObject_HasAttrString(obj, "kwonlyargs")) {
6124 int res;
6125 Py_ssize_t len;
6126 Py_ssize_t i;
6127 tmp = PyObject_GetAttrString(obj, "kwonlyargs");
6128 if (tmp == NULL) goto failed;
6129 if (!PyList_Check(tmp)) {
6130 PyErr_Format(PyExc_TypeError, "arguments field \"kwonlyargs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6131 goto failed;
6132 }
6133 len = PyList_GET_SIZE(tmp);
6134 kwonlyargs = asdl_seq_new(len, arena);
6135 if (kwonlyargs == NULL) goto failed;
6136 for (i = 0; i < len; i++) {
6137 arg_ty value;
6138 res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena);
6139 if (res != 0) goto failed;
6140 asdl_seq_SET(kwonlyargs, i, value);
6141 }
6142 Py_XDECREF(tmp);
6143 tmp = NULL;
6144 } else {
6145 PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");
6146 return 1;
6147 }
6148 if (PyObject_HasAttrString(obj, "kwarg")) {
6149 int res;
6150 tmp = PyObject_GetAttrString(obj, "kwarg");
6151 if (tmp == NULL) goto failed;
6152 res = obj2ast_identifier(tmp, &kwarg, arena);
6153 if (res != 0) goto failed;
6154 Py_XDECREF(tmp);
6155 tmp = NULL;
6156 } else {
6157 kwarg = NULL;
6158 }
6159 if (PyObject_HasAttrString(obj, "kwargannotation")) {
6160 int res;
6161 tmp = PyObject_GetAttrString(obj, "kwargannotation");
6162 if (tmp == NULL) goto failed;
6163 res = obj2ast_expr(tmp, &kwargannotation, arena);
6164 if (res != 0) goto failed;
6165 Py_XDECREF(tmp);
6166 tmp = NULL;
6167 } else {
6168 kwargannotation = NULL;
6169 }
6170 if (PyObject_HasAttrString(obj, "defaults")) {
6171 int res;
6172 Py_ssize_t len;
6173 Py_ssize_t i;
6174 tmp = PyObject_GetAttrString(obj, "defaults");
6175 if (tmp == NULL) goto failed;
6176 if (!PyList_Check(tmp)) {
6177 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6178 goto failed;
6179 }
6180 len = PyList_GET_SIZE(tmp);
6181 defaults = asdl_seq_new(len, arena);
6182 if (defaults == NULL) goto failed;
6183 for (i = 0; i < len; i++) {
6184 expr_ty value;
6185 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6186 if (res != 0) goto failed;
6187 asdl_seq_SET(defaults, i, value);
6188 }
6189 Py_XDECREF(tmp);
6190 tmp = NULL;
6191 } else {
6192 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6193 return 1;
6194 }
6195 if (PyObject_HasAttrString(obj, "kw_defaults")) {
6196 int res;
6197 Py_ssize_t len;
6198 Py_ssize_t i;
6199 tmp = PyObject_GetAttrString(obj, "kw_defaults");
6200 if (tmp == NULL) goto failed;
6201 if (!PyList_Check(tmp)) {
6202 PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6203 goto failed;
6204 }
6205 len = PyList_GET_SIZE(tmp);
6206 kw_defaults = asdl_seq_new(len, arena);
6207 if (kw_defaults == NULL) goto failed;
6208 for (i = 0; i < len; i++) {
6209 expr_ty value;
6210 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6211 if (res != 0) goto failed;
6212 asdl_seq_SET(kw_defaults, i, value);
6213 }
6214 Py_XDECREF(tmp);
6215 tmp = NULL;
6216 } else {
6217 PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments");
6218 return 1;
6219 }
6220 *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg,
6221 kwargannotation, defaults, kw_defaults, arena);
6222 return 0;
6223failed:
6224 Py_XDECREF(tmp);
6225 return 1;
6226}
6227
6228int
6229obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)
6230{
6231 PyObject* tmp = NULL;
6232 identifier arg;
6233 expr_ty annotation;
6234
6235 if (PyObject_HasAttrString(obj, "arg")) {
6236 int res;
6237 tmp = PyObject_GetAttrString(obj, "arg");
6238 if (tmp == NULL) goto failed;
6239 res = obj2ast_identifier(tmp, &arg, arena);
6240 if (res != 0) goto failed;
6241 Py_XDECREF(tmp);
6242 tmp = NULL;
6243 } else {
6244 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");
6245 return 1;
6246 }
6247 if (PyObject_HasAttrString(obj, "annotation")) {
6248 int res;
6249 tmp = PyObject_GetAttrString(obj, "annotation");
6250 if (tmp == NULL) goto failed;
6251 res = obj2ast_expr(tmp, &annotation, arena);
6252 if (res != 0) goto failed;
6253 Py_XDECREF(tmp);
6254 tmp = NULL;
6255 } else {
6256 annotation = NULL;
6257 }
6258 *out = arg(arg, annotation, arena);
6259 return 0;
6260failed:
6261 Py_XDECREF(tmp);
6262 return 1;
6263}
6264
6265int
6266obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6267{
6268 PyObject* tmp = NULL;
6269 identifier arg;
6270 expr_ty value;
6271
6272 if (PyObject_HasAttrString(obj, "arg")) {
6273 int res;
6274 tmp = PyObject_GetAttrString(obj, "arg");
6275 if (tmp == NULL) goto failed;
6276 res = obj2ast_identifier(tmp, &arg, arena);
6277 if (res != 0) goto failed;
6278 Py_XDECREF(tmp);
6279 tmp = NULL;
6280 } else {
6281 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6282 return 1;
6283 }
6284 if (PyObject_HasAttrString(obj, "value")) {
6285 int res;
6286 tmp = PyObject_GetAttrString(obj, "value");
6287 if (tmp == NULL) goto failed;
6288 res = obj2ast_expr(tmp, &value, arena);
6289 if (res != 0) goto failed;
6290 Py_XDECREF(tmp);
6291 tmp = NULL;
6292 } else {
6293 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6294 return 1;
6295 }
6296 *out = keyword(arg, value, arena);
6297 return 0;
6298failed:
6299 Py_XDECREF(tmp);
6300 return 1;
6301}
6302
6303int
6304obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6305{
6306 PyObject* tmp = NULL;
6307 identifier name;
6308 identifier asname;
6309
6310 if (PyObject_HasAttrString(obj, "name")) {
6311 int res;
6312 tmp = PyObject_GetAttrString(obj, "name");
6313 if (tmp == NULL) goto failed;
6314 res = obj2ast_identifier(tmp, &name, arena);
6315 if (res != 0) goto failed;
6316 Py_XDECREF(tmp);
6317 tmp = NULL;
6318 } else {
6319 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6320 return 1;
6321 }
6322 if (PyObject_HasAttrString(obj, "asname")) {
6323 int res;
6324 tmp = PyObject_GetAttrString(obj, "asname");
6325 if (tmp == NULL) goto failed;
6326 res = obj2ast_identifier(tmp, &asname, arena);
6327 if (res != 0) goto failed;
6328 Py_XDECREF(tmp);
6329 tmp = NULL;
6330 } else {
6331 asname = NULL;
6332 }
6333 *out = alias(name, asname, arena);
6334 return 0;
6335failed:
6336 Py_XDECREF(tmp);
6337 return 1;
6338}
6339
6340
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006341PyMODINIT_FUNC
6342init_ast(void)
6343{
6344 PyObject *m, *d;
6345 if (!init_types()) return;
6346 m = Py_InitModule3("_ast", NULL, NULL);
6347 if (!m) return;
6348 d = PyModule_GetDict(m);
Neal Norwitz207c9f32008-03-31 04:42:11 +00006349 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006350 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6351 return;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00006352 if (PyModule_AddStringConstant(m, "__version__", "57783") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006353 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006354 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6355 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6356 return;
6357 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6358 < 0) return;
6359 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6360 0) return;
6361 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6362 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6363 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6364 < 0) return;
6365 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6366 return;
6367 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6368 return;
6369 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6370 return;
6371 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6372 return;
6373 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6374 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006375 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6376 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6377 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6378 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6379 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6380 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6381 0) return;
6382 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6383 0) return;
6384 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6385 return;
6386 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6387 return;
6388 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6389 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006390 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6391 return;
Jeremy Hylton81e95022007-02-27 06:50:52 +00006392 if (PyDict_SetItemString(d, "Nonlocal", (PyObject*)Nonlocal_type) < 0)
6393 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006394 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6395 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6396 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6397 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6398 return;
6399 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6400 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6401 return;
6402 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6403 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6404 return;
6405 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6406 return;
6407 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6408 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Guido van Rossum86e58e22006-08-28 15:27:34 +00006409 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006410 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6411 return;
Nick Coghlan650f0d02007-04-15 12:05:43 +00006412 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6413 return;
Guido van Rossum992d4a32007-07-11 13:09:30 +00006414 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6415 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006416 if (PyDict_SetItemString(d, "GeneratorExp",
6417 (PyObject*)GeneratorExp_type) < 0) return;
6418 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6419 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6420 return;
6421 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006422 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6423 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
Thomas Wouters00e41de2007-02-23 19:56:57 +00006424 if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return;
Georg Brandl52318d62006-09-06 07:06:08 +00006425 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6426 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006427 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6428 0) return;
6429 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6430 0) return;
Guido van Rossum0368b722007-05-11 16:50:42 +00006431 if (PyDict_SetItemString(d, "Starred", (PyObject*)Starred_type) < 0)
6432 return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006433 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6434 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6435 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6436 if (PyDict_SetItemString(d, "expr_context",
6437 (PyObject*)expr_context_type) < 0) return;
6438 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6439 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6440 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6441 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6442 return;
6443 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6444 return;
6445 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6446 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006447 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6448 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6449 return;
6450 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6451 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6452 return;
6453 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6454 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6455 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6456 return;
6457 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6458 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6459 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6460 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6461 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6462 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6463 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6464 return;
6465 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6466 return;
6467 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6468 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6469 return;
6470 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6471 return;
6472 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6473 return;
6474 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6475 return;
6476 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6477 return;
6478 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6479 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6480 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6481 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6482 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6483 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6484 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6485 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6486 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6487 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6488 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6489 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6490 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6491 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6492 if (PyDict_SetItemString(d, "comprehension",
6493 (PyObject*)comprehension_type) < 0) return;
6494 if (PyDict_SetItemString(d, "excepthandler",
6495 (PyObject*)excepthandler_type) < 0) return;
6496 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6497 0) return;
Neal Norwitzc1505362006-12-28 06:47:50 +00006498 if (PyDict_SetItemString(d, "arg", (PyObject*)arg_type) < 0) return;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006499 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6500 return;
6501 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006502}
6503
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006504
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006505PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006506{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006507 init_types();
6508 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006509}
Martin v. Löwis5b222132007-06-10 09:51:05 +00006510
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006511/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6512mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006513{
6514 mod_ty res;
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006515 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6516 (PyObject*)Interactive_type};
6517 char *req_name[] = {"Module", "Expression", "Interactive"};
6518 assert(0 <= mode && mode <= 2);
6519
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006520 init_types();
Neal Norwitzdb4115f2008-03-31 04:20:05 +00006521
6522 if (!PyObject_IsInstance(ast, req_type[mode])) {
6523 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6524 req_name[mode], Py_TYPE(ast)->tp_name);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006525 return NULL;
6526 }
6527 if (obj2ast_mod(ast, &res, arena) != 0)
6528 return NULL;
6529 else
6530 return res;
6531}
6532
6533int PyAST_Check(PyObject* obj)
6534{
6535 init_types();
Neal Norwitz207c9f32008-03-31 04:42:11 +00006536 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00006537}
6538
Martin v. Löwis5b222132007-06-10 09:51:05 +00006539