blob: 9a93bf3e47f1c939ee3f5d7f9ea75fe6c5f4be07 [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Alexandre Vassalotti102d70e2010-01-11 22:46:43 +00005 __version__ 77422.
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};
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000200static PyTypeObject *SetComp_type;
201static char *SetComp_fields[]={
202 "elt",
203 "generators",
204};
205static PyTypeObject *DictComp_type;
206static char *DictComp_fields[]={
207 "key",
208 "value",
209 "generators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *GeneratorExp_type;
212static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "elt",
214 "generators",
215};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000216static PyTypeObject *Yield_type;
217static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000218 "value",
219};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000220static PyTypeObject *Compare_type;
221static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000222 "left",
223 "ops",
224 "comparators",
225};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000226static PyTypeObject *Call_type;
227static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000228 "func",
229 "args",
230 "keywords",
231 "starargs",
232 "kwargs",
233};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000234static PyTypeObject *Repr_type;
235static char *Repr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000236 "value",
237};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000238static PyTypeObject *Num_type;
239static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000240 "n",
241};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000242static PyTypeObject *Str_type;
243static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000244 "s",
245};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000246static PyTypeObject *Attribute_type;
247static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000248 "value",
249 "attr",
250 "ctx",
251};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000252static PyTypeObject *Subscript_type;
253static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000254 "value",
255 "slice",
256 "ctx",
257};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000258static PyTypeObject *Name_type;
259static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000260 "id",
261 "ctx",
262};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000263static PyTypeObject *List_type;
264static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000265 "elts",
266 "ctx",
267};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000268static PyTypeObject *Tuple_type;
269static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000270 "elts",
271 "ctx",
272};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000273static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000274static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
275*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
276static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *Load_type;
278static PyTypeObject *Store_type;
279static PyTypeObject *Del_type;
280static PyTypeObject *AugLoad_type;
281static PyTypeObject *AugStore_type;
282static PyTypeObject *Param_type;
283static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000284static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *Ellipsis_type;
286static PyTypeObject *Slice_type;
287static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000288 "lower",
289 "upper",
290 "step",
291};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000292static PyTypeObject *ExtSlice_type;
293static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000294 "dims",
295};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Index_type;
297static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000298 "value",
299};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000300static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000301static PyObject *And_singleton, *Or_singleton;
302static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000303static PyTypeObject *And_type;
304static PyTypeObject *Or_type;
305static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000306static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
307*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
308*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
309*FloorDiv_singleton;
310static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000311static PyTypeObject *Add_type;
312static PyTypeObject *Sub_type;
313static PyTypeObject *Mult_type;
314static PyTypeObject *Div_type;
315static PyTypeObject *Mod_type;
316static PyTypeObject *Pow_type;
317static PyTypeObject *LShift_type;
318static PyTypeObject *RShift_type;
319static PyTypeObject *BitOr_type;
320static PyTypeObject *BitXor_type;
321static PyTypeObject *BitAnd_type;
322static PyTypeObject *FloorDiv_type;
323static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000324static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
325*USub_singleton;
326static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000327static PyTypeObject *Invert_type;
328static PyTypeObject *Not_type;
329static PyTypeObject *UAdd_type;
330static PyTypeObject *USub_type;
331static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
333*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
334*NotIn_singleton;
335static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000336static PyTypeObject *Eq_type;
337static PyTypeObject *NotEq_type;
338static PyTypeObject *Lt_type;
339static PyTypeObject *LtE_type;
340static PyTypeObject *Gt_type;
341static PyTypeObject *GtE_type;
342static PyTypeObject *Is_type;
343static PyTypeObject *IsNot_type;
344static PyTypeObject *In_type;
345static PyTypeObject *NotIn_type;
346static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000347static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000348static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000349 "target",
350 "iter",
351 "ifs",
352};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000353static PyTypeObject *excepthandler_type;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000354static char *excepthandler_attributes[] = {
355 "lineno",
356 "col_offset",
357};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358static PyObject* ast2obj_excepthandler(void*);
Georg Brandla48f3ab2008-03-30 06:40:17 +0000359static PyTypeObject *ExceptHandler_type;
360static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "type",
362 "name",
363 "body",
364};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000365static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000366static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000367static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000368 "args",
369 "vararg",
370 "kwarg",
371 "defaults",
372};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000373static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000374static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000375static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000376 "arg",
377 "value",
378};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000379static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000380static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000381static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000382 "name",
383 "asname",
384};
385
386
Georg Brandlc52ed592008-03-30 07:01:47 +0000387static int
388ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
389{
390 Py_ssize_t i, numfields = 0;
391 int res = -1;
392 PyObject *key, *value, *fields;
393 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
394 if (!fields)
395 PyErr_Clear();
396 if (fields) {
397 numfields = PySequence_Size(fields);
398 if (numfields == -1)
399 goto cleanup;
400 }
401 res = 0; /* if no error occurs, this stays 0 to the end */
402 if (PyTuple_GET_SIZE(args) > 0) {
403 if (numfields != PyTuple_GET_SIZE(args)) {
Georg Brandl1721e752008-03-30 19:43:27 +0000404 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc05e34492008-09-10 22:04:45 +0000405 "%zd positional argument%s",
Georg Brandl1721e752008-03-30 19:43:27 +0000406 Py_TYPE(self)->tp_name,
407 numfields == 0 ? "" : "either 0 or ",
Georg Brandlc52ed592008-03-30 07:01:47 +0000408 numfields, numfields == 1 ? "" : "s");
409 res = -1;
410 goto cleanup;
411 }
412 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
413 /* cannot be reached when fields is NULL */
414 PyObject *name = PySequence_GetItem(fields, i);
415 if (!name) {
416 res = -1;
417 goto cleanup;
418 }
419 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
420 Py_DECREF(name);
421 if (res < 0)
422 goto cleanup;
423 }
424 }
425 if (kw) {
426 i = 0; /* needed by PyDict_Next */
427 while (PyDict_Next(kw, &i, &key, &value)) {
428 res = PyObject_SetAttr(self, key, value);
429 if (res < 0)
430 goto cleanup;
431 }
432 }
433 cleanup:
434 Py_XDECREF(fields);
435 return res;
436}
437
Georg Brandle34c21c2008-03-30 20:20:39 +0000438/* Pickling support */
439static PyObject *
440ast_type_reduce(PyObject *self, PyObject *unused)
441{
442 PyObject *res;
443 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
444 if (dict == NULL) {
445 if (PyErr_ExceptionMatches(PyExc_AttributeError))
446 PyErr_Clear();
447 else
448 return NULL;
449 }
450 if (dict) {
451 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
452 Py_DECREF(dict);
453 return res;
454 }
455 return Py_BuildValue("O()", Py_TYPE(self));
456}
457
458static PyMethodDef ast_type_methods[] = {
459 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
460 {NULL}
461};
462
Georg Brandlc52ed592008-03-30 07:01:47 +0000463static PyTypeObject AST_type = {
464 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Georg Brandle34c21c2008-03-30 20:20:39 +0000465 "_ast.AST",
Georg Brandlc52ed592008-03-30 07:01:47 +0000466 sizeof(PyObject),
467 0,
468 0, /* tp_dealloc */
469 0, /* tp_print */
470 0, /* tp_getattr */
471 0, /* tp_setattr */
472 0, /* tp_compare */
473 0, /* tp_repr */
474 0, /* tp_as_number */
475 0, /* tp_as_sequence */
476 0, /* tp_as_mapping */
477 0, /* tp_hash */
478 0, /* tp_call */
479 0, /* tp_str */
480 PyObject_GenericGetAttr, /* tp_getattro */
481 PyObject_GenericSetAttr, /* tp_setattro */
482 0, /* tp_as_buffer */
483 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
484 0, /* tp_doc */
485 0, /* tp_traverse */
486 0, /* tp_clear */
487 0, /* tp_richcompare */
488 0, /* tp_weaklistoffset */
489 0, /* tp_iter */
490 0, /* tp_iternext */
Georg Brandle34c21c2008-03-30 20:20:39 +0000491 ast_type_methods, /* tp_methods */
Georg Brandlc52ed592008-03-30 07:01:47 +0000492 0, /* tp_members */
493 0, /* tp_getset */
494 0, /* tp_base */
495 0, /* tp_dict */
496 0, /* tp_descr_get */
497 0, /* tp_descr_set */
498 0, /* tp_dictoffset */
499 (initproc)ast_type_init, /* tp_init */
500 PyType_GenericAlloc, /* tp_alloc */
501 PyType_GenericNew, /* tp_new */
502 PyObject_Del, /* tp_free */
503};
504
505
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000506static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
507{
508 PyObject *fnames, *result;
509 int i;
Georg Brandl2c55c592008-03-30 19:00:49 +0000510 fnames = PyTuple_New(num_fields);
511 if (!fnames) return NULL;
512 for (i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000513 PyObject *field = PyString_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000514 if (!field) {
515 Py_DECREF(fnames);
516 return NULL;
517 }
518 PyTuple_SET_ITEM(fnames, i, field);
519 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000520 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000521 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000522 Py_DECREF(fnames);
523 return (PyTypeObject*)result;
524}
525
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000526static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
527{
Neal Norwitz19379f12006-04-03 04:50:58 +0000528 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000529 PyObject *s, *l = PyTuple_New(num_fields);
Benjamin Peterson1056ca22010-06-09 19:45:04 +0000530 if (!l)
531 return 0;
532 for (i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000533 s = PyString_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000534 if (!s) {
535 Py_DECREF(l);
536 return 0;
537 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000538 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000539 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000540 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
541 Py_DECREF(l);
542 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000543}
544
Georg Brandlfc8eef32008-03-28 12:11:56 +0000545/* Conversion AST -> Python */
546
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000547static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
548{
549 int i, n = asdl_seq_LEN(seq);
550 PyObject *result = PyList_New(n);
551 PyObject *value;
552 if (!result)
553 return NULL;
554 for (i = 0; i < n; i++) {
555 value = func(asdl_seq_GET(seq, i));
556 if (!value) {
557 Py_DECREF(result);
558 return NULL;
559 }
560 PyList_SET_ITEM(result, i, value);
561 }
562 return result;
563}
564
565static PyObject* ast2obj_object(void *o)
566{
567 if (!o)
568 o = Py_None;
569 Py_INCREF((PyObject*)o);
570 return (PyObject*)o;
571}
572#define ast2obj_identifier ast2obj_object
573#define ast2obj_string ast2obj_object
574static PyObject* ast2obj_bool(bool b)
575{
576 return PyBool_FromLong(b);
577}
578
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000579static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000580{
581 return PyInt_FromLong(b);
582}
583
Georg Brandlfc8eef32008-03-28 12:11:56 +0000584/* Conversion Python -> AST */
585
586static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
587{
588 if (obj == Py_None)
589 obj = NULL;
590 if (obj)
591 PyArena_AddPyObject(arena, obj);
592 Py_XINCREF(obj);
593 *out = obj;
594 return 0;
595}
596
597#define obj2ast_identifier obj2ast_object
598#define obj2ast_string obj2ast_object
599
600static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
601{
602 int i;
603 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
604 PyObject *s = PyObject_Repr(obj);
605 if (s == NULL) return 1;
606 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000607 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000608 Py_DECREF(s);
609 return 1;
610 }
611
612 i = (int)PyLong_AsLong(obj);
613 if (i == -1 && PyErr_Occurred())
614 return 1;
615 *out = i;
616 return 0;
617}
618
619static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
620{
621 if (!PyBool_Check(obj)) {
622 PyObject *s = PyObject_Repr(obj);
623 if (s == NULL) return 1;
624 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000625 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000626 Py_DECREF(s);
627 return 1;
628 }
629
630 *out = (obj == Py_True);
631 return 0;
632}
633
Benjamin Petersonaff36f12008-10-19 13:59:01 +0000634static int add_ast_fields(void)
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000635{
636 PyObject *empty_tuple, *d;
637 if (PyType_Ready(&AST_type) < 0)
638 return -1;
639 d = AST_type.tp_dict;
640 empty_tuple = PyTuple_New(0);
641 if (!empty_tuple ||
642 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
643 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
644 Py_XDECREF(empty_tuple);
645 return -1;
646 }
647 Py_DECREF(empty_tuple);
648 return 0;
649}
650
Georg Brandlfc8eef32008-03-28 12:11:56 +0000651
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652static int init_types(void)
653{
Neal Norwitz19379f12006-04-03 04:50:58 +0000654 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000655 if (initialized) return 1;
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000656 if (add_ast_fields() < 0) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000657 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!mod_type) return 0;
659 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000660 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000662 Interactive_type = make_type("Interactive", mod_type,
663 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000665 Expression_type = make_type("Expression", mod_type, Expression_fields,
666 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000670 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000672 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 FunctionDef_type = make_type("FunctionDef", stmt_type,
674 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000676 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000694 With_type = make_type("With", stmt_type, With_fields, 3);
695 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
701 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000705 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000707 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000708 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000710 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000716 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000718 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000720 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000722 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000724 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000725 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000733 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Dict_type) return 0;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000737 Set_type = make_type("Set", expr_type, Set_fields, 1);
738 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!ListComp_type) return 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000741 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
742 if (!SetComp_type) return 0;
743 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
744 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000745 GeneratorExp_type = make_type("GeneratorExp", expr_type,
746 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000754 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000760 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000764 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000768 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000770 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!expr_context_type) return 0;
772 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000773 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000777 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000779 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000781 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000783 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000785 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000789 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000793 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000795 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000797 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!slice_type) return 0;
799 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000800 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000806 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000808 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!boolop_type) return 0;
810 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000811 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000813 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000815 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000816 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000817 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000819 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!operator_type) return 0;
821 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000850 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000852 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000854 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000856 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000858 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000860 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000862 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000866 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000868 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000870 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!unaryop_type) return 0;
872 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000873 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000875 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000877 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000879 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000881 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000883 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000885 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000887 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000889 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000890 if (!cmpop_type) return 0;
891 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000892 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000894 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000896 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000898 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000900 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000902 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000904 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000906 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000908 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000911 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000912 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000913 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000914 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000915 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000916 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000917 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000918 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000919 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000920 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000922 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000924 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000926 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000927 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000928 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000929 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000930 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000931 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000932 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000933 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000934 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000935 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000936 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000937 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
938 return 0;
939 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
940 ExceptHandler_fields, 3);
941 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000942 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000943 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000944 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000945 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000946 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000947 if (!alias_type) return 0;
948 initialized = 1;
949 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000950}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000951
Georg Brandlfc8eef32008-03-28 12:11:56 +0000952static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
953static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
954static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
955static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
956 arena);
957static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
958static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
959static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
960static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
961static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
962static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
963 arena);
964static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
965 arena);
966static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
967static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
968static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
969
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000971Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972{
973 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000974 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000975 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 p->kind = Module_kind;
978 p->v.Module.body = body;
979 return p;
980}
981
982mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000983Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984{
985 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000986 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000987 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 p->kind = Interactive_kind;
990 p->v.Interactive.body = body;
991 return p;
992}
993
994mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000995Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996{
997 mod_ty p;
998 if (!body) {
999 PyErr_SetString(PyExc_ValueError,
1000 "field body is required for Expression");
1001 return NULL;
1002 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001003 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001004 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 p->kind = Expression_kind;
1007 p->v.Expression.body = body;
1008 return p;
1009}
1010
1011mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
1014 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001015 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001016 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 p->kind = Suite_kind;
1019 p->v.Suite.body = body;
1020 return p;
1021}
1022
1023stmt_ty
1024FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +00001025 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026{
1027 stmt_ty p;
1028 if (!name) {
1029 PyErr_SetString(PyExc_ValueError,
1030 "field name is required for FunctionDef");
1031 return NULL;
1032 }
1033 if (!args) {
1034 PyErr_SetString(PyExc_ValueError,
1035 "field args is required for FunctionDef");
1036 return NULL;
1037 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001038 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001039 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 p->kind = FunctionDef_kind;
1042 p->v.FunctionDef.name = name;
1043 p->v.FunctionDef.args = args;
1044 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001045 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001047 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048 return p;
1049}
1050
1051stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001052ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1053 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054{
1055 stmt_ty p;
1056 if (!name) {
1057 PyErr_SetString(PyExc_ValueError,
1058 "field name is required for ClassDef");
1059 return NULL;
1060 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001061 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001062 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 p->kind = ClassDef_kind;
1065 p->v.ClassDef.name = name;
1066 p->v.ClassDef.bases = bases;
1067 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001068 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001070 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 return p;
1072}
1073
1074stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001075Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076{
1077 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001078 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001079 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 p->kind = Return_kind;
1082 p->v.Return.value = value;
1083 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001084 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 return p;
1086}
1087
1088stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001089Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090{
1091 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001092 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001093 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095 p->kind = Delete_kind;
1096 p->v.Delete.targets = targets;
1097 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001098 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099 return p;
1100}
1101
1102stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001103Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1104 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105{
1106 stmt_ty p;
1107 if (!value) {
1108 PyErr_SetString(PyExc_ValueError,
1109 "field value is required for Assign");
1110 return NULL;
1111 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001112 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001113 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115 p->kind = Assign_kind;
1116 p->v.Assign.targets = targets;
1117 p->v.Assign.value = value;
1118 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001119 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return p;
1121}
1122
1123stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001124AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1125 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126{
1127 stmt_ty p;
1128 if (!target) {
1129 PyErr_SetString(PyExc_ValueError,
1130 "field target is required for AugAssign");
1131 return NULL;
1132 }
1133 if (!op) {
1134 PyErr_SetString(PyExc_ValueError,
1135 "field op is required for AugAssign");
1136 return NULL;
1137 }
1138 if (!value) {
1139 PyErr_SetString(PyExc_ValueError,
1140 "field value is required for AugAssign");
1141 return NULL;
1142 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001143 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001144 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 p->kind = AugAssign_kind;
1147 p->v.AugAssign.target = target;
1148 p->v.AugAssign.op = op;
1149 p->v.AugAssign.value = value;
1150 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001151 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 return p;
1153}
1154
1155stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001156Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1157 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158{
1159 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001160 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001161 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 p->kind = Print_kind;
1164 p->v.Print.dest = dest;
1165 p->v.Print.values = values;
1166 p->v.Print.nl = nl;
1167 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001168 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return p;
1170}
1171
1172stmt_ty
1173For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
1176 stmt_ty p;
1177 if (!target) {
1178 PyErr_SetString(PyExc_ValueError,
1179 "field target is required for For");
1180 return NULL;
1181 }
1182 if (!iter) {
1183 PyErr_SetString(PyExc_ValueError,
1184 "field iter is required for For");
1185 return NULL;
1186 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001187 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001188 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 p->kind = For_kind;
1191 p->v.For.target = target;
1192 p->v.For.iter = iter;
1193 p->v.For.body = body;
1194 p->v.For.orelse = orelse;
1195 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001196 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return p;
1198}
1199
1200stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001201While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1202 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
1204 stmt_ty p;
1205 if (!test) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "field test is required for While");
1208 return NULL;
1209 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001211 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 p->kind = While_kind;
1214 p->v.While.test = test;
1215 p->v.While.body = body;
1216 p->v.While.orelse = orelse;
1217 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 return p;
1220}
1221
1222stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1224 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225{
1226 stmt_ty p;
1227 if (!test) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "field test is required for If");
1230 return NULL;
1231 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001233 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 p->kind = If_kind;
1236 p->v.If.test = test;
1237 p->v.If.body = body;
1238 p->v.If.orelse = orelse;
1239 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 return p;
1242}
1243
1244stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001245With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001246 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001247{
1248 stmt_ty p;
1249 if (!context_expr) {
1250 PyErr_SetString(PyExc_ValueError,
1251 "field context_expr is required for With");
1252 return NULL;
1253 }
1254 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001255 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001256 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001257 p->kind = With_kind;
1258 p->v.With.context_expr = context_expr;
1259 p->v.With.optional_vars = optional_vars;
1260 p->v.With.body = body;
1261 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001262 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001263 return p;
1264}
1265
1266stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001267Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1268 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269{
1270 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001272 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 p->kind = Raise_kind;
1275 p->v.Raise.type = type;
1276 p->v.Raise.inst = inst;
1277 p->v.Raise.tback = tback;
1278 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return p;
1281}
1282
1283stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001285 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286{
1287 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001288 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001289 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 p->kind = TryExcept_kind;
1292 p->v.TryExcept.body = body;
1293 p->v.TryExcept.handlers = handlers;
1294 p->v.TryExcept.orelse = orelse;
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 +00001301TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1302 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303{
1304 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001305 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001306 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 p->kind = TryFinally_kind;
1309 p->v.TryFinally.body = body;
1310 p->v.TryFinally.finalbody = finalbody;
1311 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001312 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313 return p;
1314}
1315
1316stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001317Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318{
1319 stmt_ty p;
1320 if (!test) {
1321 PyErr_SetString(PyExc_ValueError,
1322 "field test is required for Assert");
1323 return NULL;
1324 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001325 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001326 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001328 p->kind = Assert_kind;
1329 p->v.Assert.test = test;
1330 p->v.Assert.msg = msg;
1331 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001332 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 return p;
1334}
1335
1336stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001337Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338{
1339 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001340 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001341 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 p->kind = Import_kind;
1344 p->v.Import.names = names;
1345 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001346 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 return p;
1348}
1349
1350stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001351ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1352 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353{
1354 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001356 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 p->kind = ImportFrom_kind;
1359 p->v.ImportFrom.module = module;
1360 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001361 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 return p;
1365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1369 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370{
1371 stmt_ty p;
1372 if (!body) {
1373 PyErr_SetString(PyExc_ValueError,
1374 "field body is required for Exec");
1375 return NULL;
1376 }
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 = Exec_kind;
1381 p->v.Exec.body = body;
1382 p->v.Exec.globals = globals;
1383 p->v.Exec.locals = locals;
1384 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001385 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001386 return p;
1387}
1388
1389stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001390Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391{
1392 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001393 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001394 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 p->kind = Global_kind;
1397 p->v.Global.names = names;
1398 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001399 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 return p;
1401}
1402
1403stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001404Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405{
1406 stmt_ty p;
1407 if (!value) {
1408 PyErr_SetString(PyExc_ValueError,
1409 "field value is required for Expr");
1410 return NULL;
1411 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001412 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001413 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 p->kind = Expr_kind;
1416 p->v.Expr.value = value;
1417 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001418 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return p;
1420}
1421
1422stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001423Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424{
1425 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001426 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001427 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 p->kind = Pass_kind;
1430 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001431 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return p;
1433}
1434
1435stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001436Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437{
1438 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001439 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001440 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 p->kind = Break_kind;
1443 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001444 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 return p;
1446}
1447
1448stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001449Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450{
1451 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001452 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001453 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 p->kind = Continue_kind;
1456 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001457 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 return p;
1459}
1460
1461expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001462BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1463 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464{
1465 expr_ty p;
1466 if (!op) {
1467 PyErr_SetString(PyExc_ValueError,
1468 "field op is required for BoolOp");
1469 return NULL;
1470 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001471 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001472 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 p->kind = BoolOp_kind;
1475 p->v.BoolOp.op = op;
1476 p->v.BoolOp.values = values;
1477 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001478 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 return p;
1480}
1481
1482expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001483BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1484 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485{
1486 expr_ty p;
1487 if (!left) {
1488 PyErr_SetString(PyExc_ValueError,
1489 "field left is required for BinOp");
1490 return NULL;
1491 }
1492 if (!op) {
1493 PyErr_SetString(PyExc_ValueError,
1494 "field op is required for BinOp");
1495 return NULL;
1496 }
1497 if (!right) {
1498 PyErr_SetString(PyExc_ValueError,
1499 "field right is required for BinOp");
1500 return NULL;
1501 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001502 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001503 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505 p->kind = BinOp_kind;
1506 p->v.BinOp.left = left;
1507 p->v.BinOp.op = op;
1508 p->v.BinOp.right = right;
1509 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511 return p;
1512}
1513
1514expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001515UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1516 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517{
1518 expr_ty p;
1519 if (!op) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field op is required for UnaryOp");
1522 return NULL;
1523 }
1524 if (!operand) {
1525 PyErr_SetString(PyExc_ValueError,
1526 "field operand is required for UnaryOp");
1527 return NULL;
1528 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001529 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001530 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 p->kind = UnaryOp_kind;
1533 p->v.UnaryOp.op = op;
1534 p->v.UnaryOp.operand = operand;
1535 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001536 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 return p;
1538}
1539
1540expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001541Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1542 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543{
1544 expr_ty p;
1545 if (!args) {
1546 PyErr_SetString(PyExc_ValueError,
1547 "field args is required for Lambda");
1548 return NULL;
1549 }
1550 if (!body) {
1551 PyErr_SetString(PyExc_ValueError,
1552 "field body is required for Lambda");
1553 return NULL;
1554 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001555 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001556 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 p->kind = Lambda_kind;
1559 p->v.Lambda.args = args;
1560 p->v.Lambda.body = body;
1561 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001562 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563 return p;
1564}
1565
1566expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001567IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1568 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001569{
1570 expr_ty p;
1571 if (!test) {
1572 PyErr_SetString(PyExc_ValueError,
1573 "field test is required for IfExp");
1574 return NULL;
1575 }
1576 if (!body) {
1577 PyErr_SetString(PyExc_ValueError,
1578 "field body is required for IfExp");
1579 return NULL;
1580 }
1581 if (!orelse) {
1582 PyErr_SetString(PyExc_ValueError,
1583 "field orelse is required for IfExp");
1584 return NULL;
1585 }
1586 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001587 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001588 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001589 p->kind = IfExp_kind;
1590 p->v.IfExp.test = test;
1591 p->v.IfExp.body = body;
1592 p->v.IfExp.orelse = orelse;
1593 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001594 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001595 return p;
1596}
1597
1598expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001599Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1600 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601{
1602 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001603 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001604 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 p->kind = Dict_kind;
1607 p->v.Dict.keys = keys;
1608 p->v.Dict.values = values;
1609 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001610 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 return p;
1612}
1613
1614expr_ty
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001615Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1616{
1617 expr_ty p;
1618 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1619 if (!p)
1620 return NULL;
1621 p->kind = Set_kind;
1622 p->v.Set.elts = elts;
1623 p->lineno = lineno;
1624 p->col_offset = col_offset;
1625 return p;
1626}
1627
1628expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001629ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1630 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631{
1632 expr_ty p;
1633 if (!elt) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field elt is required for ListComp");
1636 return NULL;
1637 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001639 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 p->kind = ListComp_kind;
1642 p->v.ListComp.elt = elt;
1643 p->v.ListComp.generators = generators;
1644 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001645 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 return p;
1647}
1648
1649expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001650SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1651 *arena)
1652{
1653 expr_ty p;
1654 if (!elt) {
1655 PyErr_SetString(PyExc_ValueError,
1656 "field elt is required for SetComp");
1657 return NULL;
1658 }
1659 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1660 if (!p)
1661 return NULL;
1662 p->kind = SetComp_kind;
1663 p->v.SetComp.elt = elt;
1664 p->v.SetComp.generators = generators;
1665 p->lineno = lineno;
1666 p->col_offset = col_offset;
1667 return p;
1668}
1669
1670expr_ty
1671DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1672 col_offset, PyArena *arena)
1673{
1674 expr_ty p;
1675 if (!key) {
1676 PyErr_SetString(PyExc_ValueError,
1677 "field key is required for DictComp");
1678 return NULL;
1679 }
1680 if (!value) {
1681 PyErr_SetString(PyExc_ValueError,
1682 "field value is required for DictComp");
1683 return NULL;
1684 }
1685 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1686 if (!p)
1687 return NULL;
1688 p->kind = DictComp_kind;
1689 p->v.DictComp.key = key;
1690 p->v.DictComp.value = value;
1691 p->v.DictComp.generators = generators;
1692 p->lineno = lineno;
1693 p->col_offset = col_offset;
1694 return p;
1695}
1696
1697expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001698GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1699 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700{
1701 expr_ty p;
1702 if (!elt) {
1703 PyErr_SetString(PyExc_ValueError,
1704 "field elt is required for GeneratorExp");
1705 return NULL;
1706 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001707 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001708 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 p->kind = GeneratorExp_kind;
1711 p->v.GeneratorExp.elt = elt;
1712 p->v.GeneratorExp.generators = generators;
1713 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001714 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715 return p;
1716}
1717
1718expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001719Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720{
1721 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001722 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001723 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 p->kind = Yield_kind;
1726 p->v.Yield.value = value;
1727 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 return p;
1730}
1731
1732expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001733Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1734 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735{
1736 expr_ty p;
1737 if (!left) {
1738 PyErr_SetString(PyExc_ValueError,
1739 "field left is required for Compare");
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 = Compare_kind;
1746 p->v.Compare.left = left;
1747 p->v.Compare.ops = ops;
1748 p->v.Compare.comparators = comparators;
1749 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001750 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 return p;
1752}
1753
1754expr_ty
1755Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001756 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757{
1758 expr_ty p;
1759 if (!func) {
1760 PyErr_SetString(PyExc_ValueError,
1761 "field func is required for Call");
1762 return NULL;
1763 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001764 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001765 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 p->kind = Call_kind;
1768 p->v.Call.func = func;
1769 p->v.Call.args = args;
1770 p->v.Call.keywords = keywords;
1771 p->v.Call.starargs = starargs;
1772 p->v.Call.kwargs = kwargs;
1773 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001774 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 return p;
1776}
1777
1778expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001779Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780{
1781 expr_ty p;
1782 if (!value) {
1783 PyErr_SetString(PyExc_ValueError,
1784 "field value is required for Repr");
1785 return NULL;
1786 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001787 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001788 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790 p->kind = Repr_kind;
1791 p->v.Repr.value = value;
1792 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001793 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return p;
1795}
1796
1797expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001798Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799{
1800 expr_ty p;
1801 if (!n) {
1802 PyErr_SetString(PyExc_ValueError,
1803 "field n is required for Num");
1804 return NULL;
1805 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001806 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001807 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 p->kind = Num_kind;
1810 p->v.Num.n = n;
1811 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001812 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 return p;
1814}
1815
1816expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001817Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
1819 expr_ty p;
1820 if (!s) {
1821 PyErr_SetString(PyExc_ValueError,
1822 "field s is required for Str");
1823 return NULL;
1824 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001825 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001826 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 p->kind = Str_kind;
1829 p->v.Str.s = s;
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 +00001836Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1837 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838{
1839 expr_ty p;
1840 if (!value) {
1841 PyErr_SetString(PyExc_ValueError,
1842 "field value is required for Attribute");
1843 return NULL;
1844 }
1845 if (!attr) {
1846 PyErr_SetString(PyExc_ValueError,
1847 "field attr is required for Attribute");
1848 return NULL;
1849 }
1850 if (!ctx) {
1851 PyErr_SetString(PyExc_ValueError,
1852 "field ctx is required for Attribute");
1853 return NULL;
1854 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001855 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001856 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 p->kind = Attribute_kind;
1859 p->v.Attribute.value = value;
1860 p->v.Attribute.attr = attr;
1861 p->v.Attribute.ctx = ctx;
1862 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001863 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 return p;
1865}
1866
1867expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001868Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1869 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870{
1871 expr_ty p;
1872 if (!value) {
1873 PyErr_SetString(PyExc_ValueError,
1874 "field value is required for Subscript");
1875 return NULL;
1876 }
1877 if (!slice) {
1878 PyErr_SetString(PyExc_ValueError,
1879 "field slice is required for Subscript");
1880 return NULL;
1881 }
1882 if (!ctx) {
1883 PyErr_SetString(PyExc_ValueError,
1884 "field ctx is required for Subscript");
1885 return NULL;
1886 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001887 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001888 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 p->kind = Subscript_kind;
1891 p->v.Subscript.value = value;
1892 p->v.Subscript.slice = slice;
1893 p->v.Subscript.ctx = ctx;
1894 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001895 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 return p;
1897}
1898
1899expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001900Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1901 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902{
1903 expr_ty p;
1904 if (!id) {
1905 PyErr_SetString(PyExc_ValueError,
1906 "field id is required for Name");
1907 return NULL;
1908 }
1909 if (!ctx) {
1910 PyErr_SetString(PyExc_ValueError,
1911 "field ctx is required for Name");
1912 return NULL;
1913 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001914 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001915 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 p->kind = Name_kind;
1918 p->v.Name.id = id;
1919 p->v.Name.ctx = ctx;
1920 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001921 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 return p;
1923}
1924
1925expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001926List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1927 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928{
1929 expr_ty p;
1930 if (!ctx) {
1931 PyErr_SetString(PyExc_ValueError,
1932 "field ctx is required for List");
1933 return NULL;
1934 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001935 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001936 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938 p->kind = List_kind;
1939 p->v.List.elts = elts;
1940 p->v.List.ctx = ctx;
1941 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001942 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 return p;
1944}
1945
1946expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001947Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1948 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949{
1950 expr_ty p;
1951 if (!ctx) {
1952 PyErr_SetString(PyExc_ValueError,
1953 "field ctx is required for Tuple");
1954 return NULL;
1955 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001956 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001957 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 p->kind = Tuple_kind;
1960 p->v.Tuple.elts = elts;
1961 p->v.Tuple.ctx = ctx;
1962 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001963 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001964 return p;
1965}
1966
1967slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001968Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969{
1970 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001971 p = (slice_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->kind = Ellipsis_kind;
1975 return p;
1976}
1977
1978slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001979Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980{
1981 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001983 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 p->kind = Slice_kind;
1986 p->v.Slice.lower = lower;
1987 p->v.Slice.upper = upper;
1988 p->v.Slice.step = step;
1989 return p;
1990}
1991
1992slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994{
1995 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001996 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001997 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 p->kind = ExtSlice_kind;
2000 p->v.ExtSlice.dims = dims;
2001 return p;
2002}
2003
2004slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002005Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006{
2007 slice_ty p;
2008 if (!value) {
2009 PyErr_SetString(PyExc_ValueError,
2010 "field value is required for Index");
2011 return NULL;
2012 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002013 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002014 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 p->kind = Index_kind;
2017 p->v.Index.value = value;
2018 return p;
2019}
2020
2021comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023{
2024 comprehension_ty p;
2025 if (!target) {
2026 PyErr_SetString(PyExc_ValueError,
2027 "field target is required for comprehension");
2028 return NULL;
2029 }
2030 if (!iter) {
2031 PyErr_SetString(PyExc_ValueError,
2032 "field iter is required for comprehension");
2033 return NULL;
2034 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002035 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002036 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 p->target = target;
2039 p->iter = iter;
2040 p->ifs = ifs;
2041 return p;
2042}
2043
2044excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00002045ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002046 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047{
2048 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002049 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002050 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00002052 p->kind = ExceptHandler_kind;
2053 p->v.ExceptHandler.type = type;
2054 p->v.ExceptHandler.name = name;
2055 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002056 p->lineno = lineno;
2057 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 return p;
2059}
2060
2061arguments_ty
2062arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002063 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064{
2065 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002066 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002067 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 p->args = args;
2070 p->vararg = vararg;
2071 p->kwarg = kwarg;
2072 p->defaults = defaults;
2073 return p;
2074}
2075
2076keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002077keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078{
2079 keyword_ty p;
2080 if (!arg) {
2081 PyErr_SetString(PyExc_ValueError,
2082 "field arg is required for keyword");
2083 return NULL;
2084 }
2085 if (!value) {
2086 PyErr_SetString(PyExc_ValueError,
2087 "field value is required for keyword");
2088 return NULL;
2089 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002090 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002091 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 p->arg = arg;
2094 p->value = value;
2095 return p;
2096}
2097
2098alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002099alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100{
2101 alias_ty p;
2102 if (!name) {
2103 PyErr_SetString(PyExc_ValueError,
2104 "field name is required for alias");
2105 return NULL;
2106 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002108 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 p->name = name;
2111 p->asname = asname;
2112 return p;
2113}
2114
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002115
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002116PyObject*
2117ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002118{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002119 mod_ty o = (mod_ty)_o;
2120 PyObject *result = NULL, *value = NULL;
2121 if (!o) {
2122 Py_INCREF(Py_None);
2123 return Py_None;
2124 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 switch (o->kind) {
2127 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002128 result = PyType_GenericNew(Module_type, NULL, NULL);
2129 if (!result) goto failed;
2130 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2131 if (!value) goto failed;
2132 if (PyObject_SetAttrString(result, "body", value) == -1)
2133 goto failed;
2134 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 break;
2136 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002137 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2138 if (!result) goto failed;
2139 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2140 if (!value) goto failed;
2141 if (PyObject_SetAttrString(result, "body", value) == -1)
2142 goto failed;
2143 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 break;
2145 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002146 result = PyType_GenericNew(Expression_type, NULL, NULL);
2147 if (!result) goto failed;
2148 value = ast2obj_expr(o->v.Expression.body);
2149 if (!value) goto failed;
2150 if (PyObject_SetAttrString(result, "body", value) == -1)
2151 goto failed;
2152 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 break;
2154 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002155 result = PyType_GenericNew(Suite_type, NULL, NULL);
2156 if (!result) goto failed;
2157 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2158 if (!value) goto failed;
2159 if (PyObject_SetAttrString(result, "body", value) == -1)
2160 goto failed;
2161 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 break;
2163 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002164 return result;
2165failed:
2166 Py_XDECREF(value);
2167 Py_XDECREF(result);
2168 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169}
2170
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002171PyObject*
2172ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002174 stmt_ty o = (stmt_ty)_o;
2175 PyObject *result = NULL, *value = NULL;
2176 if (!o) {
2177 Py_INCREF(Py_None);
2178 return Py_None;
2179 }
2180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 switch (o->kind) {
2182 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002183 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2184 if (!result) goto failed;
2185 value = ast2obj_identifier(o->v.FunctionDef.name);
2186 if (!value) goto failed;
2187 if (PyObject_SetAttrString(result, "name", value) == -1)
2188 goto failed;
2189 Py_DECREF(value);
2190 value = ast2obj_arguments(o->v.FunctionDef.args);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "args", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2196 if (!value) goto failed;
2197 if (PyObject_SetAttrString(result, "body", value) == -1)
2198 goto failed;
2199 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002200 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2201 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002202 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002203 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2204 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002205 goto failed;
2206 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 break;
2208 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002209 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2210 if (!result) goto failed;
2211 value = ast2obj_identifier(o->v.ClassDef.name);
2212 if (!value) goto failed;
2213 if (PyObject_SetAttrString(result, "name", value) == -1)
2214 goto failed;
2215 Py_DECREF(value);
2216 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2217 if (!value) goto failed;
2218 if (PyObject_SetAttrString(result, "bases", value) == -1)
2219 goto failed;
2220 Py_DECREF(value);
2221 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2222 if (!value) goto failed;
2223 if (PyObject_SetAttrString(result, "body", value) == -1)
2224 goto failed;
2225 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002226 value = ast2obj_list(o->v.ClassDef.decorator_list,
2227 ast2obj_expr);
2228 if (!value) goto failed;
2229 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2230 -1)
2231 goto failed;
2232 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233 break;
2234 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002235 result = PyType_GenericNew(Return_type, NULL, NULL);
2236 if (!result) goto failed;
2237 value = ast2obj_expr(o->v.Return.value);
2238 if (!value) goto failed;
2239 if (PyObject_SetAttrString(result, "value", value) == -1)
2240 goto failed;
2241 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 break;
2243 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002244 result = PyType_GenericNew(Delete_type, NULL, NULL);
2245 if (!result) goto failed;
2246 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2247 if (!value) goto failed;
2248 if (PyObject_SetAttrString(result, "targets", value) == -1)
2249 goto failed;
2250 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 break;
2252 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002253 result = PyType_GenericNew(Assign_type, NULL, NULL);
2254 if (!result) goto failed;
2255 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2256 if (!value) goto failed;
2257 if (PyObject_SetAttrString(result, "targets", value) == -1)
2258 goto failed;
2259 Py_DECREF(value);
2260 value = ast2obj_expr(o->v.Assign.value);
2261 if (!value) goto failed;
2262 if (PyObject_SetAttrString(result, "value", value) == -1)
2263 goto failed;
2264 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 break;
2266 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002267 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2268 if (!result) goto failed;
2269 value = ast2obj_expr(o->v.AugAssign.target);
2270 if (!value) goto failed;
2271 if (PyObject_SetAttrString(result, "target", value) == -1)
2272 goto failed;
2273 Py_DECREF(value);
2274 value = ast2obj_operator(o->v.AugAssign.op);
2275 if (!value) goto failed;
2276 if (PyObject_SetAttrString(result, "op", value) == -1)
2277 goto failed;
2278 Py_DECREF(value);
2279 value = ast2obj_expr(o->v.AugAssign.value);
2280 if (!value) goto failed;
2281 if (PyObject_SetAttrString(result, "value", value) == -1)
2282 goto failed;
2283 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 break;
2285 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002286 result = PyType_GenericNew(Print_type, NULL, NULL);
2287 if (!result) goto failed;
2288 value = ast2obj_expr(o->v.Print.dest);
2289 if (!value) goto failed;
2290 if (PyObject_SetAttrString(result, "dest", value) == -1)
2291 goto failed;
2292 Py_DECREF(value);
2293 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2294 if (!value) goto failed;
2295 if (PyObject_SetAttrString(result, "values", value) == -1)
2296 goto failed;
2297 Py_DECREF(value);
2298 value = ast2obj_bool(o->v.Print.nl);
2299 if (!value) goto failed;
2300 if (PyObject_SetAttrString(result, "nl", value) == -1)
2301 goto failed;
2302 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 break;
2304 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002305 result = PyType_GenericNew(For_type, NULL, NULL);
2306 if (!result) goto failed;
2307 value = ast2obj_expr(o->v.For.target);
2308 if (!value) goto failed;
2309 if (PyObject_SetAttrString(result, "target", value) == -1)
2310 goto failed;
2311 Py_DECREF(value);
2312 value = ast2obj_expr(o->v.For.iter);
2313 if (!value) goto failed;
2314 if (PyObject_SetAttrString(result, "iter", value) == -1)
2315 goto failed;
2316 Py_DECREF(value);
2317 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2318 if (!value) goto failed;
2319 if (PyObject_SetAttrString(result, "body", value) == -1)
2320 goto failed;
2321 Py_DECREF(value);
2322 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2323 if (!value) goto failed;
2324 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2325 goto failed;
2326 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 break;
2328 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002329 result = PyType_GenericNew(While_type, NULL, NULL);
2330 if (!result) goto failed;
2331 value = ast2obj_expr(o->v.While.test);
2332 if (!value) goto failed;
2333 if (PyObject_SetAttrString(result, "test", value) == -1)
2334 goto failed;
2335 Py_DECREF(value);
2336 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2337 if (!value) goto failed;
2338 if (PyObject_SetAttrString(result, "body", value) == -1)
2339 goto failed;
2340 Py_DECREF(value);
2341 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2342 if (!value) goto failed;
2343 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2344 goto failed;
2345 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 break;
2347 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002348 result = PyType_GenericNew(If_type, NULL, NULL);
2349 if (!result) goto failed;
2350 value = ast2obj_expr(o->v.If.test);
2351 if (!value) goto failed;
2352 if (PyObject_SetAttrString(result, "test", value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
2355 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2356 if (!value) goto failed;
2357 if (PyObject_SetAttrString(result, "body", value) == -1)
2358 goto failed;
2359 Py_DECREF(value);
2360 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2361 if (!value) goto failed;
2362 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2363 goto failed;
2364 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002366 case With_kind:
2367 result = PyType_GenericNew(With_type, NULL, NULL);
2368 if (!result) goto failed;
2369 value = ast2obj_expr(o->v.With.context_expr);
2370 if (!value) goto failed;
2371 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2372 goto failed;
2373 Py_DECREF(value);
2374 value = ast2obj_expr(o->v.With.optional_vars);
2375 if (!value) goto failed;
2376 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2377 -1)
2378 goto failed;
2379 Py_DECREF(value);
2380 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2381 if (!value) goto failed;
2382 if (PyObject_SetAttrString(result, "body", value) == -1)
2383 goto failed;
2384 Py_DECREF(value);
2385 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002387 result = PyType_GenericNew(Raise_type, NULL, NULL);
2388 if (!result) goto failed;
2389 value = ast2obj_expr(o->v.Raise.type);
2390 if (!value) goto failed;
2391 if (PyObject_SetAttrString(result, "type", value) == -1)
2392 goto failed;
2393 Py_DECREF(value);
2394 value = ast2obj_expr(o->v.Raise.inst);
2395 if (!value) goto failed;
2396 if (PyObject_SetAttrString(result, "inst", value) == -1)
2397 goto failed;
2398 Py_DECREF(value);
2399 value = ast2obj_expr(o->v.Raise.tback);
2400 if (!value) goto failed;
2401 if (PyObject_SetAttrString(result, "tback", value) == -1)
2402 goto failed;
2403 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 break;
2405 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002406 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2407 if (!result) goto failed;
2408 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2409 if (!value) goto failed;
2410 if (PyObject_SetAttrString(result, "body", value) == -1)
2411 goto failed;
2412 Py_DECREF(value);
2413 value = ast2obj_list(o->v.TryExcept.handlers,
2414 ast2obj_excepthandler);
2415 if (!value) goto failed;
2416 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2417 goto failed;
2418 Py_DECREF(value);
2419 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 break;
2425 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2427 if (!result) goto failed;
2428 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2429 if (!value) goto failed;
2430 if (PyObject_SetAttrString(result, "body", value) == -1)
2431 goto failed;
2432 Py_DECREF(value);
2433 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2434 if (!value) goto failed;
2435 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2436 goto failed;
2437 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 break;
2439 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002440 result = PyType_GenericNew(Assert_type, NULL, NULL);
2441 if (!result) goto failed;
2442 value = ast2obj_expr(o->v.Assert.test);
2443 if (!value) goto failed;
2444 if (PyObject_SetAttrString(result, "test", value) == -1)
2445 goto failed;
2446 Py_DECREF(value);
2447 value = ast2obj_expr(o->v.Assert.msg);
2448 if (!value) goto failed;
2449 if (PyObject_SetAttrString(result, "msg", value) == -1)
2450 goto failed;
2451 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 break;
2453 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002454 result = PyType_GenericNew(Import_type, NULL, NULL);
2455 if (!result) goto failed;
2456 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2457 if (!value) goto failed;
2458 if (PyObject_SetAttrString(result, "names", value) == -1)
2459 goto failed;
2460 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 break;
2462 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002463 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2464 if (!result) goto failed;
2465 value = ast2obj_identifier(o->v.ImportFrom.module);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "module", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
2470 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2471 if (!value) goto failed;
2472 if (PyObject_SetAttrString(result, "names", value) == -1)
2473 goto failed;
2474 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002475 value = ast2obj_int(o->v.ImportFrom.level);
2476 if (!value) goto failed;
2477 if (PyObject_SetAttrString(result, "level", value) == -1)
2478 goto failed;
2479 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 break;
2481 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002482 result = PyType_GenericNew(Exec_type, NULL, NULL);
2483 if (!result) goto failed;
2484 value = ast2obj_expr(o->v.Exec.body);
2485 if (!value) goto failed;
2486 if (PyObject_SetAttrString(result, "body", value) == -1)
2487 goto failed;
2488 Py_DECREF(value);
2489 value = ast2obj_expr(o->v.Exec.globals);
2490 if (!value) goto failed;
2491 if (PyObject_SetAttrString(result, "globals", value) == -1)
2492 goto failed;
2493 Py_DECREF(value);
2494 value = ast2obj_expr(o->v.Exec.locals);
2495 if (!value) goto failed;
2496 if (PyObject_SetAttrString(result, "locals", value) == -1)
2497 goto failed;
2498 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002499 break;
2500 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002501 result = PyType_GenericNew(Global_type, NULL, NULL);
2502 if (!result) goto failed;
2503 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2504 if (!value) goto failed;
2505 if (PyObject_SetAttrString(result, "names", value) == -1)
2506 goto failed;
2507 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 break;
2509 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002510 result = PyType_GenericNew(Expr_type, NULL, NULL);
2511 if (!result) goto failed;
2512 value = ast2obj_expr(o->v.Expr.value);
2513 if (!value) goto failed;
2514 if (PyObject_SetAttrString(result, "value", value) == -1)
2515 goto failed;
2516 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 break;
2518 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002519 result = PyType_GenericNew(Pass_type, NULL, NULL);
2520 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 break;
2522 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002523 result = PyType_GenericNew(Break_type, NULL, NULL);
2524 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 break;
2526 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002527 result = PyType_GenericNew(Continue_type, NULL, NULL);
2528 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 break;
2530 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002531 value = ast2obj_int(o->lineno);
2532 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002533 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2534 goto failed;
2535 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002536 value = ast2obj_int(o->col_offset);
2537 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002538 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2539 goto failed;
2540 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002541 return result;
2542failed:
2543 Py_XDECREF(value);
2544 Py_XDECREF(result);
2545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546}
2547
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002548PyObject*
2549ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002551 expr_ty o = (expr_ty)_o;
2552 PyObject *result = NULL, *value = NULL;
2553 if (!o) {
2554 Py_INCREF(Py_None);
2555 return Py_None;
2556 }
2557
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 switch (o->kind) {
2559 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002560 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2561 if (!result) goto failed;
2562 value = ast2obj_boolop(o->v.BoolOp.op);
2563 if (!value) goto failed;
2564 if (PyObject_SetAttrString(result, "op", value) == -1)
2565 goto failed;
2566 Py_DECREF(value);
2567 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2568 if (!value) goto failed;
2569 if (PyObject_SetAttrString(result, "values", value) == -1)
2570 goto failed;
2571 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 break;
2573 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002574 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2575 if (!result) goto failed;
2576 value = ast2obj_expr(o->v.BinOp.left);
2577 if (!value) goto failed;
2578 if (PyObject_SetAttrString(result, "left", value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
2581 value = ast2obj_operator(o->v.BinOp.op);
2582 if (!value) goto failed;
2583 if (PyObject_SetAttrString(result, "op", value) == -1)
2584 goto failed;
2585 Py_DECREF(value);
2586 value = ast2obj_expr(o->v.BinOp.right);
2587 if (!value) goto failed;
2588 if (PyObject_SetAttrString(result, "right", value) == -1)
2589 goto failed;
2590 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 break;
2592 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002593 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2594 if (!result) goto failed;
2595 value = ast2obj_unaryop(o->v.UnaryOp.op);
2596 if (!value) goto failed;
2597 if (PyObject_SetAttrString(result, "op", value) == -1)
2598 goto failed;
2599 Py_DECREF(value);
2600 value = ast2obj_expr(o->v.UnaryOp.operand);
2601 if (!value) goto failed;
2602 if (PyObject_SetAttrString(result, "operand", value) == -1)
2603 goto failed;
2604 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 break;
2606 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002607 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2608 if (!result) goto failed;
2609 value = ast2obj_arguments(o->v.Lambda.args);
2610 if (!value) goto failed;
2611 if (PyObject_SetAttrString(result, "args", value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 value = ast2obj_expr(o->v.Lambda.body);
2615 if (!value) goto failed;
2616 if (PyObject_SetAttrString(result, "body", value) == -1)
2617 goto failed;
2618 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002620 case IfExp_kind:
2621 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2622 if (!result) goto failed;
2623 value = ast2obj_expr(o->v.IfExp.test);
2624 if (!value) goto failed;
2625 if (PyObject_SetAttrString(result, "test", value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 value = ast2obj_expr(o->v.IfExp.body);
2629 if (!value) goto failed;
2630 if (PyObject_SetAttrString(result, "body", value) == -1)
2631 goto failed;
2632 Py_DECREF(value);
2633 value = ast2obj_expr(o->v.IfExp.orelse);
2634 if (!value) goto failed;
2635 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2636 goto failed;
2637 Py_DECREF(value);
2638 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002640 result = PyType_GenericNew(Dict_type, NULL, NULL);
2641 if (!result) goto failed;
2642 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2643 if (!value) goto failed;
2644 if (PyObject_SetAttrString(result, "keys", value) == -1)
2645 goto failed;
2646 Py_DECREF(value);
2647 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2648 if (!value) goto failed;
2649 if (PyObject_SetAttrString(result, "values", value) == -1)
2650 goto failed;
2651 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002653 case Set_kind:
2654 result = PyType_GenericNew(Set_type, NULL, NULL);
2655 if (!result) goto failed;
2656 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2657 if (!value) goto failed;
2658 if (PyObject_SetAttrString(result, "elts", value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
2661 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002663 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_expr(o->v.ListComp.elt);
2666 if (!value) goto failed;
2667 if (PyObject_SetAttrString(result, "elt", value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
2670 value = ast2obj_list(o->v.ListComp.generators,
2671 ast2obj_comprehension);
2672 if (!value) goto failed;
2673 if (PyObject_SetAttrString(result, "generators", value) == -1)
2674 goto failed;
2675 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002677 case SetComp_kind:
2678 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2679 if (!result) goto failed;
2680 value = ast2obj_expr(o->v.SetComp.elt);
2681 if (!value) goto failed;
2682 if (PyObject_SetAttrString(result, "elt", value) == -1)
2683 goto failed;
2684 Py_DECREF(value);
2685 value = ast2obj_list(o->v.SetComp.generators,
2686 ast2obj_comprehension);
2687 if (!value) goto failed;
2688 if (PyObject_SetAttrString(result, "generators", value) == -1)
2689 goto failed;
2690 Py_DECREF(value);
2691 break;
2692 case DictComp_kind:
2693 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2694 if (!result) goto failed;
2695 value = ast2obj_expr(o->v.DictComp.key);
2696 if (!value) goto failed;
2697 if (PyObject_SetAttrString(result, "key", value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 value = ast2obj_expr(o->v.DictComp.value);
2701 if (!value) goto failed;
2702 if (PyObject_SetAttrString(result, "value", value) == -1)
2703 goto failed;
2704 Py_DECREF(value);
2705 value = ast2obj_list(o->v.DictComp.generators,
2706 ast2obj_comprehension);
2707 if (!value) goto failed;
2708 if (PyObject_SetAttrString(result, "generators", value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
2711 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002713 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2714 if (!result) goto failed;
2715 value = ast2obj_expr(o->v.GeneratorExp.elt);
2716 if (!value) goto failed;
2717 if (PyObject_SetAttrString(result, "elt", value) == -1)
2718 goto failed;
2719 Py_DECREF(value);
2720 value = ast2obj_list(o->v.GeneratorExp.generators,
2721 ast2obj_comprehension);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "generators", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 break;
2727 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002728 result = PyType_GenericNew(Yield_type, NULL, NULL);
2729 if (!result) goto failed;
2730 value = ast2obj_expr(o->v.Yield.value);
2731 if (!value) goto failed;
2732 if (PyObject_SetAttrString(result, "value", value) == -1)
2733 goto failed;
2734 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735 break;
2736 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002737 result = PyType_GenericNew(Compare_type, NULL, NULL);
2738 if (!result) goto failed;
2739 value = ast2obj_expr(o->v.Compare.left);
2740 if (!value) goto failed;
2741 if (PyObject_SetAttrString(result, "left", value) == -1)
2742 goto failed;
2743 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002744 {
2745 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2746 value = PyList_New(n);
2747 if (!value) goto failed;
2748 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002749 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 +00002750 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002751 if (!value) goto failed;
2752 if (PyObject_SetAttrString(result, "ops", value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
2755 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2756 if (!value) goto failed;
2757 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2758 goto failed;
2759 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 break;
2761 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002762 result = PyType_GenericNew(Call_type, NULL, NULL);
2763 if (!result) goto failed;
2764 value = ast2obj_expr(o->v.Call.func);
2765 if (!value) goto failed;
2766 if (PyObject_SetAttrString(result, "func", value) == -1)
2767 goto failed;
2768 Py_DECREF(value);
2769 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2770 if (!value) goto failed;
2771 if (PyObject_SetAttrString(result, "args", value) == -1)
2772 goto failed;
2773 Py_DECREF(value);
2774 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2775 if (!value) goto failed;
2776 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2777 goto failed;
2778 Py_DECREF(value);
2779 value = ast2obj_expr(o->v.Call.starargs);
2780 if (!value) goto failed;
2781 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2782 goto failed;
2783 Py_DECREF(value);
2784 value = ast2obj_expr(o->v.Call.kwargs);
2785 if (!value) goto failed;
2786 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2787 goto failed;
2788 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 break;
2790 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002791 result = PyType_GenericNew(Repr_type, NULL, NULL);
2792 if (!result) goto failed;
2793 value = ast2obj_expr(o->v.Repr.value);
2794 if (!value) goto failed;
2795 if (PyObject_SetAttrString(result, "value", value) == -1)
2796 goto failed;
2797 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 break;
2799 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002800 result = PyType_GenericNew(Num_type, NULL, NULL);
2801 if (!result) goto failed;
2802 value = ast2obj_object(o->v.Num.n);
2803 if (!value) goto failed;
2804 if (PyObject_SetAttrString(result, "n", value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807 break;
2808 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002809 result = PyType_GenericNew(Str_type, NULL, NULL);
2810 if (!result) goto failed;
2811 value = ast2obj_string(o->v.Str.s);
2812 if (!value) goto failed;
2813 if (PyObject_SetAttrString(result, "s", value) == -1)
2814 goto failed;
2815 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 break;
2817 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002818 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2819 if (!result) goto failed;
2820 value = ast2obj_expr(o->v.Attribute.value);
2821 if (!value) goto failed;
2822 if (PyObject_SetAttrString(result, "value", value) == -1)
2823 goto failed;
2824 Py_DECREF(value);
2825 value = ast2obj_identifier(o->v.Attribute.attr);
2826 if (!value) goto failed;
2827 if (PyObject_SetAttrString(result, "attr", value) == -1)
2828 goto failed;
2829 Py_DECREF(value);
2830 value = ast2obj_expr_context(o->v.Attribute.ctx);
2831 if (!value) goto failed;
2832 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2833 goto failed;
2834 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 break;
2836 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002837 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2838 if (!result) goto failed;
2839 value = ast2obj_expr(o->v.Subscript.value);
2840 if (!value) goto failed;
2841 if (PyObject_SetAttrString(result, "value", value) == -1)
2842 goto failed;
2843 Py_DECREF(value);
2844 value = ast2obj_slice(o->v.Subscript.slice);
2845 if (!value) goto failed;
2846 if (PyObject_SetAttrString(result, "slice", value) == -1)
2847 goto failed;
2848 Py_DECREF(value);
2849 value = ast2obj_expr_context(o->v.Subscript.ctx);
2850 if (!value) goto failed;
2851 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2852 goto failed;
2853 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 break;
2855 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002856 result = PyType_GenericNew(Name_type, NULL, NULL);
2857 if (!result) goto failed;
2858 value = ast2obj_identifier(o->v.Name.id);
2859 if (!value) goto failed;
2860 if (PyObject_SetAttrString(result, "id", value) == -1)
2861 goto failed;
2862 Py_DECREF(value);
2863 value = ast2obj_expr_context(o->v.Name.ctx);
2864 if (!value) goto failed;
2865 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2866 goto failed;
2867 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868 break;
2869 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002870 result = PyType_GenericNew(List_type, NULL, NULL);
2871 if (!result) goto failed;
2872 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2873 if (!value) goto failed;
2874 if (PyObject_SetAttrString(result, "elts", value) == -1)
2875 goto failed;
2876 Py_DECREF(value);
2877 value = ast2obj_expr_context(o->v.List.ctx);
2878 if (!value) goto failed;
2879 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2880 goto failed;
2881 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 break;
2883 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002884 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2885 if (!result) goto failed;
2886 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2887 if (!value) goto failed;
2888 if (PyObject_SetAttrString(result, "elts", value) == -1)
2889 goto failed;
2890 Py_DECREF(value);
2891 value = ast2obj_expr_context(o->v.Tuple.ctx);
2892 if (!value) goto failed;
2893 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2894 goto failed;
2895 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 break;
2897 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002898 value = ast2obj_int(o->lineno);
2899 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002900 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2901 goto failed;
2902 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002903 value = ast2obj_int(o->col_offset);
2904 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002905 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2906 goto failed;
2907 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002908 return result;
2909failed:
2910 Py_XDECREF(value);
2911 Py_XDECREF(result);
2912 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913}
2914
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002915PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002917 switch(o) {
2918 case Load:
2919 Py_INCREF(Load_singleton);
2920 return Load_singleton;
2921 case Store:
2922 Py_INCREF(Store_singleton);
2923 return Store_singleton;
2924 case Del:
2925 Py_INCREF(Del_singleton);
2926 return Del_singleton;
2927 case AugLoad:
2928 Py_INCREF(AugLoad_singleton);
2929 return AugLoad_singleton;
2930 case AugStore:
2931 Py_INCREF(AugStore_singleton);
2932 return AugStore_singleton;
2933 case Param:
2934 Py_INCREF(Param_singleton);
2935 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002936 default:
2937 /* should never happen, but just in case ... */
2938 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2939 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002942PyObject*
2943ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002945 slice_ty o = (slice_ty)_o;
2946 PyObject *result = NULL, *value = NULL;
2947 if (!o) {
2948 Py_INCREF(Py_None);
2949 return Py_None;
2950 }
2951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 switch (o->kind) {
2953 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002954 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2955 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002956 break;
2957 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002958 result = PyType_GenericNew(Slice_type, NULL, NULL);
2959 if (!result) goto failed;
2960 value = ast2obj_expr(o->v.Slice.lower);
2961 if (!value) goto failed;
2962 if (PyObject_SetAttrString(result, "lower", value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
2965 value = ast2obj_expr(o->v.Slice.upper);
2966 if (!value) goto failed;
2967 if (PyObject_SetAttrString(result, "upper", value) == -1)
2968 goto failed;
2969 Py_DECREF(value);
2970 value = ast2obj_expr(o->v.Slice.step);
2971 if (!value) goto failed;
2972 if (PyObject_SetAttrString(result, "step", value) == -1)
2973 goto failed;
2974 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 break;
2976 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002977 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2978 if (!result) goto failed;
2979 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2980 if (!value) goto failed;
2981 if (PyObject_SetAttrString(result, "dims", value) == -1)
2982 goto failed;
2983 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 break;
2985 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002986 result = PyType_GenericNew(Index_type, NULL, NULL);
2987 if (!result) goto failed;
2988 value = ast2obj_expr(o->v.Index.value);
2989 if (!value) goto failed;
2990 if (PyObject_SetAttrString(result, "value", value) == -1)
2991 goto failed;
2992 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 break;
2994 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002995 return result;
2996failed:
2997 Py_XDECREF(value);
2998 Py_XDECREF(result);
2999 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000}
3001
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003002PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003004 switch(o) {
3005 case And:
3006 Py_INCREF(And_singleton);
3007 return And_singleton;
3008 case Or:
3009 Py_INCREF(Or_singleton);
3010 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003011 default:
3012 /* should never happen, but just in case ... */
3013 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003016}
3017PyObject* ast2obj_operator(operator_ty o)
3018{
3019 switch(o) {
3020 case Add:
3021 Py_INCREF(Add_singleton);
3022 return Add_singleton;
3023 case Sub:
3024 Py_INCREF(Sub_singleton);
3025 return Sub_singleton;
3026 case Mult:
3027 Py_INCREF(Mult_singleton);
3028 return Mult_singleton;
3029 case Div:
3030 Py_INCREF(Div_singleton);
3031 return Div_singleton;
3032 case Mod:
3033 Py_INCREF(Mod_singleton);
3034 return Mod_singleton;
3035 case Pow:
3036 Py_INCREF(Pow_singleton);
3037 return Pow_singleton;
3038 case LShift:
3039 Py_INCREF(LShift_singleton);
3040 return LShift_singleton;
3041 case RShift:
3042 Py_INCREF(RShift_singleton);
3043 return RShift_singleton;
3044 case BitOr:
3045 Py_INCREF(BitOr_singleton);
3046 return BitOr_singleton;
3047 case BitXor:
3048 Py_INCREF(BitXor_singleton);
3049 return BitXor_singleton;
3050 case BitAnd:
3051 Py_INCREF(BitAnd_singleton);
3052 return BitAnd_singleton;
3053 case FloorDiv:
3054 Py_INCREF(FloorDiv_singleton);
3055 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003056 default:
3057 /* should never happen, but just in case ... */
3058 PyErr_Format(PyExc_SystemError, "unknown operator found");
3059 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003060 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003061}
3062PyObject* ast2obj_unaryop(unaryop_ty o)
3063{
3064 switch(o) {
3065 case Invert:
3066 Py_INCREF(Invert_singleton);
3067 return Invert_singleton;
3068 case Not:
3069 Py_INCREF(Not_singleton);
3070 return Not_singleton;
3071 case UAdd:
3072 Py_INCREF(UAdd_singleton);
3073 return UAdd_singleton;
3074 case USub:
3075 Py_INCREF(USub_singleton);
3076 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003077 default:
3078 /* should never happen, but just in case ... */
3079 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3080 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003081 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003082}
3083PyObject* ast2obj_cmpop(cmpop_ty o)
3084{
3085 switch(o) {
3086 case Eq:
3087 Py_INCREF(Eq_singleton);
3088 return Eq_singleton;
3089 case NotEq:
3090 Py_INCREF(NotEq_singleton);
3091 return NotEq_singleton;
3092 case Lt:
3093 Py_INCREF(Lt_singleton);
3094 return Lt_singleton;
3095 case LtE:
3096 Py_INCREF(LtE_singleton);
3097 return LtE_singleton;
3098 case Gt:
3099 Py_INCREF(Gt_singleton);
3100 return Gt_singleton;
3101 case GtE:
3102 Py_INCREF(GtE_singleton);
3103 return GtE_singleton;
3104 case Is:
3105 Py_INCREF(Is_singleton);
3106 return Is_singleton;
3107 case IsNot:
3108 Py_INCREF(IsNot_singleton);
3109 return IsNot_singleton;
3110 case In:
3111 Py_INCREF(In_singleton);
3112 return In_singleton;
3113 case NotIn:
3114 Py_INCREF(NotIn_singleton);
3115 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003116 default:
3117 /* should never happen, but just in case ... */
3118 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3119 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003120 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003121}
3122PyObject*
3123ast2obj_comprehension(void* _o)
3124{
3125 comprehension_ty o = (comprehension_ty)_o;
3126 PyObject *result = NULL, *value = NULL;
3127 if (!o) {
3128 Py_INCREF(Py_None);
3129 return Py_None;
3130 }
3131
3132 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3133 if (!result) return NULL;
3134 value = ast2obj_expr(o->target);
3135 if (!value) goto failed;
3136 if (PyObject_SetAttrString(result, "target", value) == -1)
3137 goto failed;
3138 Py_DECREF(value);
3139 value = ast2obj_expr(o->iter);
3140 if (!value) goto failed;
3141 if (PyObject_SetAttrString(result, "iter", value) == -1)
3142 goto failed;
3143 Py_DECREF(value);
3144 value = ast2obj_list(o->ifs, ast2obj_expr);
3145 if (!value) goto failed;
3146 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3147 goto failed;
3148 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003149 return result;
3150failed:
3151 Py_XDECREF(value);
3152 Py_XDECREF(result);
3153 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154}
3155
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003156PyObject*
3157ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003159 excepthandler_ty o = (excepthandler_ty)_o;
3160 PyObject *result = NULL, *value = NULL;
3161 if (!o) {
3162 Py_INCREF(Py_None);
3163 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003165
Georg Brandla48f3ab2008-03-30 06:40:17 +00003166 switch (o->kind) {
3167 case ExceptHandler_kind:
3168 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3169 if (!result) goto failed;
3170 value = ast2obj_expr(o->v.ExceptHandler.type);
3171 if (!value) goto failed;
3172 if (PyObject_SetAttrString(result, "type", value) == -1)
3173 goto failed;
3174 Py_DECREF(value);
3175 value = ast2obj_expr(o->v.ExceptHandler.name);
3176 if (!value) goto failed;
3177 if (PyObject_SetAttrString(result, "name", value) == -1)
3178 goto failed;
3179 Py_DECREF(value);
3180 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3181 if (!value) goto failed;
3182 if (PyObject_SetAttrString(result, "body", value) == -1)
3183 goto failed;
3184 Py_DECREF(value);
3185 break;
3186 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003187 value = ast2obj_int(o->lineno);
3188 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003189 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003190 goto failed;
3191 Py_DECREF(value);
3192 value = ast2obj_int(o->col_offset);
3193 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003194 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003195 goto failed;
3196 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003197 return result;
3198failed:
3199 Py_XDECREF(value);
3200 Py_XDECREF(result);
3201 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202}
3203
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003204PyObject*
3205ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003207 arguments_ty o = (arguments_ty)_o;
3208 PyObject *result = NULL, *value = NULL;
3209 if (!o) {
3210 Py_INCREF(Py_None);
3211 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003213
3214 result = PyType_GenericNew(arguments_type, NULL, NULL);
3215 if (!result) return NULL;
3216 value = ast2obj_list(o->args, ast2obj_expr);
3217 if (!value) goto failed;
3218 if (PyObject_SetAttrString(result, "args", value) == -1)
3219 goto failed;
3220 Py_DECREF(value);
3221 value = ast2obj_identifier(o->vararg);
3222 if (!value) goto failed;
3223 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3224 goto failed;
3225 Py_DECREF(value);
3226 value = ast2obj_identifier(o->kwarg);
3227 if (!value) goto failed;
3228 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3229 goto failed;
3230 Py_DECREF(value);
3231 value = ast2obj_list(o->defaults, ast2obj_expr);
3232 if (!value) goto failed;
3233 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3234 goto failed;
3235 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003236 return result;
3237failed:
3238 Py_XDECREF(value);
3239 Py_XDECREF(result);
3240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241}
3242
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003243PyObject*
3244ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003246 keyword_ty o = (keyword_ty)_o;
3247 PyObject *result = NULL, *value = NULL;
3248 if (!o) {
3249 Py_INCREF(Py_None);
3250 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003252
3253 result = PyType_GenericNew(keyword_type, NULL, NULL);
3254 if (!result) return NULL;
3255 value = ast2obj_identifier(o->arg);
3256 if (!value) goto failed;
3257 if (PyObject_SetAttrString(result, "arg", value) == -1)
3258 goto failed;
3259 Py_DECREF(value);
3260 value = ast2obj_expr(o->value);
3261 if (!value) goto failed;
3262 if (PyObject_SetAttrString(result, "value", value) == -1)
3263 goto failed;
3264 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003265 return result;
3266failed:
3267 Py_XDECREF(value);
3268 Py_XDECREF(result);
3269 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270}
3271
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003272PyObject*
3273ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003275 alias_ty o = (alias_ty)_o;
3276 PyObject *result = NULL, *value = NULL;
3277 if (!o) {
3278 Py_INCREF(Py_None);
3279 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003281
3282 result = PyType_GenericNew(alias_type, NULL, NULL);
3283 if (!result) return NULL;
3284 value = ast2obj_identifier(o->name);
3285 if (!value) goto failed;
3286 if (PyObject_SetAttrString(result, "name", value) == -1)
3287 goto failed;
3288 Py_DECREF(value);
3289 value = ast2obj_identifier(o->asname);
3290 if (!value) goto failed;
3291 if (PyObject_SetAttrString(result, "asname", value) == -1)
3292 goto failed;
3293 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003294 return result;
3295failed:
3296 Py_XDECREF(value);
3297 Py_XDECREF(result);
3298 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
3300
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301
Georg Brandlfc8eef32008-03-28 12:11:56 +00003302int
3303obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3304{
3305 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003306 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003307
3308
3309 if (obj == Py_None) {
3310 *out = NULL;
3311 return 0;
3312 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003313 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3314 if (isinstance == -1) {
3315 return 1;
3316 }
3317 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003318 asdl_seq* body;
3319
3320 if (PyObject_HasAttrString(obj, "body")) {
3321 int res;
3322 Py_ssize_t len;
3323 Py_ssize_t i;
3324 tmp = PyObject_GetAttrString(obj, "body");
3325 if (tmp == NULL) goto failed;
3326 if (!PyList_Check(tmp)) {
3327 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3328 goto failed;
3329 }
3330 len = PyList_GET_SIZE(tmp);
3331 body = asdl_seq_new(len, arena);
3332 if (body == NULL) goto failed;
3333 for (i = 0; i < len; i++) {
3334 stmt_ty value;
3335 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3336 if (res != 0) goto failed;
3337 asdl_seq_SET(body, i, value);
3338 }
3339 Py_XDECREF(tmp);
3340 tmp = NULL;
3341 } else {
3342 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3343 return 1;
3344 }
3345 *out = Module(body, arena);
3346 if (*out == NULL) goto failed;
3347 return 0;
3348 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003349 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3350 if (isinstance == -1) {
3351 return 1;
3352 }
3353 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003354 asdl_seq* body;
3355
3356 if (PyObject_HasAttrString(obj, "body")) {
3357 int res;
3358 Py_ssize_t len;
3359 Py_ssize_t i;
3360 tmp = PyObject_GetAttrString(obj, "body");
3361 if (tmp == NULL) goto failed;
3362 if (!PyList_Check(tmp)) {
3363 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3364 goto failed;
3365 }
3366 len = PyList_GET_SIZE(tmp);
3367 body = asdl_seq_new(len, arena);
3368 if (body == NULL) goto failed;
3369 for (i = 0; i < len; i++) {
3370 stmt_ty value;
3371 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3372 if (res != 0) goto failed;
3373 asdl_seq_SET(body, i, value);
3374 }
3375 Py_XDECREF(tmp);
3376 tmp = NULL;
3377 } else {
3378 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3379 return 1;
3380 }
3381 *out = Interactive(body, arena);
3382 if (*out == NULL) goto failed;
3383 return 0;
3384 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003385 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3386 if (isinstance == -1) {
3387 return 1;
3388 }
3389 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003390 expr_ty body;
3391
3392 if (PyObject_HasAttrString(obj, "body")) {
3393 int res;
3394 tmp = PyObject_GetAttrString(obj, "body");
3395 if (tmp == NULL) goto failed;
3396 res = obj2ast_expr(tmp, &body, arena);
3397 if (res != 0) goto failed;
3398 Py_XDECREF(tmp);
3399 tmp = NULL;
3400 } else {
3401 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3402 return 1;
3403 }
3404 *out = Expression(body, arena);
3405 if (*out == NULL) goto failed;
3406 return 0;
3407 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003408 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3409 if (isinstance == -1) {
3410 return 1;
3411 }
3412 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003413 asdl_seq* body;
3414
3415 if (PyObject_HasAttrString(obj, "body")) {
3416 int res;
3417 Py_ssize_t len;
3418 Py_ssize_t i;
3419 tmp = PyObject_GetAttrString(obj, "body");
3420 if (tmp == NULL) goto failed;
3421 if (!PyList_Check(tmp)) {
3422 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3423 goto failed;
3424 }
3425 len = PyList_GET_SIZE(tmp);
3426 body = asdl_seq_new(len, arena);
3427 if (body == NULL) goto failed;
3428 for (i = 0; i < len; i++) {
3429 stmt_ty value;
3430 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3431 if (res != 0) goto failed;
3432 asdl_seq_SET(body, i, value);
3433 }
3434 Py_XDECREF(tmp);
3435 tmp = NULL;
3436 } else {
3437 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3438 return 1;
3439 }
3440 *out = Suite(body, arena);
3441 if (*out == NULL) goto failed;
3442 return 0;
3443 }
3444
3445 tmp = PyObject_Repr(obj);
3446 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003447 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00003448failed:
3449 Py_XDECREF(tmp);
3450 return 1;
3451}
3452
3453int
3454obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3455{
3456 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003457 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003458
3459 int lineno;
3460 int col_offset;
3461
3462 if (obj == Py_None) {
3463 *out = NULL;
3464 return 0;
3465 }
3466 if (PyObject_HasAttrString(obj, "lineno")) {
3467 int res;
3468 tmp = PyObject_GetAttrString(obj, "lineno");
3469 if (tmp == NULL) goto failed;
3470 res = obj2ast_int(tmp, &lineno, arena);
3471 if (res != 0) goto failed;
3472 Py_XDECREF(tmp);
3473 tmp = NULL;
3474 } else {
3475 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3476 return 1;
3477 }
3478 if (PyObject_HasAttrString(obj, "col_offset")) {
3479 int res;
3480 tmp = PyObject_GetAttrString(obj, "col_offset");
3481 if (tmp == NULL) goto failed;
3482 res = obj2ast_int(tmp, &col_offset, arena);
3483 if (res != 0) goto failed;
3484 Py_XDECREF(tmp);
3485 tmp = NULL;
3486 } else {
3487 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3488 return 1;
3489 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003490 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3491 if (isinstance == -1) {
3492 return 1;
3493 }
3494 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003495 identifier name;
3496 arguments_ty args;
3497 asdl_seq* body;
3498 asdl_seq* decorator_list;
3499
3500 if (PyObject_HasAttrString(obj, "name")) {
3501 int res;
3502 tmp = PyObject_GetAttrString(obj, "name");
3503 if (tmp == NULL) goto failed;
3504 res = obj2ast_identifier(tmp, &name, arena);
3505 if (res != 0) goto failed;
3506 Py_XDECREF(tmp);
3507 tmp = NULL;
3508 } else {
3509 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3510 return 1;
3511 }
3512 if (PyObject_HasAttrString(obj, "args")) {
3513 int res;
3514 tmp = PyObject_GetAttrString(obj, "args");
3515 if (tmp == NULL) goto failed;
3516 res = obj2ast_arguments(tmp, &args, arena);
3517 if (res != 0) goto failed;
3518 Py_XDECREF(tmp);
3519 tmp = NULL;
3520 } else {
3521 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3522 return 1;
3523 }
3524 if (PyObject_HasAttrString(obj, "body")) {
3525 int res;
3526 Py_ssize_t len;
3527 Py_ssize_t i;
3528 tmp = PyObject_GetAttrString(obj, "body");
3529 if (tmp == NULL) goto failed;
3530 if (!PyList_Check(tmp)) {
3531 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3532 goto failed;
3533 }
3534 len = PyList_GET_SIZE(tmp);
3535 body = asdl_seq_new(len, arena);
3536 if (body == NULL) goto failed;
3537 for (i = 0; i < len; i++) {
3538 stmt_ty value;
3539 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3540 if (res != 0) goto failed;
3541 asdl_seq_SET(body, i, value);
3542 }
3543 Py_XDECREF(tmp);
3544 tmp = NULL;
3545 } else {
3546 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3547 return 1;
3548 }
3549 if (PyObject_HasAttrString(obj, "decorator_list")) {
3550 int res;
3551 Py_ssize_t len;
3552 Py_ssize_t i;
3553 tmp = PyObject_GetAttrString(obj, "decorator_list");
3554 if (tmp == NULL) goto failed;
3555 if (!PyList_Check(tmp)) {
3556 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3557 goto failed;
3558 }
3559 len = PyList_GET_SIZE(tmp);
3560 decorator_list = asdl_seq_new(len, arena);
3561 if (decorator_list == NULL) goto failed;
3562 for (i = 0; i < len; i++) {
3563 expr_ty value;
3564 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3565 if (res != 0) goto failed;
3566 asdl_seq_SET(decorator_list, i, value);
3567 }
3568 Py_XDECREF(tmp);
3569 tmp = NULL;
3570 } else {
3571 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3572 return 1;
3573 }
3574 *out = FunctionDef(name, args, body, decorator_list, lineno,
3575 col_offset, arena);
3576 if (*out == NULL) goto failed;
3577 return 0;
3578 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003579 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3580 if (isinstance == -1) {
3581 return 1;
3582 }
3583 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003584 identifier name;
3585 asdl_seq* bases;
3586 asdl_seq* body;
3587 asdl_seq* decorator_list;
3588
3589 if (PyObject_HasAttrString(obj, "name")) {
3590 int res;
3591 tmp = PyObject_GetAttrString(obj, "name");
3592 if (tmp == NULL) goto failed;
3593 res = obj2ast_identifier(tmp, &name, arena);
3594 if (res != 0) goto failed;
3595 Py_XDECREF(tmp);
3596 tmp = NULL;
3597 } else {
3598 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3599 return 1;
3600 }
3601 if (PyObject_HasAttrString(obj, "bases")) {
3602 int res;
3603 Py_ssize_t len;
3604 Py_ssize_t i;
3605 tmp = PyObject_GetAttrString(obj, "bases");
3606 if (tmp == NULL) goto failed;
3607 if (!PyList_Check(tmp)) {
3608 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3609 goto failed;
3610 }
3611 len = PyList_GET_SIZE(tmp);
3612 bases = asdl_seq_new(len, arena);
3613 if (bases == NULL) goto failed;
3614 for (i = 0; i < len; i++) {
3615 expr_ty value;
3616 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3617 if (res != 0) goto failed;
3618 asdl_seq_SET(bases, i, value);
3619 }
3620 Py_XDECREF(tmp);
3621 tmp = NULL;
3622 } else {
3623 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3624 return 1;
3625 }
3626 if (PyObject_HasAttrString(obj, "body")) {
3627 int res;
3628 Py_ssize_t len;
3629 Py_ssize_t i;
3630 tmp = PyObject_GetAttrString(obj, "body");
3631 if (tmp == NULL) goto failed;
3632 if (!PyList_Check(tmp)) {
3633 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3634 goto failed;
3635 }
3636 len = PyList_GET_SIZE(tmp);
3637 body = asdl_seq_new(len, arena);
3638 if (body == NULL) goto failed;
3639 for (i = 0; i < len; i++) {
3640 stmt_ty value;
3641 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3642 if (res != 0) goto failed;
3643 asdl_seq_SET(body, i, value);
3644 }
3645 Py_XDECREF(tmp);
3646 tmp = NULL;
3647 } else {
3648 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3649 return 1;
3650 }
3651 if (PyObject_HasAttrString(obj, "decorator_list")) {
3652 int res;
3653 Py_ssize_t len;
3654 Py_ssize_t i;
3655 tmp = PyObject_GetAttrString(obj, "decorator_list");
3656 if (tmp == NULL) goto failed;
3657 if (!PyList_Check(tmp)) {
3658 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3659 goto failed;
3660 }
3661 len = PyList_GET_SIZE(tmp);
3662 decorator_list = asdl_seq_new(len, arena);
3663 if (decorator_list == NULL) goto failed;
3664 for (i = 0; i < len; i++) {
3665 expr_ty value;
3666 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3667 if (res != 0) goto failed;
3668 asdl_seq_SET(decorator_list, i, value);
3669 }
3670 Py_XDECREF(tmp);
3671 tmp = NULL;
3672 } else {
3673 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3674 return 1;
3675 }
3676 *out = ClassDef(name, bases, body, decorator_list, lineno,
3677 col_offset, arena);
3678 if (*out == NULL) goto failed;
3679 return 0;
3680 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003681 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3682 if (isinstance == -1) {
3683 return 1;
3684 }
3685 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003686 expr_ty value;
3687
3688 if (PyObject_HasAttrString(obj, "value")) {
3689 int res;
3690 tmp = PyObject_GetAttrString(obj, "value");
3691 if (tmp == NULL) goto failed;
3692 res = obj2ast_expr(tmp, &value, arena);
3693 if (res != 0) goto failed;
3694 Py_XDECREF(tmp);
3695 tmp = NULL;
3696 } else {
3697 value = NULL;
3698 }
3699 *out = Return(value, lineno, col_offset, arena);
3700 if (*out == NULL) goto failed;
3701 return 0;
3702 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003703 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3704 if (isinstance == -1) {
3705 return 1;
3706 }
3707 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003708 asdl_seq* targets;
3709
3710 if (PyObject_HasAttrString(obj, "targets")) {
3711 int res;
3712 Py_ssize_t len;
3713 Py_ssize_t i;
3714 tmp = PyObject_GetAttrString(obj, "targets");
3715 if (tmp == NULL) goto failed;
3716 if (!PyList_Check(tmp)) {
3717 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3718 goto failed;
3719 }
3720 len = PyList_GET_SIZE(tmp);
3721 targets = asdl_seq_new(len, arena);
3722 if (targets == NULL) goto failed;
3723 for (i = 0; i < len; i++) {
3724 expr_ty value;
3725 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3726 if (res != 0) goto failed;
3727 asdl_seq_SET(targets, i, value);
3728 }
3729 Py_XDECREF(tmp);
3730 tmp = NULL;
3731 } else {
3732 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3733 return 1;
3734 }
3735 *out = Delete(targets, lineno, col_offset, arena);
3736 if (*out == NULL) goto failed;
3737 return 0;
3738 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003739 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3740 if (isinstance == -1) {
3741 return 1;
3742 }
3743 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003744 asdl_seq* targets;
3745 expr_ty value;
3746
3747 if (PyObject_HasAttrString(obj, "targets")) {
3748 int res;
3749 Py_ssize_t len;
3750 Py_ssize_t i;
3751 tmp = PyObject_GetAttrString(obj, "targets");
3752 if (tmp == NULL) goto failed;
3753 if (!PyList_Check(tmp)) {
3754 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3755 goto failed;
3756 }
3757 len = PyList_GET_SIZE(tmp);
3758 targets = asdl_seq_new(len, arena);
3759 if (targets == NULL) goto failed;
3760 for (i = 0; i < len; i++) {
3761 expr_ty value;
3762 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3763 if (res != 0) goto failed;
3764 asdl_seq_SET(targets, i, value);
3765 }
3766 Py_XDECREF(tmp);
3767 tmp = NULL;
3768 } else {
3769 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3770 return 1;
3771 }
3772 if (PyObject_HasAttrString(obj, "value")) {
3773 int res;
3774 tmp = PyObject_GetAttrString(obj, "value");
3775 if (tmp == NULL) goto failed;
3776 res = obj2ast_expr(tmp, &value, arena);
3777 if (res != 0) goto failed;
3778 Py_XDECREF(tmp);
3779 tmp = NULL;
3780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3782 return 1;
3783 }
3784 *out = Assign(targets, value, lineno, col_offset, arena);
3785 if (*out == NULL) goto failed;
3786 return 0;
3787 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003788 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3789 if (isinstance == -1) {
3790 return 1;
3791 }
3792 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003793 expr_ty target;
3794 operator_ty op;
3795 expr_ty value;
3796
3797 if (PyObject_HasAttrString(obj, "target")) {
3798 int res;
3799 tmp = PyObject_GetAttrString(obj, "target");
3800 if (tmp == NULL) goto failed;
3801 res = obj2ast_expr(tmp, &target, arena);
3802 if (res != 0) goto failed;
3803 Py_XDECREF(tmp);
3804 tmp = NULL;
3805 } else {
3806 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3807 return 1;
3808 }
3809 if (PyObject_HasAttrString(obj, "op")) {
3810 int res;
3811 tmp = PyObject_GetAttrString(obj, "op");
3812 if (tmp == NULL) goto failed;
3813 res = obj2ast_operator(tmp, &op, arena);
3814 if (res != 0) goto failed;
3815 Py_XDECREF(tmp);
3816 tmp = NULL;
3817 } else {
3818 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3819 return 1;
3820 }
3821 if (PyObject_HasAttrString(obj, "value")) {
3822 int res;
3823 tmp = PyObject_GetAttrString(obj, "value");
3824 if (tmp == NULL) goto failed;
3825 res = obj2ast_expr(tmp, &value, arena);
3826 if (res != 0) goto failed;
3827 Py_XDECREF(tmp);
3828 tmp = NULL;
3829 } else {
3830 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3831 return 1;
3832 }
3833 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3834 if (*out == NULL) goto failed;
3835 return 0;
3836 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003837 isinstance = PyObject_IsInstance(obj, (PyObject*)Print_type);
3838 if (isinstance == -1) {
3839 return 1;
3840 }
3841 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003842 expr_ty dest;
3843 asdl_seq* values;
3844 bool nl;
3845
3846 if (PyObject_HasAttrString(obj, "dest")) {
3847 int res;
3848 tmp = PyObject_GetAttrString(obj, "dest");
3849 if (tmp == NULL) goto failed;
3850 res = obj2ast_expr(tmp, &dest, arena);
3851 if (res != 0) goto failed;
3852 Py_XDECREF(tmp);
3853 tmp = NULL;
3854 } else {
3855 dest = NULL;
3856 }
3857 if (PyObject_HasAttrString(obj, "values")) {
3858 int res;
3859 Py_ssize_t len;
3860 Py_ssize_t i;
3861 tmp = PyObject_GetAttrString(obj, "values");
3862 if (tmp == NULL) goto failed;
3863 if (!PyList_Check(tmp)) {
3864 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3865 goto failed;
3866 }
3867 len = PyList_GET_SIZE(tmp);
3868 values = asdl_seq_new(len, arena);
3869 if (values == NULL) goto failed;
3870 for (i = 0; i < len; i++) {
3871 expr_ty value;
3872 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3873 if (res != 0) goto failed;
3874 asdl_seq_SET(values, i, value);
3875 }
3876 Py_XDECREF(tmp);
3877 tmp = NULL;
3878 } else {
3879 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3880 return 1;
3881 }
3882 if (PyObject_HasAttrString(obj, "nl")) {
3883 int res;
3884 tmp = PyObject_GetAttrString(obj, "nl");
3885 if (tmp == NULL) goto failed;
3886 res = obj2ast_bool(tmp, &nl, arena);
3887 if (res != 0) goto failed;
3888 Py_XDECREF(tmp);
3889 tmp = NULL;
3890 } else {
3891 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3892 return 1;
3893 }
3894 *out = Print(dest, values, nl, lineno, col_offset, arena);
3895 if (*out == NULL) goto failed;
3896 return 0;
3897 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003898 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3899 if (isinstance == -1) {
3900 return 1;
3901 }
3902 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003903 expr_ty target;
3904 expr_ty iter;
3905 asdl_seq* body;
3906 asdl_seq* orelse;
3907
3908 if (PyObject_HasAttrString(obj, "target")) {
3909 int res;
3910 tmp = PyObject_GetAttrString(obj, "target");
3911 if (tmp == NULL) goto failed;
3912 res = obj2ast_expr(tmp, &target, arena);
3913 if (res != 0) goto failed;
3914 Py_XDECREF(tmp);
3915 tmp = NULL;
3916 } else {
3917 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3918 return 1;
3919 }
3920 if (PyObject_HasAttrString(obj, "iter")) {
3921 int res;
3922 tmp = PyObject_GetAttrString(obj, "iter");
3923 if (tmp == NULL) goto failed;
3924 res = obj2ast_expr(tmp, &iter, arena);
3925 if (res != 0) goto failed;
3926 Py_XDECREF(tmp);
3927 tmp = NULL;
3928 } else {
3929 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3930 return 1;
3931 }
3932 if (PyObject_HasAttrString(obj, "body")) {
3933 int res;
3934 Py_ssize_t len;
3935 Py_ssize_t i;
3936 tmp = PyObject_GetAttrString(obj, "body");
3937 if (tmp == NULL) goto failed;
3938 if (!PyList_Check(tmp)) {
3939 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3940 goto failed;
3941 }
3942 len = PyList_GET_SIZE(tmp);
3943 body = asdl_seq_new(len, arena);
3944 if (body == NULL) goto failed;
3945 for (i = 0; i < len; i++) {
3946 stmt_ty value;
3947 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3948 if (res != 0) goto failed;
3949 asdl_seq_SET(body, i, value);
3950 }
3951 Py_XDECREF(tmp);
3952 tmp = NULL;
3953 } else {
3954 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3955 return 1;
3956 }
3957 if (PyObject_HasAttrString(obj, "orelse")) {
3958 int res;
3959 Py_ssize_t len;
3960 Py_ssize_t i;
3961 tmp = PyObject_GetAttrString(obj, "orelse");
3962 if (tmp == NULL) goto failed;
3963 if (!PyList_Check(tmp)) {
3964 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3965 goto failed;
3966 }
3967 len = PyList_GET_SIZE(tmp);
3968 orelse = asdl_seq_new(len, arena);
3969 if (orelse == NULL) goto failed;
3970 for (i = 0; i < len; i++) {
3971 stmt_ty value;
3972 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3973 if (res != 0) goto failed;
3974 asdl_seq_SET(orelse, i, value);
3975 }
3976 Py_XDECREF(tmp);
3977 tmp = NULL;
3978 } else {
3979 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3980 return 1;
3981 }
3982 *out = For(target, iter, body, orelse, lineno, col_offset,
3983 arena);
3984 if (*out == NULL) goto failed;
3985 return 0;
3986 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003987 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
3988 if (isinstance == -1) {
3989 return 1;
3990 }
3991 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003992 expr_ty test;
3993 asdl_seq* body;
3994 asdl_seq* orelse;
3995
3996 if (PyObject_HasAttrString(obj, "test")) {
3997 int res;
3998 tmp = PyObject_GetAttrString(obj, "test");
3999 if (tmp == NULL) goto failed;
4000 res = obj2ast_expr(tmp, &test, arena);
4001 if (res != 0) goto failed;
4002 Py_XDECREF(tmp);
4003 tmp = NULL;
4004 } else {
4005 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4006 return 1;
4007 }
4008 if (PyObject_HasAttrString(obj, "body")) {
4009 int res;
4010 Py_ssize_t len;
4011 Py_ssize_t i;
4012 tmp = PyObject_GetAttrString(obj, "body");
4013 if (tmp == NULL) goto failed;
4014 if (!PyList_Check(tmp)) {
4015 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4016 goto failed;
4017 }
4018 len = PyList_GET_SIZE(tmp);
4019 body = asdl_seq_new(len, arena);
4020 if (body == NULL) goto failed;
4021 for (i = 0; i < len; i++) {
4022 stmt_ty value;
4023 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4024 if (res != 0) goto failed;
4025 asdl_seq_SET(body, i, value);
4026 }
4027 Py_XDECREF(tmp);
4028 tmp = NULL;
4029 } else {
4030 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4031 return 1;
4032 }
4033 if (PyObject_HasAttrString(obj, "orelse")) {
4034 int res;
4035 Py_ssize_t len;
4036 Py_ssize_t i;
4037 tmp = PyObject_GetAttrString(obj, "orelse");
4038 if (tmp == NULL) goto failed;
4039 if (!PyList_Check(tmp)) {
4040 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4041 goto failed;
4042 }
4043 len = PyList_GET_SIZE(tmp);
4044 orelse = asdl_seq_new(len, arena);
4045 if (orelse == NULL) goto failed;
4046 for (i = 0; i < len; i++) {
4047 stmt_ty value;
4048 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4049 if (res != 0) goto failed;
4050 asdl_seq_SET(orelse, i, value);
4051 }
4052 Py_XDECREF(tmp);
4053 tmp = NULL;
4054 } else {
4055 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4056 return 1;
4057 }
4058 *out = While(test, body, orelse, lineno, col_offset, arena);
4059 if (*out == NULL) goto failed;
4060 return 0;
4061 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004062 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4063 if (isinstance == -1) {
4064 return 1;
4065 }
4066 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004067 expr_ty test;
4068 asdl_seq* body;
4069 asdl_seq* orelse;
4070
4071 if (PyObject_HasAttrString(obj, "test")) {
4072 int res;
4073 tmp = PyObject_GetAttrString(obj, "test");
4074 if (tmp == NULL) goto failed;
4075 res = obj2ast_expr(tmp, &test, arena);
4076 if (res != 0) goto failed;
4077 Py_XDECREF(tmp);
4078 tmp = NULL;
4079 } else {
4080 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4081 return 1;
4082 }
4083 if (PyObject_HasAttrString(obj, "body")) {
4084 int res;
4085 Py_ssize_t len;
4086 Py_ssize_t i;
4087 tmp = PyObject_GetAttrString(obj, "body");
4088 if (tmp == NULL) goto failed;
4089 if (!PyList_Check(tmp)) {
4090 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4091 goto failed;
4092 }
4093 len = PyList_GET_SIZE(tmp);
4094 body = asdl_seq_new(len, arena);
4095 if (body == NULL) goto failed;
4096 for (i = 0; i < len; i++) {
4097 stmt_ty value;
4098 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4099 if (res != 0) goto failed;
4100 asdl_seq_SET(body, i, value);
4101 }
4102 Py_XDECREF(tmp);
4103 tmp = NULL;
4104 } else {
4105 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4106 return 1;
4107 }
4108 if (PyObject_HasAttrString(obj, "orelse")) {
4109 int res;
4110 Py_ssize_t len;
4111 Py_ssize_t i;
4112 tmp = PyObject_GetAttrString(obj, "orelse");
4113 if (tmp == NULL) goto failed;
4114 if (!PyList_Check(tmp)) {
4115 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4116 goto failed;
4117 }
4118 len = PyList_GET_SIZE(tmp);
4119 orelse = asdl_seq_new(len, arena);
4120 if (orelse == NULL) goto failed;
4121 for (i = 0; i < len; i++) {
4122 stmt_ty value;
4123 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4124 if (res != 0) goto failed;
4125 asdl_seq_SET(orelse, i, value);
4126 }
4127 Py_XDECREF(tmp);
4128 tmp = NULL;
4129 } else {
4130 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4131 return 1;
4132 }
4133 *out = If(test, body, orelse, lineno, col_offset, arena);
4134 if (*out == NULL) goto failed;
4135 return 0;
4136 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004137 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4138 if (isinstance == -1) {
4139 return 1;
4140 }
4141 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004142 expr_ty context_expr;
4143 expr_ty optional_vars;
4144 asdl_seq* body;
4145
4146 if (PyObject_HasAttrString(obj, "context_expr")) {
4147 int res;
4148 tmp = PyObject_GetAttrString(obj, "context_expr");
4149 if (tmp == NULL) goto failed;
4150 res = obj2ast_expr(tmp, &context_expr, arena);
4151 if (res != 0) goto failed;
4152 Py_XDECREF(tmp);
4153 tmp = NULL;
4154 } else {
4155 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4156 return 1;
4157 }
4158 if (PyObject_HasAttrString(obj, "optional_vars")) {
4159 int res;
4160 tmp = PyObject_GetAttrString(obj, "optional_vars");
4161 if (tmp == NULL) goto failed;
4162 res = obj2ast_expr(tmp, &optional_vars, arena);
4163 if (res != 0) goto failed;
4164 Py_XDECREF(tmp);
4165 tmp = NULL;
4166 } else {
4167 optional_vars = NULL;
4168 }
4169 if (PyObject_HasAttrString(obj, "body")) {
4170 int res;
4171 Py_ssize_t len;
4172 Py_ssize_t i;
4173 tmp = PyObject_GetAttrString(obj, "body");
4174 if (tmp == NULL) goto failed;
4175 if (!PyList_Check(tmp)) {
4176 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4177 goto failed;
4178 }
4179 len = PyList_GET_SIZE(tmp);
4180 body = asdl_seq_new(len, arena);
4181 if (body == NULL) goto failed;
4182 for (i = 0; i < len; i++) {
4183 stmt_ty value;
4184 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4185 if (res != 0) goto failed;
4186 asdl_seq_SET(body, i, value);
4187 }
4188 Py_XDECREF(tmp);
4189 tmp = NULL;
4190 } else {
4191 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4192 return 1;
4193 }
4194 *out = With(context_expr, optional_vars, body, lineno,
4195 col_offset, arena);
4196 if (*out == NULL) goto failed;
4197 return 0;
4198 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004199 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4200 if (isinstance == -1) {
4201 return 1;
4202 }
4203 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004204 expr_ty type;
4205 expr_ty inst;
4206 expr_ty tback;
4207
4208 if (PyObject_HasAttrString(obj, "type")) {
4209 int res;
4210 tmp = PyObject_GetAttrString(obj, "type");
4211 if (tmp == NULL) goto failed;
4212 res = obj2ast_expr(tmp, &type, arena);
4213 if (res != 0) goto failed;
4214 Py_XDECREF(tmp);
4215 tmp = NULL;
4216 } else {
4217 type = NULL;
4218 }
4219 if (PyObject_HasAttrString(obj, "inst")) {
4220 int res;
4221 tmp = PyObject_GetAttrString(obj, "inst");
4222 if (tmp == NULL) goto failed;
4223 res = obj2ast_expr(tmp, &inst, arena);
4224 if (res != 0) goto failed;
4225 Py_XDECREF(tmp);
4226 tmp = NULL;
4227 } else {
4228 inst = NULL;
4229 }
4230 if (PyObject_HasAttrString(obj, "tback")) {
4231 int res;
4232 tmp = PyObject_GetAttrString(obj, "tback");
4233 if (tmp == NULL) goto failed;
4234 res = obj2ast_expr(tmp, &tback, arena);
4235 if (res != 0) goto failed;
4236 Py_XDECREF(tmp);
4237 tmp = NULL;
4238 } else {
4239 tback = NULL;
4240 }
4241 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4242 if (*out == NULL) goto failed;
4243 return 0;
4244 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004245 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4246 if (isinstance == -1) {
4247 return 1;
4248 }
4249 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004250 asdl_seq* body;
4251 asdl_seq* handlers;
4252 asdl_seq* orelse;
4253
4254 if (PyObject_HasAttrString(obj, "body")) {
4255 int res;
4256 Py_ssize_t len;
4257 Py_ssize_t i;
4258 tmp = PyObject_GetAttrString(obj, "body");
4259 if (tmp == NULL) goto failed;
4260 if (!PyList_Check(tmp)) {
4261 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4262 goto failed;
4263 }
4264 len = PyList_GET_SIZE(tmp);
4265 body = asdl_seq_new(len, arena);
4266 if (body == NULL) goto failed;
4267 for (i = 0; i < len; i++) {
4268 stmt_ty value;
4269 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4270 if (res != 0) goto failed;
4271 asdl_seq_SET(body, i, value);
4272 }
4273 Py_XDECREF(tmp);
4274 tmp = NULL;
4275 } else {
4276 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4277 return 1;
4278 }
4279 if (PyObject_HasAttrString(obj, "handlers")) {
4280 int res;
4281 Py_ssize_t len;
4282 Py_ssize_t i;
4283 tmp = PyObject_GetAttrString(obj, "handlers");
4284 if (tmp == NULL) goto failed;
4285 if (!PyList_Check(tmp)) {
4286 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4287 goto failed;
4288 }
4289 len = PyList_GET_SIZE(tmp);
4290 handlers = asdl_seq_new(len, arena);
4291 if (handlers == NULL) goto failed;
4292 for (i = 0; i < len; i++) {
4293 excepthandler_ty value;
4294 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4295 if (res != 0) goto failed;
4296 asdl_seq_SET(handlers, i, value);
4297 }
4298 Py_XDECREF(tmp);
4299 tmp = NULL;
4300 } else {
4301 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4302 return 1;
4303 }
4304 if (PyObject_HasAttrString(obj, "orelse")) {
4305 int res;
4306 Py_ssize_t len;
4307 Py_ssize_t i;
4308 tmp = PyObject_GetAttrString(obj, "orelse");
4309 if (tmp == NULL) goto failed;
4310 if (!PyList_Check(tmp)) {
4311 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4312 goto failed;
4313 }
4314 len = PyList_GET_SIZE(tmp);
4315 orelse = asdl_seq_new(len, arena);
4316 if (orelse == NULL) goto failed;
4317 for (i = 0; i < len; i++) {
4318 stmt_ty value;
4319 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4320 if (res != 0) goto failed;
4321 asdl_seq_SET(orelse, i, value);
4322 }
4323 Py_XDECREF(tmp);
4324 tmp = NULL;
4325 } else {
4326 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4327 return 1;
4328 }
4329 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4330 arena);
4331 if (*out == NULL) goto failed;
4332 return 0;
4333 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004334 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4335 if (isinstance == -1) {
4336 return 1;
4337 }
4338 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004339 asdl_seq* body;
4340 asdl_seq* finalbody;
4341
4342 if (PyObject_HasAttrString(obj, "body")) {
4343 int res;
4344 Py_ssize_t len;
4345 Py_ssize_t i;
4346 tmp = PyObject_GetAttrString(obj, "body");
4347 if (tmp == NULL) goto failed;
4348 if (!PyList_Check(tmp)) {
4349 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4350 goto failed;
4351 }
4352 len = PyList_GET_SIZE(tmp);
4353 body = asdl_seq_new(len, arena);
4354 if (body == NULL) goto failed;
4355 for (i = 0; i < len; i++) {
4356 stmt_ty value;
4357 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4358 if (res != 0) goto failed;
4359 asdl_seq_SET(body, i, value);
4360 }
4361 Py_XDECREF(tmp);
4362 tmp = NULL;
4363 } else {
4364 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4365 return 1;
4366 }
4367 if (PyObject_HasAttrString(obj, "finalbody")) {
4368 int res;
4369 Py_ssize_t len;
4370 Py_ssize_t i;
4371 tmp = PyObject_GetAttrString(obj, "finalbody");
4372 if (tmp == NULL) goto failed;
4373 if (!PyList_Check(tmp)) {
4374 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4375 goto failed;
4376 }
4377 len = PyList_GET_SIZE(tmp);
4378 finalbody = asdl_seq_new(len, arena);
4379 if (finalbody == NULL) goto failed;
4380 for (i = 0; i < len; i++) {
4381 stmt_ty value;
4382 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4383 if (res != 0) goto failed;
4384 asdl_seq_SET(finalbody, i, value);
4385 }
4386 Py_XDECREF(tmp);
4387 tmp = NULL;
4388 } else {
4389 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4390 return 1;
4391 }
4392 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4393 if (*out == NULL) goto failed;
4394 return 0;
4395 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004396 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4397 if (isinstance == -1) {
4398 return 1;
4399 }
4400 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004401 expr_ty test;
4402 expr_ty msg;
4403
4404 if (PyObject_HasAttrString(obj, "test")) {
4405 int res;
4406 tmp = PyObject_GetAttrString(obj, "test");
4407 if (tmp == NULL) goto failed;
4408 res = obj2ast_expr(tmp, &test, arena);
4409 if (res != 0) goto failed;
4410 Py_XDECREF(tmp);
4411 tmp = NULL;
4412 } else {
4413 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4414 return 1;
4415 }
4416 if (PyObject_HasAttrString(obj, "msg")) {
4417 int res;
4418 tmp = PyObject_GetAttrString(obj, "msg");
4419 if (tmp == NULL) goto failed;
4420 res = obj2ast_expr(tmp, &msg, arena);
4421 if (res != 0) goto failed;
4422 Py_XDECREF(tmp);
4423 tmp = NULL;
4424 } else {
4425 msg = NULL;
4426 }
4427 *out = Assert(test, msg, lineno, col_offset, arena);
4428 if (*out == NULL) goto failed;
4429 return 0;
4430 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004431 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4432 if (isinstance == -1) {
4433 return 1;
4434 }
4435 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004436 asdl_seq* names;
4437
4438 if (PyObject_HasAttrString(obj, "names")) {
4439 int res;
4440 Py_ssize_t len;
4441 Py_ssize_t i;
4442 tmp = PyObject_GetAttrString(obj, "names");
4443 if (tmp == NULL) goto failed;
4444 if (!PyList_Check(tmp)) {
4445 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4446 goto failed;
4447 }
4448 len = PyList_GET_SIZE(tmp);
4449 names = asdl_seq_new(len, arena);
4450 if (names == NULL) goto failed;
4451 for (i = 0; i < len; i++) {
4452 alias_ty value;
4453 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4454 if (res != 0) goto failed;
4455 asdl_seq_SET(names, i, value);
4456 }
4457 Py_XDECREF(tmp);
4458 tmp = NULL;
4459 } else {
4460 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4461 return 1;
4462 }
4463 *out = Import(names, lineno, col_offset, arena);
4464 if (*out == NULL) goto failed;
4465 return 0;
4466 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004467 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4468 if (isinstance == -1) {
4469 return 1;
4470 }
4471 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004472 identifier module;
4473 asdl_seq* names;
4474 int level;
4475
4476 if (PyObject_HasAttrString(obj, "module")) {
4477 int res;
4478 tmp = PyObject_GetAttrString(obj, "module");
4479 if (tmp == NULL) goto failed;
4480 res = obj2ast_identifier(tmp, &module, arena);
4481 if (res != 0) goto failed;
4482 Py_XDECREF(tmp);
4483 tmp = NULL;
4484 } else {
Benjamin Petersona72be3b2009-06-13 20:23:33 +00004485 module = NULL;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004486 }
4487 if (PyObject_HasAttrString(obj, "names")) {
4488 int res;
4489 Py_ssize_t len;
4490 Py_ssize_t i;
4491 tmp = PyObject_GetAttrString(obj, "names");
4492 if (tmp == NULL) goto failed;
4493 if (!PyList_Check(tmp)) {
4494 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4495 goto failed;
4496 }
4497 len = PyList_GET_SIZE(tmp);
4498 names = asdl_seq_new(len, arena);
4499 if (names == NULL) goto failed;
4500 for (i = 0; i < len; i++) {
4501 alias_ty value;
4502 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4503 if (res != 0) goto failed;
4504 asdl_seq_SET(names, i, value);
4505 }
4506 Py_XDECREF(tmp);
4507 tmp = NULL;
4508 } else {
4509 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4510 return 1;
4511 }
4512 if (PyObject_HasAttrString(obj, "level")) {
4513 int res;
4514 tmp = PyObject_GetAttrString(obj, "level");
4515 if (tmp == NULL) goto failed;
4516 res = obj2ast_int(tmp, &level, arena);
4517 if (res != 0) goto failed;
4518 Py_XDECREF(tmp);
4519 tmp = NULL;
4520 } else {
4521 level = 0;
4522 }
4523 *out = ImportFrom(module, names, level, lineno, col_offset,
4524 arena);
4525 if (*out == NULL) goto failed;
4526 return 0;
4527 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004528 isinstance = PyObject_IsInstance(obj, (PyObject*)Exec_type);
4529 if (isinstance == -1) {
4530 return 1;
4531 }
4532 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004533 expr_ty body;
4534 expr_ty globals;
4535 expr_ty locals;
4536
4537 if (PyObject_HasAttrString(obj, "body")) {
4538 int res;
4539 tmp = PyObject_GetAttrString(obj, "body");
4540 if (tmp == NULL) goto failed;
4541 res = obj2ast_expr(tmp, &body, arena);
4542 if (res != 0) goto failed;
4543 Py_XDECREF(tmp);
4544 tmp = NULL;
4545 } else {
4546 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4547 return 1;
4548 }
4549 if (PyObject_HasAttrString(obj, "globals")) {
4550 int res;
4551 tmp = PyObject_GetAttrString(obj, "globals");
4552 if (tmp == NULL) goto failed;
4553 res = obj2ast_expr(tmp, &globals, arena);
4554 if (res != 0) goto failed;
4555 Py_XDECREF(tmp);
4556 tmp = NULL;
4557 } else {
4558 globals = NULL;
4559 }
4560 if (PyObject_HasAttrString(obj, "locals")) {
4561 int res;
4562 tmp = PyObject_GetAttrString(obj, "locals");
4563 if (tmp == NULL) goto failed;
4564 res = obj2ast_expr(tmp, &locals, arena);
4565 if (res != 0) goto failed;
4566 Py_XDECREF(tmp);
4567 tmp = NULL;
4568 } else {
4569 locals = NULL;
4570 }
4571 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4572 if (*out == NULL) goto failed;
4573 return 0;
4574 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004575 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4576 if (isinstance == -1) {
4577 return 1;
4578 }
4579 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004580 asdl_seq* names;
4581
4582 if (PyObject_HasAttrString(obj, "names")) {
4583 int res;
4584 Py_ssize_t len;
4585 Py_ssize_t i;
4586 tmp = PyObject_GetAttrString(obj, "names");
4587 if (tmp == NULL) goto failed;
4588 if (!PyList_Check(tmp)) {
4589 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4590 goto failed;
4591 }
4592 len = PyList_GET_SIZE(tmp);
4593 names = asdl_seq_new(len, arena);
4594 if (names == NULL) goto failed;
4595 for (i = 0; i < len; i++) {
4596 identifier value;
4597 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4598 if (res != 0) goto failed;
4599 asdl_seq_SET(names, i, value);
4600 }
4601 Py_XDECREF(tmp);
4602 tmp = NULL;
4603 } else {
4604 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4605 return 1;
4606 }
4607 *out = Global(names, lineno, col_offset, arena);
4608 if (*out == NULL) goto failed;
4609 return 0;
4610 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004611 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4612 if (isinstance == -1) {
4613 return 1;
4614 }
4615 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004616 expr_ty value;
4617
4618 if (PyObject_HasAttrString(obj, "value")) {
4619 int res;
4620 tmp = PyObject_GetAttrString(obj, "value");
4621 if (tmp == NULL) goto failed;
4622 res = obj2ast_expr(tmp, &value, arena);
4623 if (res != 0) goto failed;
4624 Py_XDECREF(tmp);
4625 tmp = NULL;
4626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4628 return 1;
4629 }
4630 *out = Expr(value, lineno, col_offset, arena);
4631 if (*out == NULL) goto failed;
4632 return 0;
4633 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004634 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4635 if (isinstance == -1) {
4636 return 1;
4637 }
4638 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004639
4640 *out = Pass(lineno, col_offset, arena);
4641 if (*out == NULL) goto failed;
4642 return 0;
4643 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004644 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4645 if (isinstance == -1) {
4646 return 1;
4647 }
4648 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004649
4650 *out = Break(lineno, col_offset, arena);
4651 if (*out == NULL) goto failed;
4652 return 0;
4653 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004654 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4655 if (isinstance == -1) {
4656 return 1;
4657 }
4658 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004659
4660 *out = Continue(lineno, col_offset, arena);
4661 if (*out == NULL) goto failed;
4662 return 0;
4663 }
4664
4665 tmp = PyObject_Repr(obj);
4666 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004667 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00004668failed:
4669 Py_XDECREF(tmp);
4670 return 1;
4671}
4672
4673int
4674obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4675{
4676 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004677 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004678
4679 int lineno;
4680 int col_offset;
4681
4682 if (obj == Py_None) {
4683 *out = NULL;
4684 return 0;
4685 }
4686 if (PyObject_HasAttrString(obj, "lineno")) {
4687 int res;
4688 tmp = PyObject_GetAttrString(obj, "lineno");
4689 if (tmp == NULL) goto failed;
4690 res = obj2ast_int(tmp, &lineno, arena);
4691 if (res != 0) goto failed;
4692 Py_XDECREF(tmp);
4693 tmp = NULL;
4694 } else {
4695 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4696 return 1;
4697 }
4698 if (PyObject_HasAttrString(obj, "col_offset")) {
4699 int res;
4700 tmp = PyObject_GetAttrString(obj, "col_offset");
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_int(tmp, &col_offset, arena);
4703 if (res != 0) goto failed;
4704 Py_XDECREF(tmp);
4705 tmp = NULL;
4706 } else {
4707 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4708 return 1;
4709 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004710 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4711 if (isinstance == -1) {
4712 return 1;
4713 }
4714 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004715 boolop_ty op;
4716 asdl_seq* values;
4717
4718 if (PyObject_HasAttrString(obj, "op")) {
4719 int res;
4720 tmp = PyObject_GetAttrString(obj, "op");
4721 if (tmp == NULL) goto failed;
4722 res = obj2ast_boolop(tmp, &op, arena);
4723 if (res != 0) goto failed;
4724 Py_XDECREF(tmp);
4725 tmp = NULL;
4726 } else {
4727 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4728 return 1;
4729 }
4730 if (PyObject_HasAttrString(obj, "values")) {
4731 int res;
4732 Py_ssize_t len;
4733 Py_ssize_t i;
4734 tmp = PyObject_GetAttrString(obj, "values");
4735 if (tmp == NULL) goto failed;
4736 if (!PyList_Check(tmp)) {
4737 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4738 goto failed;
4739 }
4740 len = PyList_GET_SIZE(tmp);
4741 values = asdl_seq_new(len, arena);
4742 if (values == NULL) goto failed;
4743 for (i = 0; i < len; i++) {
4744 expr_ty value;
4745 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4746 if (res != 0) goto failed;
4747 asdl_seq_SET(values, i, value);
4748 }
4749 Py_XDECREF(tmp);
4750 tmp = NULL;
4751 } else {
4752 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4753 return 1;
4754 }
4755 *out = BoolOp(op, values, lineno, col_offset, arena);
4756 if (*out == NULL) goto failed;
4757 return 0;
4758 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004759 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4760 if (isinstance == -1) {
4761 return 1;
4762 }
4763 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004764 expr_ty left;
4765 operator_ty op;
4766 expr_ty right;
4767
4768 if (PyObject_HasAttrString(obj, "left")) {
4769 int res;
4770 tmp = PyObject_GetAttrString(obj, "left");
4771 if (tmp == NULL) goto failed;
4772 res = obj2ast_expr(tmp, &left, arena);
4773 if (res != 0) goto failed;
4774 Py_XDECREF(tmp);
4775 tmp = NULL;
4776 } else {
4777 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4778 return 1;
4779 }
4780 if (PyObject_HasAttrString(obj, "op")) {
4781 int res;
4782 tmp = PyObject_GetAttrString(obj, "op");
4783 if (tmp == NULL) goto failed;
4784 res = obj2ast_operator(tmp, &op, arena);
4785 if (res != 0) goto failed;
4786 Py_XDECREF(tmp);
4787 tmp = NULL;
4788 } else {
4789 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4790 return 1;
4791 }
4792 if (PyObject_HasAttrString(obj, "right")) {
4793 int res;
4794 tmp = PyObject_GetAttrString(obj, "right");
4795 if (tmp == NULL) goto failed;
4796 res = obj2ast_expr(tmp, &right, arena);
4797 if (res != 0) goto failed;
4798 Py_XDECREF(tmp);
4799 tmp = NULL;
4800 } else {
4801 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4802 return 1;
4803 }
4804 *out = BinOp(left, op, right, lineno, col_offset, arena);
4805 if (*out == NULL) goto failed;
4806 return 0;
4807 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004808 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4809 if (isinstance == -1) {
4810 return 1;
4811 }
4812 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004813 unaryop_ty op;
4814 expr_ty operand;
4815
4816 if (PyObject_HasAttrString(obj, "op")) {
4817 int res;
4818 tmp = PyObject_GetAttrString(obj, "op");
4819 if (tmp == NULL) goto failed;
4820 res = obj2ast_unaryop(tmp, &op, arena);
4821 if (res != 0) goto failed;
4822 Py_XDECREF(tmp);
4823 tmp = NULL;
4824 } else {
4825 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4826 return 1;
4827 }
4828 if (PyObject_HasAttrString(obj, "operand")) {
4829 int res;
4830 tmp = PyObject_GetAttrString(obj, "operand");
4831 if (tmp == NULL) goto failed;
4832 res = obj2ast_expr(tmp, &operand, arena);
4833 if (res != 0) goto failed;
4834 Py_XDECREF(tmp);
4835 tmp = NULL;
4836 } else {
4837 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4838 return 1;
4839 }
4840 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4841 if (*out == NULL) goto failed;
4842 return 0;
4843 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004844 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4845 if (isinstance == -1) {
4846 return 1;
4847 }
4848 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004849 arguments_ty args;
4850 expr_ty body;
4851
4852 if (PyObject_HasAttrString(obj, "args")) {
4853 int res;
4854 tmp = PyObject_GetAttrString(obj, "args");
4855 if (tmp == NULL) goto failed;
4856 res = obj2ast_arguments(tmp, &args, arena);
4857 if (res != 0) goto failed;
4858 Py_XDECREF(tmp);
4859 tmp = NULL;
4860 } else {
4861 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4862 return 1;
4863 }
4864 if (PyObject_HasAttrString(obj, "body")) {
4865 int res;
4866 tmp = PyObject_GetAttrString(obj, "body");
4867 if (tmp == NULL) goto failed;
4868 res = obj2ast_expr(tmp, &body, arena);
4869 if (res != 0) goto failed;
4870 Py_XDECREF(tmp);
4871 tmp = NULL;
4872 } else {
4873 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4874 return 1;
4875 }
4876 *out = Lambda(args, body, lineno, col_offset, arena);
4877 if (*out == NULL) goto failed;
4878 return 0;
4879 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004880 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4881 if (isinstance == -1) {
4882 return 1;
4883 }
4884 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004885 expr_ty test;
4886 expr_ty body;
4887 expr_ty orelse;
4888
4889 if (PyObject_HasAttrString(obj, "test")) {
4890 int res;
4891 tmp = PyObject_GetAttrString(obj, "test");
4892 if (tmp == NULL) goto failed;
4893 res = obj2ast_expr(tmp, &test, arena);
4894 if (res != 0) goto failed;
4895 Py_XDECREF(tmp);
4896 tmp = NULL;
4897 } else {
4898 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4899 return 1;
4900 }
4901 if (PyObject_HasAttrString(obj, "body")) {
4902 int res;
4903 tmp = PyObject_GetAttrString(obj, "body");
4904 if (tmp == NULL) goto failed;
4905 res = obj2ast_expr(tmp, &body, arena);
4906 if (res != 0) goto failed;
4907 Py_XDECREF(tmp);
4908 tmp = NULL;
4909 } else {
4910 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4911 return 1;
4912 }
4913 if (PyObject_HasAttrString(obj, "orelse")) {
4914 int res;
4915 tmp = PyObject_GetAttrString(obj, "orelse");
4916 if (tmp == NULL) goto failed;
4917 res = obj2ast_expr(tmp, &orelse, arena);
4918 if (res != 0) goto failed;
4919 Py_XDECREF(tmp);
4920 tmp = NULL;
4921 } else {
4922 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4923 return 1;
4924 }
4925 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4926 if (*out == NULL) goto failed;
4927 return 0;
4928 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004929 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4930 if (isinstance == -1) {
4931 return 1;
4932 }
4933 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004934 asdl_seq* keys;
4935 asdl_seq* values;
4936
4937 if (PyObject_HasAttrString(obj, "keys")) {
4938 int res;
4939 Py_ssize_t len;
4940 Py_ssize_t i;
4941 tmp = PyObject_GetAttrString(obj, "keys");
4942 if (tmp == NULL) goto failed;
4943 if (!PyList_Check(tmp)) {
4944 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4945 goto failed;
4946 }
4947 len = PyList_GET_SIZE(tmp);
4948 keys = asdl_seq_new(len, arena);
4949 if (keys == NULL) goto failed;
4950 for (i = 0; i < len; i++) {
4951 expr_ty value;
4952 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4953 if (res != 0) goto failed;
4954 asdl_seq_SET(keys, i, value);
4955 }
4956 Py_XDECREF(tmp);
4957 tmp = NULL;
4958 } else {
4959 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4960 return 1;
4961 }
4962 if (PyObject_HasAttrString(obj, "values")) {
4963 int res;
4964 Py_ssize_t len;
4965 Py_ssize_t i;
4966 tmp = PyObject_GetAttrString(obj, "values");
4967 if (tmp == NULL) goto failed;
4968 if (!PyList_Check(tmp)) {
4969 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4970 goto failed;
4971 }
4972 len = PyList_GET_SIZE(tmp);
4973 values = asdl_seq_new(len, arena);
4974 if (values == NULL) goto failed;
4975 for (i = 0; i < len; i++) {
4976 expr_ty value;
4977 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4978 if (res != 0) goto failed;
4979 asdl_seq_SET(values, i, value);
4980 }
4981 Py_XDECREF(tmp);
4982 tmp = NULL;
4983 } else {
4984 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4985 return 1;
4986 }
4987 *out = Dict(keys, values, lineno, col_offset, arena);
4988 if (*out == NULL) goto failed;
4989 return 0;
4990 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00004991 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
4992 if (isinstance == -1) {
4993 return 1;
4994 }
4995 if (isinstance) {
4996 asdl_seq* elts;
4997
4998 if (PyObject_HasAttrString(obj, "elts")) {
4999 int res;
5000 Py_ssize_t len;
5001 Py_ssize_t i;
5002 tmp = PyObject_GetAttrString(obj, "elts");
5003 if (tmp == NULL) goto failed;
5004 if (!PyList_Check(tmp)) {
5005 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5006 goto failed;
5007 }
5008 len = PyList_GET_SIZE(tmp);
5009 elts = asdl_seq_new(len, arena);
5010 if (elts == NULL) goto failed;
5011 for (i = 0; i < len; i++) {
5012 expr_ty value;
5013 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5014 if (res != 0) goto failed;
5015 asdl_seq_SET(elts, i, value);
5016 }
5017 Py_XDECREF(tmp);
5018 tmp = NULL;
5019 } else {
5020 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5021 return 1;
5022 }
5023 *out = Set(elts, lineno, col_offset, arena);
5024 if (*out == NULL) goto failed;
5025 return 0;
5026 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005027 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5028 if (isinstance == -1) {
5029 return 1;
5030 }
5031 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005032 expr_ty elt;
5033 asdl_seq* generators;
5034
5035 if (PyObject_HasAttrString(obj, "elt")) {
5036 int res;
5037 tmp = PyObject_GetAttrString(obj, "elt");
5038 if (tmp == NULL) goto failed;
5039 res = obj2ast_expr(tmp, &elt, arena);
5040 if (res != 0) goto failed;
5041 Py_XDECREF(tmp);
5042 tmp = NULL;
5043 } else {
5044 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5045 return 1;
5046 }
5047 if (PyObject_HasAttrString(obj, "generators")) {
5048 int res;
5049 Py_ssize_t len;
5050 Py_ssize_t i;
5051 tmp = PyObject_GetAttrString(obj, "generators");
5052 if (tmp == NULL) goto failed;
5053 if (!PyList_Check(tmp)) {
5054 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5055 goto failed;
5056 }
5057 len = PyList_GET_SIZE(tmp);
5058 generators = asdl_seq_new(len, arena);
5059 if (generators == NULL) goto failed;
5060 for (i = 0; i < len; i++) {
5061 comprehension_ty value;
5062 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5063 if (res != 0) goto failed;
5064 asdl_seq_SET(generators, i, value);
5065 }
5066 Py_XDECREF(tmp);
5067 tmp = NULL;
5068 } else {
5069 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5070 return 1;
5071 }
5072 *out = ListComp(elt, generators, lineno, col_offset, arena);
5073 if (*out == NULL) goto failed;
5074 return 0;
5075 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00005076 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5077 if (isinstance == -1) {
5078 return 1;
5079 }
5080 if (isinstance) {
5081 expr_ty elt;
5082 asdl_seq* generators;
5083
5084 if (PyObject_HasAttrString(obj, "elt")) {
5085 int res;
5086 tmp = PyObject_GetAttrString(obj, "elt");
5087 if (tmp == NULL) goto failed;
5088 res = obj2ast_expr(tmp, &elt, arena);
5089 if (res != 0) goto failed;
5090 Py_XDECREF(tmp);
5091 tmp = NULL;
5092 } else {
5093 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5094 return 1;
5095 }
5096 if (PyObject_HasAttrString(obj, "generators")) {
5097 int res;
5098 Py_ssize_t len;
5099 Py_ssize_t i;
5100 tmp = PyObject_GetAttrString(obj, "generators");
5101 if (tmp == NULL) goto failed;
5102 if (!PyList_Check(tmp)) {
5103 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5104 goto failed;
5105 }
5106 len = PyList_GET_SIZE(tmp);
5107 generators = asdl_seq_new(len, arena);
5108 if (generators == NULL) goto failed;
5109 for (i = 0; i < len; i++) {
5110 comprehension_ty value;
5111 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5112 if (res != 0) goto failed;
5113 asdl_seq_SET(generators, i, value);
5114 }
5115 Py_XDECREF(tmp);
5116 tmp = NULL;
5117 } else {
5118 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5119 return 1;
5120 }
5121 *out = SetComp(elt, generators, lineno, col_offset, arena);
5122 if (*out == NULL) goto failed;
5123 return 0;
5124 }
5125 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5126 if (isinstance == -1) {
5127 return 1;
5128 }
5129 if (isinstance) {
5130 expr_ty key;
5131 expr_ty value;
5132 asdl_seq* generators;
5133
5134 if (PyObject_HasAttrString(obj, "key")) {
5135 int res;
5136 tmp = PyObject_GetAttrString(obj, "key");
5137 if (tmp == NULL) goto failed;
5138 res = obj2ast_expr(tmp, &key, arena);
5139 if (res != 0) goto failed;
5140 Py_XDECREF(tmp);
5141 tmp = NULL;
5142 } else {
5143 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5144 return 1;
5145 }
5146 if (PyObject_HasAttrString(obj, "value")) {
5147 int res;
5148 tmp = PyObject_GetAttrString(obj, "value");
5149 if (tmp == NULL) goto failed;
5150 res = obj2ast_expr(tmp, &value, arena);
5151 if (res != 0) goto failed;
5152 Py_XDECREF(tmp);
5153 tmp = NULL;
5154 } else {
5155 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5156 return 1;
5157 }
5158 if (PyObject_HasAttrString(obj, "generators")) {
5159 int res;
5160 Py_ssize_t len;
5161 Py_ssize_t i;
5162 tmp = PyObject_GetAttrString(obj, "generators");
5163 if (tmp == NULL) goto failed;
5164 if (!PyList_Check(tmp)) {
5165 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5166 goto failed;
5167 }
5168 len = PyList_GET_SIZE(tmp);
5169 generators = asdl_seq_new(len, arena);
5170 if (generators == NULL) goto failed;
5171 for (i = 0; i < len; i++) {
5172 comprehension_ty value;
5173 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5174 if (res != 0) goto failed;
5175 asdl_seq_SET(generators, i, value);
5176 }
5177 Py_XDECREF(tmp);
5178 tmp = NULL;
5179 } else {
5180 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5181 return 1;
5182 }
5183 *out = DictComp(key, value, generators, lineno, col_offset,
5184 arena);
5185 if (*out == NULL) goto failed;
5186 return 0;
5187 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005188 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5189 if (isinstance == -1) {
5190 return 1;
5191 }
5192 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005193 expr_ty elt;
5194 asdl_seq* generators;
5195
5196 if (PyObject_HasAttrString(obj, "elt")) {
5197 int res;
5198 tmp = PyObject_GetAttrString(obj, "elt");
5199 if (tmp == NULL) goto failed;
5200 res = obj2ast_expr(tmp, &elt, arena);
5201 if (res != 0) goto failed;
5202 Py_XDECREF(tmp);
5203 tmp = NULL;
5204 } else {
5205 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5206 return 1;
5207 }
5208 if (PyObject_HasAttrString(obj, "generators")) {
5209 int res;
5210 Py_ssize_t len;
5211 Py_ssize_t i;
5212 tmp = PyObject_GetAttrString(obj, "generators");
5213 if (tmp == NULL) goto failed;
5214 if (!PyList_Check(tmp)) {
5215 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5216 goto failed;
5217 }
5218 len = PyList_GET_SIZE(tmp);
5219 generators = asdl_seq_new(len, arena);
5220 if (generators == NULL) goto failed;
5221 for (i = 0; i < len; i++) {
5222 comprehension_ty value;
5223 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5224 if (res != 0) goto failed;
5225 asdl_seq_SET(generators, i, value);
5226 }
5227 Py_XDECREF(tmp);
5228 tmp = NULL;
5229 } else {
5230 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5231 return 1;
5232 }
5233 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5234 if (*out == NULL) goto failed;
5235 return 0;
5236 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005237 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5238 if (isinstance == -1) {
5239 return 1;
5240 }
5241 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005242 expr_ty value;
5243
5244 if (PyObject_HasAttrString(obj, "value")) {
5245 int res;
5246 tmp = PyObject_GetAttrString(obj, "value");
5247 if (tmp == NULL) goto failed;
5248 res = obj2ast_expr(tmp, &value, arena);
5249 if (res != 0) goto failed;
5250 Py_XDECREF(tmp);
5251 tmp = NULL;
5252 } else {
5253 value = NULL;
5254 }
5255 *out = Yield(value, lineno, col_offset, arena);
5256 if (*out == NULL) goto failed;
5257 return 0;
5258 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005259 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5260 if (isinstance == -1) {
5261 return 1;
5262 }
5263 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005264 expr_ty left;
5265 asdl_int_seq* ops;
5266 asdl_seq* comparators;
5267
5268 if (PyObject_HasAttrString(obj, "left")) {
5269 int res;
5270 tmp = PyObject_GetAttrString(obj, "left");
5271 if (tmp == NULL) goto failed;
5272 res = obj2ast_expr(tmp, &left, arena);
5273 if (res != 0) goto failed;
5274 Py_XDECREF(tmp);
5275 tmp = NULL;
5276 } else {
5277 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5278 return 1;
5279 }
5280 if (PyObject_HasAttrString(obj, "ops")) {
5281 int res;
5282 Py_ssize_t len;
5283 Py_ssize_t i;
5284 tmp = PyObject_GetAttrString(obj, "ops");
5285 if (tmp == NULL) goto failed;
5286 if (!PyList_Check(tmp)) {
5287 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5288 goto failed;
5289 }
5290 len = PyList_GET_SIZE(tmp);
5291 ops = asdl_int_seq_new(len, arena);
5292 if (ops == NULL) goto failed;
5293 for (i = 0; i < len; i++) {
5294 cmpop_ty value;
5295 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5296 if (res != 0) goto failed;
5297 asdl_seq_SET(ops, i, value);
5298 }
5299 Py_XDECREF(tmp);
5300 tmp = NULL;
5301 } else {
5302 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5303 return 1;
5304 }
5305 if (PyObject_HasAttrString(obj, "comparators")) {
5306 int res;
5307 Py_ssize_t len;
5308 Py_ssize_t i;
5309 tmp = PyObject_GetAttrString(obj, "comparators");
5310 if (tmp == NULL) goto failed;
5311 if (!PyList_Check(tmp)) {
5312 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5313 goto failed;
5314 }
5315 len = PyList_GET_SIZE(tmp);
5316 comparators = asdl_seq_new(len, arena);
5317 if (comparators == NULL) goto failed;
5318 for (i = 0; i < len; i++) {
5319 expr_ty value;
5320 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5321 if (res != 0) goto failed;
5322 asdl_seq_SET(comparators, i, value);
5323 }
5324 Py_XDECREF(tmp);
5325 tmp = NULL;
5326 } else {
5327 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5328 return 1;
5329 }
5330 *out = Compare(left, ops, comparators, lineno, col_offset,
5331 arena);
5332 if (*out == NULL) goto failed;
5333 return 0;
5334 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005335 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5336 if (isinstance == -1) {
5337 return 1;
5338 }
5339 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005340 expr_ty func;
5341 asdl_seq* args;
5342 asdl_seq* keywords;
5343 expr_ty starargs;
5344 expr_ty kwargs;
5345
5346 if (PyObject_HasAttrString(obj, "func")) {
5347 int res;
5348 tmp = PyObject_GetAttrString(obj, "func");
5349 if (tmp == NULL) goto failed;
5350 res = obj2ast_expr(tmp, &func, arena);
5351 if (res != 0) goto failed;
5352 Py_XDECREF(tmp);
5353 tmp = NULL;
5354 } else {
5355 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5356 return 1;
5357 }
5358 if (PyObject_HasAttrString(obj, "args")) {
5359 int res;
5360 Py_ssize_t len;
5361 Py_ssize_t i;
5362 tmp = PyObject_GetAttrString(obj, "args");
5363 if (tmp == NULL) goto failed;
5364 if (!PyList_Check(tmp)) {
5365 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5366 goto failed;
5367 }
5368 len = PyList_GET_SIZE(tmp);
5369 args = asdl_seq_new(len, arena);
5370 if (args == NULL) goto failed;
5371 for (i = 0; i < len; i++) {
5372 expr_ty value;
5373 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5374 if (res != 0) goto failed;
5375 asdl_seq_SET(args, i, value);
5376 }
5377 Py_XDECREF(tmp);
5378 tmp = NULL;
5379 } else {
5380 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5381 return 1;
5382 }
5383 if (PyObject_HasAttrString(obj, "keywords")) {
5384 int res;
5385 Py_ssize_t len;
5386 Py_ssize_t i;
5387 tmp = PyObject_GetAttrString(obj, "keywords");
5388 if (tmp == NULL) goto failed;
5389 if (!PyList_Check(tmp)) {
5390 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5391 goto failed;
5392 }
5393 len = PyList_GET_SIZE(tmp);
5394 keywords = asdl_seq_new(len, arena);
5395 if (keywords == NULL) goto failed;
5396 for (i = 0; i < len; i++) {
5397 keyword_ty value;
5398 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5399 if (res != 0) goto failed;
5400 asdl_seq_SET(keywords, i, value);
5401 }
5402 Py_XDECREF(tmp);
5403 tmp = NULL;
5404 } else {
5405 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5406 return 1;
5407 }
5408 if (PyObject_HasAttrString(obj, "starargs")) {
5409 int res;
5410 tmp = PyObject_GetAttrString(obj, "starargs");
5411 if (tmp == NULL) goto failed;
5412 res = obj2ast_expr(tmp, &starargs, arena);
5413 if (res != 0) goto failed;
5414 Py_XDECREF(tmp);
5415 tmp = NULL;
5416 } else {
5417 starargs = NULL;
5418 }
5419 if (PyObject_HasAttrString(obj, "kwargs")) {
5420 int res;
5421 tmp = PyObject_GetAttrString(obj, "kwargs");
5422 if (tmp == NULL) goto failed;
5423 res = obj2ast_expr(tmp, &kwargs, arena);
5424 if (res != 0) goto failed;
5425 Py_XDECREF(tmp);
5426 tmp = NULL;
5427 } else {
5428 kwargs = NULL;
5429 }
5430 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5431 col_offset, arena);
5432 if (*out == NULL) goto failed;
5433 return 0;
5434 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005435 isinstance = PyObject_IsInstance(obj, (PyObject*)Repr_type);
5436 if (isinstance == -1) {
5437 return 1;
5438 }
5439 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005440 expr_ty value;
5441
5442 if (PyObject_HasAttrString(obj, "value")) {
5443 int res;
5444 tmp = PyObject_GetAttrString(obj, "value");
5445 if (tmp == NULL) goto failed;
5446 res = obj2ast_expr(tmp, &value, arena);
5447 if (res != 0) goto failed;
5448 Py_XDECREF(tmp);
5449 tmp = NULL;
5450 } else {
5451 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5452 return 1;
5453 }
5454 *out = Repr(value, lineno, col_offset, arena);
5455 if (*out == NULL) goto failed;
5456 return 0;
5457 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005458 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5459 if (isinstance == -1) {
5460 return 1;
5461 }
5462 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005463 object n;
5464
5465 if (PyObject_HasAttrString(obj, "n")) {
5466 int res;
5467 tmp = PyObject_GetAttrString(obj, "n");
5468 if (tmp == NULL) goto failed;
5469 res = obj2ast_object(tmp, &n, arena);
5470 if (res != 0) goto failed;
5471 Py_XDECREF(tmp);
5472 tmp = NULL;
5473 } else {
5474 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5475 return 1;
5476 }
5477 *out = Num(n, lineno, col_offset, arena);
5478 if (*out == NULL) goto failed;
5479 return 0;
5480 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005481 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5482 if (isinstance == -1) {
5483 return 1;
5484 }
5485 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005486 string s;
5487
5488 if (PyObject_HasAttrString(obj, "s")) {
5489 int res;
5490 tmp = PyObject_GetAttrString(obj, "s");
5491 if (tmp == NULL) goto failed;
5492 res = obj2ast_string(tmp, &s, arena);
5493 if (res != 0) goto failed;
5494 Py_XDECREF(tmp);
5495 tmp = NULL;
5496 } else {
5497 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5498 return 1;
5499 }
5500 *out = Str(s, lineno, col_offset, arena);
5501 if (*out == NULL) goto failed;
5502 return 0;
5503 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005504 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5505 if (isinstance == -1) {
5506 return 1;
5507 }
5508 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005509 expr_ty value;
5510 identifier attr;
5511 expr_context_ty ctx;
5512
5513 if (PyObject_HasAttrString(obj, "value")) {
5514 int res;
5515 tmp = PyObject_GetAttrString(obj, "value");
5516 if (tmp == NULL) goto failed;
5517 res = obj2ast_expr(tmp, &value, arena);
5518 if (res != 0) goto failed;
5519 Py_XDECREF(tmp);
5520 tmp = NULL;
5521 } else {
5522 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5523 return 1;
5524 }
5525 if (PyObject_HasAttrString(obj, "attr")) {
5526 int res;
5527 tmp = PyObject_GetAttrString(obj, "attr");
5528 if (tmp == NULL) goto failed;
5529 res = obj2ast_identifier(tmp, &attr, arena);
5530 if (res != 0) goto failed;
5531 Py_XDECREF(tmp);
5532 tmp = NULL;
5533 } else {
5534 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5535 return 1;
5536 }
5537 if (PyObject_HasAttrString(obj, "ctx")) {
5538 int res;
5539 tmp = PyObject_GetAttrString(obj, "ctx");
5540 if (tmp == NULL) goto failed;
5541 res = obj2ast_expr_context(tmp, &ctx, arena);
5542 if (res != 0) goto failed;
5543 Py_XDECREF(tmp);
5544 tmp = NULL;
5545 } else {
5546 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5547 return 1;
5548 }
5549 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5550 if (*out == NULL) goto failed;
5551 return 0;
5552 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005553 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5554 if (isinstance == -1) {
5555 return 1;
5556 }
5557 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005558 expr_ty value;
5559 slice_ty slice;
5560 expr_context_ty ctx;
5561
5562 if (PyObject_HasAttrString(obj, "value")) {
5563 int res;
5564 tmp = PyObject_GetAttrString(obj, "value");
5565 if (tmp == NULL) goto failed;
5566 res = obj2ast_expr(tmp, &value, arena);
5567 if (res != 0) goto failed;
5568 Py_XDECREF(tmp);
5569 tmp = NULL;
5570 } else {
5571 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5572 return 1;
5573 }
5574 if (PyObject_HasAttrString(obj, "slice")) {
5575 int res;
5576 tmp = PyObject_GetAttrString(obj, "slice");
5577 if (tmp == NULL) goto failed;
5578 res = obj2ast_slice(tmp, &slice, arena);
5579 if (res != 0) goto failed;
5580 Py_XDECREF(tmp);
5581 tmp = NULL;
5582 } else {
5583 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5584 return 1;
5585 }
5586 if (PyObject_HasAttrString(obj, "ctx")) {
5587 int res;
5588 tmp = PyObject_GetAttrString(obj, "ctx");
5589 if (tmp == NULL) goto failed;
5590 res = obj2ast_expr_context(tmp, &ctx, arena);
5591 if (res != 0) goto failed;
5592 Py_XDECREF(tmp);
5593 tmp = NULL;
5594 } else {
5595 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5596 return 1;
5597 }
5598 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5599 if (*out == NULL) goto failed;
5600 return 0;
5601 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005602 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5603 if (isinstance == -1) {
5604 return 1;
5605 }
5606 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005607 identifier id;
5608 expr_context_ty ctx;
5609
5610 if (PyObject_HasAttrString(obj, "id")) {
5611 int res;
5612 tmp = PyObject_GetAttrString(obj, "id");
5613 if (tmp == NULL) goto failed;
5614 res = obj2ast_identifier(tmp, &id, arena);
5615 if (res != 0) goto failed;
5616 Py_XDECREF(tmp);
5617 tmp = NULL;
5618 } else {
5619 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5620 return 1;
5621 }
5622 if (PyObject_HasAttrString(obj, "ctx")) {
5623 int res;
5624 tmp = PyObject_GetAttrString(obj, "ctx");
5625 if (tmp == NULL) goto failed;
5626 res = obj2ast_expr_context(tmp, &ctx, arena);
5627 if (res != 0) goto failed;
5628 Py_XDECREF(tmp);
5629 tmp = NULL;
5630 } else {
5631 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5632 return 1;
5633 }
5634 *out = Name(id, ctx, lineno, col_offset, arena);
5635 if (*out == NULL) goto failed;
5636 return 0;
5637 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005638 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5639 if (isinstance == -1) {
5640 return 1;
5641 }
5642 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005643 asdl_seq* elts;
5644 expr_context_ty ctx;
5645
5646 if (PyObject_HasAttrString(obj, "elts")) {
5647 int res;
5648 Py_ssize_t len;
5649 Py_ssize_t i;
5650 tmp = PyObject_GetAttrString(obj, "elts");
5651 if (tmp == NULL) goto failed;
5652 if (!PyList_Check(tmp)) {
5653 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5654 goto failed;
5655 }
5656 len = PyList_GET_SIZE(tmp);
5657 elts = asdl_seq_new(len, arena);
5658 if (elts == NULL) goto failed;
5659 for (i = 0; i < len; i++) {
5660 expr_ty value;
5661 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5662 if (res != 0) goto failed;
5663 asdl_seq_SET(elts, i, value);
5664 }
5665 Py_XDECREF(tmp);
5666 tmp = NULL;
5667 } else {
5668 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5669 return 1;
5670 }
5671 if (PyObject_HasAttrString(obj, "ctx")) {
5672 int res;
5673 tmp = PyObject_GetAttrString(obj, "ctx");
5674 if (tmp == NULL) goto failed;
5675 res = obj2ast_expr_context(tmp, &ctx, arena);
5676 if (res != 0) goto failed;
5677 Py_XDECREF(tmp);
5678 tmp = NULL;
5679 } else {
5680 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5681 return 1;
5682 }
5683 *out = List(elts, ctx, lineno, col_offset, arena);
5684 if (*out == NULL) goto failed;
5685 return 0;
5686 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005687 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5688 if (isinstance == -1) {
5689 return 1;
5690 }
5691 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005692 asdl_seq* elts;
5693 expr_context_ty ctx;
5694
5695 if (PyObject_HasAttrString(obj, "elts")) {
5696 int res;
5697 Py_ssize_t len;
5698 Py_ssize_t i;
5699 tmp = PyObject_GetAttrString(obj, "elts");
5700 if (tmp == NULL) goto failed;
5701 if (!PyList_Check(tmp)) {
5702 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5703 goto failed;
5704 }
5705 len = PyList_GET_SIZE(tmp);
5706 elts = asdl_seq_new(len, arena);
5707 if (elts == NULL) goto failed;
5708 for (i = 0; i < len; i++) {
5709 expr_ty value;
5710 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5711 if (res != 0) goto failed;
5712 asdl_seq_SET(elts, i, value);
5713 }
5714 Py_XDECREF(tmp);
5715 tmp = NULL;
5716 } else {
5717 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5718 return 1;
5719 }
5720 if (PyObject_HasAttrString(obj, "ctx")) {
5721 int res;
5722 tmp = PyObject_GetAttrString(obj, "ctx");
5723 if (tmp == NULL) goto failed;
5724 res = obj2ast_expr_context(tmp, &ctx, arena);
5725 if (res != 0) goto failed;
5726 Py_XDECREF(tmp);
5727 tmp = NULL;
5728 } else {
5729 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5730 return 1;
5731 }
5732 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5733 if (*out == NULL) goto failed;
5734 return 0;
5735 }
5736
5737 tmp = PyObject_Repr(obj);
5738 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005739 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005740failed:
5741 Py_XDECREF(tmp);
5742 return 1;
5743}
5744
5745int
5746obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5747{
5748 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005749 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005750
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005751 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5752 if (isinstance == -1) {
5753 return 1;
5754 }
5755 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005756 *out = Load;
5757 return 0;
5758 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005759 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5760 if (isinstance == -1) {
5761 return 1;
5762 }
5763 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005764 *out = Store;
5765 return 0;
5766 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005767 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5768 if (isinstance == -1) {
5769 return 1;
5770 }
5771 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005772 *out = Del;
5773 return 0;
5774 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005775 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5776 if (isinstance == -1) {
5777 return 1;
5778 }
5779 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005780 *out = AugLoad;
5781 return 0;
5782 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005783 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5784 if (isinstance == -1) {
5785 return 1;
5786 }
5787 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005788 *out = AugStore;
5789 return 0;
5790 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005791 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5792 if (isinstance == -1) {
5793 return 1;
5794 }
5795 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005796 *out = Param;
5797 return 0;
5798 }
5799
5800 tmp = PyObject_Repr(obj);
5801 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005802 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005803failed:
5804 Py_XDECREF(tmp);
5805 return 1;
5806}
5807
5808int
5809obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5810{
5811 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005812 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005813
5814
5815 if (obj == Py_None) {
5816 *out = NULL;
5817 return 0;
5818 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005819 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5820 if (isinstance == -1) {
5821 return 1;
5822 }
5823 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005824
5825 *out = Ellipsis(arena);
5826 if (*out == NULL) goto failed;
5827 return 0;
5828 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005829 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5830 if (isinstance == -1) {
5831 return 1;
5832 }
5833 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005834 expr_ty lower;
5835 expr_ty upper;
5836 expr_ty step;
5837
5838 if (PyObject_HasAttrString(obj, "lower")) {
5839 int res;
5840 tmp = PyObject_GetAttrString(obj, "lower");
5841 if (tmp == NULL) goto failed;
5842 res = obj2ast_expr(tmp, &lower, arena);
5843 if (res != 0) goto failed;
5844 Py_XDECREF(tmp);
5845 tmp = NULL;
5846 } else {
5847 lower = NULL;
5848 }
5849 if (PyObject_HasAttrString(obj, "upper")) {
5850 int res;
5851 tmp = PyObject_GetAttrString(obj, "upper");
5852 if (tmp == NULL) goto failed;
5853 res = obj2ast_expr(tmp, &upper, arena);
5854 if (res != 0) goto failed;
5855 Py_XDECREF(tmp);
5856 tmp = NULL;
5857 } else {
5858 upper = NULL;
5859 }
5860 if (PyObject_HasAttrString(obj, "step")) {
5861 int res;
5862 tmp = PyObject_GetAttrString(obj, "step");
5863 if (tmp == NULL) goto failed;
5864 res = obj2ast_expr(tmp, &step, arena);
5865 if (res != 0) goto failed;
5866 Py_XDECREF(tmp);
5867 tmp = NULL;
5868 } else {
5869 step = NULL;
5870 }
5871 *out = Slice(lower, upper, step, arena);
5872 if (*out == NULL) goto failed;
5873 return 0;
5874 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005875 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5876 if (isinstance == -1) {
5877 return 1;
5878 }
5879 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005880 asdl_seq* dims;
5881
5882 if (PyObject_HasAttrString(obj, "dims")) {
5883 int res;
5884 Py_ssize_t len;
5885 Py_ssize_t i;
5886 tmp = PyObject_GetAttrString(obj, "dims");
5887 if (tmp == NULL) goto failed;
5888 if (!PyList_Check(tmp)) {
5889 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5890 goto failed;
5891 }
5892 len = PyList_GET_SIZE(tmp);
5893 dims = asdl_seq_new(len, arena);
5894 if (dims == NULL) goto failed;
5895 for (i = 0; i < len; i++) {
5896 slice_ty value;
5897 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5898 if (res != 0) goto failed;
5899 asdl_seq_SET(dims, i, value);
5900 }
5901 Py_XDECREF(tmp);
5902 tmp = NULL;
5903 } else {
5904 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5905 return 1;
5906 }
5907 *out = ExtSlice(dims, arena);
5908 if (*out == NULL) goto failed;
5909 return 0;
5910 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005911 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5912 if (isinstance == -1) {
5913 return 1;
5914 }
5915 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005916 expr_ty value;
5917
5918 if (PyObject_HasAttrString(obj, "value")) {
5919 int res;
5920 tmp = PyObject_GetAttrString(obj, "value");
5921 if (tmp == NULL) goto failed;
5922 res = obj2ast_expr(tmp, &value, arena);
5923 if (res != 0) goto failed;
5924 Py_XDECREF(tmp);
5925 tmp = NULL;
5926 } else {
5927 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5928 return 1;
5929 }
5930 *out = Index(value, arena);
5931 if (*out == NULL) goto failed;
5932 return 0;
5933 }
5934
5935 tmp = PyObject_Repr(obj);
5936 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005937 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005938failed:
5939 Py_XDECREF(tmp);
5940 return 1;
5941}
5942
5943int
5944obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5945{
5946 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005947 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005948
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005949 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5950 if (isinstance == -1) {
5951 return 1;
5952 }
5953 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005954 *out = And;
5955 return 0;
5956 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005957 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5958 if (isinstance == -1) {
5959 return 1;
5960 }
5961 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005962 *out = Or;
5963 return 0;
5964 }
5965
5966 tmp = PyObject_Repr(obj);
5967 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005968 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005969failed:
5970 Py_XDECREF(tmp);
5971 return 1;
5972}
5973
5974int
5975obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5976{
5977 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005978 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005979
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005980 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
5981 if (isinstance == -1) {
5982 return 1;
5983 }
5984 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005985 *out = Add;
5986 return 0;
5987 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005988 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
5989 if (isinstance == -1) {
5990 return 1;
5991 }
5992 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005993 *out = Sub;
5994 return 0;
5995 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005996 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
5997 if (isinstance == -1) {
5998 return 1;
5999 }
6000 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006001 *out = Mult;
6002 return 0;
6003 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006004 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6005 if (isinstance == -1) {
6006 return 1;
6007 }
6008 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006009 *out = Div;
6010 return 0;
6011 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006012 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6013 if (isinstance == -1) {
6014 return 1;
6015 }
6016 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006017 *out = Mod;
6018 return 0;
6019 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006020 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6021 if (isinstance == -1) {
6022 return 1;
6023 }
6024 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006025 *out = Pow;
6026 return 0;
6027 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006028 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6029 if (isinstance == -1) {
6030 return 1;
6031 }
6032 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006033 *out = LShift;
6034 return 0;
6035 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006036 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6037 if (isinstance == -1) {
6038 return 1;
6039 }
6040 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006041 *out = RShift;
6042 return 0;
6043 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006044 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6045 if (isinstance == -1) {
6046 return 1;
6047 }
6048 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006049 *out = BitOr;
6050 return 0;
6051 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006052 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6053 if (isinstance == -1) {
6054 return 1;
6055 }
6056 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006057 *out = BitXor;
6058 return 0;
6059 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006060 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6061 if (isinstance == -1) {
6062 return 1;
6063 }
6064 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006065 *out = BitAnd;
6066 return 0;
6067 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006068 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6069 if (isinstance == -1) {
6070 return 1;
6071 }
6072 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006073 *out = FloorDiv;
6074 return 0;
6075 }
6076
6077 tmp = PyObject_Repr(obj);
6078 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006079 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006080failed:
6081 Py_XDECREF(tmp);
6082 return 1;
6083}
6084
6085int
6086obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6087{
6088 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006089 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006090
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006091 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6092 if (isinstance == -1) {
6093 return 1;
6094 }
6095 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006096 *out = Invert;
6097 return 0;
6098 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006099 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6100 if (isinstance == -1) {
6101 return 1;
6102 }
6103 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006104 *out = Not;
6105 return 0;
6106 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006107 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6108 if (isinstance == -1) {
6109 return 1;
6110 }
6111 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006112 *out = UAdd;
6113 return 0;
6114 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006115 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6116 if (isinstance == -1) {
6117 return 1;
6118 }
6119 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006120 *out = USub;
6121 return 0;
6122 }
6123
6124 tmp = PyObject_Repr(obj);
6125 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006126 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006127failed:
6128 Py_XDECREF(tmp);
6129 return 1;
6130}
6131
6132int
6133obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6134{
6135 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006136 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006137
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006138 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6139 if (isinstance == -1) {
6140 return 1;
6141 }
6142 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006143 *out = Eq;
6144 return 0;
6145 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006146 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6147 if (isinstance == -1) {
6148 return 1;
6149 }
6150 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006151 *out = NotEq;
6152 return 0;
6153 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006154 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6155 if (isinstance == -1) {
6156 return 1;
6157 }
6158 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006159 *out = Lt;
6160 return 0;
6161 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006162 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6163 if (isinstance == -1) {
6164 return 1;
6165 }
6166 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006167 *out = LtE;
6168 return 0;
6169 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006170 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6171 if (isinstance == -1) {
6172 return 1;
6173 }
6174 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006175 *out = Gt;
6176 return 0;
6177 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006178 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6179 if (isinstance == -1) {
6180 return 1;
6181 }
6182 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006183 *out = GtE;
6184 return 0;
6185 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006186 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6187 if (isinstance == -1) {
6188 return 1;
6189 }
6190 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006191 *out = Is;
6192 return 0;
6193 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006194 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6195 if (isinstance == -1) {
6196 return 1;
6197 }
6198 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006199 *out = IsNot;
6200 return 0;
6201 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006202 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6203 if (isinstance == -1) {
6204 return 1;
6205 }
6206 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006207 *out = In;
6208 return 0;
6209 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006210 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6211 if (isinstance == -1) {
6212 return 1;
6213 }
6214 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006215 *out = NotIn;
6216 return 0;
6217 }
6218
6219 tmp = PyObject_Repr(obj);
6220 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006221 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006222failed:
6223 Py_XDECREF(tmp);
6224 return 1;
6225}
6226
6227int
6228obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6229{
6230 PyObject* tmp = NULL;
6231 expr_ty target;
6232 expr_ty iter;
6233 asdl_seq* ifs;
6234
6235 if (PyObject_HasAttrString(obj, "target")) {
6236 int res;
6237 tmp = PyObject_GetAttrString(obj, "target");
6238 if (tmp == NULL) goto failed;
6239 res = obj2ast_expr(tmp, &target, arena);
6240 if (res != 0) goto failed;
6241 Py_XDECREF(tmp);
6242 tmp = NULL;
6243 } else {
6244 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6245 return 1;
6246 }
6247 if (PyObject_HasAttrString(obj, "iter")) {
6248 int res;
6249 tmp = PyObject_GetAttrString(obj, "iter");
6250 if (tmp == NULL) goto failed;
6251 res = obj2ast_expr(tmp, &iter, arena);
6252 if (res != 0) goto failed;
6253 Py_XDECREF(tmp);
6254 tmp = NULL;
6255 } else {
6256 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6257 return 1;
6258 }
6259 if (PyObject_HasAttrString(obj, "ifs")) {
6260 int res;
6261 Py_ssize_t len;
6262 Py_ssize_t i;
6263 tmp = PyObject_GetAttrString(obj, "ifs");
6264 if (tmp == NULL) goto failed;
6265 if (!PyList_Check(tmp)) {
6266 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6267 goto failed;
6268 }
6269 len = PyList_GET_SIZE(tmp);
6270 ifs = asdl_seq_new(len, arena);
6271 if (ifs == NULL) goto failed;
6272 for (i = 0; i < len; i++) {
6273 expr_ty value;
6274 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6275 if (res != 0) goto failed;
6276 asdl_seq_SET(ifs, i, value);
6277 }
6278 Py_XDECREF(tmp);
6279 tmp = NULL;
6280 } else {
6281 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6282 return 1;
6283 }
6284 *out = comprehension(target, iter, ifs, arena);
6285 return 0;
6286failed:
6287 Py_XDECREF(tmp);
6288 return 1;
6289}
6290
6291int
6292obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6293{
6294 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006295 int isinstance;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006296
Georg Brandlfc8eef32008-03-28 12:11:56 +00006297 int lineno;
6298 int col_offset;
6299
Georg Brandla48f3ab2008-03-30 06:40:17 +00006300 if (obj == Py_None) {
6301 *out = NULL;
6302 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006303 }
6304 if (PyObject_HasAttrString(obj, "lineno")) {
6305 int res;
6306 tmp = PyObject_GetAttrString(obj, "lineno");
6307 if (tmp == NULL) goto failed;
6308 res = obj2ast_int(tmp, &lineno, arena);
6309 if (res != 0) goto failed;
6310 Py_XDECREF(tmp);
6311 tmp = NULL;
6312 } else {
6313 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6314 return 1;
6315 }
6316 if (PyObject_HasAttrString(obj, "col_offset")) {
6317 int res;
6318 tmp = PyObject_GetAttrString(obj, "col_offset");
6319 if (tmp == NULL) goto failed;
6320 res = obj2ast_int(tmp, &col_offset, arena);
6321 if (res != 0) goto failed;
6322 Py_XDECREF(tmp);
6323 tmp = NULL;
6324 } else {
6325 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6326 return 1;
6327 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006328 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6329 if (isinstance == -1) {
6330 return 1;
6331 }
6332 if (isinstance) {
Georg Brandla48f3ab2008-03-30 06:40:17 +00006333 expr_ty type;
6334 expr_ty name;
6335 asdl_seq* body;
6336
6337 if (PyObject_HasAttrString(obj, "type")) {
6338 int res;
6339 tmp = PyObject_GetAttrString(obj, "type");
6340 if (tmp == NULL) goto failed;
6341 res = obj2ast_expr(tmp, &type, arena);
6342 if (res != 0) goto failed;
6343 Py_XDECREF(tmp);
6344 tmp = NULL;
6345 } else {
6346 type = NULL;
6347 }
6348 if (PyObject_HasAttrString(obj, "name")) {
6349 int res;
6350 tmp = PyObject_GetAttrString(obj, "name");
6351 if (tmp == NULL) goto failed;
6352 res = obj2ast_expr(tmp, &name, arena);
6353 if (res != 0) goto failed;
6354 Py_XDECREF(tmp);
6355 tmp = NULL;
6356 } else {
6357 name = NULL;
6358 }
6359 if (PyObject_HasAttrString(obj, "body")) {
6360 int res;
6361 Py_ssize_t len;
6362 Py_ssize_t i;
6363 tmp = PyObject_GetAttrString(obj, "body");
6364 if (tmp == NULL) goto failed;
6365 if (!PyList_Check(tmp)) {
6366 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6367 goto failed;
6368 }
6369 len = PyList_GET_SIZE(tmp);
6370 body = asdl_seq_new(len, arena);
6371 if (body == NULL) goto failed;
6372 for (i = 0; i < len; i++) {
6373 stmt_ty value;
6374 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6375 if (res != 0) goto failed;
6376 asdl_seq_SET(body, i, value);
6377 }
6378 Py_XDECREF(tmp);
6379 tmp = NULL;
6380 } else {
6381 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6382 return 1;
6383 }
6384 *out = ExceptHandler(type, name, body, lineno, col_offset,
6385 arena);
6386 if (*out == NULL) goto failed;
6387 return 0;
6388 }
6389
6390 tmp = PyObject_Repr(obj);
6391 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006392 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006393failed:
6394 Py_XDECREF(tmp);
6395 return 1;
6396}
6397
6398int
6399obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6400{
6401 PyObject* tmp = NULL;
6402 asdl_seq* args;
6403 identifier vararg;
6404 identifier kwarg;
6405 asdl_seq* defaults;
6406
6407 if (PyObject_HasAttrString(obj, "args")) {
6408 int res;
6409 Py_ssize_t len;
6410 Py_ssize_t i;
6411 tmp = PyObject_GetAttrString(obj, "args");
6412 if (tmp == NULL) goto failed;
6413 if (!PyList_Check(tmp)) {
6414 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6415 goto failed;
6416 }
6417 len = PyList_GET_SIZE(tmp);
6418 args = asdl_seq_new(len, arena);
6419 if (args == NULL) goto failed;
6420 for (i = 0; i < len; i++) {
6421 expr_ty value;
6422 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6423 if (res != 0) goto failed;
6424 asdl_seq_SET(args, i, value);
6425 }
6426 Py_XDECREF(tmp);
6427 tmp = NULL;
6428 } else {
6429 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6430 return 1;
6431 }
6432 if (PyObject_HasAttrString(obj, "vararg")) {
6433 int res;
6434 tmp = PyObject_GetAttrString(obj, "vararg");
6435 if (tmp == NULL) goto failed;
6436 res = obj2ast_identifier(tmp, &vararg, arena);
6437 if (res != 0) goto failed;
6438 Py_XDECREF(tmp);
6439 tmp = NULL;
6440 } else {
6441 vararg = NULL;
6442 }
6443 if (PyObject_HasAttrString(obj, "kwarg")) {
6444 int res;
6445 tmp = PyObject_GetAttrString(obj, "kwarg");
6446 if (tmp == NULL) goto failed;
6447 res = obj2ast_identifier(tmp, &kwarg, arena);
6448 if (res != 0) goto failed;
6449 Py_XDECREF(tmp);
6450 tmp = NULL;
6451 } else {
6452 kwarg = NULL;
6453 }
6454 if (PyObject_HasAttrString(obj, "defaults")) {
6455 int res;
6456 Py_ssize_t len;
6457 Py_ssize_t i;
6458 tmp = PyObject_GetAttrString(obj, "defaults");
6459 if (tmp == NULL) goto failed;
6460 if (!PyList_Check(tmp)) {
6461 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6462 goto failed;
6463 }
6464 len = PyList_GET_SIZE(tmp);
6465 defaults = asdl_seq_new(len, arena);
6466 if (defaults == NULL) goto failed;
6467 for (i = 0; i < len; i++) {
6468 expr_ty value;
6469 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6470 if (res != 0) goto failed;
6471 asdl_seq_SET(defaults, i, value);
6472 }
6473 Py_XDECREF(tmp);
6474 tmp = NULL;
6475 } else {
6476 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6477 return 1;
6478 }
6479 *out = arguments(args, vararg, kwarg, defaults, arena);
6480 return 0;
6481failed:
6482 Py_XDECREF(tmp);
6483 return 1;
6484}
6485
6486int
6487obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6488{
6489 PyObject* tmp = NULL;
6490 identifier arg;
6491 expr_ty value;
6492
6493 if (PyObject_HasAttrString(obj, "arg")) {
6494 int res;
6495 tmp = PyObject_GetAttrString(obj, "arg");
6496 if (tmp == NULL) goto failed;
6497 res = obj2ast_identifier(tmp, &arg, arena);
6498 if (res != 0) goto failed;
6499 Py_XDECREF(tmp);
6500 tmp = NULL;
6501 } else {
6502 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6503 return 1;
6504 }
6505 if (PyObject_HasAttrString(obj, "value")) {
6506 int res;
6507 tmp = PyObject_GetAttrString(obj, "value");
6508 if (tmp == NULL) goto failed;
6509 res = obj2ast_expr(tmp, &value, arena);
6510 if (res != 0) goto failed;
6511 Py_XDECREF(tmp);
6512 tmp = NULL;
6513 } else {
6514 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6515 return 1;
6516 }
6517 *out = keyword(arg, value, arena);
6518 return 0;
6519failed:
6520 Py_XDECREF(tmp);
6521 return 1;
6522}
6523
6524int
6525obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6526{
6527 PyObject* tmp = NULL;
6528 identifier name;
6529 identifier asname;
6530
6531 if (PyObject_HasAttrString(obj, "name")) {
6532 int res;
6533 tmp = PyObject_GetAttrString(obj, "name");
6534 if (tmp == NULL) goto failed;
6535 res = obj2ast_identifier(tmp, &name, arena);
6536 if (res != 0) goto failed;
6537 Py_XDECREF(tmp);
6538 tmp = NULL;
6539 } else {
6540 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6541 return 1;
6542 }
6543 if (PyObject_HasAttrString(obj, "asname")) {
6544 int res;
6545 tmp = PyObject_GetAttrString(obj, "asname");
6546 if (tmp == NULL) goto failed;
6547 res = obj2ast_identifier(tmp, &asname, arena);
6548 if (res != 0) goto failed;
6549 Py_XDECREF(tmp);
6550 tmp = NULL;
6551 } else {
6552 asname = NULL;
6553 }
6554 *out = alias(name, asname, arena);
6555 return 0;
6556failed:
6557 Py_XDECREF(tmp);
6558 return 1;
6559}
6560
6561
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006562PyMODINIT_FUNC
6563init_ast(void)
6564{
6565 PyObject *m, *d;
6566 if (!init_types()) return;
6567 m = Py_InitModule3("_ast", NULL, NULL);
6568 if (!m) return;
6569 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00006570 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006571 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6572 return;
Alexandre Vassalotti102d70e2010-01-11 22:46:43 +00006573 if (PyModule_AddStringConstant(m, "__version__", "77422") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006574 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006575 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6576 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6577 return;
6578 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6579 < 0) return;
6580 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6581 0) return;
6582 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6583 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6584 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6585 < 0) return;
6586 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6587 return;
6588 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6589 return;
6590 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6591 return;
6592 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6593 return;
6594 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6595 0) return;
6596 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
6597 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6598 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6599 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6600 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6601 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6602 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6603 0) return;
6604 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6605 0) return;
6606 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6607 return;
6608 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6609 return;
6610 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6611 0) return;
6612 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
6613 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6614 return;
6615 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6616 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6617 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6618 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6619 return;
6620 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6621 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6622 return;
6623 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6624 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6625 return;
6626 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6627 return;
6628 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6629 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00006630 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006631 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6632 return;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00006633 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6634 return;
6635 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6636 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006637 if (PyDict_SetItemString(d, "GeneratorExp",
6638 (PyObject*)GeneratorExp_type) < 0) return;
6639 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6640 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6641 return;
6642 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6643 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6644 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6645 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6646 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6647 0) return;
6648 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6649 0) return;
6650 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6651 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6652 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6653 if (PyDict_SetItemString(d, "expr_context",
6654 (PyObject*)expr_context_type) < 0) return;
6655 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6656 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6657 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6658 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6659 return;
6660 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6661 return;
6662 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6663 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6664 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6665 return;
6666 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6667 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6668 return;
6669 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6670 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6671 return;
6672 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6673 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6674 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6675 return;
6676 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6677 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6678 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6679 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6680 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6681 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6682 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6683 return;
6684 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6685 return;
6686 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6687 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6688 return;
6689 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6690 return;
6691 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6692 return;
6693 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6694 return;
6695 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6696 return;
6697 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6698 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6699 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6700 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6701 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6702 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6703 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6704 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6705 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6706 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6707 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6708 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6709 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6710 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6711 if (PyDict_SetItemString(d, "comprehension",
6712 (PyObject*)comprehension_type) < 0) return;
6713 if (PyDict_SetItemString(d, "excepthandler",
6714 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006715 if (PyDict_SetItemString(d, "ExceptHandler",
6716 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006717 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6718 0) return;
6719 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6720 return;
6721 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006722}
6723
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006724
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006725PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006726{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006727 init_types();
6728 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006729}
6730
Georg Brandlf2bfd542008-03-29 13:24:23 +00006731/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6732mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006733{
6734 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006735 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6736 (PyObject*)Interactive_type};
6737 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006738 int isinstance;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006739 assert(0 <= mode && mode <= 2);
6740
Georg Brandlfc8eef32008-03-28 12:11:56 +00006741 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006742
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006743 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6744 if (isinstance == -1)
6745 return NULL;
6746 if (!isinstance) {
Georg Brandlf2bfd542008-03-29 13:24:23 +00006747 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6748 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006749 return NULL;
6750 }
6751 if (obj2ast_mod(ast, &res, arena) != 0)
6752 return NULL;
6753 else
6754 return res;
6755}
6756
6757int PyAST_Check(PyObject* obj)
6758{
6759 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006760 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006761}
6762
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006763