blob: 4f10aa077ae57c23dea9de55f7e029447a45ac15 [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson8fa3e932009-06-13 20:30:48 +00005 __version__ 73421.
Brett Cannon0db62aa2007-02-12 03:51:02 +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
Georg Brandlc52ed592008-03-30 07:01:47 +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",
Christian Heimes5224d282008-02-23 15:01:05 +000045 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000046};
Neal Norwitz53d960c2006-02-28 22:47:29 +000047static PyTypeObject *ClassDef_type;
48static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000049 "name",
50 "bases",
51 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000052 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000053};
Neal Norwitz53d960c2006-02-28 22:47:29 +000054static PyTypeObject *Return_type;
55static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000056 "value",
57};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Delete_type;
59static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "targets",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Assign_type;
63static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65 "value",
66};
Neal Norwitz53d960c2006-02-28 22:47:29 +000067static PyTypeObject *AugAssign_type;
68static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000069 "target",
70 "op",
71 "value",
72};
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static PyTypeObject *Print_type;
74static char *Print_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075 "dest",
76 "values",
77 "nl",
78};
Neal Norwitz53d960c2006-02-28 22:47:29 +000079static PyTypeObject *For_type;
80static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000081 "target",
82 "iter",
83 "body",
84 "orelse",
85};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *While_type;
87static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088 "test",
89 "body",
90 "orelse",
91};
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static PyTypeObject *If_type;
93static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000094 "test",
95 "body",
96 "orelse",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *With_type;
99static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100 "context_expr",
101 "optional_vars",
102 "body",
103};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000104static PyTypeObject *Raise_type;
105static char *Raise_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106 "type",
107 "inst",
108 "tback",
109};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000110static PyTypeObject *TryExcept_type;
111static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000112 "body",
113 "handlers",
114 "orelse",
115};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static PyTypeObject *TryFinally_type;
117static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000118 "body",
119 "finalbody",
120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *Assert_type;
122static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000123 "test",
124 "msg",
125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *Import_type;
127static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128 "names",
129};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000130static PyTypeObject *ImportFrom_type;
131static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132 "module",
133 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000134 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *Exec_type;
137static char *Exec_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138 "body",
139 "globals",
140 "locals",
141};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000142static PyTypeObject *Global_type;
143static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144 "names",
145};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000146static PyTypeObject *Expr_type;
147static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000148 "value",
149};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000150static PyTypeObject *Pass_type;
151static PyTypeObject *Break_type;
152static PyTypeObject *Continue_type;
153static PyTypeObject *expr_type;
154static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000155 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000156 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000157};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BoolOp_type;
160static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "op",
162 "values",
163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *BinOp_type;
165static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000166 "left",
167 "op",
168 "right",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *UnaryOp_type;
171static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "op",
173 "operand",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *Lambda_type;
176static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000177 "args",
178 "body",
179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *IfExp_type;
181static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000182 "test",
183 "body",
184 "orelse",
185};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000186static PyTypeObject *Dict_type;
187static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000188 "keys",
189 "values",
190};
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000191static PyTypeObject *Set_type;
192static char *Set_fields[]={
193 "elts",
194};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000195static PyTypeObject *ListComp_type;
196static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000197 "elt",
198 "generators",
199};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000200static PyTypeObject *GeneratorExp_type;
201static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000202 "elt",
203 "generators",
204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Yield_type;
206static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207 "value",
208};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000209static PyTypeObject *Compare_type;
210static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000211 "left",
212 "ops",
213 "comparators",
214};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000215static PyTypeObject *Call_type;
216static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000217 "func",
218 "args",
219 "keywords",
220 "starargs",
221 "kwargs",
222};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000223static PyTypeObject *Repr_type;
224static char *Repr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000225 "value",
226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *Num_type;
228static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000229 "n",
230};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000231static PyTypeObject *Str_type;
232static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233 "s",
234};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000235static PyTypeObject *Attribute_type;
236static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000237 "value",
238 "attr",
239 "ctx",
240};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000241static PyTypeObject *Subscript_type;
242static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000243 "value",
244 "slice",
245 "ctx",
246};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000247static PyTypeObject *Name_type;
248static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000249 "id",
250 "ctx",
251};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000252static PyTypeObject *List_type;
253static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000254 "elts",
255 "ctx",
256};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000257static PyTypeObject *Tuple_type;
258static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259 "elts",
260 "ctx",
261};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000262static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000263static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
264*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
265static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000266static PyTypeObject *Load_type;
267static PyTypeObject *Store_type;
268static PyTypeObject *Del_type;
269static PyTypeObject *AugLoad_type;
270static PyTypeObject *AugStore_type;
271static PyTypeObject *Param_type;
272static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000273static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000274static PyTypeObject *Ellipsis_type;
275static PyTypeObject *Slice_type;
276static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000277 "lower",
278 "upper",
279 "step",
280};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *ExtSlice_type;
282static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000283 "dims",
284};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *Index_type;
286static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000287 "value",
288};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000289static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000290static PyObject *And_singleton, *Or_singleton;
291static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *And_type;
293static PyTypeObject *Or_type;
294static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000295static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
296*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
297*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
298*FloorDiv_singleton;
299static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static PyTypeObject *Add_type;
301static PyTypeObject *Sub_type;
302static PyTypeObject *Mult_type;
303static PyTypeObject *Div_type;
304static PyTypeObject *Mod_type;
305static PyTypeObject *Pow_type;
306static PyTypeObject *LShift_type;
307static PyTypeObject *RShift_type;
308static PyTypeObject *BitOr_type;
309static PyTypeObject *BitXor_type;
310static PyTypeObject *BitAnd_type;
311static PyTypeObject *FloorDiv_type;
312static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000313static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
314*USub_singleton;
315static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000316static PyTypeObject *Invert_type;
317static PyTypeObject *Not_type;
318static PyTypeObject *UAdd_type;
319static PyTypeObject *USub_type;
320static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000321static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
322*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
323*NotIn_singleton;
324static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000325static PyTypeObject *Eq_type;
326static PyTypeObject *NotEq_type;
327static PyTypeObject *Lt_type;
328static PyTypeObject *LtE_type;
329static PyTypeObject *Gt_type;
330static PyTypeObject *GtE_type;
331static PyTypeObject *Is_type;
332static PyTypeObject *IsNot_type;
333static PyTypeObject *In_type;
334static PyTypeObject *NotIn_type;
335static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000336static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000337static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000338 "target",
339 "iter",
340 "ifs",
341};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000342static PyTypeObject *excepthandler_type;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000343static char *excepthandler_attributes[] = {
344 "lineno",
345 "col_offset",
346};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject* ast2obj_excepthandler(void*);
Georg Brandla48f3ab2008-03-30 06:40:17 +0000348static PyTypeObject *ExceptHandler_type;
349static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000350 "type",
351 "name",
352 "body",
353};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000354static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000355static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000356static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000357 "args",
358 "vararg",
359 "kwarg",
360 "defaults",
361};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000362static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000363static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365 "arg",
366 "value",
367};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000368static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000369static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000370static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000371 "name",
372 "asname",
373};
374
375
Georg Brandlc52ed592008-03-30 07:01:47 +0000376static int
377ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
378{
379 Py_ssize_t i, numfields = 0;
380 int res = -1;
381 PyObject *key, *value, *fields;
382 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
383 if (!fields)
384 PyErr_Clear();
385 if (fields) {
386 numfields = PySequence_Size(fields);
387 if (numfields == -1)
388 goto cleanup;
389 }
390 res = 0; /* if no error occurs, this stays 0 to the end */
391 if (PyTuple_GET_SIZE(args) > 0) {
392 if (numfields != PyTuple_GET_SIZE(args)) {
Georg Brandl1721e752008-03-30 19:43:27 +0000393 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc05e34492008-09-10 22:04:45 +0000394 "%zd positional argument%s",
Georg Brandl1721e752008-03-30 19:43:27 +0000395 Py_TYPE(self)->tp_name,
396 numfields == 0 ? "" : "either 0 or ",
Georg Brandlc52ed592008-03-30 07:01:47 +0000397 numfields, numfields == 1 ? "" : "s");
398 res = -1;
399 goto cleanup;
400 }
401 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
402 /* cannot be reached when fields is NULL */
403 PyObject *name = PySequence_GetItem(fields, i);
404 if (!name) {
405 res = -1;
406 goto cleanup;
407 }
408 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
409 Py_DECREF(name);
410 if (res < 0)
411 goto cleanup;
412 }
413 }
414 if (kw) {
415 i = 0; /* needed by PyDict_Next */
416 while (PyDict_Next(kw, &i, &key, &value)) {
417 res = PyObject_SetAttr(self, key, value);
418 if (res < 0)
419 goto cleanup;
420 }
421 }
422 cleanup:
423 Py_XDECREF(fields);
424 return res;
425}
426
Georg Brandle34c21c2008-03-30 20:20:39 +0000427/* Pickling support */
428static PyObject *
429ast_type_reduce(PyObject *self, PyObject *unused)
430{
431 PyObject *res;
432 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
433 if (dict == NULL) {
434 if (PyErr_ExceptionMatches(PyExc_AttributeError))
435 PyErr_Clear();
436 else
437 return NULL;
438 }
439 if (dict) {
440 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
441 Py_DECREF(dict);
442 return res;
443 }
444 return Py_BuildValue("O()", Py_TYPE(self));
445}
446
447static PyMethodDef ast_type_methods[] = {
448 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
449 {NULL}
450};
451
Georg Brandlc52ed592008-03-30 07:01:47 +0000452static PyTypeObject AST_type = {
453 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Georg Brandle34c21c2008-03-30 20:20:39 +0000454 "_ast.AST",
Georg Brandlc52ed592008-03-30 07:01:47 +0000455 sizeof(PyObject),
456 0,
457 0, /* tp_dealloc */
458 0, /* tp_print */
459 0, /* tp_getattr */
460 0, /* tp_setattr */
461 0, /* tp_compare */
462 0, /* tp_repr */
463 0, /* tp_as_number */
464 0, /* tp_as_sequence */
465 0, /* tp_as_mapping */
466 0, /* tp_hash */
467 0, /* tp_call */
468 0, /* tp_str */
469 PyObject_GenericGetAttr, /* tp_getattro */
470 PyObject_GenericSetAttr, /* tp_setattro */
471 0, /* tp_as_buffer */
472 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
473 0, /* tp_doc */
474 0, /* tp_traverse */
475 0, /* tp_clear */
476 0, /* tp_richcompare */
477 0, /* tp_weaklistoffset */
478 0, /* tp_iter */
479 0, /* tp_iternext */
Georg Brandle34c21c2008-03-30 20:20:39 +0000480 ast_type_methods, /* tp_methods */
Georg Brandlc52ed592008-03-30 07:01:47 +0000481 0, /* tp_members */
482 0, /* tp_getset */
483 0, /* tp_base */
484 0, /* tp_dict */
485 0, /* tp_descr_get */
486 0, /* tp_descr_set */
487 0, /* tp_dictoffset */
488 (initproc)ast_type_init, /* tp_init */
489 PyType_GenericAlloc, /* tp_alloc */
490 PyType_GenericNew, /* tp_new */
491 PyObject_Del, /* tp_free */
492};
493
494
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000495static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
496{
497 PyObject *fnames, *result;
498 int i;
Georg Brandl2c55c592008-03-30 19:00:49 +0000499 fnames = PyTuple_New(num_fields);
500 if (!fnames) return NULL;
501 for (i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000502 PyObject *field = PyString_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000503 if (!field) {
504 Py_DECREF(fnames);
505 return NULL;
506 }
507 PyTuple_SET_ITEM(fnames, i, field);
508 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000509 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000510 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000511 Py_DECREF(fnames);
512 return (PyTypeObject*)result;
513}
514
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000515static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
516{
Neal Norwitz19379f12006-04-03 04:50:58 +0000517 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000518 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000519 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000520 for(i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000521 s = PyString_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000522 if (!s) {
523 Py_DECREF(l);
524 return 0;
525 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000526 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000527 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000528 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
529 Py_DECREF(l);
530 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000531}
532
Georg Brandlfc8eef32008-03-28 12:11:56 +0000533/* Conversion AST -> Python */
534
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000535static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
536{
537 int i, n = asdl_seq_LEN(seq);
538 PyObject *result = PyList_New(n);
539 PyObject *value;
540 if (!result)
541 return NULL;
542 for (i = 0; i < n; i++) {
543 value = func(asdl_seq_GET(seq, i));
544 if (!value) {
545 Py_DECREF(result);
546 return NULL;
547 }
548 PyList_SET_ITEM(result, i, value);
549 }
550 return result;
551}
552
553static PyObject* ast2obj_object(void *o)
554{
555 if (!o)
556 o = Py_None;
557 Py_INCREF((PyObject*)o);
558 return (PyObject*)o;
559}
560#define ast2obj_identifier ast2obj_object
561#define ast2obj_string ast2obj_object
562static PyObject* ast2obj_bool(bool b)
563{
564 return PyBool_FromLong(b);
565}
566
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000567static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000568{
569 return PyInt_FromLong(b);
570}
571
Georg Brandlfc8eef32008-03-28 12:11:56 +0000572/* Conversion Python -> AST */
573
574static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
575{
576 if (obj == Py_None)
577 obj = NULL;
578 if (obj)
579 PyArena_AddPyObject(arena, obj);
580 Py_XINCREF(obj);
581 *out = obj;
582 return 0;
583}
584
585#define obj2ast_identifier obj2ast_object
586#define obj2ast_string obj2ast_object
587
588static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
589{
590 int i;
591 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
592 PyObject *s = PyObject_Repr(obj);
593 if (s == NULL) return 1;
594 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000595 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000596 Py_DECREF(s);
597 return 1;
598 }
599
600 i = (int)PyLong_AsLong(obj);
601 if (i == -1 && PyErr_Occurred())
602 return 1;
603 *out = i;
604 return 0;
605}
606
607static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
608{
609 if (!PyBool_Check(obj)) {
610 PyObject *s = PyObject_Repr(obj);
611 if (s == NULL) return 1;
612 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000613 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000614 Py_DECREF(s);
615 return 1;
616 }
617
618 *out = (obj == Py_True);
619 return 0;
620}
621
Benjamin Petersonaff36f12008-10-19 13:59:01 +0000622static int add_ast_fields(void)
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000623{
624 PyObject *empty_tuple, *d;
625 if (PyType_Ready(&AST_type) < 0)
626 return -1;
627 d = AST_type.tp_dict;
628 empty_tuple = PyTuple_New(0);
629 if (!empty_tuple ||
630 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
631 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
632 Py_XDECREF(empty_tuple);
633 return -1;
634 }
635 Py_DECREF(empty_tuple);
636 return 0;
637}
638
Georg Brandlfc8eef32008-03-28 12:11:56 +0000639
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000640static int init_types(void)
641{
Neal Norwitz19379f12006-04-03 04:50:58 +0000642 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000643 if (initialized) return 1;
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000644 if (add_ast_fields() < 0) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000645 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000646 if (!mod_type) return 0;
647 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000649 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650 Interactive_type = make_type("Interactive", mod_type,
651 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 Expression_type = make_type("Expression", mod_type, Expression_fields,
654 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000655 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000658 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000660 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 FunctionDef_type = make_type("FunctionDef", stmt_type,
662 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000664 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000682 With_type = make_type("With", stmt_type, With_fields, 3);
683 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
689 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000696 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000704 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000706 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000708 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000710 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000712 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000721 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!Dict_type) return 0;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000725 Set_type = make_type("Set", expr_type, Set_fields, 1);
726 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 GeneratorExp_type = make_type("GeneratorExp", expr_type,
730 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000736 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000740 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000754 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!expr_context_type) return 0;
756 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000757 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000759 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000761 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000763 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000765 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000769 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000771 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000773 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000777 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000779 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000781 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!slice_type) return 0;
783 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000788 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000790 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000792 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!boolop_type) return 0;
794 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000795 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000797 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000799 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000803 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!operator_type) return 0;
805 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000814 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000816 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000818 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000820 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000850 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000852 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000854 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!unaryop_type) return 0;
856 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000865 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000867 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000869 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000871 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000873 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!cmpop_type) return 0;
875 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000876 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000878 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000880 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000884 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000886 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000888 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000890 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000892 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000894 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000896 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000898 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000900 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000902 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000904 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000906 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000908 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000912 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000914 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000916 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000917 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000919 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000921 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
922 return 0;
923 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
924 ExceptHandler_fields, 3);
925 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000926 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000928 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000930 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000931 if (!alias_type) return 0;
932 initialized = 1;
933 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000934}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000935
Georg Brandlfc8eef32008-03-28 12:11:56 +0000936static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
937static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
938static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
939static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
940 arena);
941static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
942static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
943static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
944static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
945static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
946static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
947 arena);
948static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
949 arena);
950static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
951static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
952static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956{
957 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000958 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000959 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 p->kind = Module_kind;
962 p->v.Module.body = body;
963 return p;
964}
965
966mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000967Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968{
969 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000970 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000971 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973 p->kind = Interactive_kind;
974 p->v.Interactive.body = body;
975 return p;
976}
977
978mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000979Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980{
981 mod_ty p;
982 if (!body) {
983 PyErr_SetString(PyExc_ValueError,
984 "field body is required for Expression");
985 return NULL;
986 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000987 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000988 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 p->kind = Expression_kind;
991 p->v.Expression.body = body;
992 return p;
993}
994
995mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997{
998 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001000 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 p->kind = Suite_kind;
1003 p->v.Suite.body = body;
1004 return p;
1005}
1006
1007stmt_ty
1008FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +00001009 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010{
1011 stmt_ty p;
1012 if (!name) {
1013 PyErr_SetString(PyExc_ValueError,
1014 "field name is required for FunctionDef");
1015 return NULL;
1016 }
1017 if (!args) {
1018 PyErr_SetString(PyExc_ValueError,
1019 "field args is required for FunctionDef");
1020 return NULL;
1021 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001022 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001023 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 p->kind = FunctionDef_kind;
1026 p->v.FunctionDef.name = name;
1027 p->v.FunctionDef.args = args;
1028 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001029 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001031 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 return p;
1033}
1034
1035stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001036ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1037 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038{
1039 stmt_ty p;
1040 if (!name) {
1041 PyErr_SetString(PyExc_ValueError,
1042 "field name is required for ClassDef");
1043 return NULL;
1044 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001045 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001046 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 p->kind = ClassDef_kind;
1049 p->v.ClassDef.name = name;
1050 p->v.ClassDef.bases = bases;
1051 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001052 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001054 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return p;
1056}
1057
1058stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001059Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060{
1061 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001062 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001063 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 p->kind = Return_kind;
1066 p->v.Return.value = value;
1067 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001068 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 return p;
1070}
1071
1072stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001073Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074{
1075 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001076 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001077 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 p->kind = Delete_kind;
1080 p->v.Delete.targets = targets;
1081 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001082 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 return p;
1084}
1085
1086stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001087Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1088 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089{
1090 stmt_ty p;
1091 if (!value) {
1092 PyErr_SetString(PyExc_ValueError,
1093 "field value is required for Assign");
1094 return NULL;
1095 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001096 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001097 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 p->kind = Assign_kind;
1100 p->v.Assign.targets = targets;
1101 p->v.Assign.value = value;
1102 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001103 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 return p;
1105}
1106
1107stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001108AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1109 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110{
1111 stmt_ty p;
1112 if (!target) {
1113 PyErr_SetString(PyExc_ValueError,
1114 "field target is required for AugAssign");
1115 return NULL;
1116 }
1117 if (!op) {
1118 PyErr_SetString(PyExc_ValueError,
1119 "field op is required for AugAssign");
1120 return NULL;
1121 }
1122 if (!value) {
1123 PyErr_SetString(PyExc_ValueError,
1124 "field value is required for AugAssign");
1125 return NULL;
1126 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001127 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001128 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 p->kind = AugAssign_kind;
1131 p->v.AugAssign.target = target;
1132 p->v.AugAssign.op = op;
1133 p->v.AugAssign.value = value;
1134 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001135 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return p;
1137}
1138
1139stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001140Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1141 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142{
1143 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001144 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001145 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 p->kind = Print_kind;
1148 p->v.Print.dest = dest;
1149 p->v.Print.values = values;
1150 p->v.Print.nl = nl;
1151 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 return p;
1154}
1155
1156stmt_ty
1157For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001158 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159{
1160 stmt_ty p;
1161 if (!target) {
1162 PyErr_SetString(PyExc_ValueError,
1163 "field target is required for For");
1164 return NULL;
1165 }
1166 if (!iter) {
1167 PyErr_SetString(PyExc_ValueError,
1168 "field iter is required for For");
1169 return NULL;
1170 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001171 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001172 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 p->kind = For_kind;
1175 p->v.For.target = target;
1176 p->v.For.iter = iter;
1177 p->v.For.body = body;
1178 p->v.For.orelse = orelse;
1179 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001180 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 return p;
1182}
1183
1184stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1186 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187{
1188 stmt_ty p;
1189 if (!test) {
1190 PyErr_SetString(PyExc_ValueError,
1191 "field test is required for While");
1192 return NULL;
1193 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001194 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001195 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 p->kind = While_kind;
1198 p->v.While.test = test;
1199 p->v.While.body = body;
1200 p->v.While.orelse = orelse;
1201 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001202 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 return p;
1204}
1205
1206stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001207If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1208 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209{
1210 stmt_ty p;
1211 if (!test) {
1212 PyErr_SetString(PyExc_ValueError,
1213 "field test is required for If");
1214 return NULL;
1215 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001216 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001217 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 p->kind = If_kind;
1220 p->v.If.test = test;
1221 p->v.If.body = body;
1222 p->v.If.orelse = orelse;
1223 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001224 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return p;
1226}
1227
1228stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001229With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001230 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001231{
1232 stmt_ty p;
1233 if (!context_expr) {
1234 PyErr_SetString(PyExc_ValueError,
1235 "field context_expr is required for With");
1236 return NULL;
1237 }
1238 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001239 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001240 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001241 p->kind = With_kind;
1242 p->v.With.context_expr = context_expr;
1243 p->v.With.optional_vars = optional_vars;
1244 p->v.With.body = body;
1245 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001247 return p;
1248}
1249
1250stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001251Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1252 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253{
1254 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001255 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001256 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 p->kind = Raise_kind;
1259 p->v.Raise.type = type;
1260 p->v.Raise.inst = inst;
1261 p->v.Raise.tback = tback;
1262 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001263 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return p;
1265}
1266
1267stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001268TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001269 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270{
1271 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001272 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001273 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 p->kind = TryExcept_kind;
1276 p->v.TryExcept.body = body;
1277 p->v.TryExcept.handlers = handlers;
1278 p->v.TryExcept.orelse = orelse;
1279 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001280 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 return p;
1282}
1283
1284stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001285TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1286 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287{
1288 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001289 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001290 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292 p->kind = TryFinally_kind;
1293 p->v.TryFinally.body = body;
1294 p->v.TryFinally.finalbody = finalbody;
1295 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001296 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 return p;
1298}
1299
1300stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001301Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302{
1303 stmt_ty p;
1304 if (!test) {
1305 PyErr_SetString(PyExc_ValueError,
1306 "field test is required for Assert");
1307 return NULL;
1308 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001309 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001310 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 p->kind = Assert_kind;
1313 p->v.Assert.test = test;
1314 p->v.Assert.msg = msg;
1315 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001316 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 return p;
1318}
1319
1320stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001321Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322{
1323 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001324 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001325 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 p->kind = Import_kind;
1328 p->v.Import.names = names;
1329 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001330 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 return p;
1332}
1333
1334stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001335ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1336 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337{
1338 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001339 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001340 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 p->kind = ImportFrom_kind;
1343 p->v.ImportFrom.module = module;
1344 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001345 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001347 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348 return p;
1349}
1350
1351stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001352Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1353 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354{
1355 stmt_ty p;
1356 if (!body) {
1357 PyErr_SetString(PyExc_ValueError,
1358 "field body is required for Exec");
1359 return NULL;
1360 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001361 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001362 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 p->kind = Exec_kind;
1365 p->v.Exec.body = body;
1366 p->v.Exec.globals = globals;
1367 p->v.Exec.locals = locals;
1368 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001369 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370 return p;
1371}
1372
1373stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001374Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375{
1376 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001378 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 p->kind = Global_kind;
1381 p->v.Global.names = names;
1382 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001383 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 return p;
1385}
1386
1387stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001388Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389{
1390 stmt_ty p;
1391 if (!value) {
1392 PyErr_SetString(PyExc_ValueError,
1393 "field value is required for Expr");
1394 return NULL;
1395 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001396 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001397 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 p->kind = Expr_kind;
1400 p->v.Expr.value = value;
1401 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001402 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 return p;
1404}
1405
1406stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001407Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408{
1409 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001411 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 p->kind = Pass_kind;
1414 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001415 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return p;
1417}
1418
1419stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001420Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421{
1422 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001423 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001424 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 p->kind = Break_kind;
1427 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001428 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 return p;
1430}
1431
1432stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001433Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434{
1435 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001436 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001437 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 p->kind = Continue_kind;
1440 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001441 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return p;
1443}
1444
1445expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001446BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1447 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448{
1449 expr_ty p;
1450 if (!op) {
1451 PyErr_SetString(PyExc_ValueError,
1452 "field op is required for BoolOp");
1453 return NULL;
1454 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001455 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001456 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 p->kind = BoolOp_kind;
1459 p->v.BoolOp.op = op;
1460 p->v.BoolOp.values = values;
1461 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001462 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 return p;
1464}
1465
1466expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001467BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1468 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469{
1470 expr_ty p;
1471 if (!left) {
1472 PyErr_SetString(PyExc_ValueError,
1473 "field left is required for BinOp");
1474 return NULL;
1475 }
1476 if (!op) {
1477 PyErr_SetString(PyExc_ValueError,
1478 "field op is required for BinOp");
1479 return NULL;
1480 }
1481 if (!right) {
1482 PyErr_SetString(PyExc_ValueError,
1483 "field right is required for BinOp");
1484 return NULL;
1485 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001486 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001487 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 p->kind = BinOp_kind;
1490 p->v.BinOp.left = left;
1491 p->v.BinOp.op = op;
1492 p->v.BinOp.right = right;
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 +00001499UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1500 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501{
1502 expr_ty p;
1503 if (!op) {
1504 PyErr_SetString(PyExc_ValueError,
1505 "field op is required for UnaryOp");
1506 return NULL;
1507 }
1508 if (!operand) {
1509 PyErr_SetString(PyExc_ValueError,
1510 "field operand is required for UnaryOp");
1511 return NULL;
1512 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001513 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001514 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 p->kind = UnaryOp_kind;
1517 p->v.UnaryOp.op = op;
1518 p->v.UnaryOp.operand = operand;
1519 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001520 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 return p;
1522}
1523
1524expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001525Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1526 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527{
1528 expr_ty p;
1529 if (!args) {
1530 PyErr_SetString(PyExc_ValueError,
1531 "field args is required for Lambda");
1532 return NULL;
1533 }
1534 if (!body) {
1535 PyErr_SetString(PyExc_ValueError,
1536 "field body is required for Lambda");
1537 return NULL;
1538 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001539 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001540 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542 p->kind = Lambda_kind;
1543 p->v.Lambda.args = args;
1544 p->v.Lambda.body = body;
1545 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001546 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547 return p;
1548}
1549
1550expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001551IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1552 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001553{
1554 expr_ty p;
1555 if (!test) {
1556 PyErr_SetString(PyExc_ValueError,
1557 "field test is required for IfExp");
1558 return NULL;
1559 }
1560 if (!body) {
1561 PyErr_SetString(PyExc_ValueError,
1562 "field body is required for IfExp");
1563 return NULL;
1564 }
1565 if (!orelse) {
1566 PyErr_SetString(PyExc_ValueError,
1567 "field orelse is required for IfExp");
1568 return NULL;
1569 }
1570 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001571 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001572 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001573 p->kind = IfExp_kind;
1574 p->v.IfExp.test = test;
1575 p->v.IfExp.body = body;
1576 p->v.IfExp.orelse = orelse;
1577 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001578 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001579 return p;
1580}
1581
1582expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001583Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1584 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585{
1586 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001587 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001588 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 p->kind = Dict_kind;
1591 p->v.Dict.keys = keys;
1592 p->v.Dict.values = values;
1593 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001594 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595 return p;
1596}
1597
1598expr_ty
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001599Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1600{
1601 expr_ty p;
1602 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1603 if (!p)
1604 return NULL;
1605 p->kind = Set_kind;
1606 p->v.Set.elts = elts;
1607 p->lineno = lineno;
1608 p->col_offset = col_offset;
1609 return p;
1610}
1611
1612expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001613ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1614 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615{
1616 expr_ty p;
1617 if (!elt) {
1618 PyErr_SetString(PyExc_ValueError,
1619 "field elt is required for ListComp");
1620 return NULL;
1621 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001622 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001623 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 p->kind = ListComp_kind;
1626 p->v.ListComp.elt = elt;
1627 p->v.ListComp.generators = generators;
1628 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001629 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630 return p;
1631}
1632
1633expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001634GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1635 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
1637 expr_ty p;
1638 if (!elt) {
1639 PyErr_SetString(PyExc_ValueError,
1640 "field elt is required for GeneratorExp");
1641 return NULL;
1642 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001643 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001644 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 p->kind = GeneratorExp_kind;
1647 p->v.GeneratorExp.elt = elt;
1648 p->v.GeneratorExp.generators = generators;
1649 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001650 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651 return p;
1652}
1653
1654expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656{
1657 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001659 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 p->kind = Yield_kind;
1662 p->v.Yield.value = value;
1663 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001664 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 return p;
1666}
1667
1668expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001669Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1670 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671{
1672 expr_ty p;
1673 if (!left) {
1674 PyErr_SetString(PyExc_ValueError,
1675 "field left is required for Compare");
1676 return NULL;
1677 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001678 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001679 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681 p->kind = Compare_kind;
1682 p->v.Compare.left = left;
1683 p->v.Compare.ops = ops;
1684 p->v.Compare.comparators = comparators;
1685 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001686 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 return p;
1688}
1689
1690expr_ty
1691Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001692 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693{
1694 expr_ty p;
1695 if (!func) {
1696 PyErr_SetString(PyExc_ValueError,
1697 "field func is required for Call");
1698 return NULL;
1699 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001700 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001701 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 p->kind = Call_kind;
1704 p->v.Call.func = func;
1705 p->v.Call.args = args;
1706 p->v.Call.keywords = keywords;
1707 p->v.Call.starargs = starargs;
1708 p->v.Call.kwargs = kwargs;
1709 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001710 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 return p;
1712}
1713
1714expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001715Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716{
1717 expr_ty p;
1718 if (!value) {
1719 PyErr_SetString(PyExc_ValueError,
1720 "field value is required for Repr");
1721 return NULL;
1722 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001723 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001724 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 p->kind = Repr_kind;
1727 p->v.Repr.value = value;
1728 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001729 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 return p;
1731}
1732
1733expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001734Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735{
1736 expr_ty p;
1737 if (!n) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "field n is required for Num");
1740 return NULL;
1741 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001742 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001743 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 p->kind = Num_kind;
1746 p->v.Num.n = n;
1747 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001748 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749 return p;
1750}
1751
1752expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001753Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754{
1755 expr_ty p;
1756 if (!s) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field s is required for Str");
1759 return NULL;
1760 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001761 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001762 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764 p->kind = Str_kind;
1765 p->v.Str.s = s;
1766 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001767 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 return p;
1769}
1770
1771expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001772Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1773 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774{
1775 expr_ty p;
1776 if (!value) {
1777 PyErr_SetString(PyExc_ValueError,
1778 "field value is required for Attribute");
1779 return NULL;
1780 }
1781 if (!attr) {
1782 PyErr_SetString(PyExc_ValueError,
1783 "field attr is required for Attribute");
1784 return NULL;
1785 }
1786 if (!ctx) {
1787 PyErr_SetString(PyExc_ValueError,
1788 "field ctx is required for Attribute");
1789 return NULL;
1790 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001791 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001792 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 p->kind = Attribute_kind;
1795 p->v.Attribute.value = value;
1796 p->v.Attribute.attr = attr;
1797 p->v.Attribute.ctx = ctx;
1798 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001799 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 return p;
1801}
1802
1803expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001804Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1805 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
1807 expr_ty p;
1808 if (!value) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field value is required for Subscript");
1811 return NULL;
1812 }
1813 if (!slice) {
1814 PyErr_SetString(PyExc_ValueError,
1815 "field slice is required for Subscript");
1816 return NULL;
1817 }
1818 if (!ctx) {
1819 PyErr_SetString(PyExc_ValueError,
1820 "field ctx is required for Subscript");
1821 return NULL;
1822 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001823 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001824 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 p->kind = Subscript_kind;
1827 p->v.Subscript.value = value;
1828 p->v.Subscript.slice = slice;
1829 p->v.Subscript.ctx = ctx;
1830 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001831 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 return p;
1833}
1834
1835expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001836Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1837 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838{
1839 expr_ty p;
1840 if (!id) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field id is required for Name");
1843 return NULL;
1844 }
1845 if (!ctx) {
1846 PyErr_SetString(PyExc_ValueError,
1847 "field ctx is required for Name");
1848 return NULL;
1849 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001850 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001851 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 p->kind = Name_kind;
1854 p->v.Name.id = id;
1855 p->v.Name.ctx = ctx;
1856 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001857 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 return p;
1859}
1860
1861expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001862List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1863 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864{
1865 expr_ty p;
1866 if (!ctx) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "field ctx is required for List");
1869 return NULL;
1870 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001872 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 p->kind = List_kind;
1875 p->v.List.elts = elts;
1876 p->v.List.ctx = ctx;
1877 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001878 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 return p;
1880}
1881
1882expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001883Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1884 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885{
1886 expr_ty p;
1887 if (!ctx) {
1888 PyErr_SetString(PyExc_ValueError,
1889 "field ctx is required for Tuple");
1890 return NULL;
1891 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001892 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001893 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 p->kind = Tuple_kind;
1896 p->v.Tuple.elts = elts;
1897 p->v.Tuple.ctx = ctx;
1898 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001899 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 return p;
1901}
1902
1903slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001904Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905{
1906 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001907 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001908 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 p->kind = Ellipsis_kind;
1911 return p;
1912}
1913
1914slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001915Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916{
1917 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001918 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001919 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 p->kind = Slice_kind;
1922 p->v.Slice.lower = lower;
1923 p->v.Slice.upper = upper;
1924 p->v.Slice.step = step;
1925 return p;
1926}
1927
1928slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001929ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930{
1931 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001932 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001933 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 p->kind = ExtSlice_kind;
1936 p->v.ExtSlice.dims = dims;
1937 return p;
1938}
1939
1940slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942{
1943 slice_ty p;
1944 if (!value) {
1945 PyErr_SetString(PyExc_ValueError,
1946 "field value is required for Index");
1947 return NULL;
1948 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001949 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001950 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 p->kind = Index_kind;
1953 p->v.Index.value = value;
1954 return p;
1955}
1956
1957comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959{
1960 comprehension_ty p;
1961 if (!target) {
1962 PyErr_SetString(PyExc_ValueError,
1963 "field target is required for comprehension");
1964 return NULL;
1965 }
1966 if (!iter) {
1967 PyErr_SetString(PyExc_ValueError,
1968 "field iter is required for comprehension");
1969 return NULL;
1970 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001971 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001972 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 p->target = target;
1975 p->iter = iter;
1976 p->ifs = ifs;
1977 return p;
1978}
1979
1980excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001981ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001982 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983{
1984 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001988 p->kind = ExceptHandler_kind;
1989 p->v.ExceptHandler.type = type;
1990 p->v.ExceptHandler.name = name;
1991 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001992 p->lineno = lineno;
1993 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 return p;
1995}
1996
1997arguments_ty
1998arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001999 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000{
2001 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002002 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002003 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 p->args = args;
2006 p->vararg = vararg;
2007 p->kwarg = kwarg;
2008 p->defaults = defaults;
2009 return p;
2010}
2011
2012keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002013keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014{
2015 keyword_ty p;
2016 if (!arg) {
2017 PyErr_SetString(PyExc_ValueError,
2018 "field arg is required for keyword");
2019 return NULL;
2020 }
2021 if (!value) {
2022 PyErr_SetString(PyExc_ValueError,
2023 "field value is required for keyword");
2024 return NULL;
2025 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002026 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002027 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 p->arg = arg;
2030 p->value = value;
2031 return p;
2032}
2033
2034alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036{
2037 alias_ty p;
2038 if (!name) {
2039 PyErr_SetString(PyExc_ValueError,
2040 "field name is required for alias");
2041 return NULL;
2042 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002043 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002044 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 p->name = name;
2047 p->asname = asname;
2048 return p;
2049}
2050
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002051
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002052PyObject*
2053ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002054{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002055 mod_ty o = (mod_ty)_o;
2056 PyObject *result = NULL, *value = NULL;
2057 if (!o) {
2058 Py_INCREF(Py_None);
2059 return Py_None;
2060 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062 switch (o->kind) {
2063 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002064 result = PyType_GenericNew(Module_type, NULL, NULL);
2065 if (!result) goto failed;
2066 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2067 if (!value) goto failed;
2068 if (PyObject_SetAttrString(result, "body", value) == -1)
2069 goto failed;
2070 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 break;
2072 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002073 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2074 if (!result) goto failed;
2075 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2076 if (!value) goto failed;
2077 if (PyObject_SetAttrString(result, "body", value) == -1)
2078 goto failed;
2079 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 break;
2081 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002082 result = PyType_GenericNew(Expression_type, NULL, NULL);
2083 if (!result) goto failed;
2084 value = ast2obj_expr(o->v.Expression.body);
2085 if (!value) goto failed;
2086 if (PyObject_SetAttrString(result, "body", value) == -1)
2087 goto failed;
2088 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 break;
2090 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002091 result = PyType_GenericNew(Suite_type, NULL, NULL);
2092 if (!result) goto failed;
2093 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2094 if (!value) goto failed;
2095 if (PyObject_SetAttrString(result, "body", value) == -1)
2096 goto failed;
2097 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 break;
2099 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002100 return result;
2101failed:
2102 Py_XDECREF(value);
2103 Py_XDECREF(result);
2104 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105}
2106
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002107PyObject*
2108ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002110 stmt_ty o = (stmt_ty)_o;
2111 PyObject *result = NULL, *value = NULL;
2112 if (!o) {
2113 Py_INCREF(Py_None);
2114 return Py_None;
2115 }
2116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 switch (o->kind) {
2118 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002119 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2120 if (!result) goto failed;
2121 value = ast2obj_identifier(o->v.FunctionDef.name);
2122 if (!value) goto failed;
2123 if (PyObject_SetAttrString(result, "name", value) == -1)
2124 goto failed;
2125 Py_DECREF(value);
2126 value = ast2obj_arguments(o->v.FunctionDef.args);
2127 if (!value) goto failed;
2128 if (PyObject_SetAttrString(result, "args", value) == -1)
2129 goto failed;
2130 Py_DECREF(value);
2131 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2132 if (!value) goto failed;
2133 if (PyObject_SetAttrString(result, "body", value) == -1)
2134 goto failed;
2135 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002136 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2137 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002138 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002139 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2140 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002141 goto failed;
2142 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 break;
2144 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2146 if (!result) goto failed;
2147 value = ast2obj_identifier(o->v.ClassDef.name);
2148 if (!value) goto failed;
2149 if (PyObject_SetAttrString(result, "name", value) == -1)
2150 goto failed;
2151 Py_DECREF(value);
2152 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2153 if (!value) goto failed;
2154 if (PyObject_SetAttrString(result, "bases", value) == -1)
2155 goto failed;
2156 Py_DECREF(value);
2157 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2158 if (!value) goto failed;
2159 if (PyObject_SetAttrString(result, "body", value) == -1)
2160 goto failed;
2161 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002162 value = ast2obj_list(o->v.ClassDef.decorator_list,
2163 ast2obj_expr);
2164 if (!value) goto failed;
2165 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2166 -1)
2167 goto failed;
2168 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 break;
2170 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002171 result = PyType_GenericNew(Return_type, NULL, NULL);
2172 if (!result) goto failed;
2173 value = ast2obj_expr(o->v.Return.value);
2174 if (!value) goto failed;
2175 if (PyObject_SetAttrString(result, "value", value) == -1)
2176 goto failed;
2177 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 break;
2179 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002180 result = PyType_GenericNew(Delete_type, NULL, NULL);
2181 if (!result) goto failed;
2182 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2183 if (!value) goto failed;
2184 if (PyObject_SetAttrString(result, "targets", value) == -1)
2185 goto failed;
2186 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 break;
2188 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002189 result = PyType_GenericNew(Assign_type, NULL, NULL);
2190 if (!result) goto failed;
2191 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2192 if (!value) goto failed;
2193 if (PyObject_SetAttrString(result, "targets", value) == -1)
2194 goto failed;
2195 Py_DECREF(value);
2196 value = ast2obj_expr(o->v.Assign.value);
2197 if (!value) goto failed;
2198 if (PyObject_SetAttrString(result, "value", value) == -1)
2199 goto failed;
2200 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 break;
2202 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002203 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2204 if (!result) goto failed;
2205 value = ast2obj_expr(o->v.AugAssign.target);
2206 if (!value) goto failed;
2207 if (PyObject_SetAttrString(result, "target", value) == -1)
2208 goto failed;
2209 Py_DECREF(value);
2210 value = ast2obj_operator(o->v.AugAssign.op);
2211 if (!value) goto failed;
2212 if (PyObject_SetAttrString(result, "op", value) == -1)
2213 goto failed;
2214 Py_DECREF(value);
2215 value = ast2obj_expr(o->v.AugAssign.value);
2216 if (!value) goto failed;
2217 if (PyObject_SetAttrString(result, "value", value) == -1)
2218 goto failed;
2219 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 break;
2221 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002222 result = PyType_GenericNew(Print_type, NULL, NULL);
2223 if (!result) goto failed;
2224 value = ast2obj_expr(o->v.Print.dest);
2225 if (!value) goto failed;
2226 if (PyObject_SetAttrString(result, "dest", value) == -1)
2227 goto failed;
2228 Py_DECREF(value);
2229 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2230 if (!value) goto failed;
2231 if (PyObject_SetAttrString(result, "values", value) == -1)
2232 goto failed;
2233 Py_DECREF(value);
2234 value = ast2obj_bool(o->v.Print.nl);
2235 if (!value) goto failed;
2236 if (PyObject_SetAttrString(result, "nl", value) == -1)
2237 goto failed;
2238 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 break;
2240 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002241 result = PyType_GenericNew(For_type, NULL, NULL);
2242 if (!result) goto failed;
2243 value = ast2obj_expr(o->v.For.target);
2244 if (!value) goto failed;
2245 if (PyObject_SetAttrString(result, "target", value) == -1)
2246 goto failed;
2247 Py_DECREF(value);
2248 value = ast2obj_expr(o->v.For.iter);
2249 if (!value) goto failed;
2250 if (PyObject_SetAttrString(result, "iter", value) == -1)
2251 goto failed;
2252 Py_DECREF(value);
2253 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2254 if (!value) goto failed;
2255 if (PyObject_SetAttrString(result, "body", value) == -1)
2256 goto failed;
2257 Py_DECREF(value);
2258 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 break;
2264 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002265 result = PyType_GenericNew(While_type, NULL, NULL);
2266 if (!result) goto failed;
2267 value = ast2obj_expr(o->v.While.test);
2268 if (!value) goto failed;
2269 if (PyObject_SetAttrString(result, "test", value) == -1)
2270 goto failed;
2271 Py_DECREF(value);
2272 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2273 if (!value) goto failed;
2274 if (PyObject_SetAttrString(result, "body", value) == -1)
2275 goto failed;
2276 Py_DECREF(value);
2277 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 break;
2283 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 result = PyType_GenericNew(If_type, NULL, NULL);
2285 if (!result) goto failed;
2286 value = ast2obj_expr(o->v.If.test);
2287 if (!value) goto failed;
2288 if (PyObject_SetAttrString(result, "test", value) == -1)
2289 goto failed;
2290 Py_DECREF(value);
2291 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2292 if (!value) goto failed;
2293 if (PyObject_SetAttrString(result, "body", value) == -1)
2294 goto failed;
2295 Py_DECREF(value);
2296 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2297 if (!value) goto failed;
2298 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2299 goto failed;
2300 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002302 case With_kind:
2303 result = PyType_GenericNew(With_type, NULL, NULL);
2304 if (!result) goto failed;
2305 value = ast2obj_expr(o->v.With.context_expr);
2306 if (!value) goto failed;
2307 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 value = ast2obj_expr(o->v.With.optional_vars);
2311 if (!value) goto failed;
2312 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2313 -1)
2314 goto failed;
2315 Py_DECREF(value);
2316 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2317 if (!value) goto failed;
2318 if (PyObject_SetAttrString(result, "body", value) == -1)
2319 goto failed;
2320 Py_DECREF(value);
2321 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002323 result = PyType_GenericNew(Raise_type, NULL, NULL);
2324 if (!result) goto failed;
2325 value = ast2obj_expr(o->v.Raise.type);
2326 if (!value) goto failed;
2327 if (PyObject_SetAttrString(result, "type", value) == -1)
2328 goto failed;
2329 Py_DECREF(value);
2330 value = ast2obj_expr(o->v.Raise.inst);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "inst", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_expr(o->v.Raise.tback);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "tback", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 break;
2341 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002342 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2343 if (!result) goto failed;
2344 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "body", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_list(o->v.TryExcept.handlers,
2350 ast2obj_excepthandler);
2351 if (!value) goto failed;
2352 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
2355 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2356 if (!value) goto failed;
2357 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2358 goto failed;
2359 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 break;
2361 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002362 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2363 if (!result) goto failed;
2364 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2365 if (!value) goto failed;
2366 if (PyObject_SetAttrString(result, "body", value) == -1)
2367 goto failed;
2368 Py_DECREF(value);
2369 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2370 if (!value) goto failed;
2371 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2372 goto failed;
2373 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 break;
2375 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002376 result = PyType_GenericNew(Assert_type, NULL, NULL);
2377 if (!result) goto failed;
2378 value = ast2obj_expr(o->v.Assert.test);
2379 if (!value) goto failed;
2380 if (PyObject_SetAttrString(result, "test", value) == -1)
2381 goto failed;
2382 Py_DECREF(value);
2383 value = ast2obj_expr(o->v.Assert.msg);
2384 if (!value) goto failed;
2385 if (PyObject_SetAttrString(result, "msg", value) == -1)
2386 goto failed;
2387 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 break;
2389 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002390 result = PyType_GenericNew(Import_type, NULL, NULL);
2391 if (!result) goto failed;
2392 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2393 if (!value) goto failed;
2394 if (PyObject_SetAttrString(result, "names", value) == -1)
2395 goto failed;
2396 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 break;
2398 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002399 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2400 if (!result) goto failed;
2401 value = ast2obj_identifier(o->v.ImportFrom.module);
2402 if (!value) goto failed;
2403 if (PyObject_SetAttrString(result, "module", value) == -1)
2404 goto failed;
2405 Py_DECREF(value);
2406 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2407 if (!value) goto failed;
2408 if (PyObject_SetAttrString(result, "names", value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002411 value = ast2obj_int(o->v.ImportFrom.level);
2412 if (!value) goto failed;
2413 if (PyObject_SetAttrString(result, "level", value) == -1)
2414 goto failed;
2415 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 break;
2417 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002418 result = PyType_GenericNew(Exec_type, NULL, NULL);
2419 if (!result) goto failed;
2420 value = ast2obj_expr(o->v.Exec.body);
2421 if (!value) goto failed;
2422 if (PyObject_SetAttrString(result, "body", value) == -1)
2423 goto failed;
2424 Py_DECREF(value);
2425 value = ast2obj_expr(o->v.Exec.globals);
2426 if (!value) goto failed;
2427 if (PyObject_SetAttrString(result, "globals", value) == -1)
2428 goto failed;
2429 Py_DECREF(value);
2430 value = ast2obj_expr(o->v.Exec.locals);
2431 if (!value) goto failed;
2432 if (PyObject_SetAttrString(result, "locals", value) == -1)
2433 goto failed;
2434 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 break;
2436 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002437 result = PyType_GenericNew(Global_type, NULL, NULL);
2438 if (!result) goto failed;
2439 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2440 if (!value) goto failed;
2441 if (PyObject_SetAttrString(result, "names", value) == -1)
2442 goto failed;
2443 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 break;
2445 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002446 result = PyType_GenericNew(Expr_type, NULL, NULL);
2447 if (!result) goto failed;
2448 value = ast2obj_expr(o->v.Expr.value);
2449 if (!value) goto failed;
2450 if (PyObject_SetAttrString(result, "value", value) == -1)
2451 goto failed;
2452 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 break;
2454 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002455 result = PyType_GenericNew(Pass_type, NULL, NULL);
2456 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 break;
2458 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002459 result = PyType_GenericNew(Break_type, NULL, NULL);
2460 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 break;
2462 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002463 result = PyType_GenericNew(Continue_type, NULL, NULL);
2464 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 break;
2466 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002467 value = ast2obj_int(o->lineno);
2468 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002469 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2470 goto failed;
2471 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002472 value = ast2obj_int(o->col_offset);
2473 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002474 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2475 goto failed;
2476 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002477 return result;
2478failed:
2479 Py_XDECREF(value);
2480 Py_XDECREF(result);
2481 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482}
2483
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002484PyObject*
2485ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002487 expr_ty o = (expr_ty)_o;
2488 PyObject *result = NULL, *value = NULL;
2489 if (!o) {
2490 Py_INCREF(Py_None);
2491 return Py_None;
2492 }
2493
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 switch (o->kind) {
2495 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002496 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2497 if (!result) goto failed;
2498 value = ast2obj_boolop(o->v.BoolOp.op);
2499 if (!value) goto failed;
2500 if (PyObject_SetAttrString(result, "op", value) == -1)
2501 goto failed;
2502 Py_DECREF(value);
2503 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2504 if (!value) goto failed;
2505 if (PyObject_SetAttrString(result, "values", value) == -1)
2506 goto failed;
2507 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 break;
2509 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002510 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2511 if (!result) goto failed;
2512 value = ast2obj_expr(o->v.BinOp.left);
2513 if (!value) goto failed;
2514 if (PyObject_SetAttrString(result, "left", value) == -1)
2515 goto failed;
2516 Py_DECREF(value);
2517 value = ast2obj_operator(o->v.BinOp.op);
2518 if (!value) goto failed;
2519 if (PyObject_SetAttrString(result, "op", value) == -1)
2520 goto failed;
2521 Py_DECREF(value);
2522 value = ast2obj_expr(o->v.BinOp.right);
2523 if (!value) goto failed;
2524 if (PyObject_SetAttrString(result, "right", value) == -1)
2525 goto failed;
2526 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 break;
2528 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002529 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2530 if (!result) goto failed;
2531 value = ast2obj_unaryop(o->v.UnaryOp.op);
2532 if (!value) goto failed;
2533 if (PyObject_SetAttrString(result, "op", value) == -1)
2534 goto failed;
2535 Py_DECREF(value);
2536 value = ast2obj_expr(o->v.UnaryOp.operand);
2537 if (!value) goto failed;
2538 if (PyObject_SetAttrString(result, "operand", value) == -1)
2539 goto failed;
2540 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 break;
2542 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002543 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2544 if (!result) goto failed;
2545 value = ast2obj_arguments(o->v.Lambda.args);
2546 if (!value) goto failed;
2547 if (PyObject_SetAttrString(result, "args", value) == -1)
2548 goto failed;
2549 Py_DECREF(value);
2550 value = ast2obj_expr(o->v.Lambda.body);
2551 if (!value) goto failed;
2552 if (PyObject_SetAttrString(result, "body", value) == -1)
2553 goto failed;
2554 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002556 case IfExp_kind:
2557 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2558 if (!result) goto failed;
2559 value = ast2obj_expr(o->v.IfExp.test);
2560 if (!value) goto failed;
2561 if (PyObject_SetAttrString(result, "test", value) == -1)
2562 goto failed;
2563 Py_DECREF(value);
2564 value = ast2obj_expr(o->v.IfExp.body);
2565 if (!value) goto failed;
2566 if (PyObject_SetAttrString(result, "body", value) == -1)
2567 goto failed;
2568 Py_DECREF(value);
2569 value = ast2obj_expr(o->v.IfExp.orelse);
2570 if (!value) goto failed;
2571 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2572 goto failed;
2573 Py_DECREF(value);
2574 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002576 result = PyType_GenericNew(Dict_type, NULL, NULL);
2577 if (!result) goto failed;
2578 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2579 if (!value) goto failed;
2580 if (PyObject_SetAttrString(result, "keys", value) == -1)
2581 goto failed;
2582 Py_DECREF(value);
2583 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2584 if (!value) goto failed;
2585 if (PyObject_SetAttrString(result, "values", value) == -1)
2586 goto failed;
2587 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002589 case Set_kind:
2590 result = PyType_GenericNew(Set_type, NULL, NULL);
2591 if (!result) goto failed;
2592 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2593 if (!value) goto failed;
2594 if (PyObject_SetAttrString(result, "elts", value) == -1)
2595 goto failed;
2596 Py_DECREF(value);
2597 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002599 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2600 if (!result) goto failed;
2601 value = ast2obj_expr(o->v.ListComp.elt);
2602 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "elt", value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 value = ast2obj_list(o->v.ListComp.generators,
2607 ast2obj_comprehension);
2608 if (!value) goto failed;
2609 if (PyObject_SetAttrString(result, "generators", value) == -1)
2610 goto failed;
2611 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 break;
2613 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002614 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2615 if (!result) goto failed;
2616 value = ast2obj_expr(o->v.GeneratorExp.elt);
2617 if (!value) goto failed;
2618 if (PyObject_SetAttrString(result, "elt", value) == -1)
2619 goto failed;
2620 Py_DECREF(value);
2621 value = ast2obj_list(o->v.GeneratorExp.generators,
2622 ast2obj_comprehension);
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "generators", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 break;
2628 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002629 result = PyType_GenericNew(Yield_type, NULL, NULL);
2630 if (!result) goto failed;
2631 value = ast2obj_expr(o->v.Yield.value);
2632 if (!value) goto failed;
2633 if (PyObject_SetAttrString(result, "value", value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 break;
2637 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002638 result = PyType_GenericNew(Compare_type, NULL, NULL);
2639 if (!result) goto failed;
2640 value = ast2obj_expr(o->v.Compare.left);
2641 if (!value) goto failed;
2642 if (PyObject_SetAttrString(result, "left", value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002645 {
2646 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2647 value = PyList_New(n);
2648 if (!value) goto failed;
2649 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002650 PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002651 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002652 if (!value) goto failed;
2653 if (PyObject_SetAttrString(result, "ops", value) == -1)
2654 goto failed;
2655 Py_DECREF(value);
2656 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2657 if (!value) goto failed;
2658 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 break;
2662 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002663 result = PyType_GenericNew(Call_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_expr(o->v.Call.func);
2666 if (!value) goto failed;
2667 if (PyObject_SetAttrString(result, "func", value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
2670 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2671 if (!value) goto failed;
2672 if (PyObject_SetAttrString(result, "args", value) == -1)
2673 goto failed;
2674 Py_DECREF(value);
2675 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2676 if (!value) goto failed;
2677 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2678 goto failed;
2679 Py_DECREF(value);
2680 value = ast2obj_expr(o->v.Call.starargs);
2681 if (!value) goto failed;
2682 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 value = ast2obj_expr(o->v.Call.kwargs);
2686 if (!value) goto failed;
2687 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 break;
2691 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002692 result = PyType_GenericNew(Repr_type, NULL, NULL);
2693 if (!result) goto failed;
2694 value = ast2obj_expr(o->v.Repr.value);
2695 if (!value) goto failed;
2696 if (PyObject_SetAttrString(result, "value", value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 break;
2700 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002701 result = PyType_GenericNew(Num_type, NULL, NULL);
2702 if (!result) goto failed;
2703 value = ast2obj_object(o->v.Num.n);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "n", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 break;
2709 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002710 result = PyType_GenericNew(Str_type, NULL, NULL);
2711 if (!result) goto failed;
2712 value = ast2obj_string(o->v.Str.s);
2713 if (!value) goto failed;
2714 if (PyObject_SetAttrString(result, "s", value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 break;
2718 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002719 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2720 if (!result) goto failed;
2721 value = ast2obj_expr(o->v.Attribute.value);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "value", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
2726 value = ast2obj_identifier(o->v.Attribute.attr);
2727 if (!value) goto failed;
2728 if (PyObject_SetAttrString(result, "attr", value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
2731 value = ast2obj_expr_context(o->v.Attribute.ctx);
2732 if (!value) goto failed;
2733 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 break;
2737 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002738 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2739 if (!result) goto failed;
2740 value = ast2obj_expr(o->v.Subscript.value);
2741 if (!value) goto failed;
2742 if (PyObject_SetAttrString(result, "value", value) == -1)
2743 goto failed;
2744 Py_DECREF(value);
2745 value = ast2obj_slice(o->v.Subscript.slice);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "slice", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
2750 value = ast2obj_expr_context(o->v.Subscript.ctx);
2751 if (!value) goto failed;
2752 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 break;
2756 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002757 result = PyType_GenericNew(Name_type, NULL, NULL);
2758 if (!result) goto failed;
2759 value = ast2obj_identifier(o->v.Name.id);
2760 if (!value) goto failed;
2761 if (PyObject_SetAttrString(result, "id", value) == -1)
2762 goto failed;
2763 Py_DECREF(value);
2764 value = ast2obj_expr_context(o->v.Name.ctx);
2765 if (!value) goto failed;
2766 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2767 goto failed;
2768 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 break;
2770 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002771 result = PyType_GenericNew(List_type, NULL, NULL);
2772 if (!result) goto failed;
2773 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2774 if (!value) goto failed;
2775 if (PyObject_SetAttrString(result, "elts", value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 value = ast2obj_expr_context(o->v.List.ctx);
2779 if (!value) goto failed;
2780 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2781 goto failed;
2782 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 break;
2784 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002785 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2786 if (!result) goto failed;
2787 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2788 if (!value) goto failed;
2789 if (PyObject_SetAttrString(result, "elts", value) == -1)
2790 goto failed;
2791 Py_DECREF(value);
2792 value = ast2obj_expr_context(o->v.Tuple.ctx);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 break;
2798 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002799 value = ast2obj_int(o->lineno);
2800 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002801 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2802 goto failed;
2803 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002804 value = ast2obj_int(o->col_offset);
2805 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002806 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2807 goto failed;
2808 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002809 return result;
2810failed:
2811 Py_XDECREF(value);
2812 Py_XDECREF(result);
2813 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814}
2815
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002816PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002818 switch(o) {
2819 case Load:
2820 Py_INCREF(Load_singleton);
2821 return Load_singleton;
2822 case Store:
2823 Py_INCREF(Store_singleton);
2824 return Store_singleton;
2825 case Del:
2826 Py_INCREF(Del_singleton);
2827 return Del_singleton;
2828 case AugLoad:
2829 Py_INCREF(AugLoad_singleton);
2830 return AugLoad_singleton;
2831 case AugStore:
2832 Py_INCREF(AugStore_singleton);
2833 return AugStore_singleton;
2834 case Param:
2835 Py_INCREF(Param_singleton);
2836 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002837 default:
2838 /* should never happen, but just in case ... */
2839 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2840 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002843PyObject*
2844ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002846 slice_ty o = (slice_ty)_o;
2847 PyObject *result = NULL, *value = NULL;
2848 if (!o) {
2849 Py_INCREF(Py_None);
2850 return Py_None;
2851 }
2852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 switch (o->kind) {
2854 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002855 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2856 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002857 break;
2858 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002859 result = PyType_GenericNew(Slice_type, NULL, NULL);
2860 if (!result) goto failed;
2861 value = ast2obj_expr(o->v.Slice.lower);
2862 if (!value) goto failed;
2863 if (PyObject_SetAttrString(result, "lower", value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 value = ast2obj_expr(o->v.Slice.upper);
2867 if (!value) goto failed;
2868 if (PyObject_SetAttrString(result, "upper", value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
2871 value = ast2obj_expr(o->v.Slice.step);
2872 if (!value) goto failed;
2873 if (PyObject_SetAttrString(result, "step", value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002876 break;
2877 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002878 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2879 if (!result) goto failed;
2880 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2881 if (!value) goto failed;
2882 if (PyObject_SetAttrString(result, "dims", value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 break;
2886 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002887 result = PyType_GenericNew(Index_type, NULL, NULL);
2888 if (!result) goto failed;
2889 value = ast2obj_expr(o->v.Index.value);
2890 if (!value) goto failed;
2891 if (PyObject_SetAttrString(result, "value", value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 break;
2895 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002896 return result;
2897failed:
2898 Py_XDECREF(value);
2899 Py_XDECREF(result);
2900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002901}
2902
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002903PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002905 switch(o) {
2906 case And:
2907 Py_INCREF(And_singleton);
2908 return And_singleton;
2909 case Or:
2910 Py_INCREF(Or_singleton);
2911 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002912 default:
2913 /* should never happen, but just in case ... */
2914 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002917}
2918PyObject* ast2obj_operator(operator_ty o)
2919{
2920 switch(o) {
2921 case Add:
2922 Py_INCREF(Add_singleton);
2923 return Add_singleton;
2924 case Sub:
2925 Py_INCREF(Sub_singleton);
2926 return Sub_singleton;
2927 case Mult:
2928 Py_INCREF(Mult_singleton);
2929 return Mult_singleton;
2930 case Div:
2931 Py_INCREF(Div_singleton);
2932 return Div_singleton;
2933 case Mod:
2934 Py_INCREF(Mod_singleton);
2935 return Mod_singleton;
2936 case Pow:
2937 Py_INCREF(Pow_singleton);
2938 return Pow_singleton;
2939 case LShift:
2940 Py_INCREF(LShift_singleton);
2941 return LShift_singleton;
2942 case RShift:
2943 Py_INCREF(RShift_singleton);
2944 return RShift_singleton;
2945 case BitOr:
2946 Py_INCREF(BitOr_singleton);
2947 return BitOr_singleton;
2948 case BitXor:
2949 Py_INCREF(BitXor_singleton);
2950 return BitXor_singleton;
2951 case BitAnd:
2952 Py_INCREF(BitAnd_singleton);
2953 return BitAnd_singleton;
2954 case FloorDiv:
2955 Py_INCREF(FloorDiv_singleton);
2956 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002957 default:
2958 /* should never happen, but just in case ... */
2959 PyErr_Format(PyExc_SystemError, "unknown operator found");
2960 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002961 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002962}
2963PyObject* ast2obj_unaryop(unaryop_ty o)
2964{
2965 switch(o) {
2966 case Invert:
2967 Py_INCREF(Invert_singleton);
2968 return Invert_singleton;
2969 case Not:
2970 Py_INCREF(Not_singleton);
2971 return Not_singleton;
2972 case UAdd:
2973 Py_INCREF(UAdd_singleton);
2974 return UAdd_singleton;
2975 case USub:
2976 Py_INCREF(USub_singleton);
2977 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002978 default:
2979 /* should never happen, but just in case ... */
2980 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2981 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002982 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002983}
2984PyObject* ast2obj_cmpop(cmpop_ty o)
2985{
2986 switch(o) {
2987 case Eq:
2988 Py_INCREF(Eq_singleton);
2989 return Eq_singleton;
2990 case NotEq:
2991 Py_INCREF(NotEq_singleton);
2992 return NotEq_singleton;
2993 case Lt:
2994 Py_INCREF(Lt_singleton);
2995 return Lt_singleton;
2996 case LtE:
2997 Py_INCREF(LtE_singleton);
2998 return LtE_singleton;
2999 case Gt:
3000 Py_INCREF(Gt_singleton);
3001 return Gt_singleton;
3002 case GtE:
3003 Py_INCREF(GtE_singleton);
3004 return GtE_singleton;
3005 case Is:
3006 Py_INCREF(Is_singleton);
3007 return Is_singleton;
3008 case IsNot:
3009 Py_INCREF(IsNot_singleton);
3010 return IsNot_singleton;
3011 case In:
3012 Py_INCREF(In_singleton);
3013 return In_singleton;
3014 case NotIn:
3015 Py_INCREF(NotIn_singleton);
3016 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003017 default:
3018 /* should never happen, but just in case ... */
3019 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3020 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003021 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003022}
3023PyObject*
3024ast2obj_comprehension(void* _o)
3025{
3026 comprehension_ty o = (comprehension_ty)_o;
3027 PyObject *result = NULL, *value = NULL;
3028 if (!o) {
3029 Py_INCREF(Py_None);
3030 return Py_None;
3031 }
3032
3033 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3034 if (!result) return NULL;
3035 value = ast2obj_expr(o->target);
3036 if (!value) goto failed;
3037 if (PyObject_SetAttrString(result, "target", value) == -1)
3038 goto failed;
3039 Py_DECREF(value);
3040 value = ast2obj_expr(o->iter);
3041 if (!value) goto failed;
3042 if (PyObject_SetAttrString(result, "iter", value) == -1)
3043 goto failed;
3044 Py_DECREF(value);
3045 value = ast2obj_list(o->ifs, ast2obj_expr);
3046 if (!value) goto failed;
3047 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3048 goto failed;
3049 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003050 return result;
3051failed:
3052 Py_XDECREF(value);
3053 Py_XDECREF(result);
3054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055}
3056
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003057PyObject*
3058ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003060 excepthandler_ty o = (excepthandler_ty)_o;
3061 PyObject *result = NULL, *value = NULL;
3062 if (!o) {
3063 Py_INCREF(Py_None);
3064 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003066
Georg Brandla48f3ab2008-03-30 06:40:17 +00003067 switch (o->kind) {
3068 case ExceptHandler_kind:
3069 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3070 if (!result) goto failed;
3071 value = ast2obj_expr(o->v.ExceptHandler.type);
3072 if (!value) goto failed;
3073 if (PyObject_SetAttrString(result, "type", value) == -1)
3074 goto failed;
3075 Py_DECREF(value);
3076 value = ast2obj_expr(o->v.ExceptHandler.name);
3077 if (!value) goto failed;
3078 if (PyObject_SetAttrString(result, "name", value) == -1)
3079 goto failed;
3080 Py_DECREF(value);
3081 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3082 if (!value) goto failed;
3083 if (PyObject_SetAttrString(result, "body", value) == -1)
3084 goto failed;
3085 Py_DECREF(value);
3086 break;
3087 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003088 value = ast2obj_int(o->lineno);
3089 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003090 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_int(o->col_offset);
3094 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003095 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003096 goto failed;
3097 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003098 return result;
3099failed:
3100 Py_XDECREF(value);
3101 Py_XDECREF(result);
3102 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103}
3104
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003105PyObject*
3106ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003108 arguments_ty o = (arguments_ty)_o;
3109 PyObject *result = NULL, *value = NULL;
3110 if (!o) {
3111 Py_INCREF(Py_None);
3112 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003114
3115 result = PyType_GenericNew(arguments_type, NULL, NULL);
3116 if (!result) return NULL;
3117 value = ast2obj_list(o->args, ast2obj_expr);
3118 if (!value) goto failed;
3119 if (PyObject_SetAttrString(result, "args", value) == -1)
3120 goto failed;
3121 Py_DECREF(value);
3122 value = ast2obj_identifier(o->vararg);
3123 if (!value) goto failed;
3124 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3125 goto failed;
3126 Py_DECREF(value);
3127 value = ast2obj_identifier(o->kwarg);
3128 if (!value) goto failed;
3129 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3130 goto failed;
3131 Py_DECREF(value);
3132 value = ast2obj_list(o->defaults, ast2obj_expr);
3133 if (!value) goto failed;
3134 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3135 goto failed;
3136 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003137 return result;
3138failed:
3139 Py_XDECREF(value);
3140 Py_XDECREF(result);
3141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142}
3143
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003144PyObject*
3145ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003147 keyword_ty o = (keyword_ty)_o;
3148 PyObject *result = NULL, *value = NULL;
3149 if (!o) {
3150 Py_INCREF(Py_None);
3151 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003153
3154 result = PyType_GenericNew(keyword_type, NULL, NULL);
3155 if (!result) return NULL;
3156 value = ast2obj_identifier(o->arg);
3157 if (!value) goto failed;
3158 if (PyObject_SetAttrString(result, "arg", value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 value = ast2obj_expr(o->value);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "value", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003166 return result;
3167failed:
3168 Py_XDECREF(value);
3169 Py_XDECREF(result);
3170 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171}
3172
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003173PyObject*
3174ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003176 alias_ty o = (alias_ty)_o;
3177 PyObject *result = NULL, *value = NULL;
3178 if (!o) {
3179 Py_INCREF(Py_None);
3180 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003182
3183 result = PyType_GenericNew(alias_type, NULL, NULL);
3184 if (!result) return NULL;
3185 value = ast2obj_identifier(o->name);
3186 if (!value) goto failed;
3187 if (PyObject_SetAttrString(result, "name", value) == -1)
3188 goto failed;
3189 Py_DECREF(value);
3190 value = ast2obj_identifier(o->asname);
3191 if (!value) goto failed;
3192 if (PyObject_SetAttrString(result, "asname", value) == -1)
3193 goto failed;
3194 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003195 return result;
3196failed:
3197 Py_XDECREF(value);
3198 Py_XDECREF(result);
3199 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200}
3201
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
Georg Brandlfc8eef32008-03-28 12:11:56 +00003203int
3204obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3205{
3206 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003207 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003208
3209
3210 if (obj == Py_None) {
3211 *out = NULL;
3212 return 0;
3213 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003214 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3215 if (isinstance == -1) {
3216 return 1;
3217 }
3218 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003219 asdl_seq* body;
3220
3221 if (PyObject_HasAttrString(obj, "body")) {
3222 int res;
3223 Py_ssize_t len;
3224 Py_ssize_t i;
3225 tmp = PyObject_GetAttrString(obj, "body");
3226 if (tmp == NULL) goto failed;
3227 if (!PyList_Check(tmp)) {
3228 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3229 goto failed;
3230 }
3231 len = PyList_GET_SIZE(tmp);
3232 body = asdl_seq_new(len, arena);
3233 if (body == NULL) goto failed;
3234 for (i = 0; i < len; i++) {
3235 stmt_ty value;
3236 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3237 if (res != 0) goto failed;
3238 asdl_seq_SET(body, i, value);
3239 }
3240 Py_XDECREF(tmp);
3241 tmp = NULL;
3242 } else {
3243 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3244 return 1;
3245 }
3246 *out = Module(body, arena);
3247 if (*out == NULL) goto failed;
3248 return 0;
3249 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003250 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3251 if (isinstance == -1) {
3252 return 1;
3253 }
3254 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003255 asdl_seq* body;
3256
3257 if (PyObject_HasAttrString(obj, "body")) {
3258 int res;
3259 Py_ssize_t len;
3260 Py_ssize_t i;
3261 tmp = PyObject_GetAttrString(obj, "body");
3262 if (tmp == NULL) goto failed;
3263 if (!PyList_Check(tmp)) {
3264 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3265 goto failed;
3266 }
3267 len = PyList_GET_SIZE(tmp);
3268 body = asdl_seq_new(len, arena);
3269 if (body == NULL) goto failed;
3270 for (i = 0; i < len; i++) {
3271 stmt_ty value;
3272 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3273 if (res != 0) goto failed;
3274 asdl_seq_SET(body, i, value);
3275 }
3276 Py_XDECREF(tmp);
3277 tmp = NULL;
3278 } else {
3279 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3280 return 1;
3281 }
3282 *out = Interactive(body, arena);
3283 if (*out == NULL) goto failed;
3284 return 0;
3285 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003286 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3287 if (isinstance == -1) {
3288 return 1;
3289 }
3290 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003291 expr_ty body;
3292
3293 if (PyObject_HasAttrString(obj, "body")) {
3294 int res;
3295 tmp = PyObject_GetAttrString(obj, "body");
3296 if (tmp == NULL) goto failed;
3297 res = obj2ast_expr(tmp, &body, arena);
3298 if (res != 0) goto failed;
3299 Py_XDECREF(tmp);
3300 tmp = NULL;
3301 } else {
3302 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3303 return 1;
3304 }
3305 *out = Expression(body, arena);
3306 if (*out == NULL) goto failed;
3307 return 0;
3308 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003309 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3310 if (isinstance == -1) {
3311 return 1;
3312 }
3313 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003314 asdl_seq* body;
3315
3316 if (PyObject_HasAttrString(obj, "body")) {
3317 int res;
3318 Py_ssize_t len;
3319 Py_ssize_t i;
3320 tmp = PyObject_GetAttrString(obj, "body");
3321 if (tmp == NULL) goto failed;
3322 if (!PyList_Check(tmp)) {
3323 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3324 goto failed;
3325 }
3326 len = PyList_GET_SIZE(tmp);
3327 body = asdl_seq_new(len, arena);
3328 if (body == NULL) goto failed;
3329 for (i = 0; i < len; i++) {
3330 stmt_ty value;
3331 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3332 if (res != 0) goto failed;
3333 asdl_seq_SET(body, i, value);
3334 }
3335 Py_XDECREF(tmp);
3336 tmp = NULL;
3337 } else {
3338 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3339 return 1;
3340 }
3341 *out = Suite(body, arena);
3342 if (*out == NULL) goto failed;
3343 return 0;
3344 }
3345
3346 tmp = PyObject_Repr(obj);
3347 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003348 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00003349failed:
3350 Py_XDECREF(tmp);
3351 return 1;
3352}
3353
3354int
3355obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3356{
3357 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003358 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003359
3360 int lineno;
3361 int col_offset;
3362
3363 if (obj == Py_None) {
3364 *out = NULL;
3365 return 0;
3366 }
3367 if (PyObject_HasAttrString(obj, "lineno")) {
3368 int res;
3369 tmp = PyObject_GetAttrString(obj, "lineno");
3370 if (tmp == NULL) goto failed;
3371 res = obj2ast_int(tmp, &lineno, arena);
3372 if (res != 0) goto failed;
3373 Py_XDECREF(tmp);
3374 tmp = NULL;
3375 } else {
3376 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3377 return 1;
3378 }
3379 if (PyObject_HasAttrString(obj, "col_offset")) {
3380 int res;
3381 tmp = PyObject_GetAttrString(obj, "col_offset");
3382 if (tmp == NULL) goto failed;
3383 res = obj2ast_int(tmp, &col_offset, arena);
3384 if (res != 0) goto failed;
3385 Py_XDECREF(tmp);
3386 tmp = NULL;
3387 } else {
3388 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3389 return 1;
3390 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003391 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3392 if (isinstance == -1) {
3393 return 1;
3394 }
3395 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003396 identifier name;
3397 arguments_ty args;
3398 asdl_seq* body;
3399 asdl_seq* decorator_list;
3400
3401 if (PyObject_HasAttrString(obj, "name")) {
3402 int res;
3403 tmp = PyObject_GetAttrString(obj, "name");
3404 if (tmp == NULL) goto failed;
3405 res = obj2ast_identifier(tmp, &name, arena);
3406 if (res != 0) goto failed;
3407 Py_XDECREF(tmp);
3408 tmp = NULL;
3409 } else {
3410 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3411 return 1;
3412 }
3413 if (PyObject_HasAttrString(obj, "args")) {
3414 int res;
3415 tmp = PyObject_GetAttrString(obj, "args");
3416 if (tmp == NULL) goto failed;
3417 res = obj2ast_arguments(tmp, &args, arena);
3418 if (res != 0) goto failed;
3419 Py_XDECREF(tmp);
3420 tmp = NULL;
3421 } else {
3422 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3423 return 1;
3424 }
3425 if (PyObject_HasAttrString(obj, "body")) {
3426 int res;
3427 Py_ssize_t len;
3428 Py_ssize_t i;
3429 tmp = PyObject_GetAttrString(obj, "body");
3430 if (tmp == NULL) goto failed;
3431 if (!PyList_Check(tmp)) {
3432 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3433 goto failed;
3434 }
3435 len = PyList_GET_SIZE(tmp);
3436 body = asdl_seq_new(len, arena);
3437 if (body == NULL) goto failed;
3438 for (i = 0; i < len; i++) {
3439 stmt_ty value;
3440 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3441 if (res != 0) goto failed;
3442 asdl_seq_SET(body, i, value);
3443 }
3444 Py_XDECREF(tmp);
3445 tmp = NULL;
3446 } else {
3447 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3448 return 1;
3449 }
3450 if (PyObject_HasAttrString(obj, "decorator_list")) {
3451 int res;
3452 Py_ssize_t len;
3453 Py_ssize_t i;
3454 tmp = PyObject_GetAttrString(obj, "decorator_list");
3455 if (tmp == NULL) goto failed;
3456 if (!PyList_Check(tmp)) {
3457 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3458 goto failed;
3459 }
3460 len = PyList_GET_SIZE(tmp);
3461 decorator_list = asdl_seq_new(len, arena);
3462 if (decorator_list == NULL) goto failed;
3463 for (i = 0; i < len; i++) {
3464 expr_ty value;
3465 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3466 if (res != 0) goto failed;
3467 asdl_seq_SET(decorator_list, i, value);
3468 }
3469 Py_XDECREF(tmp);
3470 tmp = NULL;
3471 } else {
3472 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3473 return 1;
3474 }
3475 *out = FunctionDef(name, args, body, decorator_list, lineno,
3476 col_offset, arena);
3477 if (*out == NULL) goto failed;
3478 return 0;
3479 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003480 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3481 if (isinstance == -1) {
3482 return 1;
3483 }
3484 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003485 identifier name;
3486 asdl_seq* bases;
3487 asdl_seq* body;
3488 asdl_seq* decorator_list;
3489
3490 if (PyObject_HasAttrString(obj, "name")) {
3491 int res;
3492 tmp = PyObject_GetAttrString(obj, "name");
3493 if (tmp == NULL) goto failed;
3494 res = obj2ast_identifier(tmp, &name, arena);
3495 if (res != 0) goto failed;
3496 Py_XDECREF(tmp);
3497 tmp = NULL;
3498 } else {
3499 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3500 return 1;
3501 }
3502 if (PyObject_HasAttrString(obj, "bases")) {
3503 int res;
3504 Py_ssize_t len;
3505 Py_ssize_t i;
3506 tmp = PyObject_GetAttrString(obj, "bases");
3507 if (tmp == NULL) goto failed;
3508 if (!PyList_Check(tmp)) {
3509 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3510 goto failed;
3511 }
3512 len = PyList_GET_SIZE(tmp);
3513 bases = asdl_seq_new(len, arena);
3514 if (bases == NULL) goto failed;
3515 for (i = 0; i < len; i++) {
3516 expr_ty value;
3517 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3518 if (res != 0) goto failed;
3519 asdl_seq_SET(bases, i, value);
3520 }
3521 Py_XDECREF(tmp);
3522 tmp = NULL;
3523 } else {
3524 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3525 return 1;
3526 }
3527 if (PyObject_HasAttrString(obj, "body")) {
3528 int res;
3529 Py_ssize_t len;
3530 Py_ssize_t i;
3531 tmp = PyObject_GetAttrString(obj, "body");
3532 if (tmp == NULL) goto failed;
3533 if (!PyList_Check(tmp)) {
3534 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3535 goto failed;
3536 }
3537 len = PyList_GET_SIZE(tmp);
3538 body = asdl_seq_new(len, arena);
3539 if (body == NULL) goto failed;
3540 for (i = 0; i < len; i++) {
3541 stmt_ty value;
3542 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3543 if (res != 0) goto failed;
3544 asdl_seq_SET(body, i, value);
3545 }
3546 Py_XDECREF(tmp);
3547 tmp = NULL;
3548 } else {
3549 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3550 return 1;
3551 }
3552 if (PyObject_HasAttrString(obj, "decorator_list")) {
3553 int res;
3554 Py_ssize_t len;
3555 Py_ssize_t i;
3556 tmp = PyObject_GetAttrString(obj, "decorator_list");
3557 if (tmp == NULL) goto failed;
3558 if (!PyList_Check(tmp)) {
3559 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3560 goto failed;
3561 }
3562 len = PyList_GET_SIZE(tmp);
3563 decorator_list = asdl_seq_new(len, arena);
3564 if (decorator_list == NULL) goto failed;
3565 for (i = 0; i < len; i++) {
3566 expr_ty value;
3567 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3568 if (res != 0) goto failed;
3569 asdl_seq_SET(decorator_list, i, value);
3570 }
3571 Py_XDECREF(tmp);
3572 tmp = NULL;
3573 } else {
3574 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3575 return 1;
3576 }
3577 *out = ClassDef(name, bases, body, decorator_list, lineno,
3578 col_offset, arena);
3579 if (*out == NULL) goto failed;
3580 return 0;
3581 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003582 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3583 if (isinstance == -1) {
3584 return 1;
3585 }
3586 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003587 expr_ty value;
3588
3589 if (PyObject_HasAttrString(obj, "value")) {
3590 int res;
3591 tmp = PyObject_GetAttrString(obj, "value");
3592 if (tmp == NULL) goto failed;
3593 res = obj2ast_expr(tmp, &value, arena);
3594 if (res != 0) goto failed;
3595 Py_XDECREF(tmp);
3596 tmp = NULL;
3597 } else {
3598 value = NULL;
3599 }
3600 *out = Return(value, lineno, col_offset, arena);
3601 if (*out == NULL) goto failed;
3602 return 0;
3603 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003604 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3605 if (isinstance == -1) {
3606 return 1;
3607 }
3608 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003609 asdl_seq* targets;
3610
3611 if (PyObject_HasAttrString(obj, "targets")) {
3612 int res;
3613 Py_ssize_t len;
3614 Py_ssize_t i;
3615 tmp = PyObject_GetAttrString(obj, "targets");
3616 if (tmp == NULL) goto failed;
3617 if (!PyList_Check(tmp)) {
3618 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3619 goto failed;
3620 }
3621 len = PyList_GET_SIZE(tmp);
3622 targets = asdl_seq_new(len, arena);
3623 if (targets == NULL) goto failed;
3624 for (i = 0; i < len; i++) {
3625 expr_ty value;
3626 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3627 if (res != 0) goto failed;
3628 asdl_seq_SET(targets, i, value);
3629 }
3630 Py_XDECREF(tmp);
3631 tmp = NULL;
3632 } else {
3633 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3634 return 1;
3635 }
3636 *out = Delete(targets, lineno, col_offset, arena);
3637 if (*out == NULL) goto failed;
3638 return 0;
3639 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003640 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3641 if (isinstance == -1) {
3642 return 1;
3643 }
3644 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003645 asdl_seq* targets;
3646 expr_ty value;
3647
3648 if (PyObject_HasAttrString(obj, "targets")) {
3649 int res;
3650 Py_ssize_t len;
3651 Py_ssize_t i;
3652 tmp = PyObject_GetAttrString(obj, "targets");
3653 if (tmp == NULL) goto failed;
3654 if (!PyList_Check(tmp)) {
3655 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3656 goto failed;
3657 }
3658 len = PyList_GET_SIZE(tmp);
3659 targets = asdl_seq_new(len, arena);
3660 if (targets == NULL) goto failed;
3661 for (i = 0; i < len; i++) {
3662 expr_ty value;
3663 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3664 if (res != 0) goto failed;
3665 asdl_seq_SET(targets, i, value);
3666 }
3667 Py_XDECREF(tmp);
3668 tmp = NULL;
3669 } else {
3670 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3671 return 1;
3672 }
3673 if (PyObject_HasAttrString(obj, "value")) {
3674 int res;
3675 tmp = PyObject_GetAttrString(obj, "value");
3676 if (tmp == NULL) goto failed;
3677 res = obj2ast_expr(tmp, &value, arena);
3678 if (res != 0) goto failed;
3679 Py_XDECREF(tmp);
3680 tmp = NULL;
3681 } else {
3682 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3683 return 1;
3684 }
3685 *out = Assign(targets, value, lineno, col_offset, arena);
3686 if (*out == NULL) goto failed;
3687 return 0;
3688 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003689 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3690 if (isinstance == -1) {
3691 return 1;
3692 }
3693 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003694 expr_ty target;
3695 operator_ty op;
3696 expr_ty value;
3697
3698 if (PyObject_HasAttrString(obj, "target")) {
3699 int res;
3700 tmp = PyObject_GetAttrString(obj, "target");
3701 if (tmp == NULL) goto failed;
3702 res = obj2ast_expr(tmp, &target, arena);
3703 if (res != 0) goto failed;
3704 Py_XDECREF(tmp);
3705 tmp = NULL;
3706 } else {
3707 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3708 return 1;
3709 }
3710 if (PyObject_HasAttrString(obj, "op")) {
3711 int res;
3712 tmp = PyObject_GetAttrString(obj, "op");
3713 if (tmp == NULL) goto failed;
3714 res = obj2ast_operator(tmp, &op, arena);
3715 if (res != 0) goto failed;
3716 Py_XDECREF(tmp);
3717 tmp = NULL;
3718 } else {
3719 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3720 return 1;
3721 }
3722 if (PyObject_HasAttrString(obj, "value")) {
3723 int res;
3724 tmp = PyObject_GetAttrString(obj, "value");
3725 if (tmp == NULL) goto failed;
3726 res = obj2ast_expr(tmp, &value, arena);
3727 if (res != 0) goto failed;
3728 Py_XDECREF(tmp);
3729 tmp = NULL;
3730 } else {
3731 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3732 return 1;
3733 }
3734 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3735 if (*out == NULL) goto failed;
3736 return 0;
3737 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003738 isinstance = PyObject_IsInstance(obj, (PyObject*)Print_type);
3739 if (isinstance == -1) {
3740 return 1;
3741 }
3742 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003743 expr_ty dest;
3744 asdl_seq* values;
3745 bool nl;
3746
3747 if (PyObject_HasAttrString(obj, "dest")) {
3748 int res;
3749 tmp = PyObject_GetAttrString(obj, "dest");
3750 if (tmp == NULL) goto failed;
3751 res = obj2ast_expr(tmp, &dest, arena);
3752 if (res != 0) goto failed;
3753 Py_XDECREF(tmp);
3754 tmp = NULL;
3755 } else {
3756 dest = NULL;
3757 }
3758 if (PyObject_HasAttrString(obj, "values")) {
3759 int res;
3760 Py_ssize_t len;
3761 Py_ssize_t i;
3762 tmp = PyObject_GetAttrString(obj, "values");
3763 if (tmp == NULL) goto failed;
3764 if (!PyList_Check(tmp)) {
3765 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3766 goto failed;
3767 }
3768 len = PyList_GET_SIZE(tmp);
3769 values = asdl_seq_new(len, arena);
3770 if (values == NULL) goto failed;
3771 for (i = 0; i < len; i++) {
3772 expr_ty value;
3773 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3774 if (res != 0) goto failed;
3775 asdl_seq_SET(values, i, value);
3776 }
3777 Py_XDECREF(tmp);
3778 tmp = NULL;
3779 } else {
3780 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3781 return 1;
3782 }
3783 if (PyObject_HasAttrString(obj, "nl")) {
3784 int res;
3785 tmp = PyObject_GetAttrString(obj, "nl");
3786 if (tmp == NULL) goto failed;
3787 res = obj2ast_bool(tmp, &nl, arena);
3788 if (res != 0) goto failed;
3789 Py_XDECREF(tmp);
3790 tmp = NULL;
3791 } else {
3792 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3793 return 1;
3794 }
3795 *out = Print(dest, values, nl, lineno, col_offset, arena);
3796 if (*out == NULL) goto failed;
3797 return 0;
3798 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003799 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3800 if (isinstance == -1) {
3801 return 1;
3802 }
3803 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003804 expr_ty target;
3805 expr_ty iter;
3806 asdl_seq* body;
3807 asdl_seq* orelse;
3808
3809 if (PyObject_HasAttrString(obj, "target")) {
3810 int res;
3811 tmp = PyObject_GetAttrString(obj, "target");
3812 if (tmp == NULL) goto failed;
3813 res = obj2ast_expr(tmp, &target, arena);
3814 if (res != 0) goto failed;
3815 Py_XDECREF(tmp);
3816 tmp = NULL;
3817 } else {
3818 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3819 return 1;
3820 }
3821 if (PyObject_HasAttrString(obj, "iter")) {
3822 int res;
3823 tmp = PyObject_GetAttrString(obj, "iter");
3824 if (tmp == NULL) goto failed;
3825 res = obj2ast_expr(tmp, &iter, arena);
3826 if (res != 0) goto failed;
3827 Py_XDECREF(tmp);
3828 tmp = NULL;
3829 } else {
3830 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3831 return 1;
3832 }
3833 if (PyObject_HasAttrString(obj, "body")) {
3834 int res;
3835 Py_ssize_t len;
3836 Py_ssize_t i;
3837 tmp = PyObject_GetAttrString(obj, "body");
3838 if (tmp == NULL) goto failed;
3839 if (!PyList_Check(tmp)) {
3840 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3841 goto failed;
3842 }
3843 len = PyList_GET_SIZE(tmp);
3844 body = asdl_seq_new(len, arena);
3845 if (body == NULL) goto failed;
3846 for (i = 0; i < len; i++) {
3847 stmt_ty value;
3848 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3849 if (res != 0) goto failed;
3850 asdl_seq_SET(body, i, value);
3851 }
3852 Py_XDECREF(tmp);
3853 tmp = NULL;
3854 } else {
3855 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3856 return 1;
3857 }
3858 if (PyObject_HasAttrString(obj, "orelse")) {
3859 int res;
3860 Py_ssize_t len;
3861 Py_ssize_t i;
3862 tmp = PyObject_GetAttrString(obj, "orelse");
3863 if (tmp == NULL) goto failed;
3864 if (!PyList_Check(tmp)) {
3865 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3866 goto failed;
3867 }
3868 len = PyList_GET_SIZE(tmp);
3869 orelse = asdl_seq_new(len, arena);
3870 if (orelse == NULL) goto failed;
3871 for (i = 0; i < len; i++) {
3872 stmt_ty value;
3873 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3874 if (res != 0) goto failed;
3875 asdl_seq_SET(orelse, i, value);
3876 }
3877 Py_XDECREF(tmp);
3878 tmp = NULL;
3879 } else {
3880 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3881 return 1;
3882 }
3883 *out = For(target, iter, body, orelse, lineno, col_offset,
3884 arena);
3885 if (*out == NULL) goto failed;
3886 return 0;
3887 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003888 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
3889 if (isinstance == -1) {
3890 return 1;
3891 }
3892 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003893 expr_ty test;
3894 asdl_seq* body;
3895 asdl_seq* orelse;
3896
3897 if (PyObject_HasAttrString(obj, "test")) {
3898 int res;
3899 tmp = PyObject_GetAttrString(obj, "test");
3900 if (tmp == NULL) goto failed;
3901 res = obj2ast_expr(tmp, &test, arena);
3902 if (res != 0) goto failed;
3903 Py_XDECREF(tmp);
3904 tmp = NULL;
3905 } else {
3906 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3907 return 1;
3908 }
3909 if (PyObject_HasAttrString(obj, "body")) {
3910 int res;
3911 Py_ssize_t len;
3912 Py_ssize_t i;
3913 tmp = PyObject_GetAttrString(obj, "body");
3914 if (tmp == NULL) goto failed;
3915 if (!PyList_Check(tmp)) {
3916 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3917 goto failed;
3918 }
3919 len = PyList_GET_SIZE(tmp);
3920 body = asdl_seq_new(len, arena);
3921 if (body == NULL) goto failed;
3922 for (i = 0; i < len; i++) {
3923 stmt_ty value;
3924 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3925 if (res != 0) goto failed;
3926 asdl_seq_SET(body, i, value);
3927 }
3928 Py_XDECREF(tmp);
3929 tmp = NULL;
3930 } else {
3931 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3932 return 1;
3933 }
3934 if (PyObject_HasAttrString(obj, "orelse")) {
3935 int res;
3936 Py_ssize_t len;
3937 Py_ssize_t i;
3938 tmp = PyObject_GetAttrString(obj, "orelse");
3939 if (tmp == NULL) goto failed;
3940 if (!PyList_Check(tmp)) {
3941 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3942 goto failed;
3943 }
3944 len = PyList_GET_SIZE(tmp);
3945 orelse = asdl_seq_new(len, arena);
3946 if (orelse == NULL) goto failed;
3947 for (i = 0; i < len; i++) {
3948 stmt_ty value;
3949 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3950 if (res != 0) goto failed;
3951 asdl_seq_SET(orelse, i, value);
3952 }
3953 Py_XDECREF(tmp);
3954 tmp = NULL;
3955 } else {
3956 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3957 return 1;
3958 }
3959 *out = While(test, body, orelse, lineno, col_offset, arena);
3960 if (*out == NULL) goto failed;
3961 return 0;
3962 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003963 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
3964 if (isinstance == -1) {
3965 return 1;
3966 }
3967 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003968 expr_ty test;
3969 asdl_seq* body;
3970 asdl_seq* orelse;
3971
3972 if (PyObject_HasAttrString(obj, "test")) {
3973 int res;
3974 tmp = PyObject_GetAttrString(obj, "test");
3975 if (tmp == NULL) goto failed;
3976 res = obj2ast_expr(tmp, &test, arena);
3977 if (res != 0) goto failed;
3978 Py_XDECREF(tmp);
3979 tmp = NULL;
3980 } else {
3981 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3982 return 1;
3983 }
3984 if (PyObject_HasAttrString(obj, "body")) {
3985 int res;
3986 Py_ssize_t len;
3987 Py_ssize_t i;
3988 tmp = PyObject_GetAttrString(obj, "body");
3989 if (tmp == NULL) goto failed;
3990 if (!PyList_Check(tmp)) {
3991 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3992 goto failed;
3993 }
3994 len = PyList_GET_SIZE(tmp);
3995 body = asdl_seq_new(len, arena);
3996 if (body == NULL) goto failed;
3997 for (i = 0; i < len; i++) {
3998 stmt_ty value;
3999 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4000 if (res != 0) goto failed;
4001 asdl_seq_SET(body, i, value);
4002 }
4003 Py_XDECREF(tmp);
4004 tmp = NULL;
4005 } else {
4006 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4007 return 1;
4008 }
4009 if (PyObject_HasAttrString(obj, "orelse")) {
4010 int res;
4011 Py_ssize_t len;
4012 Py_ssize_t i;
4013 tmp = PyObject_GetAttrString(obj, "orelse");
4014 if (tmp == NULL) goto failed;
4015 if (!PyList_Check(tmp)) {
4016 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4017 goto failed;
4018 }
4019 len = PyList_GET_SIZE(tmp);
4020 orelse = asdl_seq_new(len, arena);
4021 if (orelse == NULL) goto failed;
4022 for (i = 0; i < len; i++) {
4023 stmt_ty value;
4024 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4025 if (res != 0) goto failed;
4026 asdl_seq_SET(orelse, i, value);
4027 }
4028 Py_XDECREF(tmp);
4029 tmp = NULL;
4030 } else {
4031 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4032 return 1;
4033 }
4034 *out = If(test, body, orelse, lineno, col_offset, arena);
4035 if (*out == NULL) goto failed;
4036 return 0;
4037 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004038 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4039 if (isinstance == -1) {
4040 return 1;
4041 }
4042 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004043 expr_ty context_expr;
4044 expr_ty optional_vars;
4045 asdl_seq* body;
4046
4047 if (PyObject_HasAttrString(obj, "context_expr")) {
4048 int res;
4049 tmp = PyObject_GetAttrString(obj, "context_expr");
4050 if (tmp == NULL) goto failed;
4051 res = obj2ast_expr(tmp, &context_expr, arena);
4052 if (res != 0) goto failed;
4053 Py_XDECREF(tmp);
4054 tmp = NULL;
4055 } else {
4056 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4057 return 1;
4058 }
4059 if (PyObject_HasAttrString(obj, "optional_vars")) {
4060 int res;
4061 tmp = PyObject_GetAttrString(obj, "optional_vars");
4062 if (tmp == NULL) goto failed;
4063 res = obj2ast_expr(tmp, &optional_vars, arena);
4064 if (res != 0) goto failed;
4065 Py_XDECREF(tmp);
4066 tmp = NULL;
4067 } else {
4068 optional_vars = NULL;
4069 }
4070 if (PyObject_HasAttrString(obj, "body")) {
4071 int res;
4072 Py_ssize_t len;
4073 Py_ssize_t i;
4074 tmp = PyObject_GetAttrString(obj, "body");
4075 if (tmp == NULL) goto failed;
4076 if (!PyList_Check(tmp)) {
4077 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4078 goto failed;
4079 }
4080 len = PyList_GET_SIZE(tmp);
4081 body = asdl_seq_new(len, arena);
4082 if (body == NULL) goto failed;
4083 for (i = 0; i < len; i++) {
4084 stmt_ty value;
4085 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4086 if (res != 0) goto failed;
4087 asdl_seq_SET(body, i, value);
4088 }
4089 Py_XDECREF(tmp);
4090 tmp = NULL;
4091 } else {
4092 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4093 return 1;
4094 }
4095 *out = With(context_expr, optional_vars, body, lineno,
4096 col_offset, arena);
4097 if (*out == NULL) goto failed;
4098 return 0;
4099 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004100 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4101 if (isinstance == -1) {
4102 return 1;
4103 }
4104 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004105 expr_ty type;
4106 expr_ty inst;
4107 expr_ty tback;
4108
4109 if (PyObject_HasAttrString(obj, "type")) {
4110 int res;
4111 tmp = PyObject_GetAttrString(obj, "type");
4112 if (tmp == NULL) goto failed;
4113 res = obj2ast_expr(tmp, &type, arena);
4114 if (res != 0) goto failed;
4115 Py_XDECREF(tmp);
4116 tmp = NULL;
4117 } else {
4118 type = NULL;
4119 }
4120 if (PyObject_HasAttrString(obj, "inst")) {
4121 int res;
4122 tmp = PyObject_GetAttrString(obj, "inst");
4123 if (tmp == NULL) goto failed;
4124 res = obj2ast_expr(tmp, &inst, arena);
4125 if (res != 0) goto failed;
4126 Py_XDECREF(tmp);
4127 tmp = NULL;
4128 } else {
4129 inst = NULL;
4130 }
4131 if (PyObject_HasAttrString(obj, "tback")) {
4132 int res;
4133 tmp = PyObject_GetAttrString(obj, "tback");
4134 if (tmp == NULL) goto failed;
4135 res = obj2ast_expr(tmp, &tback, arena);
4136 if (res != 0) goto failed;
4137 Py_XDECREF(tmp);
4138 tmp = NULL;
4139 } else {
4140 tback = NULL;
4141 }
4142 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4143 if (*out == NULL) goto failed;
4144 return 0;
4145 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004146 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4147 if (isinstance == -1) {
4148 return 1;
4149 }
4150 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004151 asdl_seq* body;
4152 asdl_seq* handlers;
4153 asdl_seq* orelse;
4154
4155 if (PyObject_HasAttrString(obj, "body")) {
4156 int res;
4157 Py_ssize_t len;
4158 Py_ssize_t i;
4159 tmp = PyObject_GetAttrString(obj, "body");
4160 if (tmp == NULL) goto failed;
4161 if (!PyList_Check(tmp)) {
4162 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4163 goto failed;
4164 }
4165 len = PyList_GET_SIZE(tmp);
4166 body = asdl_seq_new(len, arena);
4167 if (body == NULL) goto failed;
4168 for (i = 0; i < len; i++) {
4169 stmt_ty value;
4170 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4171 if (res != 0) goto failed;
4172 asdl_seq_SET(body, i, value);
4173 }
4174 Py_XDECREF(tmp);
4175 tmp = NULL;
4176 } else {
4177 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4178 return 1;
4179 }
4180 if (PyObject_HasAttrString(obj, "handlers")) {
4181 int res;
4182 Py_ssize_t len;
4183 Py_ssize_t i;
4184 tmp = PyObject_GetAttrString(obj, "handlers");
4185 if (tmp == NULL) goto failed;
4186 if (!PyList_Check(tmp)) {
4187 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4188 goto failed;
4189 }
4190 len = PyList_GET_SIZE(tmp);
4191 handlers = asdl_seq_new(len, arena);
4192 if (handlers == NULL) goto failed;
4193 for (i = 0; i < len; i++) {
4194 excepthandler_ty value;
4195 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4196 if (res != 0) goto failed;
4197 asdl_seq_SET(handlers, i, value);
4198 }
4199 Py_XDECREF(tmp);
4200 tmp = NULL;
4201 } else {
4202 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4203 return 1;
4204 }
4205 if (PyObject_HasAttrString(obj, "orelse")) {
4206 int res;
4207 Py_ssize_t len;
4208 Py_ssize_t i;
4209 tmp = PyObject_GetAttrString(obj, "orelse");
4210 if (tmp == NULL) goto failed;
4211 if (!PyList_Check(tmp)) {
4212 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4213 goto failed;
4214 }
4215 len = PyList_GET_SIZE(tmp);
4216 orelse = asdl_seq_new(len, arena);
4217 if (orelse == NULL) goto failed;
4218 for (i = 0; i < len; i++) {
4219 stmt_ty value;
4220 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4221 if (res != 0) goto failed;
4222 asdl_seq_SET(orelse, i, value);
4223 }
4224 Py_XDECREF(tmp);
4225 tmp = NULL;
4226 } else {
4227 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4228 return 1;
4229 }
4230 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4231 arena);
4232 if (*out == NULL) goto failed;
4233 return 0;
4234 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004235 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4236 if (isinstance == -1) {
4237 return 1;
4238 }
4239 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004240 asdl_seq* body;
4241 asdl_seq* finalbody;
4242
4243 if (PyObject_HasAttrString(obj, "body")) {
4244 int res;
4245 Py_ssize_t len;
4246 Py_ssize_t i;
4247 tmp = PyObject_GetAttrString(obj, "body");
4248 if (tmp == NULL) goto failed;
4249 if (!PyList_Check(tmp)) {
4250 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4251 goto failed;
4252 }
4253 len = PyList_GET_SIZE(tmp);
4254 body = asdl_seq_new(len, arena);
4255 if (body == NULL) goto failed;
4256 for (i = 0; i < len; i++) {
4257 stmt_ty value;
4258 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4259 if (res != 0) goto failed;
4260 asdl_seq_SET(body, i, value);
4261 }
4262 Py_XDECREF(tmp);
4263 tmp = NULL;
4264 } else {
4265 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4266 return 1;
4267 }
4268 if (PyObject_HasAttrString(obj, "finalbody")) {
4269 int res;
4270 Py_ssize_t len;
4271 Py_ssize_t i;
4272 tmp = PyObject_GetAttrString(obj, "finalbody");
4273 if (tmp == NULL) goto failed;
4274 if (!PyList_Check(tmp)) {
4275 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4276 goto failed;
4277 }
4278 len = PyList_GET_SIZE(tmp);
4279 finalbody = asdl_seq_new(len, arena);
4280 if (finalbody == NULL) goto failed;
4281 for (i = 0; i < len; i++) {
4282 stmt_ty value;
4283 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4284 if (res != 0) goto failed;
4285 asdl_seq_SET(finalbody, i, value);
4286 }
4287 Py_XDECREF(tmp);
4288 tmp = NULL;
4289 } else {
4290 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4291 return 1;
4292 }
4293 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4294 if (*out == NULL) goto failed;
4295 return 0;
4296 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004297 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4298 if (isinstance == -1) {
4299 return 1;
4300 }
4301 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004302 expr_ty test;
4303 expr_ty msg;
4304
4305 if (PyObject_HasAttrString(obj, "test")) {
4306 int res;
4307 tmp = PyObject_GetAttrString(obj, "test");
4308 if (tmp == NULL) goto failed;
4309 res = obj2ast_expr(tmp, &test, arena);
4310 if (res != 0) goto failed;
4311 Py_XDECREF(tmp);
4312 tmp = NULL;
4313 } else {
4314 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4315 return 1;
4316 }
4317 if (PyObject_HasAttrString(obj, "msg")) {
4318 int res;
4319 tmp = PyObject_GetAttrString(obj, "msg");
4320 if (tmp == NULL) goto failed;
4321 res = obj2ast_expr(tmp, &msg, arena);
4322 if (res != 0) goto failed;
4323 Py_XDECREF(tmp);
4324 tmp = NULL;
4325 } else {
4326 msg = NULL;
4327 }
4328 *out = Assert(test, msg, lineno, col_offset, arena);
4329 if (*out == NULL) goto failed;
4330 return 0;
4331 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004332 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4333 if (isinstance == -1) {
4334 return 1;
4335 }
4336 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004337 asdl_seq* names;
4338
4339 if (PyObject_HasAttrString(obj, "names")) {
4340 int res;
4341 Py_ssize_t len;
4342 Py_ssize_t i;
4343 tmp = PyObject_GetAttrString(obj, "names");
4344 if (tmp == NULL) goto failed;
4345 if (!PyList_Check(tmp)) {
4346 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4347 goto failed;
4348 }
4349 len = PyList_GET_SIZE(tmp);
4350 names = asdl_seq_new(len, arena);
4351 if (names == NULL) goto failed;
4352 for (i = 0; i < len; i++) {
4353 alias_ty value;
4354 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4355 if (res != 0) goto failed;
4356 asdl_seq_SET(names, i, value);
4357 }
4358 Py_XDECREF(tmp);
4359 tmp = NULL;
4360 } else {
4361 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4362 return 1;
4363 }
4364 *out = Import(names, lineno, col_offset, arena);
4365 if (*out == NULL) goto failed;
4366 return 0;
4367 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004368 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4369 if (isinstance == -1) {
4370 return 1;
4371 }
4372 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004373 identifier module;
4374 asdl_seq* names;
4375 int level;
4376
4377 if (PyObject_HasAttrString(obj, "module")) {
4378 int res;
4379 tmp = PyObject_GetAttrString(obj, "module");
4380 if (tmp == NULL) goto failed;
4381 res = obj2ast_identifier(tmp, &module, arena);
4382 if (res != 0) goto failed;
4383 Py_XDECREF(tmp);
4384 tmp = NULL;
4385 } else {
Benjamin Petersona72be3b2009-06-13 20:23:33 +00004386 module = NULL;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004387 }
4388 if (PyObject_HasAttrString(obj, "names")) {
4389 int res;
4390 Py_ssize_t len;
4391 Py_ssize_t i;
4392 tmp = PyObject_GetAttrString(obj, "names");
4393 if (tmp == NULL) goto failed;
4394 if (!PyList_Check(tmp)) {
4395 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4396 goto failed;
4397 }
4398 len = PyList_GET_SIZE(tmp);
4399 names = asdl_seq_new(len, arena);
4400 if (names == NULL) goto failed;
4401 for (i = 0; i < len; i++) {
4402 alias_ty value;
4403 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4404 if (res != 0) goto failed;
4405 asdl_seq_SET(names, i, value);
4406 }
4407 Py_XDECREF(tmp);
4408 tmp = NULL;
4409 } else {
4410 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4411 return 1;
4412 }
4413 if (PyObject_HasAttrString(obj, "level")) {
4414 int res;
4415 tmp = PyObject_GetAttrString(obj, "level");
4416 if (tmp == NULL) goto failed;
4417 res = obj2ast_int(tmp, &level, arena);
4418 if (res != 0) goto failed;
4419 Py_XDECREF(tmp);
4420 tmp = NULL;
4421 } else {
4422 level = 0;
4423 }
4424 *out = ImportFrom(module, names, level, lineno, col_offset,
4425 arena);
4426 if (*out == NULL) goto failed;
4427 return 0;
4428 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004429 isinstance = PyObject_IsInstance(obj, (PyObject*)Exec_type);
4430 if (isinstance == -1) {
4431 return 1;
4432 }
4433 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004434 expr_ty body;
4435 expr_ty globals;
4436 expr_ty locals;
4437
4438 if (PyObject_HasAttrString(obj, "body")) {
4439 int res;
4440 tmp = PyObject_GetAttrString(obj, "body");
4441 if (tmp == NULL) goto failed;
4442 res = obj2ast_expr(tmp, &body, arena);
4443 if (res != 0) goto failed;
4444 Py_XDECREF(tmp);
4445 tmp = NULL;
4446 } else {
4447 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4448 return 1;
4449 }
4450 if (PyObject_HasAttrString(obj, "globals")) {
4451 int res;
4452 tmp = PyObject_GetAttrString(obj, "globals");
4453 if (tmp == NULL) goto failed;
4454 res = obj2ast_expr(tmp, &globals, arena);
4455 if (res != 0) goto failed;
4456 Py_XDECREF(tmp);
4457 tmp = NULL;
4458 } else {
4459 globals = NULL;
4460 }
4461 if (PyObject_HasAttrString(obj, "locals")) {
4462 int res;
4463 tmp = PyObject_GetAttrString(obj, "locals");
4464 if (tmp == NULL) goto failed;
4465 res = obj2ast_expr(tmp, &locals, arena);
4466 if (res != 0) goto failed;
4467 Py_XDECREF(tmp);
4468 tmp = NULL;
4469 } else {
4470 locals = NULL;
4471 }
4472 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4473 if (*out == NULL) goto failed;
4474 return 0;
4475 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004476 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4477 if (isinstance == -1) {
4478 return 1;
4479 }
4480 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004481 asdl_seq* names;
4482
4483 if (PyObject_HasAttrString(obj, "names")) {
4484 int res;
4485 Py_ssize_t len;
4486 Py_ssize_t i;
4487 tmp = PyObject_GetAttrString(obj, "names");
4488 if (tmp == NULL) goto failed;
4489 if (!PyList_Check(tmp)) {
4490 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4491 goto failed;
4492 }
4493 len = PyList_GET_SIZE(tmp);
4494 names = asdl_seq_new(len, arena);
4495 if (names == NULL) goto failed;
4496 for (i = 0; i < len; i++) {
4497 identifier value;
4498 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4499 if (res != 0) goto failed;
4500 asdl_seq_SET(names, i, value);
4501 }
4502 Py_XDECREF(tmp);
4503 tmp = NULL;
4504 } else {
4505 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4506 return 1;
4507 }
4508 *out = Global(names, lineno, col_offset, arena);
4509 if (*out == NULL) goto failed;
4510 return 0;
4511 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004512 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4513 if (isinstance == -1) {
4514 return 1;
4515 }
4516 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004517 expr_ty value;
4518
4519 if (PyObject_HasAttrString(obj, "value")) {
4520 int res;
4521 tmp = PyObject_GetAttrString(obj, "value");
4522 if (tmp == NULL) goto failed;
4523 res = obj2ast_expr(tmp, &value, arena);
4524 if (res != 0) goto failed;
4525 Py_XDECREF(tmp);
4526 tmp = NULL;
4527 } else {
4528 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4529 return 1;
4530 }
4531 *out = Expr(value, lineno, col_offset, arena);
4532 if (*out == NULL) goto failed;
4533 return 0;
4534 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004535 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4536 if (isinstance == -1) {
4537 return 1;
4538 }
4539 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004540
4541 *out = Pass(lineno, col_offset, arena);
4542 if (*out == NULL) goto failed;
4543 return 0;
4544 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004545 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4546 if (isinstance == -1) {
4547 return 1;
4548 }
4549 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004550
4551 *out = Break(lineno, col_offset, arena);
4552 if (*out == NULL) goto failed;
4553 return 0;
4554 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004555 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4556 if (isinstance == -1) {
4557 return 1;
4558 }
4559 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004560
4561 *out = Continue(lineno, col_offset, arena);
4562 if (*out == NULL) goto failed;
4563 return 0;
4564 }
4565
4566 tmp = PyObject_Repr(obj);
4567 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004568 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00004569failed:
4570 Py_XDECREF(tmp);
4571 return 1;
4572}
4573
4574int
4575obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4576{
4577 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004578 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004579
4580 int lineno;
4581 int col_offset;
4582
4583 if (obj == Py_None) {
4584 *out = NULL;
4585 return 0;
4586 }
4587 if (PyObject_HasAttrString(obj, "lineno")) {
4588 int res;
4589 tmp = PyObject_GetAttrString(obj, "lineno");
4590 if (tmp == NULL) goto failed;
4591 res = obj2ast_int(tmp, &lineno, arena);
4592 if (res != 0) goto failed;
4593 Py_XDECREF(tmp);
4594 tmp = NULL;
4595 } else {
4596 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4597 return 1;
4598 }
4599 if (PyObject_HasAttrString(obj, "col_offset")) {
4600 int res;
4601 tmp = PyObject_GetAttrString(obj, "col_offset");
4602 if (tmp == NULL) goto failed;
4603 res = obj2ast_int(tmp, &col_offset, arena);
4604 if (res != 0) goto failed;
4605 Py_XDECREF(tmp);
4606 tmp = NULL;
4607 } else {
4608 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4609 return 1;
4610 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004611 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4612 if (isinstance == -1) {
4613 return 1;
4614 }
4615 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004616 boolop_ty op;
4617 asdl_seq* values;
4618
4619 if (PyObject_HasAttrString(obj, "op")) {
4620 int res;
4621 tmp = PyObject_GetAttrString(obj, "op");
4622 if (tmp == NULL) goto failed;
4623 res = obj2ast_boolop(tmp, &op, arena);
4624 if (res != 0) goto failed;
4625 Py_XDECREF(tmp);
4626 tmp = NULL;
4627 } else {
4628 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4629 return 1;
4630 }
4631 if (PyObject_HasAttrString(obj, "values")) {
4632 int res;
4633 Py_ssize_t len;
4634 Py_ssize_t i;
4635 tmp = PyObject_GetAttrString(obj, "values");
4636 if (tmp == NULL) goto failed;
4637 if (!PyList_Check(tmp)) {
4638 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4639 goto failed;
4640 }
4641 len = PyList_GET_SIZE(tmp);
4642 values = asdl_seq_new(len, arena);
4643 if (values == NULL) goto failed;
4644 for (i = 0; i < len; i++) {
4645 expr_ty value;
4646 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4647 if (res != 0) goto failed;
4648 asdl_seq_SET(values, i, value);
4649 }
4650 Py_XDECREF(tmp);
4651 tmp = NULL;
4652 } else {
4653 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4654 return 1;
4655 }
4656 *out = BoolOp(op, values, lineno, col_offset, arena);
4657 if (*out == NULL) goto failed;
4658 return 0;
4659 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004660 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4661 if (isinstance == -1) {
4662 return 1;
4663 }
4664 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004665 expr_ty left;
4666 operator_ty op;
4667 expr_ty right;
4668
4669 if (PyObject_HasAttrString(obj, "left")) {
4670 int res;
4671 tmp = PyObject_GetAttrString(obj, "left");
4672 if (tmp == NULL) goto failed;
4673 res = obj2ast_expr(tmp, &left, arena);
4674 if (res != 0) goto failed;
4675 Py_XDECREF(tmp);
4676 tmp = NULL;
4677 } else {
4678 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4679 return 1;
4680 }
4681 if (PyObject_HasAttrString(obj, "op")) {
4682 int res;
4683 tmp = PyObject_GetAttrString(obj, "op");
4684 if (tmp == NULL) goto failed;
4685 res = obj2ast_operator(tmp, &op, arena);
4686 if (res != 0) goto failed;
4687 Py_XDECREF(tmp);
4688 tmp = NULL;
4689 } else {
4690 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4691 return 1;
4692 }
4693 if (PyObject_HasAttrString(obj, "right")) {
4694 int res;
4695 tmp = PyObject_GetAttrString(obj, "right");
4696 if (tmp == NULL) goto failed;
4697 res = obj2ast_expr(tmp, &right, arena);
4698 if (res != 0) goto failed;
4699 Py_XDECREF(tmp);
4700 tmp = NULL;
4701 } else {
4702 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4703 return 1;
4704 }
4705 *out = BinOp(left, op, right, lineno, col_offset, arena);
4706 if (*out == NULL) goto failed;
4707 return 0;
4708 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004709 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4710 if (isinstance == -1) {
4711 return 1;
4712 }
4713 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004714 unaryop_ty op;
4715 expr_ty operand;
4716
4717 if (PyObject_HasAttrString(obj, "op")) {
4718 int res;
4719 tmp = PyObject_GetAttrString(obj, "op");
4720 if (tmp == NULL) goto failed;
4721 res = obj2ast_unaryop(tmp, &op, arena);
4722 if (res != 0) goto failed;
4723 Py_XDECREF(tmp);
4724 tmp = NULL;
4725 } else {
4726 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4727 return 1;
4728 }
4729 if (PyObject_HasAttrString(obj, "operand")) {
4730 int res;
4731 tmp = PyObject_GetAttrString(obj, "operand");
4732 if (tmp == NULL) goto failed;
4733 res = obj2ast_expr(tmp, &operand, arena);
4734 if (res != 0) goto failed;
4735 Py_XDECREF(tmp);
4736 tmp = NULL;
4737 } else {
4738 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4739 return 1;
4740 }
4741 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4742 if (*out == NULL) goto failed;
4743 return 0;
4744 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004745 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4746 if (isinstance == -1) {
4747 return 1;
4748 }
4749 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004750 arguments_ty args;
4751 expr_ty body;
4752
4753 if (PyObject_HasAttrString(obj, "args")) {
4754 int res;
4755 tmp = PyObject_GetAttrString(obj, "args");
4756 if (tmp == NULL) goto failed;
4757 res = obj2ast_arguments(tmp, &args, arena);
4758 if (res != 0) goto failed;
4759 Py_XDECREF(tmp);
4760 tmp = NULL;
4761 } else {
4762 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4763 return 1;
4764 }
4765 if (PyObject_HasAttrString(obj, "body")) {
4766 int res;
4767 tmp = PyObject_GetAttrString(obj, "body");
4768 if (tmp == NULL) goto failed;
4769 res = obj2ast_expr(tmp, &body, arena);
4770 if (res != 0) goto failed;
4771 Py_XDECREF(tmp);
4772 tmp = NULL;
4773 } else {
4774 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4775 return 1;
4776 }
4777 *out = Lambda(args, body, lineno, col_offset, arena);
4778 if (*out == NULL) goto failed;
4779 return 0;
4780 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004781 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4782 if (isinstance == -1) {
4783 return 1;
4784 }
4785 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004786 expr_ty test;
4787 expr_ty body;
4788 expr_ty orelse;
4789
4790 if (PyObject_HasAttrString(obj, "test")) {
4791 int res;
4792 tmp = PyObject_GetAttrString(obj, "test");
4793 if (tmp == NULL) goto failed;
4794 res = obj2ast_expr(tmp, &test, arena);
4795 if (res != 0) goto failed;
4796 Py_XDECREF(tmp);
4797 tmp = NULL;
4798 } else {
4799 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4800 return 1;
4801 }
4802 if (PyObject_HasAttrString(obj, "body")) {
4803 int res;
4804 tmp = PyObject_GetAttrString(obj, "body");
4805 if (tmp == NULL) goto failed;
4806 res = obj2ast_expr(tmp, &body, arena);
4807 if (res != 0) goto failed;
4808 Py_XDECREF(tmp);
4809 tmp = NULL;
4810 } else {
4811 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4812 return 1;
4813 }
4814 if (PyObject_HasAttrString(obj, "orelse")) {
4815 int res;
4816 tmp = PyObject_GetAttrString(obj, "orelse");
4817 if (tmp == NULL) goto failed;
4818 res = obj2ast_expr(tmp, &orelse, arena);
4819 if (res != 0) goto failed;
4820 Py_XDECREF(tmp);
4821 tmp = NULL;
4822 } else {
4823 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4824 return 1;
4825 }
4826 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4827 if (*out == NULL) goto failed;
4828 return 0;
4829 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004830 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4831 if (isinstance == -1) {
4832 return 1;
4833 }
4834 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004835 asdl_seq* keys;
4836 asdl_seq* values;
4837
4838 if (PyObject_HasAttrString(obj, "keys")) {
4839 int res;
4840 Py_ssize_t len;
4841 Py_ssize_t i;
4842 tmp = PyObject_GetAttrString(obj, "keys");
4843 if (tmp == NULL) goto failed;
4844 if (!PyList_Check(tmp)) {
4845 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4846 goto failed;
4847 }
4848 len = PyList_GET_SIZE(tmp);
4849 keys = asdl_seq_new(len, arena);
4850 if (keys == NULL) goto failed;
4851 for (i = 0; i < len; i++) {
4852 expr_ty value;
4853 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4854 if (res != 0) goto failed;
4855 asdl_seq_SET(keys, i, value);
4856 }
4857 Py_XDECREF(tmp);
4858 tmp = NULL;
4859 } else {
4860 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4861 return 1;
4862 }
4863 if (PyObject_HasAttrString(obj, "values")) {
4864 int res;
4865 Py_ssize_t len;
4866 Py_ssize_t i;
4867 tmp = PyObject_GetAttrString(obj, "values");
4868 if (tmp == NULL) goto failed;
4869 if (!PyList_Check(tmp)) {
4870 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4871 goto failed;
4872 }
4873 len = PyList_GET_SIZE(tmp);
4874 values = asdl_seq_new(len, arena);
4875 if (values == NULL) goto failed;
4876 for (i = 0; i < len; i++) {
4877 expr_ty value;
4878 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4879 if (res != 0) goto failed;
4880 asdl_seq_SET(values, i, value);
4881 }
4882 Py_XDECREF(tmp);
4883 tmp = NULL;
4884 } else {
4885 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4886 return 1;
4887 }
4888 *out = Dict(keys, values, lineno, col_offset, arena);
4889 if (*out == NULL) goto failed;
4890 return 0;
4891 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00004892 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
4893 if (isinstance == -1) {
4894 return 1;
4895 }
4896 if (isinstance) {
4897 asdl_seq* elts;
4898
4899 if (PyObject_HasAttrString(obj, "elts")) {
4900 int res;
4901 Py_ssize_t len;
4902 Py_ssize_t i;
4903 tmp = PyObject_GetAttrString(obj, "elts");
4904 if (tmp == NULL) goto failed;
4905 if (!PyList_Check(tmp)) {
4906 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4907 goto failed;
4908 }
4909 len = PyList_GET_SIZE(tmp);
4910 elts = asdl_seq_new(len, arena);
4911 if (elts == NULL) goto failed;
4912 for (i = 0; i < len; i++) {
4913 expr_ty value;
4914 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4915 if (res != 0) goto failed;
4916 asdl_seq_SET(elts, i, value);
4917 }
4918 Py_XDECREF(tmp);
4919 tmp = NULL;
4920 } else {
4921 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
4922 return 1;
4923 }
4924 *out = Set(elts, lineno, col_offset, arena);
4925 if (*out == NULL) goto failed;
4926 return 0;
4927 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004928 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
4929 if (isinstance == -1) {
4930 return 1;
4931 }
4932 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004933 expr_ty elt;
4934 asdl_seq* generators;
4935
4936 if (PyObject_HasAttrString(obj, "elt")) {
4937 int res;
4938 tmp = PyObject_GetAttrString(obj, "elt");
4939 if (tmp == NULL) goto failed;
4940 res = obj2ast_expr(tmp, &elt, arena);
4941 if (res != 0) goto failed;
4942 Py_XDECREF(tmp);
4943 tmp = NULL;
4944 } else {
4945 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4946 return 1;
4947 }
4948 if (PyObject_HasAttrString(obj, "generators")) {
4949 int res;
4950 Py_ssize_t len;
4951 Py_ssize_t i;
4952 tmp = PyObject_GetAttrString(obj, "generators");
4953 if (tmp == NULL) goto failed;
4954 if (!PyList_Check(tmp)) {
4955 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4956 goto failed;
4957 }
4958 len = PyList_GET_SIZE(tmp);
4959 generators = asdl_seq_new(len, arena);
4960 if (generators == NULL) goto failed;
4961 for (i = 0; i < len; i++) {
4962 comprehension_ty value;
4963 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4964 if (res != 0) goto failed;
4965 asdl_seq_SET(generators, i, value);
4966 }
4967 Py_XDECREF(tmp);
4968 tmp = NULL;
4969 } else {
4970 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4971 return 1;
4972 }
4973 *out = ListComp(elt, generators, lineno, col_offset, arena);
4974 if (*out == NULL) goto failed;
4975 return 0;
4976 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004977 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
4978 if (isinstance == -1) {
4979 return 1;
4980 }
4981 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004982 expr_ty elt;
4983 asdl_seq* generators;
4984
4985 if (PyObject_HasAttrString(obj, "elt")) {
4986 int res;
4987 tmp = PyObject_GetAttrString(obj, "elt");
4988 if (tmp == NULL) goto failed;
4989 res = obj2ast_expr(tmp, &elt, arena);
4990 if (res != 0) goto failed;
4991 Py_XDECREF(tmp);
4992 tmp = NULL;
4993 } else {
4994 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4995 return 1;
4996 }
4997 if (PyObject_HasAttrString(obj, "generators")) {
4998 int res;
4999 Py_ssize_t len;
5000 Py_ssize_t i;
5001 tmp = PyObject_GetAttrString(obj, "generators");
5002 if (tmp == NULL) goto failed;
5003 if (!PyList_Check(tmp)) {
5004 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5005 goto failed;
5006 }
5007 len = PyList_GET_SIZE(tmp);
5008 generators = asdl_seq_new(len, arena);
5009 if (generators == NULL) goto failed;
5010 for (i = 0; i < len; i++) {
5011 comprehension_ty value;
5012 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5013 if (res != 0) goto failed;
5014 asdl_seq_SET(generators, i, value);
5015 }
5016 Py_XDECREF(tmp);
5017 tmp = NULL;
5018 } else {
5019 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5020 return 1;
5021 }
5022 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5023 if (*out == NULL) goto failed;
5024 return 0;
5025 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005026 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5027 if (isinstance == -1) {
5028 return 1;
5029 }
5030 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005031 expr_ty value;
5032
5033 if (PyObject_HasAttrString(obj, "value")) {
5034 int res;
5035 tmp = PyObject_GetAttrString(obj, "value");
5036 if (tmp == NULL) goto failed;
5037 res = obj2ast_expr(tmp, &value, arena);
5038 if (res != 0) goto failed;
5039 Py_XDECREF(tmp);
5040 tmp = NULL;
5041 } else {
5042 value = NULL;
5043 }
5044 *out = Yield(value, lineno, col_offset, arena);
5045 if (*out == NULL) goto failed;
5046 return 0;
5047 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005048 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5049 if (isinstance == -1) {
5050 return 1;
5051 }
5052 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005053 expr_ty left;
5054 asdl_int_seq* ops;
5055 asdl_seq* comparators;
5056
5057 if (PyObject_HasAttrString(obj, "left")) {
5058 int res;
5059 tmp = PyObject_GetAttrString(obj, "left");
5060 if (tmp == NULL) goto failed;
5061 res = obj2ast_expr(tmp, &left, arena);
5062 if (res != 0) goto failed;
5063 Py_XDECREF(tmp);
5064 tmp = NULL;
5065 } else {
5066 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5067 return 1;
5068 }
5069 if (PyObject_HasAttrString(obj, "ops")) {
5070 int res;
5071 Py_ssize_t len;
5072 Py_ssize_t i;
5073 tmp = PyObject_GetAttrString(obj, "ops");
5074 if (tmp == NULL) goto failed;
5075 if (!PyList_Check(tmp)) {
5076 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5077 goto failed;
5078 }
5079 len = PyList_GET_SIZE(tmp);
5080 ops = asdl_int_seq_new(len, arena);
5081 if (ops == NULL) goto failed;
5082 for (i = 0; i < len; i++) {
5083 cmpop_ty value;
5084 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5085 if (res != 0) goto failed;
5086 asdl_seq_SET(ops, i, value);
5087 }
5088 Py_XDECREF(tmp);
5089 tmp = NULL;
5090 } else {
5091 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5092 return 1;
5093 }
5094 if (PyObject_HasAttrString(obj, "comparators")) {
5095 int res;
5096 Py_ssize_t len;
5097 Py_ssize_t i;
5098 tmp = PyObject_GetAttrString(obj, "comparators");
5099 if (tmp == NULL) goto failed;
5100 if (!PyList_Check(tmp)) {
5101 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5102 goto failed;
5103 }
5104 len = PyList_GET_SIZE(tmp);
5105 comparators = asdl_seq_new(len, arena);
5106 if (comparators == NULL) goto failed;
5107 for (i = 0; i < len; i++) {
5108 expr_ty value;
5109 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5110 if (res != 0) goto failed;
5111 asdl_seq_SET(comparators, i, value);
5112 }
5113 Py_XDECREF(tmp);
5114 tmp = NULL;
5115 } else {
5116 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5117 return 1;
5118 }
5119 *out = Compare(left, ops, comparators, lineno, col_offset,
5120 arena);
5121 if (*out == NULL) goto failed;
5122 return 0;
5123 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005124 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5125 if (isinstance == -1) {
5126 return 1;
5127 }
5128 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005129 expr_ty func;
5130 asdl_seq* args;
5131 asdl_seq* keywords;
5132 expr_ty starargs;
5133 expr_ty kwargs;
5134
5135 if (PyObject_HasAttrString(obj, "func")) {
5136 int res;
5137 tmp = PyObject_GetAttrString(obj, "func");
5138 if (tmp == NULL) goto failed;
5139 res = obj2ast_expr(tmp, &func, arena);
5140 if (res != 0) goto failed;
5141 Py_XDECREF(tmp);
5142 tmp = NULL;
5143 } else {
5144 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5145 return 1;
5146 }
5147 if (PyObject_HasAttrString(obj, "args")) {
5148 int res;
5149 Py_ssize_t len;
5150 Py_ssize_t i;
5151 tmp = PyObject_GetAttrString(obj, "args");
5152 if (tmp == NULL) goto failed;
5153 if (!PyList_Check(tmp)) {
5154 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5155 goto failed;
5156 }
5157 len = PyList_GET_SIZE(tmp);
5158 args = asdl_seq_new(len, arena);
5159 if (args == NULL) goto failed;
5160 for (i = 0; i < len; i++) {
5161 expr_ty value;
5162 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5163 if (res != 0) goto failed;
5164 asdl_seq_SET(args, i, value);
5165 }
5166 Py_XDECREF(tmp);
5167 tmp = NULL;
5168 } else {
5169 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5170 return 1;
5171 }
5172 if (PyObject_HasAttrString(obj, "keywords")) {
5173 int res;
5174 Py_ssize_t len;
5175 Py_ssize_t i;
5176 tmp = PyObject_GetAttrString(obj, "keywords");
5177 if (tmp == NULL) goto failed;
5178 if (!PyList_Check(tmp)) {
5179 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5180 goto failed;
5181 }
5182 len = PyList_GET_SIZE(tmp);
5183 keywords = asdl_seq_new(len, arena);
5184 if (keywords == NULL) goto failed;
5185 for (i = 0; i < len; i++) {
5186 keyword_ty value;
5187 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5188 if (res != 0) goto failed;
5189 asdl_seq_SET(keywords, i, value);
5190 }
5191 Py_XDECREF(tmp);
5192 tmp = NULL;
5193 } else {
5194 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5195 return 1;
5196 }
5197 if (PyObject_HasAttrString(obj, "starargs")) {
5198 int res;
5199 tmp = PyObject_GetAttrString(obj, "starargs");
5200 if (tmp == NULL) goto failed;
5201 res = obj2ast_expr(tmp, &starargs, arena);
5202 if (res != 0) goto failed;
5203 Py_XDECREF(tmp);
5204 tmp = NULL;
5205 } else {
5206 starargs = NULL;
5207 }
5208 if (PyObject_HasAttrString(obj, "kwargs")) {
5209 int res;
5210 tmp = PyObject_GetAttrString(obj, "kwargs");
5211 if (tmp == NULL) goto failed;
5212 res = obj2ast_expr(tmp, &kwargs, arena);
5213 if (res != 0) goto failed;
5214 Py_XDECREF(tmp);
5215 tmp = NULL;
5216 } else {
5217 kwargs = NULL;
5218 }
5219 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5220 col_offset, arena);
5221 if (*out == NULL) goto failed;
5222 return 0;
5223 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005224 isinstance = PyObject_IsInstance(obj, (PyObject*)Repr_type);
5225 if (isinstance == -1) {
5226 return 1;
5227 }
5228 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005229 expr_ty value;
5230
5231 if (PyObject_HasAttrString(obj, "value")) {
5232 int res;
5233 tmp = PyObject_GetAttrString(obj, "value");
5234 if (tmp == NULL) goto failed;
5235 res = obj2ast_expr(tmp, &value, arena);
5236 if (res != 0) goto failed;
5237 Py_XDECREF(tmp);
5238 tmp = NULL;
5239 } else {
5240 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5241 return 1;
5242 }
5243 *out = Repr(value, lineno, col_offset, arena);
5244 if (*out == NULL) goto failed;
5245 return 0;
5246 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005247 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5248 if (isinstance == -1) {
5249 return 1;
5250 }
5251 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005252 object n;
5253
5254 if (PyObject_HasAttrString(obj, "n")) {
5255 int res;
5256 tmp = PyObject_GetAttrString(obj, "n");
5257 if (tmp == NULL) goto failed;
5258 res = obj2ast_object(tmp, &n, arena);
5259 if (res != 0) goto failed;
5260 Py_XDECREF(tmp);
5261 tmp = NULL;
5262 } else {
5263 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5264 return 1;
5265 }
5266 *out = Num(n, lineno, col_offset, arena);
5267 if (*out == NULL) goto failed;
5268 return 0;
5269 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005270 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5271 if (isinstance == -1) {
5272 return 1;
5273 }
5274 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005275 string s;
5276
5277 if (PyObject_HasAttrString(obj, "s")) {
5278 int res;
5279 tmp = PyObject_GetAttrString(obj, "s");
5280 if (tmp == NULL) goto failed;
5281 res = obj2ast_string(tmp, &s, arena);
5282 if (res != 0) goto failed;
5283 Py_XDECREF(tmp);
5284 tmp = NULL;
5285 } else {
5286 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5287 return 1;
5288 }
5289 *out = Str(s, lineno, col_offset, arena);
5290 if (*out == NULL) goto failed;
5291 return 0;
5292 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005293 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5294 if (isinstance == -1) {
5295 return 1;
5296 }
5297 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005298 expr_ty value;
5299 identifier attr;
5300 expr_context_ty ctx;
5301
5302 if (PyObject_HasAttrString(obj, "value")) {
5303 int res;
5304 tmp = PyObject_GetAttrString(obj, "value");
5305 if (tmp == NULL) goto failed;
5306 res = obj2ast_expr(tmp, &value, arena);
5307 if (res != 0) goto failed;
5308 Py_XDECREF(tmp);
5309 tmp = NULL;
5310 } else {
5311 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5312 return 1;
5313 }
5314 if (PyObject_HasAttrString(obj, "attr")) {
5315 int res;
5316 tmp = PyObject_GetAttrString(obj, "attr");
5317 if (tmp == NULL) goto failed;
5318 res = obj2ast_identifier(tmp, &attr, arena);
5319 if (res != 0) goto failed;
5320 Py_XDECREF(tmp);
5321 tmp = NULL;
5322 } else {
5323 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5324 return 1;
5325 }
5326 if (PyObject_HasAttrString(obj, "ctx")) {
5327 int res;
5328 tmp = PyObject_GetAttrString(obj, "ctx");
5329 if (tmp == NULL) goto failed;
5330 res = obj2ast_expr_context(tmp, &ctx, arena);
5331 if (res != 0) goto failed;
5332 Py_XDECREF(tmp);
5333 tmp = NULL;
5334 } else {
5335 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5336 return 1;
5337 }
5338 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5339 if (*out == NULL) goto failed;
5340 return 0;
5341 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005342 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5343 if (isinstance == -1) {
5344 return 1;
5345 }
5346 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005347 expr_ty value;
5348 slice_ty slice;
5349 expr_context_ty ctx;
5350
5351 if (PyObject_HasAttrString(obj, "value")) {
5352 int res;
5353 tmp = PyObject_GetAttrString(obj, "value");
5354 if (tmp == NULL) goto failed;
5355 res = obj2ast_expr(tmp, &value, arena);
5356 if (res != 0) goto failed;
5357 Py_XDECREF(tmp);
5358 tmp = NULL;
5359 } else {
5360 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5361 return 1;
5362 }
5363 if (PyObject_HasAttrString(obj, "slice")) {
5364 int res;
5365 tmp = PyObject_GetAttrString(obj, "slice");
5366 if (tmp == NULL) goto failed;
5367 res = obj2ast_slice(tmp, &slice, arena);
5368 if (res != 0) goto failed;
5369 Py_XDECREF(tmp);
5370 tmp = NULL;
5371 } else {
5372 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5373 return 1;
5374 }
5375 if (PyObject_HasAttrString(obj, "ctx")) {
5376 int res;
5377 tmp = PyObject_GetAttrString(obj, "ctx");
5378 if (tmp == NULL) goto failed;
5379 res = obj2ast_expr_context(tmp, &ctx, arena);
5380 if (res != 0) goto failed;
5381 Py_XDECREF(tmp);
5382 tmp = NULL;
5383 } else {
5384 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5385 return 1;
5386 }
5387 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5388 if (*out == NULL) goto failed;
5389 return 0;
5390 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005391 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5392 if (isinstance == -1) {
5393 return 1;
5394 }
5395 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005396 identifier id;
5397 expr_context_ty ctx;
5398
5399 if (PyObject_HasAttrString(obj, "id")) {
5400 int res;
5401 tmp = PyObject_GetAttrString(obj, "id");
5402 if (tmp == NULL) goto failed;
5403 res = obj2ast_identifier(tmp, &id, arena);
5404 if (res != 0) goto failed;
5405 Py_XDECREF(tmp);
5406 tmp = NULL;
5407 } else {
5408 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5409 return 1;
5410 }
5411 if (PyObject_HasAttrString(obj, "ctx")) {
5412 int res;
5413 tmp = PyObject_GetAttrString(obj, "ctx");
5414 if (tmp == NULL) goto failed;
5415 res = obj2ast_expr_context(tmp, &ctx, arena);
5416 if (res != 0) goto failed;
5417 Py_XDECREF(tmp);
5418 tmp = NULL;
5419 } else {
5420 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5421 return 1;
5422 }
5423 *out = Name(id, ctx, lineno, col_offset, arena);
5424 if (*out == NULL) goto failed;
5425 return 0;
5426 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005427 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5428 if (isinstance == -1) {
5429 return 1;
5430 }
5431 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005432 asdl_seq* elts;
5433 expr_context_ty ctx;
5434
5435 if (PyObject_HasAttrString(obj, "elts")) {
5436 int res;
5437 Py_ssize_t len;
5438 Py_ssize_t i;
5439 tmp = PyObject_GetAttrString(obj, "elts");
5440 if (tmp == NULL) goto failed;
5441 if (!PyList_Check(tmp)) {
5442 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5443 goto failed;
5444 }
5445 len = PyList_GET_SIZE(tmp);
5446 elts = asdl_seq_new(len, arena);
5447 if (elts == NULL) goto failed;
5448 for (i = 0; i < len; i++) {
5449 expr_ty value;
5450 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5451 if (res != 0) goto failed;
5452 asdl_seq_SET(elts, i, value);
5453 }
5454 Py_XDECREF(tmp);
5455 tmp = NULL;
5456 } else {
5457 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5458 return 1;
5459 }
5460 if (PyObject_HasAttrString(obj, "ctx")) {
5461 int res;
5462 tmp = PyObject_GetAttrString(obj, "ctx");
5463 if (tmp == NULL) goto failed;
5464 res = obj2ast_expr_context(tmp, &ctx, arena);
5465 if (res != 0) goto failed;
5466 Py_XDECREF(tmp);
5467 tmp = NULL;
5468 } else {
5469 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5470 return 1;
5471 }
5472 *out = List(elts, ctx, lineno, col_offset, arena);
5473 if (*out == NULL) goto failed;
5474 return 0;
5475 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005476 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5477 if (isinstance == -1) {
5478 return 1;
5479 }
5480 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005481 asdl_seq* elts;
5482 expr_context_ty ctx;
5483
5484 if (PyObject_HasAttrString(obj, "elts")) {
5485 int res;
5486 Py_ssize_t len;
5487 Py_ssize_t i;
5488 tmp = PyObject_GetAttrString(obj, "elts");
5489 if (tmp == NULL) goto failed;
5490 if (!PyList_Check(tmp)) {
5491 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5492 goto failed;
5493 }
5494 len = PyList_GET_SIZE(tmp);
5495 elts = asdl_seq_new(len, arena);
5496 if (elts == NULL) goto failed;
5497 for (i = 0; i < len; i++) {
5498 expr_ty value;
5499 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5500 if (res != 0) goto failed;
5501 asdl_seq_SET(elts, i, value);
5502 }
5503 Py_XDECREF(tmp);
5504 tmp = NULL;
5505 } else {
5506 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5507 return 1;
5508 }
5509 if (PyObject_HasAttrString(obj, "ctx")) {
5510 int res;
5511 tmp = PyObject_GetAttrString(obj, "ctx");
5512 if (tmp == NULL) goto failed;
5513 res = obj2ast_expr_context(tmp, &ctx, arena);
5514 if (res != 0) goto failed;
5515 Py_XDECREF(tmp);
5516 tmp = NULL;
5517 } else {
5518 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5519 return 1;
5520 }
5521 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5522 if (*out == NULL) goto failed;
5523 return 0;
5524 }
5525
5526 tmp = PyObject_Repr(obj);
5527 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005528 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005529failed:
5530 Py_XDECREF(tmp);
5531 return 1;
5532}
5533
5534int
5535obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5536{
5537 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005538 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005539
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005540 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5541 if (isinstance == -1) {
5542 return 1;
5543 }
5544 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005545 *out = Load;
5546 return 0;
5547 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005548 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5549 if (isinstance == -1) {
5550 return 1;
5551 }
5552 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005553 *out = Store;
5554 return 0;
5555 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005556 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5557 if (isinstance == -1) {
5558 return 1;
5559 }
5560 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005561 *out = Del;
5562 return 0;
5563 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005564 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5565 if (isinstance == -1) {
5566 return 1;
5567 }
5568 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005569 *out = AugLoad;
5570 return 0;
5571 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005572 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5573 if (isinstance == -1) {
5574 return 1;
5575 }
5576 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005577 *out = AugStore;
5578 return 0;
5579 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005580 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5581 if (isinstance == -1) {
5582 return 1;
5583 }
5584 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005585 *out = Param;
5586 return 0;
5587 }
5588
5589 tmp = PyObject_Repr(obj);
5590 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005591 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005592failed:
5593 Py_XDECREF(tmp);
5594 return 1;
5595}
5596
5597int
5598obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5599{
5600 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005601 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005602
5603
5604 if (obj == Py_None) {
5605 *out = NULL;
5606 return 0;
5607 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005608 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5609 if (isinstance == -1) {
5610 return 1;
5611 }
5612 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005613
5614 *out = Ellipsis(arena);
5615 if (*out == NULL) goto failed;
5616 return 0;
5617 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005618 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5619 if (isinstance == -1) {
5620 return 1;
5621 }
5622 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005623 expr_ty lower;
5624 expr_ty upper;
5625 expr_ty step;
5626
5627 if (PyObject_HasAttrString(obj, "lower")) {
5628 int res;
5629 tmp = PyObject_GetAttrString(obj, "lower");
5630 if (tmp == NULL) goto failed;
5631 res = obj2ast_expr(tmp, &lower, arena);
5632 if (res != 0) goto failed;
5633 Py_XDECREF(tmp);
5634 tmp = NULL;
5635 } else {
5636 lower = NULL;
5637 }
5638 if (PyObject_HasAttrString(obj, "upper")) {
5639 int res;
5640 tmp = PyObject_GetAttrString(obj, "upper");
5641 if (tmp == NULL) goto failed;
5642 res = obj2ast_expr(tmp, &upper, arena);
5643 if (res != 0) goto failed;
5644 Py_XDECREF(tmp);
5645 tmp = NULL;
5646 } else {
5647 upper = NULL;
5648 }
5649 if (PyObject_HasAttrString(obj, "step")) {
5650 int res;
5651 tmp = PyObject_GetAttrString(obj, "step");
5652 if (tmp == NULL) goto failed;
5653 res = obj2ast_expr(tmp, &step, arena);
5654 if (res != 0) goto failed;
5655 Py_XDECREF(tmp);
5656 tmp = NULL;
5657 } else {
5658 step = NULL;
5659 }
5660 *out = Slice(lower, upper, step, arena);
5661 if (*out == NULL) goto failed;
5662 return 0;
5663 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005664 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5665 if (isinstance == -1) {
5666 return 1;
5667 }
5668 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005669 asdl_seq* dims;
5670
5671 if (PyObject_HasAttrString(obj, "dims")) {
5672 int res;
5673 Py_ssize_t len;
5674 Py_ssize_t i;
5675 tmp = PyObject_GetAttrString(obj, "dims");
5676 if (tmp == NULL) goto failed;
5677 if (!PyList_Check(tmp)) {
5678 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5679 goto failed;
5680 }
5681 len = PyList_GET_SIZE(tmp);
5682 dims = asdl_seq_new(len, arena);
5683 if (dims == NULL) goto failed;
5684 for (i = 0; i < len; i++) {
5685 slice_ty value;
5686 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5687 if (res != 0) goto failed;
5688 asdl_seq_SET(dims, i, value);
5689 }
5690 Py_XDECREF(tmp);
5691 tmp = NULL;
5692 } else {
5693 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5694 return 1;
5695 }
5696 *out = ExtSlice(dims, arena);
5697 if (*out == NULL) goto failed;
5698 return 0;
5699 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005700 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5701 if (isinstance == -1) {
5702 return 1;
5703 }
5704 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005705 expr_ty value;
5706
5707 if (PyObject_HasAttrString(obj, "value")) {
5708 int res;
5709 tmp = PyObject_GetAttrString(obj, "value");
5710 if (tmp == NULL) goto failed;
5711 res = obj2ast_expr(tmp, &value, arena);
5712 if (res != 0) goto failed;
5713 Py_XDECREF(tmp);
5714 tmp = NULL;
5715 } else {
5716 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5717 return 1;
5718 }
5719 *out = Index(value, arena);
5720 if (*out == NULL) goto failed;
5721 return 0;
5722 }
5723
5724 tmp = PyObject_Repr(obj);
5725 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005726 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005727failed:
5728 Py_XDECREF(tmp);
5729 return 1;
5730}
5731
5732int
5733obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5734{
5735 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005736 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005737
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005738 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5739 if (isinstance == -1) {
5740 return 1;
5741 }
5742 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005743 *out = And;
5744 return 0;
5745 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005746 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5747 if (isinstance == -1) {
5748 return 1;
5749 }
5750 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005751 *out = Or;
5752 return 0;
5753 }
5754
5755 tmp = PyObject_Repr(obj);
5756 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005757 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005758failed:
5759 Py_XDECREF(tmp);
5760 return 1;
5761}
5762
5763int
5764obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5765{
5766 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005767 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005768
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005769 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
5770 if (isinstance == -1) {
5771 return 1;
5772 }
5773 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005774 *out = Add;
5775 return 0;
5776 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005777 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
5778 if (isinstance == -1) {
5779 return 1;
5780 }
5781 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005782 *out = Sub;
5783 return 0;
5784 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005785 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
5786 if (isinstance == -1) {
5787 return 1;
5788 }
5789 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005790 *out = Mult;
5791 return 0;
5792 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005793 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
5794 if (isinstance == -1) {
5795 return 1;
5796 }
5797 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005798 *out = Div;
5799 return 0;
5800 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005801 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
5802 if (isinstance == -1) {
5803 return 1;
5804 }
5805 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005806 *out = Mod;
5807 return 0;
5808 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005809 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
5810 if (isinstance == -1) {
5811 return 1;
5812 }
5813 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005814 *out = Pow;
5815 return 0;
5816 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005817 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
5818 if (isinstance == -1) {
5819 return 1;
5820 }
5821 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005822 *out = LShift;
5823 return 0;
5824 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005825 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
5826 if (isinstance == -1) {
5827 return 1;
5828 }
5829 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005830 *out = RShift;
5831 return 0;
5832 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005833 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
5834 if (isinstance == -1) {
5835 return 1;
5836 }
5837 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005838 *out = BitOr;
5839 return 0;
5840 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005841 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
5842 if (isinstance == -1) {
5843 return 1;
5844 }
5845 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005846 *out = BitXor;
5847 return 0;
5848 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005849 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
5850 if (isinstance == -1) {
5851 return 1;
5852 }
5853 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005854 *out = BitAnd;
5855 return 0;
5856 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005857 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
5858 if (isinstance == -1) {
5859 return 1;
5860 }
5861 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005862 *out = FloorDiv;
5863 return 0;
5864 }
5865
5866 tmp = PyObject_Repr(obj);
5867 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005868 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005869failed:
5870 Py_XDECREF(tmp);
5871 return 1;
5872}
5873
5874int
5875obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5876{
5877 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005878 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005879
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005880 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
5881 if (isinstance == -1) {
5882 return 1;
5883 }
5884 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005885 *out = Invert;
5886 return 0;
5887 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005888 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
5889 if (isinstance == -1) {
5890 return 1;
5891 }
5892 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005893 *out = Not;
5894 return 0;
5895 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005896 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
5897 if (isinstance == -1) {
5898 return 1;
5899 }
5900 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005901 *out = UAdd;
5902 return 0;
5903 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005904 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
5905 if (isinstance == -1) {
5906 return 1;
5907 }
5908 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005909 *out = USub;
5910 return 0;
5911 }
5912
5913 tmp = PyObject_Repr(obj);
5914 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005915 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005916failed:
5917 Py_XDECREF(tmp);
5918 return 1;
5919}
5920
5921int
5922obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5923{
5924 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005925 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005926
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005927 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
5928 if (isinstance == -1) {
5929 return 1;
5930 }
5931 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005932 *out = Eq;
5933 return 0;
5934 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005935 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
5936 if (isinstance == -1) {
5937 return 1;
5938 }
5939 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005940 *out = NotEq;
5941 return 0;
5942 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005943 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
5944 if (isinstance == -1) {
5945 return 1;
5946 }
5947 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005948 *out = Lt;
5949 return 0;
5950 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005951 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
5952 if (isinstance == -1) {
5953 return 1;
5954 }
5955 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005956 *out = LtE;
5957 return 0;
5958 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005959 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
5960 if (isinstance == -1) {
5961 return 1;
5962 }
5963 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005964 *out = Gt;
5965 return 0;
5966 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005967 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
5968 if (isinstance == -1) {
5969 return 1;
5970 }
5971 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005972 *out = GtE;
5973 return 0;
5974 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005975 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
5976 if (isinstance == -1) {
5977 return 1;
5978 }
5979 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005980 *out = Is;
5981 return 0;
5982 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005983 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
5984 if (isinstance == -1) {
5985 return 1;
5986 }
5987 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005988 *out = IsNot;
5989 return 0;
5990 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005991 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
5992 if (isinstance == -1) {
5993 return 1;
5994 }
5995 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005996 *out = In;
5997 return 0;
5998 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005999 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6000 if (isinstance == -1) {
6001 return 1;
6002 }
6003 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006004 *out = NotIn;
6005 return 0;
6006 }
6007
6008 tmp = PyObject_Repr(obj);
6009 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006010 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006011failed:
6012 Py_XDECREF(tmp);
6013 return 1;
6014}
6015
6016int
6017obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6018{
6019 PyObject* tmp = NULL;
6020 expr_ty target;
6021 expr_ty iter;
6022 asdl_seq* ifs;
6023
6024 if (PyObject_HasAttrString(obj, "target")) {
6025 int res;
6026 tmp = PyObject_GetAttrString(obj, "target");
6027 if (tmp == NULL) goto failed;
6028 res = obj2ast_expr(tmp, &target, arena);
6029 if (res != 0) goto failed;
6030 Py_XDECREF(tmp);
6031 tmp = NULL;
6032 } else {
6033 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6034 return 1;
6035 }
6036 if (PyObject_HasAttrString(obj, "iter")) {
6037 int res;
6038 tmp = PyObject_GetAttrString(obj, "iter");
6039 if (tmp == NULL) goto failed;
6040 res = obj2ast_expr(tmp, &iter, arena);
6041 if (res != 0) goto failed;
6042 Py_XDECREF(tmp);
6043 tmp = NULL;
6044 } else {
6045 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6046 return 1;
6047 }
6048 if (PyObject_HasAttrString(obj, "ifs")) {
6049 int res;
6050 Py_ssize_t len;
6051 Py_ssize_t i;
6052 tmp = PyObject_GetAttrString(obj, "ifs");
6053 if (tmp == NULL) goto failed;
6054 if (!PyList_Check(tmp)) {
6055 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6056 goto failed;
6057 }
6058 len = PyList_GET_SIZE(tmp);
6059 ifs = asdl_seq_new(len, arena);
6060 if (ifs == NULL) goto failed;
6061 for (i = 0; i < len; i++) {
6062 expr_ty value;
6063 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6064 if (res != 0) goto failed;
6065 asdl_seq_SET(ifs, i, value);
6066 }
6067 Py_XDECREF(tmp);
6068 tmp = NULL;
6069 } else {
6070 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6071 return 1;
6072 }
6073 *out = comprehension(target, iter, ifs, arena);
6074 return 0;
6075failed:
6076 Py_XDECREF(tmp);
6077 return 1;
6078}
6079
6080int
6081obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6082{
6083 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006084 int isinstance;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006085
Georg Brandlfc8eef32008-03-28 12:11:56 +00006086 int lineno;
6087 int col_offset;
6088
Georg Brandla48f3ab2008-03-30 06:40:17 +00006089 if (obj == Py_None) {
6090 *out = NULL;
6091 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006092 }
6093 if (PyObject_HasAttrString(obj, "lineno")) {
6094 int res;
6095 tmp = PyObject_GetAttrString(obj, "lineno");
6096 if (tmp == NULL) goto failed;
6097 res = obj2ast_int(tmp, &lineno, arena);
6098 if (res != 0) goto failed;
6099 Py_XDECREF(tmp);
6100 tmp = NULL;
6101 } else {
6102 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6103 return 1;
6104 }
6105 if (PyObject_HasAttrString(obj, "col_offset")) {
6106 int res;
6107 tmp = PyObject_GetAttrString(obj, "col_offset");
6108 if (tmp == NULL) goto failed;
6109 res = obj2ast_int(tmp, &col_offset, arena);
6110 if (res != 0) goto failed;
6111 Py_XDECREF(tmp);
6112 tmp = NULL;
6113 } else {
6114 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6115 return 1;
6116 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006117 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6118 if (isinstance == -1) {
6119 return 1;
6120 }
6121 if (isinstance) {
Georg Brandla48f3ab2008-03-30 06:40:17 +00006122 expr_ty type;
6123 expr_ty name;
6124 asdl_seq* body;
6125
6126 if (PyObject_HasAttrString(obj, "type")) {
6127 int res;
6128 tmp = PyObject_GetAttrString(obj, "type");
6129 if (tmp == NULL) goto failed;
6130 res = obj2ast_expr(tmp, &type, arena);
6131 if (res != 0) goto failed;
6132 Py_XDECREF(tmp);
6133 tmp = NULL;
6134 } else {
6135 type = NULL;
6136 }
6137 if (PyObject_HasAttrString(obj, "name")) {
6138 int res;
6139 tmp = PyObject_GetAttrString(obj, "name");
6140 if (tmp == NULL) goto failed;
6141 res = obj2ast_expr(tmp, &name, arena);
6142 if (res != 0) goto failed;
6143 Py_XDECREF(tmp);
6144 tmp = NULL;
6145 } else {
6146 name = NULL;
6147 }
6148 if (PyObject_HasAttrString(obj, "body")) {
6149 int res;
6150 Py_ssize_t len;
6151 Py_ssize_t i;
6152 tmp = PyObject_GetAttrString(obj, "body");
6153 if (tmp == NULL) goto failed;
6154 if (!PyList_Check(tmp)) {
6155 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6156 goto failed;
6157 }
6158 len = PyList_GET_SIZE(tmp);
6159 body = asdl_seq_new(len, arena);
6160 if (body == NULL) goto failed;
6161 for (i = 0; i < len; i++) {
6162 stmt_ty value;
6163 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6164 if (res != 0) goto failed;
6165 asdl_seq_SET(body, i, value);
6166 }
6167 Py_XDECREF(tmp);
6168 tmp = NULL;
6169 } else {
6170 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6171 return 1;
6172 }
6173 *out = ExceptHandler(type, name, body, lineno, col_offset,
6174 arena);
6175 if (*out == NULL) goto failed;
6176 return 0;
6177 }
6178
6179 tmp = PyObject_Repr(obj);
6180 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006181 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006182failed:
6183 Py_XDECREF(tmp);
6184 return 1;
6185}
6186
6187int
6188obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6189{
6190 PyObject* tmp = NULL;
6191 asdl_seq* args;
6192 identifier vararg;
6193 identifier kwarg;
6194 asdl_seq* defaults;
6195
6196 if (PyObject_HasAttrString(obj, "args")) {
6197 int res;
6198 Py_ssize_t len;
6199 Py_ssize_t i;
6200 tmp = PyObject_GetAttrString(obj, "args");
6201 if (tmp == NULL) goto failed;
6202 if (!PyList_Check(tmp)) {
6203 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6204 goto failed;
6205 }
6206 len = PyList_GET_SIZE(tmp);
6207 args = asdl_seq_new(len, arena);
6208 if (args == NULL) goto failed;
6209 for (i = 0; i < len; i++) {
6210 expr_ty value;
6211 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6212 if (res != 0) goto failed;
6213 asdl_seq_SET(args, i, value);
6214 }
6215 Py_XDECREF(tmp);
6216 tmp = NULL;
6217 } else {
6218 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6219 return 1;
6220 }
6221 if (PyObject_HasAttrString(obj, "vararg")) {
6222 int res;
6223 tmp = PyObject_GetAttrString(obj, "vararg");
6224 if (tmp == NULL) goto failed;
6225 res = obj2ast_identifier(tmp, &vararg, arena);
6226 if (res != 0) goto failed;
6227 Py_XDECREF(tmp);
6228 tmp = NULL;
6229 } else {
6230 vararg = NULL;
6231 }
6232 if (PyObject_HasAttrString(obj, "kwarg")) {
6233 int res;
6234 tmp = PyObject_GetAttrString(obj, "kwarg");
6235 if (tmp == NULL) goto failed;
6236 res = obj2ast_identifier(tmp, &kwarg, arena);
6237 if (res != 0) goto failed;
6238 Py_XDECREF(tmp);
6239 tmp = NULL;
6240 } else {
6241 kwarg = NULL;
6242 }
6243 if (PyObject_HasAttrString(obj, "defaults")) {
6244 int res;
6245 Py_ssize_t len;
6246 Py_ssize_t i;
6247 tmp = PyObject_GetAttrString(obj, "defaults");
6248 if (tmp == NULL) goto failed;
6249 if (!PyList_Check(tmp)) {
6250 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6251 goto failed;
6252 }
6253 len = PyList_GET_SIZE(tmp);
6254 defaults = asdl_seq_new(len, arena);
6255 if (defaults == NULL) goto failed;
6256 for (i = 0; i < len; i++) {
6257 expr_ty value;
6258 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6259 if (res != 0) goto failed;
6260 asdl_seq_SET(defaults, i, value);
6261 }
6262 Py_XDECREF(tmp);
6263 tmp = NULL;
6264 } else {
6265 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6266 return 1;
6267 }
6268 *out = arguments(args, vararg, kwarg, defaults, arena);
6269 return 0;
6270failed:
6271 Py_XDECREF(tmp);
6272 return 1;
6273}
6274
6275int
6276obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6277{
6278 PyObject* tmp = NULL;
6279 identifier arg;
6280 expr_ty value;
6281
6282 if (PyObject_HasAttrString(obj, "arg")) {
6283 int res;
6284 tmp = PyObject_GetAttrString(obj, "arg");
6285 if (tmp == NULL) goto failed;
6286 res = obj2ast_identifier(tmp, &arg, arena);
6287 if (res != 0) goto failed;
6288 Py_XDECREF(tmp);
6289 tmp = NULL;
6290 } else {
6291 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6292 return 1;
6293 }
6294 if (PyObject_HasAttrString(obj, "value")) {
6295 int res;
6296 tmp = PyObject_GetAttrString(obj, "value");
6297 if (tmp == NULL) goto failed;
6298 res = obj2ast_expr(tmp, &value, arena);
6299 if (res != 0) goto failed;
6300 Py_XDECREF(tmp);
6301 tmp = NULL;
6302 } else {
6303 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6304 return 1;
6305 }
6306 *out = keyword(arg, value, arena);
6307 return 0;
6308failed:
6309 Py_XDECREF(tmp);
6310 return 1;
6311}
6312
6313int
6314obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6315{
6316 PyObject* tmp = NULL;
6317 identifier name;
6318 identifier asname;
6319
6320 if (PyObject_HasAttrString(obj, "name")) {
6321 int res;
6322 tmp = PyObject_GetAttrString(obj, "name");
6323 if (tmp == NULL) goto failed;
6324 res = obj2ast_identifier(tmp, &name, arena);
6325 if (res != 0) goto failed;
6326 Py_XDECREF(tmp);
6327 tmp = NULL;
6328 } else {
6329 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6330 return 1;
6331 }
6332 if (PyObject_HasAttrString(obj, "asname")) {
6333 int res;
6334 tmp = PyObject_GetAttrString(obj, "asname");
6335 if (tmp == NULL) goto failed;
6336 res = obj2ast_identifier(tmp, &asname, arena);
6337 if (res != 0) goto failed;
6338 Py_XDECREF(tmp);
6339 tmp = NULL;
6340 } else {
6341 asname = NULL;
6342 }
6343 *out = alias(name, asname, arena);
6344 return 0;
6345failed:
6346 Py_XDECREF(tmp);
6347 return 1;
6348}
6349
6350
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006351PyMODINIT_FUNC
6352init_ast(void)
6353{
6354 PyObject *m, *d;
6355 if (!init_types()) return;
6356 m = Py_InitModule3("_ast", NULL, NULL);
6357 if (!m) return;
6358 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00006359 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006360 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6361 return;
Benjamin Peterson8fa3e932009-06-13 20:30:48 +00006362 if (PyModule_AddStringConstant(m, "__version__", "73421") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006363 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006364 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6365 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6366 return;
6367 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6368 < 0) return;
6369 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6370 0) return;
6371 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6372 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6373 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6374 < 0) return;
6375 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6376 return;
6377 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6378 return;
6379 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6380 return;
6381 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6382 return;
6383 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6384 0) return;
6385 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
6386 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6387 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6388 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6389 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6390 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6391 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6392 0) return;
6393 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6394 0) return;
6395 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6396 return;
6397 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6398 return;
6399 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6400 0) return;
6401 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
6402 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6403 return;
6404 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6405 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6406 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6407 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6408 return;
6409 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6410 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6411 return;
6412 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6413 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6414 return;
6415 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6416 return;
6417 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6418 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00006419 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006420 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6421 return;
6422 if (PyDict_SetItemString(d, "GeneratorExp",
6423 (PyObject*)GeneratorExp_type) < 0) return;
6424 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6425 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6426 return;
6427 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6428 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6429 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6430 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6431 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6432 0) return;
6433 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6434 0) return;
6435 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6436 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6437 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6438 if (PyDict_SetItemString(d, "expr_context",
6439 (PyObject*)expr_context_type) < 0) return;
6440 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6441 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6442 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6443 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6444 return;
6445 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6446 return;
6447 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6448 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6449 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6450 return;
6451 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6452 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6453 return;
6454 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6455 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6456 return;
6457 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6458 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6459 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6460 return;
6461 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6462 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6463 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6464 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6465 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6466 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6467 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6468 return;
6469 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6470 return;
6471 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6472 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6473 return;
6474 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6475 return;
6476 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6477 return;
6478 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6479 return;
6480 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6481 return;
6482 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6483 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6484 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6485 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6486 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6487 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6488 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6489 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6490 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6491 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6492 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6493 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6494 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6495 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6496 if (PyDict_SetItemString(d, "comprehension",
6497 (PyObject*)comprehension_type) < 0) return;
6498 if (PyDict_SetItemString(d, "excepthandler",
6499 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006500 if (PyDict_SetItemString(d, "ExceptHandler",
6501 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006502 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6503 0) return;
6504 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6505 return;
6506 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006507}
6508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006509
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006510PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006511{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006512 init_types();
6513 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006514}
6515
Georg Brandlf2bfd542008-03-29 13:24:23 +00006516/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6517mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006518{
6519 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006520 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6521 (PyObject*)Interactive_type};
6522 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006523 int isinstance;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006524 assert(0 <= mode && mode <= 2);
6525
Georg Brandlfc8eef32008-03-28 12:11:56 +00006526 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006527
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006528 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6529 if (isinstance == -1)
6530 return NULL;
6531 if (!isinstance) {
Georg Brandlf2bfd542008-03-29 13:24:23 +00006532 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6533 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006534 return NULL;
6535 }
6536 if (obj2ast_mod(ast, &res, arena) != 0)
6537 return NULL;
6538 else
6539 return res;
6540}
6541
6542int PyAST_Check(PyObject* obj)
6543{
6544 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006545 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006546}
6547
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006548