blob: dcfde3c349ba18eee31f51943dcbcfeec4de669a [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson5eed3062011-07-22 17:20:58 -05005 __version__ 82160.
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
Benjamin Peterson77820242011-07-22 10:39:50 -0500597static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
598{
Benjamin Peterson5eed3062011-07-22 17:20:58 -0500599 if (!PyString_CheckExact(obj) && obj != Py_None) {
Benjamin Peterson77820242011-07-22 10:39:50 -0500600 PyErr_Format(PyExc_TypeError,
601 "AST identifier must be of type str");
602 return 1;
603 }
604 return obj2ast_object(obj, out, arena);
605}
606
607static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
608{
609 if (!PyString_CheckExact(obj) && !PyUnicode_CheckExact(obj)) {
610 PyErr_SetString(PyExc_TypeError,
611 "AST string must be of type str or unicode");
612 return 1;
613 }
614 return obj2ast_object(obj, out, arena);
615}
Georg Brandlfc8eef32008-03-28 12:11:56 +0000616
617static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
618{
619 int i;
620 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
621 PyObject *s = PyObject_Repr(obj);
622 if (s == NULL) return 1;
623 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000624 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000625 Py_DECREF(s);
626 return 1;
627 }
628
629 i = (int)PyLong_AsLong(obj);
630 if (i == -1 && PyErr_Occurred())
631 return 1;
632 *out = i;
633 return 0;
634}
635
636static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
637{
638 if (!PyBool_Check(obj)) {
639 PyObject *s = PyObject_Repr(obj);
640 if (s == NULL) return 1;
641 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000642 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000643 Py_DECREF(s);
644 return 1;
645 }
646
647 *out = (obj == Py_True);
648 return 0;
649}
650
Benjamin Petersonaff36f12008-10-19 13:59:01 +0000651static int add_ast_fields(void)
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000652{
653 PyObject *empty_tuple, *d;
654 if (PyType_Ready(&AST_type) < 0)
655 return -1;
656 d = AST_type.tp_dict;
657 empty_tuple = PyTuple_New(0);
658 if (!empty_tuple ||
659 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
660 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
661 Py_XDECREF(empty_tuple);
662 return -1;
663 }
664 Py_DECREF(empty_tuple);
665 return 0;
666}
667
Georg Brandlfc8eef32008-03-28 12:11:56 +0000668
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669static int init_types(void)
670{
Neal Norwitz19379f12006-04-03 04:50:58 +0000671 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 if (initialized) return 1;
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000673 if (add_ast_fields() < 0) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000674 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!mod_type) return 0;
676 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000677 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679 Interactive_type = make_type("Interactive", mod_type,
680 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 Expression_type = make_type("Expression", mod_type, Expression_fields,
683 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000684 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000687 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000689 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 FunctionDef_type = make_type("FunctionDef", stmt_type,
691 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000693 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000705 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000707 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000711 With_type = make_type("With", stmt_type, With_fields, 3);
712 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
718 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000725 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000733 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000735 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000737 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000739 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000741 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000750 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Dict_type) return 0;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000754 Set_type = make_type("Set", expr_type, Set_fields, 1);
755 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000756 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!ListComp_type) return 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000758 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
759 if (!SetComp_type) return 0;
760 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
761 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 GeneratorExp_type = make_type("GeneratorExp", expr_type,
763 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000769 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000771 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000779 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000781 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000783 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000785 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000787 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!expr_context_type) return 0;
789 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000790 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000792 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000794 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000796 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000798 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000800 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000802 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000814 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!slice_type) return 0;
816 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000817 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000819 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000821 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000823 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000825 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!boolop_type) return 0;
827 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000828 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000830 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000832 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000834 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000836 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!operator_type) return 0;
838 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000839 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000841 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000843 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000845 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000847 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000849 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000851 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000853 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000855 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000857 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000859 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000861 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000863 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000865 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000867 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000869 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000871 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000873 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000875 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000876 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000877 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000879 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000881 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000883 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000884 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000885 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000886 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000887 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000888 if (!unaryop_type) return 0;
889 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000890 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000892 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000894 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000896 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000898 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000900 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000902 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000904 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000906 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!cmpop_type) return 0;
908 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000909 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000910 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000911 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000913 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000915 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000917 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000919 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000921 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000922 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000923 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000924 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000925 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000926 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000927 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000928 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000929 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000930 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000931 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000932 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000933 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000934 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000935 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000936 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000937 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000938 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000939 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000940 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000941 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000942 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000943 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000944 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000945 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000946 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000947 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000948 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000949 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000950 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000951 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000952 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000953 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000954 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
955 return 0;
956 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
957 ExceptHandler_fields, 3);
958 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000959 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000960 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000961 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000962 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000963 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000964 if (!alias_type) return 0;
965 initialized = 1;
966 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000967}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000968
Georg Brandlfc8eef32008-03-28 12:11:56 +0000969static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
970static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
971static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
972static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
973 arena);
974static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
975static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
976static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
977static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
978static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
979static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
980 arena);
981static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
982 arena);
983static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
984static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
985static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
986
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000988Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
990 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000991 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000992 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 p->kind = Module_kind;
995 p->v.Module.body = body;
996 return p;
997}
998
999mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001{
1002 mod_ty p;
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 = Interactive_kind;
1007 p->v.Interactive.body = body;
1008 return p;
1009}
1010
1011mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001012Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
1014 mod_ty p;
1015 if (!body) {
1016 PyErr_SetString(PyExc_ValueError,
1017 "field body is required for Expression");
1018 return NULL;
1019 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001020 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001021 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 p->kind = Expression_kind;
1024 p->v.Expression.body = body;
1025 return p;
1026}
1027
1028mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001029Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030{
1031 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001032 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001033 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035 p->kind = Suite_kind;
1036 p->v.Suite.body = body;
1037 return p;
1038}
1039
1040stmt_ty
1041FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +00001042 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 stmt_ty p;
1045 if (!name) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field name is required for FunctionDef");
1048 return NULL;
1049 }
1050 if (!args) {
1051 PyErr_SetString(PyExc_ValueError,
1052 "field args is required for FunctionDef");
1053 return NULL;
1054 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001055 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001056 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 p->kind = FunctionDef_kind;
1059 p->v.FunctionDef.name = name;
1060 p->v.FunctionDef.args = args;
1061 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001062 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001064 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065 return p;
1066}
1067
1068stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001069ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1070 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071{
1072 stmt_ty p;
1073 if (!name) {
1074 PyErr_SetString(PyExc_ValueError,
1075 "field name is required for ClassDef");
1076 return NULL;
1077 }
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 = ClassDef_kind;
1082 p->v.ClassDef.name = name;
1083 p->v.ClassDef.bases = bases;
1084 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001085 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001087 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088 return p;
1089}
1090
1091stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001092Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
1094 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001095 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001096 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 p->kind = Return_kind;
1099 p->v.Return.value = value;
1100 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001101 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return p;
1103}
1104
1105stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107{
1108 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001109 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001110 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 p->kind = Delete_kind;
1113 p->v.Delete.targets = targets;
1114 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001115 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return p;
1117}
1118
1119stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001120Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1121 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122{
1123 stmt_ty p;
1124 if (!value) {
1125 PyErr_SetString(PyExc_ValueError,
1126 "field value is required for Assign");
1127 return NULL;
1128 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001129 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001130 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 p->kind = Assign_kind;
1133 p->v.Assign.targets = targets;
1134 p->v.Assign.value = value;
1135 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return p;
1138}
1139
1140stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1142 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143{
1144 stmt_ty p;
1145 if (!target) {
1146 PyErr_SetString(PyExc_ValueError,
1147 "field target is required for AugAssign");
1148 return NULL;
1149 }
1150 if (!op) {
1151 PyErr_SetString(PyExc_ValueError,
1152 "field op is required for AugAssign");
1153 return NULL;
1154 }
1155 if (!value) {
1156 PyErr_SetString(PyExc_ValueError,
1157 "field value is required for AugAssign");
1158 return NULL;
1159 }
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 = AugAssign_kind;
1164 p->v.AugAssign.target = target;
1165 p->v.AugAssign.op = op;
1166 p->v.AugAssign.value = value;
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
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001173Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1174 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
1176 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001177 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001178 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 p->kind = Print_kind;
1181 p->v.Print.dest = dest;
1182 p->v.Print.values = values;
1183 p->v.Print.nl = nl;
1184 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001185 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 return p;
1187}
1188
1189stmt_ty
1190For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001191 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192{
1193 stmt_ty p;
1194 if (!target) {
1195 PyErr_SetString(PyExc_ValueError,
1196 "field target is required for For");
1197 return NULL;
1198 }
1199 if (!iter) {
1200 PyErr_SetString(PyExc_ValueError,
1201 "field iter is required for For");
1202 return NULL;
1203 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001204 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001205 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 p->kind = For_kind;
1208 p->v.For.target = target;
1209 p->v.For.iter = iter;
1210 p->v.For.body = body;
1211 p->v.For.orelse = orelse;
1212 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001213 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 return p;
1215}
1216
1217stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1219 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220{
1221 stmt_ty p;
1222 if (!test) {
1223 PyErr_SetString(PyExc_ValueError,
1224 "field test is required for While");
1225 return NULL;
1226 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001227 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001228 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 p->kind = While_kind;
1231 p->v.While.test = test;
1232 p->v.While.body = body;
1233 p->v.While.orelse = orelse;
1234 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001235 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236 return p;
1237}
1238
1239stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1241 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242{
1243 stmt_ty p;
1244 if (!test) {
1245 PyErr_SetString(PyExc_ValueError,
1246 "field test is required for If");
1247 return NULL;
1248 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001250 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 p->kind = If_kind;
1253 p->v.If.test = test;
1254 p->v.If.body = body;
1255 p->v.If.orelse = orelse;
1256 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 return p;
1259}
1260
1261stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001262With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001263 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001264{
1265 stmt_ty p;
1266 if (!context_expr) {
1267 PyErr_SetString(PyExc_ValueError,
1268 "field context_expr is required for With");
1269 return NULL;
1270 }
1271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001272 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001273 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001274 p->kind = With_kind;
1275 p->v.With.context_expr = context_expr;
1276 p->v.With.optional_vars = optional_vars;
1277 p->v.With.body = body;
1278 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001280 return p;
1281}
1282
1283stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1285 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 = Raise_kind;
1292 p->v.Raise.type = type;
1293 p->v.Raise.inst = inst;
1294 p->v.Raise.tback = tback;
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
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001301TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001302 int col_offset, 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 = TryExcept_kind;
1309 p->v.TryExcept.body = body;
1310 p->v.TryExcept.handlers = handlers;
1311 p->v.TryExcept.orelse = orelse;
1312 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001313 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 return p;
1315}
1316
1317stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001318TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1319 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320{
1321 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001322 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001323 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 p->kind = TryFinally_kind;
1326 p->v.TryFinally.body = body;
1327 p->v.TryFinally.finalbody = finalbody;
1328 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330 return p;
1331}
1332
1333stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001334Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335{
1336 stmt_ty p;
1337 if (!test) {
1338 PyErr_SetString(PyExc_ValueError,
1339 "field test is required for Assert");
1340 return NULL;
1341 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001343 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 p->kind = Assert_kind;
1346 p->v.Assert.test = test;
1347 p->v.Assert.msg = msg;
1348 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001349 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 return p;
1351}
1352
1353stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001354Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355{
1356 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001357 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001358 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 p->kind = Import_kind;
1361 p->v.Import.names = names;
1362 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 return p;
1365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1369 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370{
1371 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001372 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001373 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 p->kind = ImportFrom_kind;
1376 p->v.ImportFrom.module = module;
1377 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001378 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001380 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 return p;
1382}
1383
1384stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001385Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1386 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387{
1388 stmt_ty p;
1389 if (!body) {
1390 PyErr_SetString(PyExc_ValueError,
1391 "field body is required for Exec");
1392 return NULL;
1393 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001394 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001395 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 p->kind = Exec_kind;
1398 p->v.Exec.body = body;
1399 p->v.Exec.globals = globals;
1400 p->v.Exec.locals = locals;
1401 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001402 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 return p;
1404}
1405
1406stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001407Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408{
1409 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001410 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001411 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 p->kind = Global_kind;
1414 p->v.Global.names = names;
1415 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001416 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 return p;
1418}
1419
1420stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422{
1423 stmt_ty p;
1424 if (!value) {
1425 PyErr_SetString(PyExc_ValueError,
1426 "field value is required for Expr");
1427 return NULL;
1428 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001429 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001430 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 p->kind = Expr_kind;
1433 p->v.Expr.value = value;
1434 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001435 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return p;
1437}
1438
1439stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001440Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441{
1442 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001443 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001444 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 p->kind = Pass_kind;
1447 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001448 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 return p;
1450}
1451
1452stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001453Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454{
1455 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001456 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001457 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 p->kind = Break_kind;
1460 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001461 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 return p;
1463}
1464
1465stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001466Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467{
1468 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001470 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 p->kind = Continue_kind;
1473 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001474 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 return p;
1476}
1477
1478expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1480 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481{
1482 expr_ty p;
1483 if (!op) {
1484 PyErr_SetString(PyExc_ValueError,
1485 "field op is required for BoolOp");
1486 return NULL;
1487 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001488 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001489 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 p->kind = BoolOp_kind;
1492 p->v.BoolOp.op = op;
1493 p->v.BoolOp.values = values;
1494 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001495 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 return p;
1497}
1498
1499expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001500BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1501 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502{
1503 expr_ty p;
1504 if (!left) {
1505 PyErr_SetString(PyExc_ValueError,
1506 "field left is required for BinOp");
1507 return NULL;
1508 }
1509 if (!op) {
1510 PyErr_SetString(PyExc_ValueError,
1511 "field op is required for BinOp");
1512 return NULL;
1513 }
1514 if (!right) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field right is required for BinOp");
1517 return NULL;
1518 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001519 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001520 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 p->kind = BinOp_kind;
1523 p->v.BinOp.left = left;
1524 p->v.BinOp.op = op;
1525 p->v.BinOp.right = right;
1526 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001527 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 return p;
1529}
1530
1531expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001532UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1533 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534{
1535 expr_ty p;
1536 if (!op) {
1537 PyErr_SetString(PyExc_ValueError,
1538 "field op is required for UnaryOp");
1539 return NULL;
1540 }
1541 if (!operand) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "field operand is required for UnaryOp");
1544 return NULL;
1545 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001546 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001547 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 p->kind = UnaryOp_kind;
1550 p->v.UnaryOp.op = op;
1551 p->v.UnaryOp.operand = operand;
1552 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 return p;
1555}
1556
1557expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1559 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560{
1561 expr_ty p;
1562 if (!args) {
1563 PyErr_SetString(PyExc_ValueError,
1564 "field args is required for Lambda");
1565 return NULL;
1566 }
1567 if (!body) {
1568 PyErr_SetString(PyExc_ValueError,
1569 "field body is required for Lambda");
1570 return NULL;
1571 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001572 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001573 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 p->kind = Lambda_kind;
1576 p->v.Lambda.args = args;
1577 p->v.Lambda.body = body;
1578 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001579 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 return p;
1581}
1582
1583expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001584IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1585 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001586{
1587 expr_ty p;
1588 if (!test) {
1589 PyErr_SetString(PyExc_ValueError,
1590 "field test is required for IfExp");
1591 return NULL;
1592 }
1593 if (!body) {
1594 PyErr_SetString(PyExc_ValueError,
1595 "field body is required for IfExp");
1596 return NULL;
1597 }
1598 if (!orelse) {
1599 PyErr_SetString(PyExc_ValueError,
1600 "field orelse is required for IfExp");
1601 return NULL;
1602 }
1603 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001604 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001605 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001606 p->kind = IfExp_kind;
1607 p->v.IfExp.test = test;
1608 p->v.IfExp.body = body;
1609 p->v.IfExp.orelse = orelse;
1610 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001611 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001612 return p;
1613}
1614
1615expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001616Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1617 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618{
1619 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001620 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001621 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 p->kind = Dict_kind;
1624 p->v.Dict.keys = keys;
1625 p->v.Dict.values = values;
1626 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001627 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 return p;
1629}
1630
1631expr_ty
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001632Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1633{
1634 expr_ty p;
1635 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1636 if (!p)
1637 return NULL;
1638 p->kind = Set_kind;
1639 p->v.Set.elts = elts;
1640 p->lineno = lineno;
1641 p->col_offset = col_offset;
1642 return p;
1643}
1644
1645expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1647 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648{
1649 expr_ty p;
1650 if (!elt) {
1651 PyErr_SetString(PyExc_ValueError,
1652 "field elt is required for ListComp");
1653 return NULL;
1654 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001655 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001656 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 p->kind = ListComp_kind;
1659 p->v.ListComp.elt = elt;
1660 p->v.ListComp.generators = generators;
1661 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001662 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 return p;
1664}
1665
1666expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001667SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1668 *arena)
1669{
1670 expr_ty p;
1671 if (!elt) {
1672 PyErr_SetString(PyExc_ValueError,
1673 "field elt is required for SetComp");
1674 return NULL;
1675 }
1676 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1677 if (!p)
1678 return NULL;
1679 p->kind = SetComp_kind;
1680 p->v.SetComp.elt = elt;
1681 p->v.SetComp.generators = generators;
1682 p->lineno = lineno;
1683 p->col_offset = col_offset;
1684 return p;
1685}
1686
1687expr_ty
1688DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1689 col_offset, PyArena *arena)
1690{
1691 expr_ty p;
1692 if (!key) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "field key is required for DictComp");
1695 return NULL;
1696 }
1697 if (!value) {
1698 PyErr_SetString(PyExc_ValueError,
1699 "field value is required for DictComp");
1700 return NULL;
1701 }
1702 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1703 if (!p)
1704 return NULL;
1705 p->kind = DictComp_kind;
1706 p->v.DictComp.key = key;
1707 p->v.DictComp.value = value;
1708 p->v.DictComp.generators = generators;
1709 p->lineno = lineno;
1710 p->col_offset = col_offset;
1711 return p;
1712}
1713
1714expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001715GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1716 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717{
1718 expr_ty p;
1719 if (!elt) {
1720 PyErr_SetString(PyExc_ValueError,
1721 "field elt is required for GeneratorExp");
1722 return NULL;
1723 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001724 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001725 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727 p->kind = GeneratorExp_kind;
1728 p->v.GeneratorExp.elt = elt;
1729 p->v.GeneratorExp.generators = generators;
1730 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001731 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 return p;
1733}
1734
1735expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001736Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737{
1738 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001740 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 p->kind = Yield_kind;
1743 p->v.Yield.value = value;
1744 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001745 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 return p;
1747}
1748
1749expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001750Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1751 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
1753 expr_ty p;
1754 if (!left) {
1755 PyErr_SetString(PyExc_ValueError,
1756 "field left is required for Compare");
1757 return NULL;
1758 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001759 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001760 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762 p->kind = Compare_kind;
1763 p->v.Compare.left = left;
1764 p->v.Compare.ops = ops;
1765 p->v.Compare.comparators = comparators;
1766 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001767 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 return p;
1769}
1770
1771expr_ty
1772Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774{
1775 expr_ty p;
1776 if (!func) {
1777 PyErr_SetString(PyExc_ValueError,
1778 "field func is required for Call");
1779 return NULL;
1780 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001781 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001782 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 p->kind = Call_kind;
1785 p->v.Call.func = func;
1786 p->v.Call.args = args;
1787 p->v.Call.keywords = keywords;
1788 p->v.Call.starargs = starargs;
1789 p->v.Call.kwargs = kwargs;
1790 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001791 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 return p;
1793}
1794
1795expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001796Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797{
1798 expr_ty p;
1799 if (!value) {
1800 PyErr_SetString(PyExc_ValueError,
1801 "field value is required for Repr");
1802 return NULL;
1803 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001804 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001805 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 p->kind = Repr_kind;
1808 p->v.Repr.value = value;
1809 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001810 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 return p;
1812}
1813
1814expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001815Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816{
1817 expr_ty p;
1818 if (!n) {
1819 PyErr_SetString(PyExc_ValueError,
1820 "field n is required for Num");
1821 return NULL;
1822 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001823 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001824 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 p->kind = Num_kind;
1827 p->v.Num.n = n;
1828 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001829 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 return p;
1831}
1832
1833expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001834Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835{
1836 expr_ty p;
1837 if (!s) {
1838 PyErr_SetString(PyExc_ValueError,
1839 "field s is required for Str");
1840 return NULL;
1841 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001842 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001843 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 p->kind = Str_kind;
1846 p->v.Str.s = s;
1847 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001848 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 return p;
1850}
1851
1852expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001853Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1854 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855{
1856 expr_ty p;
1857 if (!value) {
1858 PyErr_SetString(PyExc_ValueError,
1859 "field value is required for Attribute");
1860 return NULL;
1861 }
1862 if (!attr) {
1863 PyErr_SetString(PyExc_ValueError,
1864 "field attr is required for Attribute");
1865 return NULL;
1866 }
1867 if (!ctx) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field ctx is required for Attribute");
1870 return NULL;
1871 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001873 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 p->kind = Attribute_kind;
1876 p->v.Attribute.value = value;
1877 p->v.Attribute.attr = attr;
1878 p->v.Attribute.ctx = ctx;
1879 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001880 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 return p;
1882}
1883
1884expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001885Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1886 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887{
1888 expr_ty p;
1889 if (!value) {
1890 PyErr_SetString(PyExc_ValueError,
1891 "field value is required for Subscript");
1892 return NULL;
1893 }
1894 if (!slice) {
1895 PyErr_SetString(PyExc_ValueError,
1896 "field slice is required for Subscript");
1897 return NULL;
1898 }
1899 if (!ctx) {
1900 PyErr_SetString(PyExc_ValueError,
1901 "field ctx is required for Subscript");
1902 return NULL;
1903 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001904 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001905 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 p->kind = Subscript_kind;
1908 p->v.Subscript.value = value;
1909 p->v.Subscript.slice = slice;
1910 p->v.Subscript.ctx = ctx;
1911 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001912 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 return p;
1914}
1915
1916expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001917Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1918 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919{
1920 expr_ty p;
1921 if (!id) {
1922 PyErr_SetString(PyExc_ValueError,
1923 "field id is required for Name");
1924 return NULL;
1925 }
1926 if (!ctx) {
1927 PyErr_SetString(PyExc_ValueError,
1928 "field ctx is required for Name");
1929 return NULL;
1930 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001931 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001932 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 p->kind = Name_kind;
1935 p->v.Name.id = id;
1936 p->v.Name.ctx = ctx;
1937 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001938 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 return p;
1940}
1941
1942expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001943List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1944 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945{
1946 expr_ty p;
1947 if (!ctx) {
1948 PyErr_SetString(PyExc_ValueError,
1949 "field ctx is required for List");
1950 return NULL;
1951 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001952 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001953 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955 p->kind = List_kind;
1956 p->v.List.elts = elts;
1957 p->v.List.ctx = ctx;
1958 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001959 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 return p;
1961}
1962
1963expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001964Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1965 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966{
1967 expr_ty p;
1968 if (!ctx) {
1969 PyErr_SetString(PyExc_ValueError,
1970 "field ctx is required for Tuple");
1971 return NULL;
1972 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001973 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001974 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 p->kind = Tuple_kind;
1977 p->v.Tuple.elts = elts;
1978 p->v.Tuple.ctx = ctx;
1979 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001980 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 return p;
1982}
1983
1984slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986{
1987 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001988 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001989 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 p->kind = Ellipsis_kind;
1992 return p;
1993}
1994
1995slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001996Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997{
1998 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001999 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002000 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002 p->kind = Slice_kind;
2003 p->v.Slice.lower = lower;
2004 p->v.Slice.upper = upper;
2005 p->v.Slice.step = step;
2006 return p;
2007}
2008
2009slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011{
2012 slice_ty p;
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 = ExtSlice_kind;
2017 p->v.ExtSlice.dims = dims;
2018 return p;
2019}
2020
2021slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002022Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023{
2024 slice_ty p;
2025 if (!value) {
2026 PyErr_SetString(PyExc_ValueError,
2027 "field value is required for Index");
2028 return NULL;
2029 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002030 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002031 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 p->kind = Index_kind;
2034 p->v.Index.value = value;
2035 return p;
2036}
2037
2038comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002039comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040{
2041 comprehension_ty p;
2042 if (!target) {
2043 PyErr_SetString(PyExc_ValueError,
2044 "field target is required for comprehension");
2045 return NULL;
2046 }
2047 if (!iter) {
2048 PyErr_SetString(PyExc_ValueError,
2049 "field iter is required for comprehension");
2050 return NULL;
2051 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002052 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002053 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 p->target = target;
2056 p->iter = iter;
2057 p->ifs = ifs;
2058 return p;
2059}
2060
2061excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00002062ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002063 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064{
2065 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002066 p = (excepthandler_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;
Georg Brandla48f3ab2008-03-30 06:40:17 +00002069 p->kind = ExceptHandler_kind;
2070 p->v.ExceptHandler.type = type;
2071 p->v.ExceptHandler.name = name;
2072 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002073 p->lineno = lineno;
2074 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 return p;
2076}
2077
2078arguments_ty
2079arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002080 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081{
2082 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002083 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002084 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 p->args = args;
2087 p->vararg = vararg;
2088 p->kwarg = kwarg;
2089 p->defaults = defaults;
2090 return p;
2091}
2092
2093keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002094keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095{
2096 keyword_ty p;
2097 if (!arg) {
2098 PyErr_SetString(PyExc_ValueError,
2099 "field arg is required for keyword");
2100 return NULL;
2101 }
2102 if (!value) {
2103 PyErr_SetString(PyExc_ValueError,
2104 "field value is required for keyword");
2105 return NULL;
2106 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002107 p = (keyword_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->arg = arg;
2111 p->value = value;
2112 return p;
2113}
2114
2115alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002116alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117{
2118 alias_ty p;
2119 if (!name) {
2120 PyErr_SetString(PyExc_ValueError,
2121 "field name is required for alias");
2122 return NULL;
2123 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002124 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002125 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 p->name = name;
2128 p->asname = asname;
2129 return p;
2130}
2131
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002132
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002133PyObject*
2134ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002135{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002136 mod_ty o = (mod_ty)_o;
2137 PyObject *result = NULL, *value = NULL;
2138 if (!o) {
2139 Py_INCREF(Py_None);
2140 return Py_None;
2141 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 switch (o->kind) {
2144 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 result = PyType_GenericNew(Module_type, NULL, NULL);
2146 if (!result) goto failed;
2147 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2148 if (!value) goto failed;
2149 if (PyObject_SetAttrString(result, "body", value) == -1)
2150 goto failed;
2151 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 break;
2153 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002154 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2155 if (!result) goto failed;
2156 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2157 if (!value) goto failed;
2158 if (PyObject_SetAttrString(result, "body", value) == -1)
2159 goto failed;
2160 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 break;
2162 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002163 result = PyType_GenericNew(Expression_type, NULL, NULL);
2164 if (!result) goto failed;
2165 value = ast2obj_expr(o->v.Expression.body);
2166 if (!value) goto failed;
2167 if (PyObject_SetAttrString(result, "body", value) == -1)
2168 goto failed;
2169 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 break;
2171 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002172 result = PyType_GenericNew(Suite_type, NULL, NULL);
2173 if (!result) goto failed;
2174 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2175 if (!value) goto failed;
2176 if (PyObject_SetAttrString(result, "body", value) == -1)
2177 goto failed;
2178 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 break;
2180 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002181 return result;
2182failed:
2183 Py_XDECREF(value);
2184 Py_XDECREF(result);
2185 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186}
2187
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002188PyObject*
2189ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002191 stmt_ty o = (stmt_ty)_o;
2192 PyObject *result = NULL, *value = NULL;
2193 if (!o) {
2194 Py_INCREF(Py_None);
2195 return Py_None;
2196 }
2197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 switch (o->kind) {
2199 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002200 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2201 if (!result) goto failed;
2202 value = ast2obj_identifier(o->v.FunctionDef.name);
2203 if (!value) goto failed;
2204 if (PyObject_SetAttrString(result, "name", value) == -1)
2205 goto failed;
2206 Py_DECREF(value);
2207 value = ast2obj_arguments(o->v.FunctionDef.args);
2208 if (!value) goto failed;
2209 if (PyObject_SetAttrString(result, "args", value) == -1)
2210 goto failed;
2211 Py_DECREF(value);
2212 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "body", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002217 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2218 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002219 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002220 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2221 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002222 goto failed;
2223 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 break;
2225 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002226 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2227 if (!result) goto failed;
2228 value = ast2obj_identifier(o->v.ClassDef.name);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "name", value) == -1)
2231 goto failed;
2232 Py_DECREF(value);
2233 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "bases", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
2238 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "body", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002243 value = ast2obj_list(o->v.ClassDef.decorator_list,
2244 ast2obj_expr);
2245 if (!value) goto failed;
2246 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2247 -1)
2248 goto failed;
2249 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 break;
2251 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002252 result = PyType_GenericNew(Return_type, NULL, NULL);
2253 if (!result) goto failed;
2254 value = ast2obj_expr(o->v.Return.value);
2255 if (!value) goto failed;
2256 if (PyObject_SetAttrString(result, "value", value) == -1)
2257 goto failed;
2258 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 break;
2260 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002261 result = PyType_GenericNew(Delete_type, NULL, NULL);
2262 if (!result) goto failed;
2263 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "targets", value) == -1)
2266 goto failed;
2267 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 break;
2269 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002270 result = PyType_GenericNew(Assign_type, NULL, NULL);
2271 if (!result) goto failed;
2272 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2273 if (!value) goto failed;
2274 if (PyObject_SetAttrString(result, "targets", value) == -1)
2275 goto failed;
2276 Py_DECREF(value);
2277 value = ast2obj_expr(o->v.Assign.value);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "value", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 break;
2283 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2285 if (!result) goto failed;
2286 value = ast2obj_expr(o->v.AugAssign.target);
2287 if (!value) goto failed;
2288 if (PyObject_SetAttrString(result, "target", value) == -1)
2289 goto failed;
2290 Py_DECREF(value);
2291 value = ast2obj_operator(o->v.AugAssign.op);
2292 if (!value) goto failed;
2293 if (PyObject_SetAttrString(result, "op", value) == -1)
2294 goto failed;
2295 Py_DECREF(value);
2296 value = ast2obj_expr(o->v.AugAssign.value);
2297 if (!value) goto failed;
2298 if (PyObject_SetAttrString(result, "value", value) == -1)
2299 goto failed;
2300 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 break;
2302 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002303 result = PyType_GenericNew(Print_type, NULL, NULL);
2304 if (!result) goto failed;
2305 value = ast2obj_expr(o->v.Print.dest);
2306 if (!value) goto failed;
2307 if (PyObject_SetAttrString(result, "dest", value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2311 if (!value) goto failed;
2312 if (PyObject_SetAttrString(result, "values", value) == -1)
2313 goto failed;
2314 Py_DECREF(value);
2315 value = ast2obj_bool(o->v.Print.nl);
2316 if (!value) goto failed;
2317 if (PyObject_SetAttrString(result, "nl", value) == -1)
2318 goto failed;
2319 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 break;
2321 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002322 result = PyType_GenericNew(For_type, NULL, NULL);
2323 if (!result) goto failed;
2324 value = ast2obj_expr(o->v.For.target);
2325 if (!value) goto failed;
2326 if (PyObject_SetAttrString(result, "target", value) == -1)
2327 goto failed;
2328 Py_DECREF(value);
2329 value = ast2obj_expr(o->v.For.iter);
2330 if (!value) goto failed;
2331 if (PyObject_SetAttrString(result, "iter", value) == -1)
2332 goto failed;
2333 Py_DECREF(value);
2334 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2335 if (!value) goto failed;
2336 if (PyObject_SetAttrString(result, "body", value) == -1)
2337 goto failed;
2338 Py_DECREF(value);
2339 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2340 if (!value) goto failed;
2341 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2342 goto failed;
2343 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 break;
2345 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002346 result = PyType_GenericNew(While_type, NULL, NULL);
2347 if (!result) goto failed;
2348 value = ast2obj_expr(o->v.While.test);
2349 if (!value) goto failed;
2350 if (PyObject_SetAttrString(result, "test", value) == -1)
2351 goto failed;
2352 Py_DECREF(value);
2353 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2354 if (!value) goto failed;
2355 if (PyObject_SetAttrString(result, "body", value) == -1)
2356 goto failed;
2357 Py_DECREF(value);
2358 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 break;
2364 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002365 result = PyType_GenericNew(If_type, NULL, NULL);
2366 if (!result) goto failed;
2367 value = ast2obj_expr(o->v.If.test);
2368 if (!value) goto failed;
2369 if (PyObject_SetAttrString(result, "test", value) == -1)
2370 goto failed;
2371 Py_DECREF(value);
2372 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "body", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
2377 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002383 case With_kind:
2384 result = PyType_GenericNew(With_type, NULL, NULL);
2385 if (!result) goto failed;
2386 value = ast2obj_expr(o->v.With.context_expr);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
2391 value = ast2obj_expr(o->v.With.optional_vars);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2394 -1)
2395 goto failed;
2396 Py_DECREF(value);
2397 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2398 if (!value) goto failed;
2399 if (PyObject_SetAttrString(result, "body", value) == -1)
2400 goto failed;
2401 Py_DECREF(value);
2402 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002404 result = PyType_GenericNew(Raise_type, NULL, NULL);
2405 if (!result) goto failed;
2406 value = ast2obj_expr(o->v.Raise.type);
2407 if (!value) goto failed;
2408 if (PyObject_SetAttrString(result, "type", value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
2411 value = ast2obj_expr(o->v.Raise.inst);
2412 if (!value) goto failed;
2413 if (PyObject_SetAttrString(result, "inst", value) == -1)
2414 goto failed;
2415 Py_DECREF(value);
2416 value = ast2obj_expr(o->v.Raise.tback);
2417 if (!value) goto failed;
2418 if (PyObject_SetAttrString(result, "tback", value) == -1)
2419 goto failed;
2420 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 break;
2422 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002423 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2424 if (!result) goto failed;
2425 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2426 if (!value) goto failed;
2427 if (PyObject_SetAttrString(result, "body", value) == -1)
2428 goto failed;
2429 Py_DECREF(value);
2430 value = ast2obj_list(o->v.TryExcept.handlers,
2431 ast2obj_excepthandler);
2432 if (!value) goto failed;
2433 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2434 goto failed;
2435 Py_DECREF(value);
2436 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2437 if (!value) goto failed;
2438 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2439 goto failed;
2440 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 break;
2442 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002443 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2444 if (!result) goto failed;
2445 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2446 if (!value) goto failed;
2447 if (PyObject_SetAttrString(result, "body", value) == -1)
2448 goto failed;
2449 Py_DECREF(value);
2450 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2451 if (!value) goto failed;
2452 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2453 goto failed;
2454 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 break;
2456 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002457 result = PyType_GenericNew(Assert_type, NULL, NULL);
2458 if (!result) goto failed;
2459 value = ast2obj_expr(o->v.Assert.test);
2460 if (!value) goto failed;
2461 if (PyObject_SetAttrString(result, "test", value) == -1)
2462 goto failed;
2463 Py_DECREF(value);
2464 value = ast2obj_expr(o->v.Assert.msg);
2465 if (!value) goto failed;
2466 if (PyObject_SetAttrString(result, "msg", value) == -1)
2467 goto failed;
2468 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 break;
2470 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002471 result = PyType_GenericNew(Import_type, NULL, NULL);
2472 if (!result) goto failed;
2473 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2474 if (!value) goto failed;
2475 if (PyObject_SetAttrString(result, "names", value) == -1)
2476 goto failed;
2477 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 break;
2479 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002480 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2481 if (!result) goto failed;
2482 value = ast2obj_identifier(o->v.ImportFrom.module);
2483 if (!value) goto failed;
2484 if (PyObject_SetAttrString(result, "module", value) == -1)
2485 goto failed;
2486 Py_DECREF(value);
2487 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2488 if (!value) goto failed;
2489 if (PyObject_SetAttrString(result, "names", value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002492 value = ast2obj_int(o->v.ImportFrom.level);
2493 if (!value) goto failed;
2494 if (PyObject_SetAttrString(result, "level", value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 break;
2498 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002499 result = PyType_GenericNew(Exec_type, NULL, NULL);
2500 if (!result) goto failed;
2501 value = ast2obj_expr(o->v.Exec.body);
2502 if (!value) goto failed;
2503 if (PyObject_SetAttrString(result, "body", value) == -1)
2504 goto failed;
2505 Py_DECREF(value);
2506 value = ast2obj_expr(o->v.Exec.globals);
2507 if (!value) goto failed;
2508 if (PyObject_SetAttrString(result, "globals", value) == -1)
2509 goto failed;
2510 Py_DECREF(value);
2511 value = ast2obj_expr(o->v.Exec.locals);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "locals", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 break;
2517 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 result = PyType_GenericNew(Global_type, NULL, NULL);
2519 if (!result) goto failed;
2520 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2521 if (!value) goto failed;
2522 if (PyObject_SetAttrString(result, "names", value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 break;
2526 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002527 result = PyType_GenericNew(Expr_type, NULL, NULL);
2528 if (!result) goto failed;
2529 value = ast2obj_expr(o->v.Expr.value);
2530 if (!value) goto failed;
2531 if (PyObject_SetAttrString(result, "value", value) == -1)
2532 goto failed;
2533 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 break;
2535 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002536 result = PyType_GenericNew(Pass_type, NULL, NULL);
2537 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 break;
2539 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002540 result = PyType_GenericNew(Break_type, NULL, NULL);
2541 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 break;
2543 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002544 result = PyType_GenericNew(Continue_type, NULL, NULL);
2545 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 break;
2547 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002548 value = ast2obj_int(o->lineno);
2549 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002550 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2551 goto failed;
2552 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002553 value = ast2obj_int(o->col_offset);
2554 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002555 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2556 goto failed;
2557 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002558 return result;
2559failed:
2560 Py_XDECREF(value);
2561 Py_XDECREF(result);
2562 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563}
2564
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002565PyObject*
2566ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002568 expr_ty o = (expr_ty)_o;
2569 PyObject *result = NULL, *value = NULL;
2570 if (!o) {
2571 Py_INCREF(Py_None);
2572 return Py_None;
2573 }
2574
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 switch (o->kind) {
2576 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002577 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2578 if (!result) goto failed;
2579 value = ast2obj_boolop(o->v.BoolOp.op);
2580 if (!value) goto failed;
2581 if (PyObject_SetAttrString(result, "op", value) == -1)
2582 goto failed;
2583 Py_DECREF(value);
2584 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2585 if (!value) goto failed;
2586 if (PyObject_SetAttrString(result, "values", value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 break;
2590 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002591 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2592 if (!result) goto failed;
2593 value = ast2obj_expr(o->v.BinOp.left);
2594 if (!value) goto failed;
2595 if (PyObject_SetAttrString(result, "left", value) == -1)
2596 goto failed;
2597 Py_DECREF(value);
2598 value = ast2obj_operator(o->v.BinOp.op);
2599 if (!value) goto failed;
2600 if (PyObject_SetAttrString(result, "op", value) == -1)
2601 goto failed;
2602 Py_DECREF(value);
2603 value = ast2obj_expr(o->v.BinOp.right);
2604 if (!value) goto failed;
2605 if (PyObject_SetAttrString(result, "right", value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 break;
2609 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002610 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2611 if (!result) goto failed;
2612 value = ast2obj_unaryop(o->v.UnaryOp.op);
2613 if (!value) goto failed;
2614 if (PyObject_SetAttrString(result, "op", value) == -1)
2615 goto failed;
2616 Py_DECREF(value);
2617 value = ast2obj_expr(o->v.UnaryOp.operand);
2618 if (!value) goto failed;
2619 if (PyObject_SetAttrString(result, "operand", value) == -1)
2620 goto failed;
2621 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 break;
2623 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002624 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2625 if (!result) goto failed;
2626 value = ast2obj_arguments(o->v.Lambda.args);
2627 if (!value) goto failed;
2628 if (PyObject_SetAttrString(result, "args", value) == -1)
2629 goto failed;
2630 Py_DECREF(value);
2631 value = ast2obj_expr(o->v.Lambda.body);
2632 if (!value) goto failed;
2633 if (PyObject_SetAttrString(result, "body", value) == -1)
2634 goto failed;
2635 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002637 case IfExp_kind:
2638 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2639 if (!result) goto failed;
2640 value = ast2obj_expr(o->v.IfExp.test);
2641 if (!value) goto failed;
2642 if (PyObject_SetAttrString(result, "test", value) == -1)
2643 goto failed;
2644 Py_DECREF(value);
2645 value = ast2obj_expr(o->v.IfExp.body);
2646 if (!value) goto failed;
2647 if (PyObject_SetAttrString(result, "body", value) == -1)
2648 goto failed;
2649 Py_DECREF(value);
2650 value = ast2obj_expr(o->v.IfExp.orelse);
2651 if (!value) goto failed;
2652 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2653 goto failed;
2654 Py_DECREF(value);
2655 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002657 result = PyType_GenericNew(Dict_type, NULL, NULL);
2658 if (!result) goto failed;
2659 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2660 if (!value) goto failed;
2661 if (PyObject_SetAttrString(result, "keys", value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "values", value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002670 case Set_kind:
2671 result = PyType_GenericNew(Set_type, NULL, NULL);
2672 if (!result) goto failed;
2673 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2674 if (!value) goto failed;
2675 if (PyObject_SetAttrString(result, "elts", value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
2678 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002680 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2681 if (!result) goto failed;
2682 value = ast2obj_expr(o->v.ListComp.elt);
2683 if (!value) goto failed;
2684 if (PyObject_SetAttrString(result, "elt", value) == -1)
2685 goto failed;
2686 Py_DECREF(value);
2687 value = ast2obj_list(o->v.ListComp.generators,
2688 ast2obj_comprehension);
2689 if (!value) goto failed;
2690 if (PyObject_SetAttrString(result, "generators", value) == -1)
2691 goto failed;
2692 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002694 case SetComp_kind:
2695 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2696 if (!result) goto failed;
2697 value = ast2obj_expr(o->v.SetComp.elt);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "elt", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
2702 value = ast2obj_list(o->v.SetComp.generators,
2703 ast2obj_comprehension);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "generators", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 break;
2709 case DictComp_kind:
2710 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2711 if (!result) goto failed;
2712 value = ast2obj_expr(o->v.DictComp.key);
2713 if (!value) goto failed;
2714 if (PyObject_SetAttrString(result, "key", value) == -1)
2715 goto failed;
2716 Py_DECREF(value);
2717 value = ast2obj_expr(o->v.DictComp.value);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "value", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_list(o->v.DictComp.generators,
2723 ast2obj_comprehension);
2724 if (!value) goto failed;
2725 if (PyObject_SetAttrString(result, "generators", value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002730 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2731 if (!result) goto failed;
2732 value = ast2obj_expr(o->v.GeneratorExp.elt);
2733 if (!value) goto failed;
2734 if (PyObject_SetAttrString(result, "elt", value) == -1)
2735 goto failed;
2736 Py_DECREF(value);
2737 value = ast2obj_list(o->v.GeneratorExp.generators,
2738 ast2obj_comprehension);
2739 if (!value) goto failed;
2740 if (PyObject_SetAttrString(result, "generators", value) == -1)
2741 goto failed;
2742 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 break;
2744 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002745 result = PyType_GenericNew(Yield_type, NULL, NULL);
2746 if (!result) goto failed;
2747 value = ast2obj_expr(o->v.Yield.value);
2748 if (!value) goto failed;
2749 if (PyObject_SetAttrString(result, "value", value) == -1)
2750 goto failed;
2751 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 break;
2753 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002754 result = PyType_GenericNew(Compare_type, NULL, NULL);
2755 if (!result) goto failed;
2756 value = ast2obj_expr(o->v.Compare.left);
2757 if (!value) goto failed;
2758 if (PyObject_SetAttrString(result, "left", value) == -1)
2759 goto failed;
2760 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002761 {
2762 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2763 value = PyList_New(n);
2764 if (!value) goto failed;
2765 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002766 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 +00002767 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002768 if (!value) goto failed;
2769 if (PyObject_SetAttrString(result, "ops", value) == -1)
2770 goto failed;
2771 Py_DECREF(value);
2772 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2773 if (!value) goto failed;
2774 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2775 goto failed;
2776 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 break;
2778 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002779 result = PyType_GenericNew(Call_type, NULL, NULL);
2780 if (!result) goto failed;
2781 value = ast2obj_expr(o->v.Call.func);
2782 if (!value) goto failed;
2783 if (PyObject_SetAttrString(result, "func", value) == -1)
2784 goto failed;
2785 Py_DECREF(value);
2786 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2787 if (!value) goto failed;
2788 if (PyObject_SetAttrString(result, "args", value) == -1)
2789 goto failed;
2790 Py_DECREF(value);
2791 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2792 if (!value) goto failed;
2793 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2794 goto failed;
2795 Py_DECREF(value);
2796 value = ast2obj_expr(o->v.Call.starargs);
2797 if (!value) goto failed;
2798 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2799 goto failed;
2800 Py_DECREF(value);
2801 value = ast2obj_expr(o->v.Call.kwargs);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 break;
2807 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002808 result = PyType_GenericNew(Repr_type, NULL, NULL);
2809 if (!result) goto failed;
2810 value = ast2obj_expr(o->v.Repr.value);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "value", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 break;
2816 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002817 result = PyType_GenericNew(Num_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_object(o->v.Num.n);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "n", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 break;
2825 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002826 result = PyType_GenericNew(Str_type, NULL, NULL);
2827 if (!result) goto failed;
2828 value = ast2obj_string(o->v.Str.s);
2829 if (!value) goto failed;
2830 if (PyObject_SetAttrString(result, "s", value) == -1)
2831 goto failed;
2832 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 break;
2834 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002835 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2836 if (!result) goto failed;
2837 value = ast2obj_expr(o->v.Attribute.value);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "value", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_identifier(o->v.Attribute.attr);
2843 if (!value) goto failed;
2844 if (PyObject_SetAttrString(result, "attr", value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
2847 value = ast2obj_expr_context(o->v.Attribute.ctx);
2848 if (!value) goto failed;
2849 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 break;
2853 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002854 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2855 if (!result) goto failed;
2856 value = ast2obj_expr(o->v.Subscript.value);
2857 if (!value) goto failed;
2858 if (PyObject_SetAttrString(result, "value", value) == -1)
2859 goto failed;
2860 Py_DECREF(value);
2861 value = ast2obj_slice(o->v.Subscript.slice);
2862 if (!value) goto failed;
2863 if (PyObject_SetAttrString(result, "slice", value) == -1)
2864 goto failed;
2865 Py_DECREF(value);
2866 value = ast2obj_expr_context(o->v.Subscript.ctx);
2867 if (!value) goto failed;
2868 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2869 goto failed;
2870 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 break;
2872 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002873 result = PyType_GenericNew(Name_type, NULL, NULL);
2874 if (!result) goto failed;
2875 value = ast2obj_identifier(o->v.Name.id);
2876 if (!value) goto failed;
2877 if (PyObject_SetAttrString(result, "id", value) == -1)
2878 goto failed;
2879 Py_DECREF(value);
2880 value = ast2obj_expr_context(o->v.Name.ctx);
2881 if (!value) goto failed;
2882 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2883 goto failed;
2884 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 break;
2886 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002887 result = PyType_GenericNew(List_type, NULL, NULL);
2888 if (!result) goto failed;
2889 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2890 if (!value) goto failed;
2891 if (PyObject_SetAttrString(result, "elts", value) == -1)
2892 goto failed;
2893 Py_DECREF(value);
2894 value = ast2obj_expr_context(o->v.List.ctx);
2895 if (!value) goto failed;
2896 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2897 goto failed;
2898 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002899 break;
2900 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002901 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2902 if (!result) goto failed;
2903 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2904 if (!value) goto failed;
2905 if (PyObject_SetAttrString(result, "elts", value) == -1)
2906 goto failed;
2907 Py_DECREF(value);
2908 value = ast2obj_expr_context(o->v.Tuple.ctx);
2909 if (!value) goto failed;
2910 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2911 goto failed;
2912 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002913 break;
2914 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002915 value = ast2obj_int(o->lineno);
2916 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002917 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2918 goto failed;
2919 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002920 value = ast2obj_int(o->col_offset);
2921 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002922 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2923 goto failed;
2924 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002925 return result;
2926failed:
2927 Py_XDECREF(value);
2928 Py_XDECREF(result);
2929 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930}
2931
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002932PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002933{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002934 switch(o) {
2935 case Load:
2936 Py_INCREF(Load_singleton);
2937 return Load_singleton;
2938 case Store:
2939 Py_INCREF(Store_singleton);
2940 return Store_singleton;
2941 case Del:
2942 Py_INCREF(Del_singleton);
2943 return Del_singleton;
2944 case AugLoad:
2945 Py_INCREF(AugLoad_singleton);
2946 return AugLoad_singleton;
2947 case AugStore:
2948 Py_INCREF(AugStore_singleton);
2949 return AugStore_singleton;
2950 case Param:
2951 Py_INCREF(Param_singleton);
2952 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002953 default:
2954 /* should never happen, but just in case ... */
2955 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2956 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002959PyObject*
2960ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002962 slice_ty o = (slice_ty)_o;
2963 PyObject *result = NULL, *value = NULL;
2964 if (!o) {
2965 Py_INCREF(Py_None);
2966 return Py_None;
2967 }
2968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 switch (o->kind) {
2970 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002971 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2972 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 break;
2974 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002975 result = PyType_GenericNew(Slice_type, NULL, NULL);
2976 if (!result) goto failed;
2977 value = ast2obj_expr(o->v.Slice.lower);
2978 if (!value) goto failed;
2979 if (PyObject_SetAttrString(result, "lower", value) == -1)
2980 goto failed;
2981 Py_DECREF(value);
2982 value = ast2obj_expr(o->v.Slice.upper);
2983 if (!value) goto failed;
2984 if (PyObject_SetAttrString(result, "upper", value) == -1)
2985 goto failed;
2986 Py_DECREF(value);
2987 value = ast2obj_expr(o->v.Slice.step);
2988 if (!value) goto failed;
2989 if (PyObject_SetAttrString(result, "step", value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 break;
2993 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002994 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2995 if (!result) goto failed;
2996 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2997 if (!value) goto failed;
2998 if (PyObject_SetAttrString(result, "dims", value) == -1)
2999 goto failed;
3000 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 break;
3002 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003003 result = PyType_GenericNew(Index_type, NULL, NULL);
3004 if (!result) goto failed;
3005 value = ast2obj_expr(o->v.Index.value);
3006 if (!value) goto failed;
3007 if (PyObject_SetAttrString(result, "value", value) == -1)
3008 goto failed;
3009 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 break;
3011 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003012 return result;
3013failed:
3014 Py_XDECREF(value);
3015 Py_XDECREF(result);
3016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017}
3018
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003019PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003021 switch(o) {
3022 case And:
3023 Py_INCREF(And_singleton);
3024 return And_singleton;
3025 case Or:
3026 Py_INCREF(Or_singleton);
3027 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003028 default:
3029 /* should never happen, but just in case ... */
3030 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3031 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003033}
3034PyObject* ast2obj_operator(operator_ty o)
3035{
3036 switch(o) {
3037 case Add:
3038 Py_INCREF(Add_singleton);
3039 return Add_singleton;
3040 case Sub:
3041 Py_INCREF(Sub_singleton);
3042 return Sub_singleton;
3043 case Mult:
3044 Py_INCREF(Mult_singleton);
3045 return Mult_singleton;
3046 case Div:
3047 Py_INCREF(Div_singleton);
3048 return Div_singleton;
3049 case Mod:
3050 Py_INCREF(Mod_singleton);
3051 return Mod_singleton;
3052 case Pow:
3053 Py_INCREF(Pow_singleton);
3054 return Pow_singleton;
3055 case LShift:
3056 Py_INCREF(LShift_singleton);
3057 return LShift_singleton;
3058 case RShift:
3059 Py_INCREF(RShift_singleton);
3060 return RShift_singleton;
3061 case BitOr:
3062 Py_INCREF(BitOr_singleton);
3063 return BitOr_singleton;
3064 case BitXor:
3065 Py_INCREF(BitXor_singleton);
3066 return BitXor_singleton;
3067 case BitAnd:
3068 Py_INCREF(BitAnd_singleton);
3069 return BitAnd_singleton;
3070 case FloorDiv:
3071 Py_INCREF(FloorDiv_singleton);
3072 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003073 default:
3074 /* should never happen, but just in case ... */
3075 PyErr_Format(PyExc_SystemError, "unknown operator found");
3076 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003077 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003078}
3079PyObject* ast2obj_unaryop(unaryop_ty o)
3080{
3081 switch(o) {
3082 case Invert:
3083 Py_INCREF(Invert_singleton);
3084 return Invert_singleton;
3085 case Not:
3086 Py_INCREF(Not_singleton);
3087 return Not_singleton;
3088 case UAdd:
3089 Py_INCREF(UAdd_singleton);
3090 return UAdd_singleton;
3091 case USub:
3092 Py_INCREF(USub_singleton);
3093 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003094 default:
3095 /* should never happen, but just in case ... */
3096 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3097 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003098 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003099}
3100PyObject* ast2obj_cmpop(cmpop_ty o)
3101{
3102 switch(o) {
3103 case Eq:
3104 Py_INCREF(Eq_singleton);
3105 return Eq_singleton;
3106 case NotEq:
3107 Py_INCREF(NotEq_singleton);
3108 return NotEq_singleton;
3109 case Lt:
3110 Py_INCREF(Lt_singleton);
3111 return Lt_singleton;
3112 case LtE:
3113 Py_INCREF(LtE_singleton);
3114 return LtE_singleton;
3115 case Gt:
3116 Py_INCREF(Gt_singleton);
3117 return Gt_singleton;
3118 case GtE:
3119 Py_INCREF(GtE_singleton);
3120 return GtE_singleton;
3121 case Is:
3122 Py_INCREF(Is_singleton);
3123 return Is_singleton;
3124 case IsNot:
3125 Py_INCREF(IsNot_singleton);
3126 return IsNot_singleton;
3127 case In:
3128 Py_INCREF(In_singleton);
3129 return In_singleton;
3130 case NotIn:
3131 Py_INCREF(NotIn_singleton);
3132 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003133 default:
3134 /* should never happen, but just in case ... */
3135 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3136 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003137 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003138}
3139PyObject*
3140ast2obj_comprehension(void* _o)
3141{
3142 comprehension_ty o = (comprehension_ty)_o;
3143 PyObject *result = NULL, *value = NULL;
3144 if (!o) {
3145 Py_INCREF(Py_None);
3146 return Py_None;
3147 }
3148
3149 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3150 if (!result) return NULL;
3151 value = ast2obj_expr(o->target);
3152 if (!value) goto failed;
3153 if (PyObject_SetAttrString(result, "target", value) == -1)
3154 goto failed;
3155 Py_DECREF(value);
3156 value = ast2obj_expr(o->iter);
3157 if (!value) goto failed;
3158 if (PyObject_SetAttrString(result, "iter", value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 value = ast2obj_list(o->ifs, ast2obj_expr);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003166 return result;
3167failed:
3168 Py_XDECREF(value);
3169 Py_XDECREF(result);
3170 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171}
3172
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003173PyObject*
3174ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003176 excepthandler_ty o = (excepthandler_ty)_o;
3177 PyObject *result = NULL, *value = NULL;
3178 if (!o) {
3179 Py_INCREF(Py_None);
3180 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003182
Georg Brandla48f3ab2008-03-30 06:40:17 +00003183 switch (o->kind) {
3184 case ExceptHandler_kind:
3185 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3186 if (!result) goto failed;
3187 value = ast2obj_expr(o->v.ExceptHandler.type);
3188 if (!value) goto failed;
3189 if (PyObject_SetAttrString(result, "type", value) == -1)
3190 goto failed;
3191 Py_DECREF(value);
3192 value = ast2obj_expr(o->v.ExceptHandler.name);
3193 if (!value) goto failed;
3194 if (PyObject_SetAttrString(result, "name", value) == -1)
3195 goto failed;
3196 Py_DECREF(value);
3197 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3198 if (!value) goto failed;
3199 if (PyObject_SetAttrString(result, "body", value) == -1)
3200 goto failed;
3201 Py_DECREF(value);
3202 break;
3203 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003204 value = ast2obj_int(o->lineno);
3205 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003206 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003207 goto failed;
3208 Py_DECREF(value);
3209 value = ast2obj_int(o->col_offset);
3210 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003211 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003212 goto failed;
3213 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003214 return result;
3215failed:
3216 Py_XDECREF(value);
3217 Py_XDECREF(result);
3218 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219}
3220
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003221PyObject*
3222ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003224 arguments_ty o = (arguments_ty)_o;
3225 PyObject *result = NULL, *value = NULL;
3226 if (!o) {
3227 Py_INCREF(Py_None);
3228 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003230
3231 result = PyType_GenericNew(arguments_type, NULL, NULL);
3232 if (!result) return NULL;
3233 value = ast2obj_list(o->args, ast2obj_expr);
3234 if (!value) goto failed;
3235 if (PyObject_SetAttrString(result, "args", value) == -1)
3236 goto failed;
3237 Py_DECREF(value);
3238 value = ast2obj_identifier(o->vararg);
3239 if (!value) goto failed;
3240 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3241 goto failed;
3242 Py_DECREF(value);
3243 value = ast2obj_identifier(o->kwarg);
3244 if (!value) goto failed;
3245 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3246 goto failed;
3247 Py_DECREF(value);
3248 value = ast2obj_list(o->defaults, ast2obj_expr);
3249 if (!value) goto failed;
3250 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3251 goto failed;
3252 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003253 return result;
3254failed:
3255 Py_XDECREF(value);
3256 Py_XDECREF(result);
3257 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258}
3259
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003260PyObject*
3261ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003263 keyword_ty o = (keyword_ty)_o;
3264 PyObject *result = NULL, *value = NULL;
3265 if (!o) {
3266 Py_INCREF(Py_None);
3267 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003269
3270 result = PyType_GenericNew(keyword_type, NULL, NULL);
3271 if (!result) return NULL;
3272 value = ast2obj_identifier(o->arg);
3273 if (!value) goto failed;
3274 if (PyObject_SetAttrString(result, "arg", value) == -1)
3275 goto failed;
3276 Py_DECREF(value);
3277 value = ast2obj_expr(o->value);
3278 if (!value) goto failed;
3279 if (PyObject_SetAttrString(result, "value", value) == -1)
3280 goto failed;
3281 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003282 return result;
3283failed:
3284 Py_XDECREF(value);
3285 Py_XDECREF(result);
3286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287}
3288
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003289PyObject*
3290ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003292 alias_ty o = (alias_ty)_o;
3293 PyObject *result = NULL, *value = NULL;
3294 if (!o) {
3295 Py_INCREF(Py_None);
3296 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003298
3299 result = PyType_GenericNew(alias_type, NULL, NULL);
3300 if (!result) return NULL;
3301 value = ast2obj_identifier(o->name);
3302 if (!value) goto failed;
3303 if (PyObject_SetAttrString(result, "name", value) == -1)
3304 goto failed;
3305 Py_DECREF(value);
3306 value = ast2obj_identifier(o->asname);
3307 if (!value) goto failed;
3308 if (PyObject_SetAttrString(result, "asname", value) == -1)
3309 goto failed;
3310 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003311 return result;
3312failed:
3313 Py_XDECREF(value);
3314 Py_XDECREF(result);
3315 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316}
3317
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318
Georg Brandlfc8eef32008-03-28 12:11:56 +00003319int
3320obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3321{
3322 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003323 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003324
3325
3326 if (obj == Py_None) {
3327 *out = NULL;
3328 return 0;
3329 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003330 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3331 if (isinstance == -1) {
3332 return 1;
3333 }
3334 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003335 asdl_seq* body;
3336
3337 if (PyObject_HasAttrString(obj, "body")) {
3338 int res;
3339 Py_ssize_t len;
3340 Py_ssize_t i;
3341 tmp = PyObject_GetAttrString(obj, "body");
3342 if (tmp == NULL) goto failed;
3343 if (!PyList_Check(tmp)) {
3344 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3345 goto failed;
3346 }
3347 len = PyList_GET_SIZE(tmp);
3348 body = asdl_seq_new(len, arena);
3349 if (body == NULL) goto failed;
3350 for (i = 0; i < len; i++) {
3351 stmt_ty value;
3352 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3353 if (res != 0) goto failed;
3354 asdl_seq_SET(body, i, value);
3355 }
3356 Py_XDECREF(tmp);
3357 tmp = NULL;
3358 } else {
3359 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3360 return 1;
3361 }
3362 *out = Module(body, arena);
3363 if (*out == NULL) goto failed;
3364 return 0;
3365 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003366 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3367 if (isinstance == -1) {
3368 return 1;
3369 }
3370 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003371 asdl_seq* body;
3372
3373 if (PyObject_HasAttrString(obj, "body")) {
3374 int res;
3375 Py_ssize_t len;
3376 Py_ssize_t i;
3377 tmp = PyObject_GetAttrString(obj, "body");
3378 if (tmp == NULL) goto failed;
3379 if (!PyList_Check(tmp)) {
3380 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3381 goto failed;
3382 }
3383 len = PyList_GET_SIZE(tmp);
3384 body = asdl_seq_new(len, arena);
3385 if (body == NULL) goto failed;
3386 for (i = 0; i < len; i++) {
3387 stmt_ty value;
3388 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3389 if (res != 0) goto failed;
3390 asdl_seq_SET(body, i, value);
3391 }
3392 Py_XDECREF(tmp);
3393 tmp = NULL;
3394 } else {
3395 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3396 return 1;
3397 }
3398 *out = Interactive(body, arena);
3399 if (*out == NULL) goto failed;
3400 return 0;
3401 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003402 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3403 if (isinstance == -1) {
3404 return 1;
3405 }
3406 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003407 expr_ty body;
3408
3409 if (PyObject_HasAttrString(obj, "body")) {
3410 int res;
3411 tmp = PyObject_GetAttrString(obj, "body");
3412 if (tmp == NULL) goto failed;
3413 res = obj2ast_expr(tmp, &body, arena);
3414 if (res != 0) goto failed;
3415 Py_XDECREF(tmp);
3416 tmp = NULL;
3417 } else {
3418 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3419 return 1;
3420 }
3421 *out = Expression(body, arena);
3422 if (*out == NULL) goto failed;
3423 return 0;
3424 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003425 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3426 if (isinstance == -1) {
3427 return 1;
3428 }
3429 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003430 asdl_seq* body;
3431
3432 if (PyObject_HasAttrString(obj, "body")) {
3433 int res;
3434 Py_ssize_t len;
3435 Py_ssize_t i;
3436 tmp = PyObject_GetAttrString(obj, "body");
3437 if (tmp == NULL) goto failed;
3438 if (!PyList_Check(tmp)) {
3439 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3440 goto failed;
3441 }
3442 len = PyList_GET_SIZE(tmp);
3443 body = asdl_seq_new(len, arena);
3444 if (body == NULL) goto failed;
3445 for (i = 0; i < len; i++) {
3446 stmt_ty value;
3447 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3448 if (res != 0) goto failed;
3449 asdl_seq_SET(body, i, value);
3450 }
3451 Py_XDECREF(tmp);
3452 tmp = NULL;
3453 } else {
3454 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3455 return 1;
3456 }
3457 *out = Suite(body, arena);
3458 if (*out == NULL) goto failed;
3459 return 0;
3460 }
3461
3462 tmp = PyObject_Repr(obj);
3463 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003464 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00003465failed:
3466 Py_XDECREF(tmp);
3467 return 1;
3468}
3469
3470int
3471obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3472{
3473 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003474 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003475
3476 int lineno;
3477 int col_offset;
3478
3479 if (obj == Py_None) {
3480 *out = NULL;
3481 return 0;
3482 }
3483 if (PyObject_HasAttrString(obj, "lineno")) {
3484 int res;
3485 tmp = PyObject_GetAttrString(obj, "lineno");
3486 if (tmp == NULL) goto failed;
3487 res = obj2ast_int(tmp, &lineno, arena);
3488 if (res != 0) goto failed;
3489 Py_XDECREF(tmp);
3490 tmp = NULL;
3491 } else {
3492 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3493 return 1;
3494 }
3495 if (PyObject_HasAttrString(obj, "col_offset")) {
3496 int res;
3497 tmp = PyObject_GetAttrString(obj, "col_offset");
3498 if (tmp == NULL) goto failed;
3499 res = obj2ast_int(tmp, &col_offset, arena);
3500 if (res != 0) goto failed;
3501 Py_XDECREF(tmp);
3502 tmp = NULL;
3503 } else {
3504 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3505 return 1;
3506 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003507 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3508 if (isinstance == -1) {
3509 return 1;
3510 }
3511 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003512 identifier name;
3513 arguments_ty args;
3514 asdl_seq* body;
3515 asdl_seq* decorator_list;
3516
3517 if (PyObject_HasAttrString(obj, "name")) {
3518 int res;
3519 tmp = PyObject_GetAttrString(obj, "name");
3520 if (tmp == NULL) goto failed;
3521 res = obj2ast_identifier(tmp, &name, arena);
3522 if (res != 0) goto failed;
3523 Py_XDECREF(tmp);
3524 tmp = NULL;
3525 } else {
3526 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3527 return 1;
3528 }
3529 if (PyObject_HasAttrString(obj, "args")) {
3530 int res;
3531 tmp = PyObject_GetAttrString(obj, "args");
3532 if (tmp == NULL) goto failed;
3533 res = obj2ast_arguments(tmp, &args, arena);
3534 if (res != 0) goto failed;
3535 Py_XDECREF(tmp);
3536 tmp = NULL;
3537 } else {
3538 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3539 return 1;
3540 }
3541 if (PyObject_HasAttrString(obj, "body")) {
3542 int res;
3543 Py_ssize_t len;
3544 Py_ssize_t i;
3545 tmp = PyObject_GetAttrString(obj, "body");
3546 if (tmp == NULL) goto failed;
3547 if (!PyList_Check(tmp)) {
3548 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3549 goto failed;
3550 }
3551 len = PyList_GET_SIZE(tmp);
3552 body = asdl_seq_new(len, arena);
3553 if (body == NULL) goto failed;
3554 for (i = 0; i < len; i++) {
3555 stmt_ty value;
3556 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3557 if (res != 0) goto failed;
3558 asdl_seq_SET(body, i, value);
3559 }
3560 Py_XDECREF(tmp);
3561 tmp = NULL;
3562 } else {
3563 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3564 return 1;
3565 }
3566 if (PyObject_HasAttrString(obj, "decorator_list")) {
3567 int res;
3568 Py_ssize_t len;
3569 Py_ssize_t i;
3570 tmp = PyObject_GetAttrString(obj, "decorator_list");
3571 if (tmp == NULL) goto failed;
3572 if (!PyList_Check(tmp)) {
3573 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3574 goto failed;
3575 }
3576 len = PyList_GET_SIZE(tmp);
3577 decorator_list = asdl_seq_new(len, arena);
3578 if (decorator_list == NULL) goto failed;
3579 for (i = 0; i < len; i++) {
3580 expr_ty value;
3581 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3582 if (res != 0) goto failed;
3583 asdl_seq_SET(decorator_list, i, value);
3584 }
3585 Py_XDECREF(tmp);
3586 tmp = NULL;
3587 } else {
3588 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3589 return 1;
3590 }
3591 *out = FunctionDef(name, args, body, decorator_list, lineno,
3592 col_offset, arena);
3593 if (*out == NULL) goto failed;
3594 return 0;
3595 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003596 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3597 if (isinstance == -1) {
3598 return 1;
3599 }
3600 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003601 identifier name;
3602 asdl_seq* bases;
3603 asdl_seq* body;
3604 asdl_seq* decorator_list;
3605
3606 if (PyObject_HasAttrString(obj, "name")) {
3607 int res;
3608 tmp = PyObject_GetAttrString(obj, "name");
3609 if (tmp == NULL) goto failed;
3610 res = obj2ast_identifier(tmp, &name, arena);
3611 if (res != 0) goto failed;
3612 Py_XDECREF(tmp);
3613 tmp = NULL;
3614 } else {
3615 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3616 return 1;
3617 }
3618 if (PyObject_HasAttrString(obj, "bases")) {
3619 int res;
3620 Py_ssize_t len;
3621 Py_ssize_t i;
3622 tmp = PyObject_GetAttrString(obj, "bases");
3623 if (tmp == NULL) goto failed;
3624 if (!PyList_Check(tmp)) {
3625 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3626 goto failed;
3627 }
3628 len = PyList_GET_SIZE(tmp);
3629 bases = asdl_seq_new(len, arena);
3630 if (bases == NULL) goto failed;
3631 for (i = 0; i < len; i++) {
3632 expr_ty value;
3633 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3634 if (res != 0) goto failed;
3635 asdl_seq_SET(bases, i, value);
3636 }
3637 Py_XDECREF(tmp);
3638 tmp = NULL;
3639 } else {
3640 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3641 return 1;
3642 }
3643 if (PyObject_HasAttrString(obj, "body")) {
3644 int res;
3645 Py_ssize_t len;
3646 Py_ssize_t i;
3647 tmp = PyObject_GetAttrString(obj, "body");
3648 if (tmp == NULL) goto failed;
3649 if (!PyList_Check(tmp)) {
3650 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3651 goto failed;
3652 }
3653 len = PyList_GET_SIZE(tmp);
3654 body = asdl_seq_new(len, arena);
3655 if (body == NULL) goto failed;
3656 for (i = 0; i < len; i++) {
3657 stmt_ty value;
3658 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3659 if (res != 0) goto failed;
3660 asdl_seq_SET(body, i, value);
3661 }
3662 Py_XDECREF(tmp);
3663 tmp = NULL;
3664 } else {
3665 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3666 return 1;
3667 }
3668 if (PyObject_HasAttrString(obj, "decorator_list")) {
3669 int res;
3670 Py_ssize_t len;
3671 Py_ssize_t i;
3672 tmp = PyObject_GetAttrString(obj, "decorator_list");
3673 if (tmp == NULL) goto failed;
3674 if (!PyList_Check(tmp)) {
3675 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3676 goto failed;
3677 }
3678 len = PyList_GET_SIZE(tmp);
3679 decorator_list = asdl_seq_new(len, arena);
3680 if (decorator_list == NULL) goto failed;
3681 for (i = 0; i < len; i++) {
3682 expr_ty value;
3683 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3684 if (res != 0) goto failed;
3685 asdl_seq_SET(decorator_list, i, value);
3686 }
3687 Py_XDECREF(tmp);
3688 tmp = NULL;
3689 } else {
3690 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3691 return 1;
3692 }
3693 *out = ClassDef(name, bases, body, decorator_list, lineno,
3694 col_offset, arena);
3695 if (*out == NULL) goto failed;
3696 return 0;
3697 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003698 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3699 if (isinstance == -1) {
3700 return 1;
3701 }
3702 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003703 expr_ty value;
3704
3705 if (PyObject_HasAttrString(obj, "value")) {
3706 int res;
3707 tmp = PyObject_GetAttrString(obj, "value");
3708 if (tmp == NULL) goto failed;
3709 res = obj2ast_expr(tmp, &value, arena);
3710 if (res != 0) goto failed;
3711 Py_XDECREF(tmp);
3712 tmp = NULL;
3713 } else {
3714 value = NULL;
3715 }
3716 *out = Return(value, lineno, col_offset, arena);
3717 if (*out == NULL) goto failed;
3718 return 0;
3719 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003720 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3721 if (isinstance == -1) {
3722 return 1;
3723 }
3724 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003725 asdl_seq* targets;
3726
3727 if (PyObject_HasAttrString(obj, "targets")) {
3728 int res;
3729 Py_ssize_t len;
3730 Py_ssize_t i;
3731 tmp = PyObject_GetAttrString(obj, "targets");
3732 if (tmp == NULL) goto failed;
3733 if (!PyList_Check(tmp)) {
3734 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3735 goto failed;
3736 }
3737 len = PyList_GET_SIZE(tmp);
3738 targets = asdl_seq_new(len, arena);
3739 if (targets == NULL) goto failed;
3740 for (i = 0; i < len; i++) {
3741 expr_ty value;
3742 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3743 if (res != 0) goto failed;
3744 asdl_seq_SET(targets, i, value);
3745 }
3746 Py_XDECREF(tmp);
3747 tmp = NULL;
3748 } else {
3749 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3750 return 1;
3751 }
3752 *out = Delete(targets, lineno, col_offset, arena);
3753 if (*out == NULL) goto failed;
3754 return 0;
3755 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003756 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3757 if (isinstance == -1) {
3758 return 1;
3759 }
3760 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003761 asdl_seq* targets;
3762 expr_ty value;
3763
3764 if (PyObject_HasAttrString(obj, "targets")) {
3765 int res;
3766 Py_ssize_t len;
3767 Py_ssize_t i;
3768 tmp = PyObject_GetAttrString(obj, "targets");
3769 if (tmp == NULL) goto failed;
3770 if (!PyList_Check(tmp)) {
3771 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3772 goto failed;
3773 }
3774 len = PyList_GET_SIZE(tmp);
3775 targets = asdl_seq_new(len, arena);
3776 if (targets == NULL) goto failed;
3777 for (i = 0; i < len; i++) {
3778 expr_ty value;
3779 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3780 if (res != 0) goto failed;
3781 asdl_seq_SET(targets, i, value);
3782 }
3783 Py_XDECREF(tmp);
3784 tmp = NULL;
3785 } else {
3786 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3787 return 1;
3788 }
3789 if (PyObject_HasAttrString(obj, "value")) {
3790 int res;
3791 tmp = PyObject_GetAttrString(obj, "value");
3792 if (tmp == NULL) goto failed;
3793 res = obj2ast_expr(tmp, &value, arena);
3794 if (res != 0) goto failed;
3795 Py_XDECREF(tmp);
3796 tmp = NULL;
3797 } else {
3798 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3799 return 1;
3800 }
3801 *out = Assign(targets, value, lineno, col_offset, arena);
3802 if (*out == NULL) goto failed;
3803 return 0;
3804 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003805 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3806 if (isinstance == -1) {
3807 return 1;
3808 }
3809 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003810 expr_ty target;
3811 operator_ty op;
3812 expr_ty value;
3813
3814 if (PyObject_HasAttrString(obj, "target")) {
3815 int res;
3816 tmp = PyObject_GetAttrString(obj, "target");
3817 if (tmp == NULL) goto failed;
3818 res = obj2ast_expr(tmp, &target, arena);
3819 if (res != 0) goto failed;
3820 Py_XDECREF(tmp);
3821 tmp = NULL;
3822 } else {
3823 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3824 return 1;
3825 }
3826 if (PyObject_HasAttrString(obj, "op")) {
3827 int res;
3828 tmp = PyObject_GetAttrString(obj, "op");
3829 if (tmp == NULL) goto failed;
3830 res = obj2ast_operator(tmp, &op, arena);
3831 if (res != 0) goto failed;
3832 Py_XDECREF(tmp);
3833 tmp = NULL;
3834 } else {
3835 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3836 return 1;
3837 }
3838 if (PyObject_HasAttrString(obj, "value")) {
3839 int res;
3840 tmp = PyObject_GetAttrString(obj, "value");
3841 if (tmp == NULL) goto failed;
3842 res = obj2ast_expr(tmp, &value, arena);
3843 if (res != 0) goto failed;
3844 Py_XDECREF(tmp);
3845 tmp = NULL;
3846 } else {
3847 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3848 return 1;
3849 }
3850 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3851 if (*out == NULL) goto failed;
3852 return 0;
3853 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003854 isinstance = PyObject_IsInstance(obj, (PyObject*)Print_type);
3855 if (isinstance == -1) {
3856 return 1;
3857 }
3858 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003859 expr_ty dest;
3860 asdl_seq* values;
3861 bool nl;
3862
3863 if (PyObject_HasAttrString(obj, "dest")) {
3864 int res;
3865 tmp = PyObject_GetAttrString(obj, "dest");
3866 if (tmp == NULL) goto failed;
3867 res = obj2ast_expr(tmp, &dest, arena);
3868 if (res != 0) goto failed;
3869 Py_XDECREF(tmp);
3870 tmp = NULL;
3871 } else {
3872 dest = NULL;
3873 }
3874 if (PyObject_HasAttrString(obj, "values")) {
3875 int res;
3876 Py_ssize_t len;
3877 Py_ssize_t i;
3878 tmp = PyObject_GetAttrString(obj, "values");
3879 if (tmp == NULL) goto failed;
3880 if (!PyList_Check(tmp)) {
3881 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3882 goto failed;
3883 }
3884 len = PyList_GET_SIZE(tmp);
3885 values = asdl_seq_new(len, arena);
3886 if (values == NULL) goto failed;
3887 for (i = 0; i < len; i++) {
3888 expr_ty value;
3889 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3890 if (res != 0) goto failed;
3891 asdl_seq_SET(values, i, value);
3892 }
3893 Py_XDECREF(tmp);
3894 tmp = NULL;
3895 } else {
3896 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3897 return 1;
3898 }
3899 if (PyObject_HasAttrString(obj, "nl")) {
3900 int res;
3901 tmp = PyObject_GetAttrString(obj, "nl");
3902 if (tmp == NULL) goto failed;
3903 res = obj2ast_bool(tmp, &nl, arena);
3904 if (res != 0) goto failed;
3905 Py_XDECREF(tmp);
3906 tmp = NULL;
3907 } else {
3908 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3909 return 1;
3910 }
3911 *out = Print(dest, values, nl, lineno, col_offset, arena);
3912 if (*out == NULL) goto failed;
3913 return 0;
3914 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003915 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3916 if (isinstance == -1) {
3917 return 1;
3918 }
3919 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003920 expr_ty target;
3921 expr_ty iter;
3922 asdl_seq* body;
3923 asdl_seq* orelse;
3924
3925 if (PyObject_HasAttrString(obj, "target")) {
3926 int res;
3927 tmp = PyObject_GetAttrString(obj, "target");
3928 if (tmp == NULL) goto failed;
3929 res = obj2ast_expr(tmp, &target, arena);
3930 if (res != 0) goto failed;
3931 Py_XDECREF(tmp);
3932 tmp = NULL;
3933 } else {
3934 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3935 return 1;
3936 }
3937 if (PyObject_HasAttrString(obj, "iter")) {
3938 int res;
3939 tmp = PyObject_GetAttrString(obj, "iter");
3940 if (tmp == NULL) goto failed;
3941 res = obj2ast_expr(tmp, &iter, arena);
3942 if (res != 0) goto failed;
3943 Py_XDECREF(tmp);
3944 tmp = NULL;
3945 } else {
3946 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3947 return 1;
3948 }
3949 if (PyObject_HasAttrString(obj, "body")) {
3950 int res;
3951 Py_ssize_t len;
3952 Py_ssize_t i;
3953 tmp = PyObject_GetAttrString(obj, "body");
3954 if (tmp == NULL) goto failed;
3955 if (!PyList_Check(tmp)) {
3956 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3957 goto failed;
3958 }
3959 len = PyList_GET_SIZE(tmp);
3960 body = asdl_seq_new(len, arena);
3961 if (body == NULL) goto failed;
3962 for (i = 0; i < len; i++) {
3963 stmt_ty value;
3964 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3965 if (res != 0) goto failed;
3966 asdl_seq_SET(body, i, value);
3967 }
3968 Py_XDECREF(tmp);
3969 tmp = NULL;
3970 } else {
3971 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3972 return 1;
3973 }
3974 if (PyObject_HasAttrString(obj, "orelse")) {
3975 int res;
3976 Py_ssize_t len;
3977 Py_ssize_t i;
3978 tmp = PyObject_GetAttrString(obj, "orelse");
3979 if (tmp == NULL) goto failed;
3980 if (!PyList_Check(tmp)) {
3981 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3982 goto failed;
3983 }
3984 len = PyList_GET_SIZE(tmp);
3985 orelse = asdl_seq_new(len, arena);
3986 if (orelse == NULL) goto failed;
3987 for (i = 0; i < len; i++) {
3988 stmt_ty value;
3989 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3990 if (res != 0) goto failed;
3991 asdl_seq_SET(orelse, i, value);
3992 }
3993 Py_XDECREF(tmp);
3994 tmp = NULL;
3995 } else {
3996 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3997 return 1;
3998 }
3999 *out = For(target, iter, body, orelse, lineno, col_offset,
4000 arena);
4001 if (*out == NULL) goto failed;
4002 return 0;
4003 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004004 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
4005 if (isinstance == -1) {
4006 return 1;
4007 }
4008 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004009 expr_ty test;
4010 asdl_seq* body;
4011 asdl_seq* orelse;
4012
4013 if (PyObject_HasAttrString(obj, "test")) {
4014 int res;
4015 tmp = PyObject_GetAttrString(obj, "test");
4016 if (tmp == NULL) goto failed;
4017 res = obj2ast_expr(tmp, &test, arena);
4018 if (res != 0) goto failed;
4019 Py_XDECREF(tmp);
4020 tmp = NULL;
4021 } else {
4022 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4023 return 1;
4024 }
4025 if (PyObject_HasAttrString(obj, "body")) {
4026 int res;
4027 Py_ssize_t len;
4028 Py_ssize_t i;
4029 tmp = PyObject_GetAttrString(obj, "body");
4030 if (tmp == NULL) goto failed;
4031 if (!PyList_Check(tmp)) {
4032 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4033 goto failed;
4034 }
4035 len = PyList_GET_SIZE(tmp);
4036 body = asdl_seq_new(len, arena);
4037 if (body == NULL) goto failed;
4038 for (i = 0; i < len; i++) {
4039 stmt_ty value;
4040 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4041 if (res != 0) goto failed;
4042 asdl_seq_SET(body, i, value);
4043 }
4044 Py_XDECREF(tmp);
4045 tmp = NULL;
4046 } else {
4047 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4048 return 1;
4049 }
4050 if (PyObject_HasAttrString(obj, "orelse")) {
4051 int res;
4052 Py_ssize_t len;
4053 Py_ssize_t i;
4054 tmp = PyObject_GetAttrString(obj, "orelse");
4055 if (tmp == NULL) goto failed;
4056 if (!PyList_Check(tmp)) {
4057 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4058 goto failed;
4059 }
4060 len = PyList_GET_SIZE(tmp);
4061 orelse = asdl_seq_new(len, arena);
4062 if (orelse == NULL) goto failed;
4063 for (i = 0; i < len; i++) {
4064 stmt_ty value;
4065 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4066 if (res != 0) goto failed;
4067 asdl_seq_SET(orelse, i, value);
4068 }
4069 Py_XDECREF(tmp);
4070 tmp = NULL;
4071 } else {
4072 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4073 return 1;
4074 }
4075 *out = While(test, body, orelse, lineno, col_offset, arena);
4076 if (*out == NULL) goto failed;
4077 return 0;
4078 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004079 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4080 if (isinstance == -1) {
4081 return 1;
4082 }
4083 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004084 expr_ty test;
4085 asdl_seq* body;
4086 asdl_seq* orelse;
4087
4088 if (PyObject_HasAttrString(obj, "test")) {
4089 int res;
4090 tmp = PyObject_GetAttrString(obj, "test");
4091 if (tmp == NULL) goto failed;
4092 res = obj2ast_expr(tmp, &test, arena);
4093 if (res != 0) goto failed;
4094 Py_XDECREF(tmp);
4095 tmp = NULL;
4096 } else {
4097 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4098 return 1;
4099 }
4100 if (PyObject_HasAttrString(obj, "body")) {
4101 int res;
4102 Py_ssize_t len;
4103 Py_ssize_t i;
4104 tmp = PyObject_GetAttrString(obj, "body");
4105 if (tmp == NULL) goto failed;
4106 if (!PyList_Check(tmp)) {
4107 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4108 goto failed;
4109 }
4110 len = PyList_GET_SIZE(tmp);
4111 body = asdl_seq_new(len, arena);
4112 if (body == NULL) goto failed;
4113 for (i = 0; i < len; i++) {
4114 stmt_ty value;
4115 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4116 if (res != 0) goto failed;
4117 asdl_seq_SET(body, i, value);
4118 }
4119 Py_XDECREF(tmp);
4120 tmp = NULL;
4121 } else {
4122 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4123 return 1;
4124 }
4125 if (PyObject_HasAttrString(obj, "orelse")) {
4126 int res;
4127 Py_ssize_t len;
4128 Py_ssize_t i;
4129 tmp = PyObject_GetAttrString(obj, "orelse");
4130 if (tmp == NULL) goto failed;
4131 if (!PyList_Check(tmp)) {
4132 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4133 goto failed;
4134 }
4135 len = PyList_GET_SIZE(tmp);
4136 orelse = asdl_seq_new(len, arena);
4137 if (orelse == NULL) goto failed;
4138 for (i = 0; i < len; i++) {
4139 stmt_ty value;
4140 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4141 if (res != 0) goto failed;
4142 asdl_seq_SET(orelse, i, value);
4143 }
4144 Py_XDECREF(tmp);
4145 tmp = NULL;
4146 } else {
4147 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4148 return 1;
4149 }
4150 *out = If(test, body, orelse, lineno, col_offset, arena);
4151 if (*out == NULL) goto failed;
4152 return 0;
4153 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004154 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4155 if (isinstance == -1) {
4156 return 1;
4157 }
4158 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004159 expr_ty context_expr;
4160 expr_ty optional_vars;
4161 asdl_seq* body;
4162
4163 if (PyObject_HasAttrString(obj, "context_expr")) {
4164 int res;
4165 tmp = PyObject_GetAttrString(obj, "context_expr");
4166 if (tmp == NULL) goto failed;
4167 res = obj2ast_expr(tmp, &context_expr, arena);
4168 if (res != 0) goto failed;
4169 Py_XDECREF(tmp);
4170 tmp = NULL;
4171 } else {
4172 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4173 return 1;
4174 }
4175 if (PyObject_HasAttrString(obj, "optional_vars")) {
4176 int res;
4177 tmp = PyObject_GetAttrString(obj, "optional_vars");
4178 if (tmp == NULL) goto failed;
4179 res = obj2ast_expr(tmp, &optional_vars, arena);
4180 if (res != 0) goto failed;
4181 Py_XDECREF(tmp);
4182 tmp = NULL;
4183 } else {
4184 optional_vars = NULL;
4185 }
4186 if (PyObject_HasAttrString(obj, "body")) {
4187 int res;
4188 Py_ssize_t len;
4189 Py_ssize_t i;
4190 tmp = PyObject_GetAttrString(obj, "body");
4191 if (tmp == NULL) goto failed;
4192 if (!PyList_Check(tmp)) {
4193 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4194 goto failed;
4195 }
4196 len = PyList_GET_SIZE(tmp);
4197 body = asdl_seq_new(len, arena);
4198 if (body == NULL) goto failed;
4199 for (i = 0; i < len; i++) {
4200 stmt_ty value;
4201 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4202 if (res != 0) goto failed;
4203 asdl_seq_SET(body, i, value);
4204 }
4205 Py_XDECREF(tmp);
4206 tmp = NULL;
4207 } else {
4208 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4209 return 1;
4210 }
4211 *out = With(context_expr, optional_vars, body, lineno,
4212 col_offset, arena);
4213 if (*out == NULL) goto failed;
4214 return 0;
4215 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004216 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4217 if (isinstance == -1) {
4218 return 1;
4219 }
4220 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004221 expr_ty type;
4222 expr_ty inst;
4223 expr_ty tback;
4224
4225 if (PyObject_HasAttrString(obj, "type")) {
4226 int res;
4227 tmp = PyObject_GetAttrString(obj, "type");
4228 if (tmp == NULL) goto failed;
4229 res = obj2ast_expr(tmp, &type, arena);
4230 if (res != 0) goto failed;
4231 Py_XDECREF(tmp);
4232 tmp = NULL;
4233 } else {
4234 type = NULL;
4235 }
4236 if (PyObject_HasAttrString(obj, "inst")) {
4237 int res;
4238 tmp = PyObject_GetAttrString(obj, "inst");
4239 if (tmp == NULL) goto failed;
4240 res = obj2ast_expr(tmp, &inst, arena);
4241 if (res != 0) goto failed;
4242 Py_XDECREF(tmp);
4243 tmp = NULL;
4244 } else {
4245 inst = NULL;
4246 }
4247 if (PyObject_HasAttrString(obj, "tback")) {
4248 int res;
4249 tmp = PyObject_GetAttrString(obj, "tback");
4250 if (tmp == NULL) goto failed;
4251 res = obj2ast_expr(tmp, &tback, arena);
4252 if (res != 0) goto failed;
4253 Py_XDECREF(tmp);
4254 tmp = NULL;
4255 } else {
4256 tback = NULL;
4257 }
4258 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4259 if (*out == NULL) goto failed;
4260 return 0;
4261 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004262 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4263 if (isinstance == -1) {
4264 return 1;
4265 }
4266 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004267 asdl_seq* body;
4268 asdl_seq* handlers;
4269 asdl_seq* orelse;
4270
4271 if (PyObject_HasAttrString(obj, "body")) {
4272 int res;
4273 Py_ssize_t len;
4274 Py_ssize_t i;
4275 tmp = PyObject_GetAttrString(obj, "body");
4276 if (tmp == NULL) goto failed;
4277 if (!PyList_Check(tmp)) {
4278 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4279 goto failed;
4280 }
4281 len = PyList_GET_SIZE(tmp);
4282 body = asdl_seq_new(len, arena);
4283 if (body == NULL) goto failed;
4284 for (i = 0; i < len; i++) {
4285 stmt_ty value;
4286 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4287 if (res != 0) goto failed;
4288 asdl_seq_SET(body, i, value);
4289 }
4290 Py_XDECREF(tmp);
4291 tmp = NULL;
4292 } else {
4293 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4294 return 1;
4295 }
4296 if (PyObject_HasAttrString(obj, "handlers")) {
4297 int res;
4298 Py_ssize_t len;
4299 Py_ssize_t i;
4300 tmp = PyObject_GetAttrString(obj, "handlers");
4301 if (tmp == NULL) goto failed;
4302 if (!PyList_Check(tmp)) {
4303 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4304 goto failed;
4305 }
4306 len = PyList_GET_SIZE(tmp);
4307 handlers = asdl_seq_new(len, arena);
4308 if (handlers == NULL) goto failed;
4309 for (i = 0; i < len; i++) {
4310 excepthandler_ty value;
4311 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4312 if (res != 0) goto failed;
4313 asdl_seq_SET(handlers, i, value);
4314 }
4315 Py_XDECREF(tmp);
4316 tmp = NULL;
4317 } else {
4318 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4319 return 1;
4320 }
4321 if (PyObject_HasAttrString(obj, "orelse")) {
4322 int res;
4323 Py_ssize_t len;
4324 Py_ssize_t i;
4325 tmp = PyObject_GetAttrString(obj, "orelse");
4326 if (tmp == NULL) goto failed;
4327 if (!PyList_Check(tmp)) {
4328 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4329 goto failed;
4330 }
4331 len = PyList_GET_SIZE(tmp);
4332 orelse = asdl_seq_new(len, arena);
4333 if (orelse == NULL) goto failed;
4334 for (i = 0; i < len; i++) {
4335 stmt_ty value;
4336 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4337 if (res != 0) goto failed;
4338 asdl_seq_SET(orelse, i, value);
4339 }
4340 Py_XDECREF(tmp);
4341 tmp = NULL;
4342 } else {
4343 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4344 return 1;
4345 }
4346 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4347 arena);
4348 if (*out == NULL) goto failed;
4349 return 0;
4350 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004351 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4352 if (isinstance == -1) {
4353 return 1;
4354 }
4355 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004356 asdl_seq* body;
4357 asdl_seq* finalbody;
4358
4359 if (PyObject_HasAttrString(obj, "body")) {
4360 int res;
4361 Py_ssize_t len;
4362 Py_ssize_t i;
4363 tmp = PyObject_GetAttrString(obj, "body");
4364 if (tmp == NULL) goto failed;
4365 if (!PyList_Check(tmp)) {
4366 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4367 goto failed;
4368 }
4369 len = PyList_GET_SIZE(tmp);
4370 body = asdl_seq_new(len, arena);
4371 if (body == NULL) goto failed;
4372 for (i = 0; i < len; i++) {
4373 stmt_ty value;
4374 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4375 if (res != 0) goto failed;
4376 asdl_seq_SET(body, i, value);
4377 }
4378 Py_XDECREF(tmp);
4379 tmp = NULL;
4380 } else {
4381 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4382 return 1;
4383 }
4384 if (PyObject_HasAttrString(obj, "finalbody")) {
4385 int res;
4386 Py_ssize_t len;
4387 Py_ssize_t i;
4388 tmp = PyObject_GetAttrString(obj, "finalbody");
4389 if (tmp == NULL) goto failed;
4390 if (!PyList_Check(tmp)) {
4391 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4392 goto failed;
4393 }
4394 len = PyList_GET_SIZE(tmp);
4395 finalbody = asdl_seq_new(len, arena);
4396 if (finalbody == NULL) goto failed;
4397 for (i = 0; i < len; i++) {
4398 stmt_ty value;
4399 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4400 if (res != 0) goto failed;
4401 asdl_seq_SET(finalbody, i, value);
4402 }
4403 Py_XDECREF(tmp);
4404 tmp = NULL;
4405 } else {
4406 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4407 return 1;
4408 }
4409 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4410 if (*out == NULL) goto failed;
4411 return 0;
4412 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004413 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4414 if (isinstance == -1) {
4415 return 1;
4416 }
4417 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004418 expr_ty test;
4419 expr_ty msg;
4420
4421 if (PyObject_HasAttrString(obj, "test")) {
4422 int res;
4423 tmp = PyObject_GetAttrString(obj, "test");
4424 if (tmp == NULL) goto failed;
4425 res = obj2ast_expr(tmp, &test, arena);
4426 if (res != 0) goto failed;
4427 Py_XDECREF(tmp);
4428 tmp = NULL;
4429 } else {
4430 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4431 return 1;
4432 }
4433 if (PyObject_HasAttrString(obj, "msg")) {
4434 int res;
4435 tmp = PyObject_GetAttrString(obj, "msg");
4436 if (tmp == NULL) goto failed;
4437 res = obj2ast_expr(tmp, &msg, arena);
4438 if (res != 0) goto failed;
4439 Py_XDECREF(tmp);
4440 tmp = NULL;
4441 } else {
4442 msg = NULL;
4443 }
4444 *out = Assert(test, msg, lineno, col_offset, arena);
4445 if (*out == NULL) goto failed;
4446 return 0;
4447 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004448 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4449 if (isinstance == -1) {
4450 return 1;
4451 }
4452 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004453 asdl_seq* names;
4454
4455 if (PyObject_HasAttrString(obj, "names")) {
4456 int res;
4457 Py_ssize_t len;
4458 Py_ssize_t i;
4459 tmp = PyObject_GetAttrString(obj, "names");
4460 if (tmp == NULL) goto failed;
4461 if (!PyList_Check(tmp)) {
4462 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4463 goto failed;
4464 }
4465 len = PyList_GET_SIZE(tmp);
4466 names = asdl_seq_new(len, arena);
4467 if (names == NULL) goto failed;
4468 for (i = 0; i < len; i++) {
4469 alias_ty value;
4470 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4471 if (res != 0) goto failed;
4472 asdl_seq_SET(names, i, value);
4473 }
4474 Py_XDECREF(tmp);
4475 tmp = NULL;
4476 } else {
4477 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4478 return 1;
4479 }
4480 *out = Import(names, lineno, col_offset, arena);
4481 if (*out == NULL) goto failed;
4482 return 0;
4483 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004484 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4485 if (isinstance == -1) {
4486 return 1;
4487 }
4488 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004489 identifier module;
4490 asdl_seq* names;
4491 int level;
4492
4493 if (PyObject_HasAttrString(obj, "module")) {
4494 int res;
4495 tmp = PyObject_GetAttrString(obj, "module");
4496 if (tmp == NULL) goto failed;
4497 res = obj2ast_identifier(tmp, &module, arena);
4498 if (res != 0) goto failed;
4499 Py_XDECREF(tmp);
4500 tmp = NULL;
4501 } else {
Benjamin Petersona72be3b2009-06-13 20:23:33 +00004502 module = NULL;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004503 }
4504 if (PyObject_HasAttrString(obj, "names")) {
4505 int res;
4506 Py_ssize_t len;
4507 Py_ssize_t i;
4508 tmp = PyObject_GetAttrString(obj, "names");
4509 if (tmp == NULL) goto failed;
4510 if (!PyList_Check(tmp)) {
4511 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4512 goto failed;
4513 }
4514 len = PyList_GET_SIZE(tmp);
4515 names = asdl_seq_new(len, arena);
4516 if (names == NULL) goto failed;
4517 for (i = 0; i < len; i++) {
4518 alias_ty value;
4519 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4520 if (res != 0) goto failed;
4521 asdl_seq_SET(names, i, value);
4522 }
4523 Py_XDECREF(tmp);
4524 tmp = NULL;
4525 } else {
4526 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4527 return 1;
4528 }
4529 if (PyObject_HasAttrString(obj, "level")) {
4530 int res;
4531 tmp = PyObject_GetAttrString(obj, "level");
4532 if (tmp == NULL) goto failed;
4533 res = obj2ast_int(tmp, &level, arena);
4534 if (res != 0) goto failed;
4535 Py_XDECREF(tmp);
4536 tmp = NULL;
4537 } else {
4538 level = 0;
4539 }
4540 *out = ImportFrom(module, names, level, lineno, col_offset,
4541 arena);
4542 if (*out == NULL) goto failed;
4543 return 0;
4544 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004545 isinstance = PyObject_IsInstance(obj, (PyObject*)Exec_type);
4546 if (isinstance == -1) {
4547 return 1;
4548 }
4549 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004550 expr_ty body;
4551 expr_ty globals;
4552 expr_ty locals;
4553
4554 if (PyObject_HasAttrString(obj, "body")) {
4555 int res;
4556 tmp = PyObject_GetAttrString(obj, "body");
4557 if (tmp == NULL) goto failed;
4558 res = obj2ast_expr(tmp, &body, arena);
4559 if (res != 0) goto failed;
4560 Py_XDECREF(tmp);
4561 tmp = NULL;
4562 } else {
4563 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4564 return 1;
4565 }
4566 if (PyObject_HasAttrString(obj, "globals")) {
4567 int res;
4568 tmp = PyObject_GetAttrString(obj, "globals");
4569 if (tmp == NULL) goto failed;
4570 res = obj2ast_expr(tmp, &globals, arena);
4571 if (res != 0) goto failed;
4572 Py_XDECREF(tmp);
4573 tmp = NULL;
4574 } else {
4575 globals = NULL;
4576 }
4577 if (PyObject_HasAttrString(obj, "locals")) {
4578 int res;
4579 tmp = PyObject_GetAttrString(obj, "locals");
4580 if (tmp == NULL) goto failed;
4581 res = obj2ast_expr(tmp, &locals, arena);
4582 if (res != 0) goto failed;
4583 Py_XDECREF(tmp);
4584 tmp = NULL;
4585 } else {
4586 locals = NULL;
4587 }
4588 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4589 if (*out == NULL) goto failed;
4590 return 0;
4591 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004592 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4593 if (isinstance == -1) {
4594 return 1;
4595 }
4596 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004597 asdl_seq* names;
4598
4599 if (PyObject_HasAttrString(obj, "names")) {
4600 int res;
4601 Py_ssize_t len;
4602 Py_ssize_t i;
4603 tmp = PyObject_GetAttrString(obj, "names");
4604 if (tmp == NULL) goto failed;
4605 if (!PyList_Check(tmp)) {
4606 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4607 goto failed;
4608 }
4609 len = PyList_GET_SIZE(tmp);
4610 names = asdl_seq_new(len, arena);
4611 if (names == NULL) goto failed;
4612 for (i = 0; i < len; i++) {
4613 identifier value;
4614 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4615 if (res != 0) goto failed;
4616 asdl_seq_SET(names, i, value);
4617 }
4618 Py_XDECREF(tmp);
4619 tmp = NULL;
4620 } else {
4621 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4622 return 1;
4623 }
4624 *out = Global(names, lineno, col_offset, arena);
4625 if (*out == NULL) goto failed;
4626 return 0;
4627 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004628 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4629 if (isinstance == -1) {
4630 return 1;
4631 }
4632 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004633 expr_ty value;
4634
4635 if (PyObject_HasAttrString(obj, "value")) {
4636 int res;
4637 tmp = PyObject_GetAttrString(obj, "value");
4638 if (tmp == NULL) goto failed;
4639 res = obj2ast_expr(tmp, &value, arena);
4640 if (res != 0) goto failed;
4641 Py_XDECREF(tmp);
4642 tmp = NULL;
4643 } else {
4644 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4645 return 1;
4646 }
4647 *out = Expr(value, lineno, col_offset, arena);
4648 if (*out == NULL) goto failed;
4649 return 0;
4650 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004651 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4652 if (isinstance == -1) {
4653 return 1;
4654 }
4655 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004656
4657 *out = Pass(lineno, col_offset, arena);
4658 if (*out == NULL) goto failed;
4659 return 0;
4660 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004661 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4662 if (isinstance == -1) {
4663 return 1;
4664 }
4665 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004666
4667 *out = Break(lineno, col_offset, arena);
4668 if (*out == NULL) goto failed;
4669 return 0;
4670 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004671 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4672 if (isinstance == -1) {
4673 return 1;
4674 }
4675 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004676
4677 *out = Continue(lineno, col_offset, arena);
4678 if (*out == NULL) goto failed;
4679 return 0;
4680 }
4681
4682 tmp = PyObject_Repr(obj);
4683 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004684 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00004685failed:
4686 Py_XDECREF(tmp);
4687 return 1;
4688}
4689
4690int
4691obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4692{
4693 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004694 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004695
4696 int lineno;
4697 int col_offset;
4698
4699 if (obj == Py_None) {
4700 *out = NULL;
4701 return 0;
4702 }
4703 if (PyObject_HasAttrString(obj, "lineno")) {
4704 int res;
4705 tmp = PyObject_GetAttrString(obj, "lineno");
4706 if (tmp == NULL) goto failed;
4707 res = obj2ast_int(tmp, &lineno, arena);
4708 if (res != 0) goto failed;
4709 Py_XDECREF(tmp);
4710 tmp = NULL;
4711 } else {
4712 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4713 return 1;
4714 }
4715 if (PyObject_HasAttrString(obj, "col_offset")) {
4716 int res;
4717 tmp = PyObject_GetAttrString(obj, "col_offset");
4718 if (tmp == NULL) goto failed;
4719 res = obj2ast_int(tmp, &col_offset, arena);
4720 if (res != 0) goto failed;
4721 Py_XDECREF(tmp);
4722 tmp = NULL;
4723 } else {
4724 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4725 return 1;
4726 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004727 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4728 if (isinstance == -1) {
4729 return 1;
4730 }
4731 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004732 boolop_ty op;
4733 asdl_seq* values;
4734
4735 if (PyObject_HasAttrString(obj, "op")) {
4736 int res;
4737 tmp = PyObject_GetAttrString(obj, "op");
4738 if (tmp == NULL) goto failed;
4739 res = obj2ast_boolop(tmp, &op, arena);
4740 if (res != 0) goto failed;
4741 Py_XDECREF(tmp);
4742 tmp = NULL;
4743 } else {
4744 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4745 return 1;
4746 }
4747 if (PyObject_HasAttrString(obj, "values")) {
4748 int res;
4749 Py_ssize_t len;
4750 Py_ssize_t i;
4751 tmp = PyObject_GetAttrString(obj, "values");
4752 if (tmp == NULL) goto failed;
4753 if (!PyList_Check(tmp)) {
4754 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4755 goto failed;
4756 }
4757 len = PyList_GET_SIZE(tmp);
4758 values = asdl_seq_new(len, arena);
4759 if (values == NULL) goto failed;
4760 for (i = 0; i < len; i++) {
4761 expr_ty value;
4762 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4763 if (res != 0) goto failed;
4764 asdl_seq_SET(values, i, value);
4765 }
4766 Py_XDECREF(tmp);
4767 tmp = NULL;
4768 } else {
4769 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4770 return 1;
4771 }
4772 *out = BoolOp(op, values, lineno, col_offset, arena);
4773 if (*out == NULL) goto failed;
4774 return 0;
4775 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004776 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4777 if (isinstance == -1) {
4778 return 1;
4779 }
4780 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004781 expr_ty left;
4782 operator_ty op;
4783 expr_ty right;
4784
4785 if (PyObject_HasAttrString(obj, "left")) {
4786 int res;
4787 tmp = PyObject_GetAttrString(obj, "left");
4788 if (tmp == NULL) goto failed;
4789 res = obj2ast_expr(tmp, &left, arena);
4790 if (res != 0) goto failed;
4791 Py_XDECREF(tmp);
4792 tmp = NULL;
4793 } else {
4794 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4795 return 1;
4796 }
4797 if (PyObject_HasAttrString(obj, "op")) {
4798 int res;
4799 tmp = PyObject_GetAttrString(obj, "op");
4800 if (tmp == NULL) goto failed;
4801 res = obj2ast_operator(tmp, &op, arena);
4802 if (res != 0) goto failed;
4803 Py_XDECREF(tmp);
4804 tmp = NULL;
4805 } else {
4806 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4807 return 1;
4808 }
4809 if (PyObject_HasAttrString(obj, "right")) {
4810 int res;
4811 tmp = PyObject_GetAttrString(obj, "right");
4812 if (tmp == NULL) goto failed;
4813 res = obj2ast_expr(tmp, &right, arena);
4814 if (res != 0) goto failed;
4815 Py_XDECREF(tmp);
4816 tmp = NULL;
4817 } else {
4818 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4819 return 1;
4820 }
4821 *out = BinOp(left, op, right, lineno, col_offset, arena);
4822 if (*out == NULL) goto failed;
4823 return 0;
4824 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004825 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4826 if (isinstance == -1) {
4827 return 1;
4828 }
4829 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004830 unaryop_ty op;
4831 expr_ty operand;
4832
4833 if (PyObject_HasAttrString(obj, "op")) {
4834 int res;
4835 tmp = PyObject_GetAttrString(obj, "op");
4836 if (tmp == NULL) goto failed;
4837 res = obj2ast_unaryop(tmp, &op, arena);
4838 if (res != 0) goto failed;
4839 Py_XDECREF(tmp);
4840 tmp = NULL;
4841 } else {
4842 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4843 return 1;
4844 }
4845 if (PyObject_HasAttrString(obj, "operand")) {
4846 int res;
4847 tmp = PyObject_GetAttrString(obj, "operand");
4848 if (tmp == NULL) goto failed;
4849 res = obj2ast_expr(tmp, &operand, arena);
4850 if (res != 0) goto failed;
4851 Py_XDECREF(tmp);
4852 tmp = NULL;
4853 } else {
4854 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4855 return 1;
4856 }
4857 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4858 if (*out == NULL) goto failed;
4859 return 0;
4860 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004861 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4862 if (isinstance == -1) {
4863 return 1;
4864 }
4865 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004866 arguments_ty args;
4867 expr_ty body;
4868
4869 if (PyObject_HasAttrString(obj, "args")) {
4870 int res;
4871 tmp = PyObject_GetAttrString(obj, "args");
4872 if (tmp == NULL) goto failed;
4873 res = obj2ast_arguments(tmp, &args, arena);
4874 if (res != 0) goto failed;
4875 Py_XDECREF(tmp);
4876 tmp = NULL;
4877 } else {
4878 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4879 return 1;
4880 }
4881 if (PyObject_HasAttrString(obj, "body")) {
4882 int res;
4883 tmp = PyObject_GetAttrString(obj, "body");
4884 if (tmp == NULL) goto failed;
4885 res = obj2ast_expr(tmp, &body, arena);
4886 if (res != 0) goto failed;
4887 Py_XDECREF(tmp);
4888 tmp = NULL;
4889 } else {
4890 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4891 return 1;
4892 }
4893 *out = Lambda(args, body, lineno, col_offset, arena);
4894 if (*out == NULL) goto failed;
4895 return 0;
4896 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004897 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4898 if (isinstance == -1) {
4899 return 1;
4900 }
4901 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004902 expr_ty test;
4903 expr_ty body;
4904 expr_ty orelse;
4905
4906 if (PyObject_HasAttrString(obj, "test")) {
4907 int res;
4908 tmp = PyObject_GetAttrString(obj, "test");
4909 if (tmp == NULL) goto failed;
4910 res = obj2ast_expr(tmp, &test, arena);
4911 if (res != 0) goto failed;
4912 Py_XDECREF(tmp);
4913 tmp = NULL;
4914 } else {
4915 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4916 return 1;
4917 }
4918 if (PyObject_HasAttrString(obj, "body")) {
4919 int res;
4920 tmp = PyObject_GetAttrString(obj, "body");
4921 if (tmp == NULL) goto failed;
4922 res = obj2ast_expr(tmp, &body, arena);
4923 if (res != 0) goto failed;
4924 Py_XDECREF(tmp);
4925 tmp = NULL;
4926 } else {
4927 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4928 return 1;
4929 }
4930 if (PyObject_HasAttrString(obj, "orelse")) {
4931 int res;
4932 tmp = PyObject_GetAttrString(obj, "orelse");
4933 if (tmp == NULL) goto failed;
4934 res = obj2ast_expr(tmp, &orelse, arena);
4935 if (res != 0) goto failed;
4936 Py_XDECREF(tmp);
4937 tmp = NULL;
4938 } else {
4939 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4940 return 1;
4941 }
4942 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4943 if (*out == NULL) goto failed;
4944 return 0;
4945 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004946 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4947 if (isinstance == -1) {
4948 return 1;
4949 }
4950 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004951 asdl_seq* keys;
4952 asdl_seq* values;
4953
4954 if (PyObject_HasAttrString(obj, "keys")) {
4955 int res;
4956 Py_ssize_t len;
4957 Py_ssize_t i;
4958 tmp = PyObject_GetAttrString(obj, "keys");
4959 if (tmp == NULL) goto failed;
4960 if (!PyList_Check(tmp)) {
4961 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4962 goto failed;
4963 }
4964 len = PyList_GET_SIZE(tmp);
4965 keys = asdl_seq_new(len, arena);
4966 if (keys == NULL) goto failed;
4967 for (i = 0; i < len; i++) {
4968 expr_ty value;
4969 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4970 if (res != 0) goto failed;
4971 asdl_seq_SET(keys, i, value);
4972 }
4973 Py_XDECREF(tmp);
4974 tmp = NULL;
4975 } else {
4976 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4977 return 1;
4978 }
4979 if (PyObject_HasAttrString(obj, "values")) {
4980 int res;
4981 Py_ssize_t len;
4982 Py_ssize_t i;
4983 tmp = PyObject_GetAttrString(obj, "values");
4984 if (tmp == NULL) goto failed;
4985 if (!PyList_Check(tmp)) {
4986 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4987 goto failed;
4988 }
4989 len = PyList_GET_SIZE(tmp);
4990 values = asdl_seq_new(len, arena);
4991 if (values == NULL) goto failed;
4992 for (i = 0; i < len; i++) {
4993 expr_ty value;
4994 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4995 if (res != 0) goto failed;
4996 asdl_seq_SET(values, i, value);
4997 }
4998 Py_XDECREF(tmp);
4999 tmp = NULL;
5000 } else {
5001 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
5002 return 1;
5003 }
5004 *out = Dict(keys, values, lineno, col_offset, arena);
5005 if (*out == NULL) goto failed;
5006 return 0;
5007 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00005008 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
5009 if (isinstance == -1) {
5010 return 1;
5011 }
5012 if (isinstance) {
5013 asdl_seq* elts;
5014
5015 if (PyObject_HasAttrString(obj, "elts")) {
5016 int res;
5017 Py_ssize_t len;
5018 Py_ssize_t i;
5019 tmp = PyObject_GetAttrString(obj, "elts");
5020 if (tmp == NULL) goto failed;
5021 if (!PyList_Check(tmp)) {
5022 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5023 goto failed;
5024 }
5025 len = PyList_GET_SIZE(tmp);
5026 elts = asdl_seq_new(len, arena);
5027 if (elts == NULL) goto failed;
5028 for (i = 0; i < len; i++) {
5029 expr_ty value;
5030 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5031 if (res != 0) goto failed;
5032 asdl_seq_SET(elts, i, value);
5033 }
5034 Py_XDECREF(tmp);
5035 tmp = NULL;
5036 } else {
5037 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5038 return 1;
5039 }
5040 *out = Set(elts, lineno, col_offset, arena);
5041 if (*out == NULL) goto failed;
5042 return 0;
5043 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005044 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5045 if (isinstance == -1) {
5046 return 1;
5047 }
5048 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005049 expr_ty elt;
5050 asdl_seq* generators;
5051
5052 if (PyObject_HasAttrString(obj, "elt")) {
5053 int res;
5054 tmp = PyObject_GetAttrString(obj, "elt");
5055 if (tmp == NULL) goto failed;
5056 res = obj2ast_expr(tmp, &elt, arena);
5057 if (res != 0) goto failed;
5058 Py_XDECREF(tmp);
5059 tmp = NULL;
5060 } else {
5061 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5062 return 1;
5063 }
5064 if (PyObject_HasAttrString(obj, "generators")) {
5065 int res;
5066 Py_ssize_t len;
5067 Py_ssize_t i;
5068 tmp = PyObject_GetAttrString(obj, "generators");
5069 if (tmp == NULL) goto failed;
5070 if (!PyList_Check(tmp)) {
5071 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5072 goto failed;
5073 }
5074 len = PyList_GET_SIZE(tmp);
5075 generators = asdl_seq_new(len, arena);
5076 if (generators == NULL) goto failed;
5077 for (i = 0; i < len; i++) {
5078 comprehension_ty value;
5079 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5080 if (res != 0) goto failed;
5081 asdl_seq_SET(generators, i, value);
5082 }
5083 Py_XDECREF(tmp);
5084 tmp = NULL;
5085 } else {
5086 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5087 return 1;
5088 }
5089 *out = ListComp(elt, generators, lineno, col_offset, arena);
5090 if (*out == NULL) goto failed;
5091 return 0;
5092 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00005093 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5094 if (isinstance == -1) {
5095 return 1;
5096 }
5097 if (isinstance) {
5098 expr_ty elt;
5099 asdl_seq* generators;
5100
5101 if (PyObject_HasAttrString(obj, "elt")) {
5102 int res;
5103 tmp = PyObject_GetAttrString(obj, "elt");
5104 if (tmp == NULL) goto failed;
5105 res = obj2ast_expr(tmp, &elt, arena);
5106 if (res != 0) goto failed;
5107 Py_XDECREF(tmp);
5108 tmp = NULL;
5109 } else {
5110 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5111 return 1;
5112 }
5113 if (PyObject_HasAttrString(obj, "generators")) {
5114 int res;
5115 Py_ssize_t len;
5116 Py_ssize_t i;
5117 tmp = PyObject_GetAttrString(obj, "generators");
5118 if (tmp == NULL) goto failed;
5119 if (!PyList_Check(tmp)) {
5120 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5121 goto failed;
5122 }
5123 len = PyList_GET_SIZE(tmp);
5124 generators = asdl_seq_new(len, arena);
5125 if (generators == NULL) goto failed;
5126 for (i = 0; i < len; i++) {
5127 comprehension_ty value;
5128 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5129 if (res != 0) goto failed;
5130 asdl_seq_SET(generators, i, value);
5131 }
5132 Py_XDECREF(tmp);
5133 tmp = NULL;
5134 } else {
5135 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5136 return 1;
5137 }
5138 *out = SetComp(elt, generators, lineno, col_offset, arena);
5139 if (*out == NULL) goto failed;
5140 return 0;
5141 }
5142 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5143 if (isinstance == -1) {
5144 return 1;
5145 }
5146 if (isinstance) {
5147 expr_ty key;
5148 expr_ty value;
5149 asdl_seq* generators;
5150
5151 if (PyObject_HasAttrString(obj, "key")) {
5152 int res;
5153 tmp = PyObject_GetAttrString(obj, "key");
5154 if (tmp == NULL) goto failed;
5155 res = obj2ast_expr(tmp, &key, arena);
5156 if (res != 0) goto failed;
5157 Py_XDECREF(tmp);
5158 tmp = NULL;
5159 } else {
5160 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5161 return 1;
5162 }
5163 if (PyObject_HasAttrString(obj, "value")) {
5164 int res;
5165 tmp = PyObject_GetAttrString(obj, "value");
5166 if (tmp == NULL) goto failed;
5167 res = obj2ast_expr(tmp, &value, arena);
5168 if (res != 0) goto failed;
5169 Py_XDECREF(tmp);
5170 tmp = NULL;
5171 } else {
5172 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5173 return 1;
5174 }
5175 if (PyObject_HasAttrString(obj, "generators")) {
5176 int res;
5177 Py_ssize_t len;
5178 Py_ssize_t i;
5179 tmp = PyObject_GetAttrString(obj, "generators");
5180 if (tmp == NULL) goto failed;
5181 if (!PyList_Check(tmp)) {
5182 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5183 goto failed;
5184 }
5185 len = PyList_GET_SIZE(tmp);
5186 generators = asdl_seq_new(len, arena);
5187 if (generators == NULL) goto failed;
5188 for (i = 0; i < len; i++) {
5189 comprehension_ty value;
5190 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5191 if (res != 0) goto failed;
5192 asdl_seq_SET(generators, i, value);
5193 }
5194 Py_XDECREF(tmp);
5195 tmp = NULL;
5196 } else {
5197 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5198 return 1;
5199 }
5200 *out = DictComp(key, value, generators, lineno, col_offset,
5201 arena);
5202 if (*out == NULL) goto failed;
5203 return 0;
5204 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005205 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5206 if (isinstance == -1) {
5207 return 1;
5208 }
5209 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005210 expr_ty elt;
5211 asdl_seq* generators;
5212
5213 if (PyObject_HasAttrString(obj, "elt")) {
5214 int res;
5215 tmp = PyObject_GetAttrString(obj, "elt");
5216 if (tmp == NULL) goto failed;
5217 res = obj2ast_expr(tmp, &elt, arena);
5218 if (res != 0) goto failed;
5219 Py_XDECREF(tmp);
5220 tmp = NULL;
5221 } else {
5222 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5223 return 1;
5224 }
5225 if (PyObject_HasAttrString(obj, "generators")) {
5226 int res;
5227 Py_ssize_t len;
5228 Py_ssize_t i;
5229 tmp = PyObject_GetAttrString(obj, "generators");
5230 if (tmp == NULL) goto failed;
5231 if (!PyList_Check(tmp)) {
5232 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5233 goto failed;
5234 }
5235 len = PyList_GET_SIZE(tmp);
5236 generators = asdl_seq_new(len, arena);
5237 if (generators == NULL) goto failed;
5238 for (i = 0; i < len; i++) {
5239 comprehension_ty value;
5240 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5241 if (res != 0) goto failed;
5242 asdl_seq_SET(generators, i, value);
5243 }
5244 Py_XDECREF(tmp);
5245 tmp = NULL;
5246 } else {
5247 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5248 return 1;
5249 }
5250 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5251 if (*out == NULL) goto failed;
5252 return 0;
5253 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005254 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5255 if (isinstance == -1) {
5256 return 1;
5257 }
5258 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005259 expr_ty value;
5260
5261 if (PyObject_HasAttrString(obj, "value")) {
5262 int res;
5263 tmp = PyObject_GetAttrString(obj, "value");
5264 if (tmp == NULL) goto failed;
5265 res = obj2ast_expr(tmp, &value, arena);
5266 if (res != 0) goto failed;
5267 Py_XDECREF(tmp);
5268 tmp = NULL;
5269 } else {
5270 value = NULL;
5271 }
5272 *out = Yield(value, lineno, col_offset, arena);
5273 if (*out == NULL) goto failed;
5274 return 0;
5275 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005276 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5277 if (isinstance == -1) {
5278 return 1;
5279 }
5280 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005281 expr_ty left;
5282 asdl_int_seq* ops;
5283 asdl_seq* comparators;
5284
5285 if (PyObject_HasAttrString(obj, "left")) {
5286 int res;
5287 tmp = PyObject_GetAttrString(obj, "left");
5288 if (tmp == NULL) goto failed;
5289 res = obj2ast_expr(tmp, &left, arena);
5290 if (res != 0) goto failed;
5291 Py_XDECREF(tmp);
5292 tmp = NULL;
5293 } else {
5294 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5295 return 1;
5296 }
5297 if (PyObject_HasAttrString(obj, "ops")) {
5298 int res;
5299 Py_ssize_t len;
5300 Py_ssize_t i;
5301 tmp = PyObject_GetAttrString(obj, "ops");
5302 if (tmp == NULL) goto failed;
5303 if (!PyList_Check(tmp)) {
5304 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5305 goto failed;
5306 }
5307 len = PyList_GET_SIZE(tmp);
5308 ops = asdl_int_seq_new(len, arena);
5309 if (ops == NULL) goto failed;
5310 for (i = 0; i < len; i++) {
5311 cmpop_ty value;
5312 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5313 if (res != 0) goto failed;
5314 asdl_seq_SET(ops, i, value);
5315 }
5316 Py_XDECREF(tmp);
5317 tmp = NULL;
5318 } else {
5319 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5320 return 1;
5321 }
5322 if (PyObject_HasAttrString(obj, "comparators")) {
5323 int res;
5324 Py_ssize_t len;
5325 Py_ssize_t i;
5326 tmp = PyObject_GetAttrString(obj, "comparators");
5327 if (tmp == NULL) goto failed;
5328 if (!PyList_Check(tmp)) {
5329 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5330 goto failed;
5331 }
5332 len = PyList_GET_SIZE(tmp);
5333 comparators = asdl_seq_new(len, arena);
5334 if (comparators == NULL) goto failed;
5335 for (i = 0; i < len; i++) {
5336 expr_ty value;
5337 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5338 if (res != 0) goto failed;
5339 asdl_seq_SET(comparators, i, value);
5340 }
5341 Py_XDECREF(tmp);
5342 tmp = NULL;
5343 } else {
5344 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5345 return 1;
5346 }
5347 *out = Compare(left, ops, comparators, lineno, col_offset,
5348 arena);
5349 if (*out == NULL) goto failed;
5350 return 0;
5351 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005352 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5353 if (isinstance == -1) {
5354 return 1;
5355 }
5356 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005357 expr_ty func;
5358 asdl_seq* args;
5359 asdl_seq* keywords;
5360 expr_ty starargs;
5361 expr_ty kwargs;
5362
5363 if (PyObject_HasAttrString(obj, "func")) {
5364 int res;
5365 tmp = PyObject_GetAttrString(obj, "func");
5366 if (tmp == NULL) goto failed;
5367 res = obj2ast_expr(tmp, &func, arena);
5368 if (res != 0) goto failed;
5369 Py_XDECREF(tmp);
5370 tmp = NULL;
5371 } else {
5372 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5373 return 1;
5374 }
5375 if (PyObject_HasAttrString(obj, "args")) {
5376 int res;
5377 Py_ssize_t len;
5378 Py_ssize_t i;
5379 tmp = PyObject_GetAttrString(obj, "args");
5380 if (tmp == NULL) goto failed;
5381 if (!PyList_Check(tmp)) {
5382 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5383 goto failed;
5384 }
5385 len = PyList_GET_SIZE(tmp);
5386 args = asdl_seq_new(len, arena);
5387 if (args == NULL) goto failed;
5388 for (i = 0; i < len; i++) {
5389 expr_ty value;
5390 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5391 if (res != 0) goto failed;
5392 asdl_seq_SET(args, i, value);
5393 }
5394 Py_XDECREF(tmp);
5395 tmp = NULL;
5396 } else {
5397 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5398 return 1;
5399 }
5400 if (PyObject_HasAttrString(obj, "keywords")) {
5401 int res;
5402 Py_ssize_t len;
5403 Py_ssize_t i;
5404 tmp = PyObject_GetAttrString(obj, "keywords");
5405 if (tmp == NULL) goto failed;
5406 if (!PyList_Check(tmp)) {
5407 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5408 goto failed;
5409 }
5410 len = PyList_GET_SIZE(tmp);
5411 keywords = asdl_seq_new(len, arena);
5412 if (keywords == NULL) goto failed;
5413 for (i = 0; i < len; i++) {
5414 keyword_ty value;
5415 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5416 if (res != 0) goto failed;
5417 asdl_seq_SET(keywords, i, value);
5418 }
5419 Py_XDECREF(tmp);
5420 tmp = NULL;
5421 } else {
5422 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5423 return 1;
5424 }
5425 if (PyObject_HasAttrString(obj, "starargs")) {
5426 int res;
5427 tmp = PyObject_GetAttrString(obj, "starargs");
5428 if (tmp == NULL) goto failed;
5429 res = obj2ast_expr(tmp, &starargs, arena);
5430 if (res != 0) goto failed;
5431 Py_XDECREF(tmp);
5432 tmp = NULL;
5433 } else {
5434 starargs = NULL;
5435 }
5436 if (PyObject_HasAttrString(obj, "kwargs")) {
5437 int res;
5438 tmp = PyObject_GetAttrString(obj, "kwargs");
5439 if (tmp == NULL) goto failed;
5440 res = obj2ast_expr(tmp, &kwargs, arena);
5441 if (res != 0) goto failed;
5442 Py_XDECREF(tmp);
5443 tmp = NULL;
5444 } else {
5445 kwargs = NULL;
5446 }
5447 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5448 col_offset, arena);
5449 if (*out == NULL) goto failed;
5450 return 0;
5451 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005452 isinstance = PyObject_IsInstance(obj, (PyObject*)Repr_type);
5453 if (isinstance == -1) {
5454 return 1;
5455 }
5456 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005457 expr_ty value;
5458
5459 if (PyObject_HasAttrString(obj, "value")) {
5460 int res;
5461 tmp = PyObject_GetAttrString(obj, "value");
5462 if (tmp == NULL) goto failed;
5463 res = obj2ast_expr(tmp, &value, arena);
5464 if (res != 0) goto failed;
5465 Py_XDECREF(tmp);
5466 tmp = NULL;
5467 } else {
5468 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5469 return 1;
5470 }
5471 *out = Repr(value, lineno, col_offset, arena);
5472 if (*out == NULL) goto failed;
5473 return 0;
5474 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005475 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5476 if (isinstance == -1) {
5477 return 1;
5478 }
5479 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005480 object n;
5481
5482 if (PyObject_HasAttrString(obj, "n")) {
5483 int res;
5484 tmp = PyObject_GetAttrString(obj, "n");
5485 if (tmp == NULL) goto failed;
5486 res = obj2ast_object(tmp, &n, arena);
5487 if (res != 0) goto failed;
5488 Py_XDECREF(tmp);
5489 tmp = NULL;
5490 } else {
5491 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5492 return 1;
5493 }
5494 *out = Num(n, lineno, col_offset, arena);
5495 if (*out == NULL) goto failed;
5496 return 0;
5497 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005498 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5499 if (isinstance == -1) {
5500 return 1;
5501 }
5502 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005503 string s;
5504
5505 if (PyObject_HasAttrString(obj, "s")) {
5506 int res;
5507 tmp = PyObject_GetAttrString(obj, "s");
5508 if (tmp == NULL) goto failed;
5509 res = obj2ast_string(tmp, &s, arena);
5510 if (res != 0) goto failed;
5511 Py_XDECREF(tmp);
5512 tmp = NULL;
5513 } else {
5514 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5515 return 1;
5516 }
5517 *out = Str(s, lineno, col_offset, arena);
5518 if (*out == NULL) goto failed;
5519 return 0;
5520 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005521 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5522 if (isinstance == -1) {
5523 return 1;
5524 }
5525 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005526 expr_ty value;
5527 identifier attr;
5528 expr_context_ty ctx;
5529
5530 if (PyObject_HasAttrString(obj, "value")) {
5531 int res;
5532 tmp = PyObject_GetAttrString(obj, "value");
5533 if (tmp == NULL) goto failed;
5534 res = obj2ast_expr(tmp, &value, arena);
5535 if (res != 0) goto failed;
5536 Py_XDECREF(tmp);
5537 tmp = NULL;
5538 } else {
5539 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5540 return 1;
5541 }
5542 if (PyObject_HasAttrString(obj, "attr")) {
5543 int res;
5544 tmp = PyObject_GetAttrString(obj, "attr");
5545 if (tmp == NULL) goto failed;
5546 res = obj2ast_identifier(tmp, &attr, arena);
5547 if (res != 0) goto failed;
5548 Py_XDECREF(tmp);
5549 tmp = NULL;
5550 } else {
5551 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5552 return 1;
5553 }
5554 if (PyObject_HasAttrString(obj, "ctx")) {
5555 int res;
5556 tmp = PyObject_GetAttrString(obj, "ctx");
5557 if (tmp == NULL) goto failed;
5558 res = obj2ast_expr_context(tmp, &ctx, arena);
5559 if (res != 0) goto failed;
5560 Py_XDECREF(tmp);
5561 tmp = NULL;
5562 } else {
5563 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5564 return 1;
5565 }
5566 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5567 if (*out == NULL) goto failed;
5568 return 0;
5569 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005570 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5571 if (isinstance == -1) {
5572 return 1;
5573 }
5574 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005575 expr_ty value;
5576 slice_ty slice;
5577 expr_context_ty ctx;
5578
5579 if (PyObject_HasAttrString(obj, "value")) {
5580 int res;
5581 tmp = PyObject_GetAttrString(obj, "value");
5582 if (tmp == NULL) goto failed;
5583 res = obj2ast_expr(tmp, &value, arena);
5584 if (res != 0) goto failed;
5585 Py_XDECREF(tmp);
5586 tmp = NULL;
5587 } else {
5588 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5589 return 1;
5590 }
5591 if (PyObject_HasAttrString(obj, "slice")) {
5592 int res;
5593 tmp = PyObject_GetAttrString(obj, "slice");
5594 if (tmp == NULL) goto failed;
5595 res = obj2ast_slice(tmp, &slice, arena);
5596 if (res != 0) goto failed;
5597 Py_XDECREF(tmp);
5598 tmp = NULL;
5599 } else {
5600 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5601 return 1;
5602 }
5603 if (PyObject_HasAttrString(obj, "ctx")) {
5604 int res;
5605 tmp = PyObject_GetAttrString(obj, "ctx");
5606 if (tmp == NULL) goto failed;
5607 res = obj2ast_expr_context(tmp, &ctx, arena);
5608 if (res != 0) goto failed;
5609 Py_XDECREF(tmp);
5610 tmp = NULL;
5611 } else {
5612 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5613 return 1;
5614 }
5615 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5616 if (*out == NULL) goto failed;
5617 return 0;
5618 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005619 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5620 if (isinstance == -1) {
5621 return 1;
5622 }
5623 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005624 identifier id;
5625 expr_context_ty ctx;
5626
5627 if (PyObject_HasAttrString(obj, "id")) {
5628 int res;
5629 tmp = PyObject_GetAttrString(obj, "id");
5630 if (tmp == NULL) goto failed;
5631 res = obj2ast_identifier(tmp, &id, arena);
5632 if (res != 0) goto failed;
5633 Py_XDECREF(tmp);
5634 tmp = NULL;
5635 } else {
5636 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5637 return 1;
5638 }
5639 if (PyObject_HasAttrString(obj, "ctx")) {
5640 int res;
5641 tmp = PyObject_GetAttrString(obj, "ctx");
5642 if (tmp == NULL) goto failed;
5643 res = obj2ast_expr_context(tmp, &ctx, arena);
5644 if (res != 0) goto failed;
5645 Py_XDECREF(tmp);
5646 tmp = NULL;
5647 } else {
5648 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5649 return 1;
5650 }
5651 *out = Name(id, ctx, lineno, col_offset, arena);
5652 if (*out == NULL) goto failed;
5653 return 0;
5654 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005655 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5656 if (isinstance == -1) {
5657 return 1;
5658 }
5659 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005660 asdl_seq* elts;
5661 expr_context_ty ctx;
5662
5663 if (PyObject_HasAttrString(obj, "elts")) {
5664 int res;
5665 Py_ssize_t len;
5666 Py_ssize_t i;
5667 tmp = PyObject_GetAttrString(obj, "elts");
5668 if (tmp == NULL) goto failed;
5669 if (!PyList_Check(tmp)) {
5670 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5671 goto failed;
5672 }
5673 len = PyList_GET_SIZE(tmp);
5674 elts = asdl_seq_new(len, arena);
5675 if (elts == NULL) goto failed;
5676 for (i = 0; i < len; i++) {
5677 expr_ty value;
5678 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5679 if (res != 0) goto failed;
5680 asdl_seq_SET(elts, i, value);
5681 }
5682 Py_XDECREF(tmp);
5683 tmp = NULL;
5684 } else {
5685 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5686 return 1;
5687 }
5688 if (PyObject_HasAttrString(obj, "ctx")) {
5689 int res;
5690 tmp = PyObject_GetAttrString(obj, "ctx");
5691 if (tmp == NULL) goto failed;
5692 res = obj2ast_expr_context(tmp, &ctx, arena);
5693 if (res != 0) goto failed;
5694 Py_XDECREF(tmp);
5695 tmp = NULL;
5696 } else {
5697 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5698 return 1;
5699 }
5700 *out = List(elts, ctx, lineno, col_offset, arena);
5701 if (*out == NULL) goto failed;
5702 return 0;
5703 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005704 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5705 if (isinstance == -1) {
5706 return 1;
5707 }
5708 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005709 asdl_seq* elts;
5710 expr_context_ty ctx;
5711
5712 if (PyObject_HasAttrString(obj, "elts")) {
5713 int res;
5714 Py_ssize_t len;
5715 Py_ssize_t i;
5716 tmp = PyObject_GetAttrString(obj, "elts");
5717 if (tmp == NULL) goto failed;
5718 if (!PyList_Check(tmp)) {
5719 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5720 goto failed;
5721 }
5722 len = PyList_GET_SIZE(tmp);
5723 elts = asdl_seq_new(len, arena);
5724 if (elts == NULL) goto failed;
5725 for (i = 0; i < len; i++) {
5726 expr_ty value;
5727 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5728 if (res != 0) goto failed;
5729 asdl_seq_SET(elts, i, value);
5730 }
5731 Py_XDECREF(tmp);
5732 tmp = NULL;
5733 } else {
5734 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5735 return 1;
5736 }
5737 if (PyObject_HasAttrString(obj, "ctx")) {
5738 int res;
5739 tmp = PyObject_GetAttrString(obj, "ctx");
5740 if (tmp == NULL) goto failed;
5741 res = obj2ast_expr_context(tmp, &ctx, arena);
5742 if (res != 0) goto failed;
5743 Py_XDECREF(tmp);
5744 tmp = NULL;
5745 } else {
5746 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5747 return 1;
5748 }
5749 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5750 if (*out == NULL) goto failed;
5751 return 0;
5752 }
5753
5754 tmp = PyObject_Repr(obj);
5755 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005756 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005757failed:
5758 Py_XDECREF(tmp);
5759 return 1;
5760}
5761
5762int
5763obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5764{
5765 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005766 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005767
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005768 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5769 if (isinstance == -1) {
5770 return 1;
5771 }
5772 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005773 *out = Load;
5774 return 0;
5775 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005776 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5777 if (isinstance == -1) {
5778 return 1;
5779 }
5780 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005781 *out = Store;
5782 return 0;
5783 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005784 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5785 if (isinstance == -1) {
5786 return 1;
5787 }
5788 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005789 *out = Del;
5790 return 0;
5791 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005792 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5793 if (isinstance == -1) {
5794 return 1;
5795 }
5796 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005797 *out = AugLoad;
5798 return 0;
5799 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005800 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5801 if (isinstance == -1) {
5802 return 1;
5803 }
5804 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005805 *out = AugStore;
5806 return 0;
5807 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005808 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5809 if (isinstance == -1) {
5810 return 1;
5811 }
5812 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005813 *out = Param;
5814 return 0;
5815 }
5816
5817 tmp = PyObject_Repr(obj);
5818 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005819 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005820failed:
5821 Py_XDECREF(tmp);
5822 return 1;
5823}
5824
5825int
5826obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5827{
5828 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005829 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005830
5831
5832 if (obj == Py_None) {
5833 *out = NULL;
5834 return 0;
5835 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005836 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5837 if (isinstance == -1) {
5838 return 1;
5839 }
5840 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005841
5842 *out = Ellipsis(arena);
5843 if (*out == NULL) goto failed;
5844 return 0;
5845 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005846 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5847 if (isinstance == -1) {
5848 return 1;
5849 }
5850 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005851 expr_ty lower;
5852 expr_ty upper;
5853 expr_ty step;
5854
5855 if (PyObject_HasAttrString(obj, "lower")) {
5856 int res;
5857 tmp = PyObject_GetAttrString(obj, "lower");
5858 if (tmp == NULL) goto failed;
5859 res = obj2ast_expr(tmp, &lower, arena);
5860 if (res != 0) goto failed;
5861 Py_XDECREF(tmp);
5862 tmp = NULL;
5863 } else {
5864 lower = NULL;
5865 }
5866 if (PyObject_HasAttrString(obj, "upper")) {
5867 int res;
5868 tmp = PyObject_GetAttrString(obj, "upper");
5869 if (tmp == NULL) goto failed;
5870 res = obj2ast_expr(tmp, &upper, arena);
5871 if (res != 0) goto failed;
5872 Py_XDECREF(tmp);
5873 tmp = NULL;
5874 } else {
5875 upper = NULL;
5876 }
5877 if (PyObject_HasAttrString(obj, "step")) {
5878 int res;
5879 tmp = PyObject_GetAttrString(obj, "step");
5880 if (tmp == NULL) goto failed;
5881 res = obj2ast_expr(tmp, &step, arena);
5882 if (res != 0) goto failed;
5883 Py_XDECREF(tmp);
5884 tmp = NULL;
5885 } else {
5886 step = NULL;
5887 }
5888 *out = Slice(lower, upper, step, arena);
5889 if (*out == NULL) goto failed;
5890 return 0;
5891 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005892 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5893 if (isinstance == -1) {
5894 return 1;
5895 }
5896 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005897 asdl_seq* dims;
5898
5899 if (PyObject_HasAttrString(obj, "dims")) {
5900 int res;
5901 Py_ssize_t len;
5902 Py_ssize_t i;
5903 tmp = PyObject_GetAttrString(obj, "dims");
5904 if (tmp == NULL) goto failed;
5905 if (!PyList_Check(tmp)) {
5906 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5907 goto failed;
5908 }
5909 len = PyList_GET_SIZE(tmp);
5910 dims = asdl_seq_new(len, arena);
5911 if (dims == NULL) goto failed;
5912 for (i = 0; i < len; i++) {
5913 slice_ty value;
5914 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5915 if (res != 0) goto failed;
5916 asdl_seq_SET(dims, i, value);
5917 }
5918 Py_XDECREF(tmp);
5919 tmp = NULL;
5920 } else {
5921 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5922 return 1;
5923 }
5924 *out = ExtSlice(dims, arena);
5925 if (*out == NULL) goto failed;
5926 return 0;
5927 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005928 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5929 if (isinstance == -1) {
5930 return 1;
5931 }
5932 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005933 expr_ty value;
5934
5935 if (PyObject_HasAttrString(obj, "value")) {
5936 int res;
5937 tmp = PyObject_GetAttrString(obj, "value");
5938 if (tmp == NULL) goto failed;
5939 res = obj2ast_expr(tmp, &value, arena);
5940 if (res != 0) goto failed;
5941 Py_XDECREF(tmp);
5942 tmp = NULL;
5943 } else {
5944 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5945 return 1;
5946 }
5947 *out = Index(value, arena);
5948 if (*out == NULL) goto failed;
5949 return 0;
5950 }
5951
5952 tmp = PyObject_Repr(obj);
5953 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005954 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005955failed:
5956 Py_XDECREF(tmp);
5957 return 1;
5958}
5959
5960int
5961obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5962{
5963 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005964 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005965
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005966 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5967 if (isinstance == -1) {
5968 return 1;
5969 }
5970 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005971 *out = And;
5972 return 0;
5973 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005974 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5975 if (isinstance == -1) {
5976 return 1;
5977 }
5978 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005979 *out = Or;
5980 return 0;
5981 }
5982
5983 tmp = PyObject_Repr(obj);
5984 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005985 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005986failed:
5987 Py_XDECREF(tmp);
5988 return 1;
5989}
5990
5991int
5992obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5993{
5994 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005995 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005996
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005997 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
5998 if (isinstance == -1) {
5999 return 1;
6000 }
6001 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006002 *out = Add;
6003 return 0;
6004 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006005 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
6006 if (isinstance == -1) {
6007 return 1;
6008 }
6009 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006010 *out = Sub;
6011 return 0;
6012 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006013 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
6014 if (isinstance == -1) {
6015 return 1;
6016 }
6017 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006018 *out = Mult;
6019 return 0;
6020 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006021 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6022 if (isinstance == -1) {
6023 return 1;
6024 }
6025 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006026 *out = Div;
6027 return 0;
6028 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006029 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6030 if (isinstance == -1) {
6031 return 1;
6032 }
6033 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006034 *out = Mod;
6035 return 0;
6036 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006037 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6038 if (isinstance == -1) {
6039 return 1;
6040 }
6041 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006042 *out = Pow;
6043 return 0;
6044 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006045 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6046 if (isinstance == -1) {
6047 return 1;
6048 }
6049 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006050 *out = LShift;
6051 return 0;
6052 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006053 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6054 if (isinstance == -1) {
6055 return 1;
6056 }
6057 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006058 *out = RShift;
6059 return 0;
6060 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006061 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6062 if (isinstance == -1) {
6063 return 1;
6064 }
6065 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006066 *out = BitOr;
6067 return 0;
6068 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006069 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6070 if (isinstance == -1) {
6071 return 1;
6072 }
6073 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006074 *out = BitXor;
6075 return 0;
6076 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006077 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6078 if (isinstance == -1) {
6079 return 1;
6080 }
6081 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006082 *out = BitAnd;
6083 return 0;
6084 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006085 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6086 if (isinstance == -1) {
6087 return 1;
6088 }
6089 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006090 *out = FloorDiv;
6091 return 0;
6092 }
6093
6094 tmp = PyObject_Repr(obj);
6095 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006096 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006097failed:
6098 Py_XDECREF(tmp);
6099 return 1;
6100}
6101
6102int
6103obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6104{
6105 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006106 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006107
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006108 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6109 if (isinstance == -1) {
6110 return 1;
6111 }
6112 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006113 *out = Invert;
6114 return 0;
6115 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006116 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6117 if (isinstance == -1) {
6118 return 1;
6119 }
6120 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006121 *out = Not;
6122 return 0;
6123 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006124 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6125 if (isinstance == -1) {
6126 return 1;
6127 }
6128 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006129 *out = UAdd;
6130 return 0;
6131 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006132 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6133 if (isinstance == -1) {
6134 return 1;
6135 }
6136 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006137 *out = USub;
6138 return 0;
6139 }
6140
6141 tmp = PyObject_Repr(obj);
6142 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006143 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006144failed:
6145 Py_XDECREF(tmp);
6146 return 1;
6147}
6148
6149int
6150obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6151{
6152 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006153 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006154
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006155 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6156 if (isinstance == -1) {
6157 return 1;
6158 }
6159 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006160 *out = Eq;
6161 return 0;
6162 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006163 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6164 if (isinstance == -1) {
6165 return 1;
6166 }
6167 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006168 *out = NotEq;
6169 return 0;
6170 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006171 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6172 if (isinstance == -1) {
6173 return 1;
6174 }
6175 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006176 *out = Lt;
6177 return 0;
6178 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006179 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6180 if (isinstance == -1) {
6181 return 1;
6182 }
6183 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006184 *out = LtE;
6185 return 0;
6186 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006187 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6188 if (isinstance == -1) {
6189 return 1;
6190 }
6191 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006192 *out = Gt;
6193 return 0;
6194 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006195 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6196 if (isinstance == -1) {
6197 return 1;
6198 }
6199 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006200 *out = GtE;
6201 return 0;
6202 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006203 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6204 if (isinstance == -1) {
6205 return 1;
6206 }
6207 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006208 *out = Is;
6209 return 0;
6210 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006211 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6212 if (isinstance == -1) {
6213 return 1;
6214 }
6215 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006216 *out = IsNot;
6217 return 0;
6218 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006219 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6220 if (isinstance == -1) {
6221 return 1;
6222 }
6223 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006224 *out = In;
6225 return 0;
6226 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006227 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6228 if (isinstance == -1) {
6229 return 1;
6230 }
6231 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006232 *out = NotIn;
6233 return 0;
6234 }
6235
6236 tmp = PyObject_Repr(obj);
6237 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006238 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006239failed:
6240 Py_XDECREF(tmp);
6241 return 1;
6242}
6243
6244int
6245obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6246{
6247 PyObject* tmp = NULL;
6248 expr_ty target;
6249 expr_ty iter;
6250 asdl_seq* ifs;
6251
6252 if (PyObject_HasAttrString(obj, "target")) {
6253 int res;
6254 tmp = PyObject_GetAttrString(obj, "target");
6255 if (tmp == NULL) goto failed;
6256 res = obj2ast_expr(tmp, &target, arena);
6257 if (res != 0) goto failed;
6258 Py_XDECREF(tmp);
6259 tmp = NULL;
6260 } else {
6261 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6262 return 1;
6263 }
6264 if (PyObject_HasAttrString(obj, "iter")) {
6265 int res;
6266 tmp = PyObject_GetAttrString(obj, "iter");
6267 if (tmp == NULL) goto failed;
6268 res = obj2ast_expr(tmp, &iter, arena);
6269 if (res != 0) goto failed;
6270 Py_XDECREF(tmp);
6271 tmp = NULL;
6272 } else {
6273 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6274 return 1;
6275 }
6276 if (PyObject_HasAttrString(obj, "ifs")) {
6277 int res;
6278 Py_ssize_t len;
6279 Py_ssize_t i;
6280 tmp = PyObject_GetAttrString(obj, "ifs");
6281 if (tmp == NULL) goto failed;
6282 if (!PyList_Check(tmp)) {
6283 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6284 goto failed;
6285 }
6286 len = PyList_GET_SIZE(tmp);
6287 ifs = asdl_seq_new(len, arena);
6288 if (ifs == NULL) goto failed;
6289 for (i = 0; i < len; i++) {
6290 expr_ty value;
6291 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6292 if (res != 0) goto failed;
6293 asdl_seq_SET(ifs, i, value);
6294 }
6295 Py_XDECREF(tmp);
6296 tmp = NULL;
6297 } else {
6298 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6299 return 1;
6300 }
6301 *out = comprehension(target, iter, ifs, arena);
6302 return 0;
6303failed:
6304 Py_XDECREF(tmp);
6305 return 1;
6306}
6307
6308int
6309obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6310{
6311 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006312 int isinstance;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006313
Georg Brandlfc8eef32008-03-28 12:11:56 +00006314 int lineno;
6315 int col_offset;
6316
Georg Brandla48f3ab2008-03-30 06:40:17 +00006317 if (obj == Py_None) {
6318 *out = NULL;
6319 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006320 }
6321 if (PyObject_HasAttrString(obj, "lineno")) {
6322 int res;
6323 tmp = PyObject_GetAttrString(obj, "lineno");
6324 if (tmp == NULL) goto failed;
6325 res = obj2ast_int(tmp, &lineno, arena);
6326 if (res != 0) goto failed;
6327 Py_XDECREF(tmp);
6328 tmp = NULL;
6329 } else {
6330 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6331 return 1;
6332 }
6333 if (PyObject_HasAttrString(obj, "col_offset")) {
6334 int res;
6335 tmp = PyObject_GetAttrString(obj, "col_offset");
6336 if (tmp == NULL) goto failed;
6337 res = obj2ast_int(tmp, &col_offset, arena);
6338 if (res != 0) goto failed;
6339 Py_XDECREF(tmp);
6340 tmp = NULL;
6341 } else {
6342 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6343 return 1;
6344 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006345 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6346 if (isinstance == -1) {
6347 return 1;
6348 }
6349 if (isinstance) {
Georg Brandla48f3ab2008-03-30 06:40:17 +00006350 expr_ty type;
6351 expr_ty name;
6352 asdl_seq* body;
6353
6354 if (PyObject_HasAttrString(obj, "type")) {
6355 int res;
6356 tmp = PyObject_GetAttrString(obj, "type");
6357 if (tmp == NULL) goto failed;
6358 res = obj2ast_expr(tmp, &type, arena);
6359 if (res != 0) goto failed;
6360 Py_XDECREF(tmp);
6361 tmp = NULL;
6362 } else {
6363 type = NULL;
6364 }
6365 if (PyObject_HasAttrString(obj, "name")) {
6366 int res;
6367 tmp = PyObject_GetAttrString(obj, "name");
6368 if (tmp == NULL) goto failed;
6369 res = obj2ast_expr(tmp, &name, arena);
6370 if (res != 0) goto failed;
6371 Py_XDECREF(tmp);
6372 tmp = NULL;
6373 } else {
6374 name = NULL;
6375 }
6376 if (PyObject_HasAttrString(obj, "body")) {
6377 int res;
6378 Py_ssize_t len;
6379 Py_ssize_t i;
6380 tmp = PyObject_GetAttrString(obj, "body");
6381 if (tmp == NULL) goto failed;
6382 if (!PyList_Check(tmp)) {
6383 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6384 goto failed;
6385 }
6386 len = PyList_GET_SIZE(tmp);
6387 body = asdl_seq_new(len, arena);
6388 if (body == NULL) goto failed;
6389 for (i = 0; i < len; i++) {
6390 stmt_ty value;
6391 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6392 if (res != 0) goto failed;
6393 asdl_seq_SET(body, i, value);
6394 }
6395 Py_XDECREF(tmp);
6396 tmp = NULL;
6397 } else {
6398 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6399 return 1;
6400 }
6401 *out = ExceptHandler(type, name, body, lineno, col_offset,
6402 arena);
6403 if (*out == NULL) goto failed;
6404 return 0;
6405 }
6406
6407 tmp = PyObject_Repr(obj);
6408 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006409 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006410failed:
6411 Py_XDECREF(tmp);
6412 return 1;
6413}
6414
6415int
6416obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6417{
6418 PyObject* tmp = NULL;
6419 asdl_seq* args;
6420 identifier vararg;
6421 identifier kwarg;
6422 asdl_seq* defaults;
6423
6424 if (PyObject_HasAttrString(obj, "args")) {
6425 int res;
6426 Py_ssize_t len;
6427 Py_ssize_t i;
6428 tmp = PyObject_GetAttrString(obj, "args");
6429 if (tmp == NULL) goto failed;
6430 if (!PyList_Check(tmp)) {
6431 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6432 goto failed;
6433 }
6434 len = PyList_GET_SIZE(tmp);
6435 args = asdl_seq_new(len, arena);
6436 if (args == NULL) goto failed;
6437 for (i = 0; i < len; i++) {
6438 expr_ty value;
6439 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6440 if (res != 0) goto failed;
6441 asdl_seq_SET(args, i, value);
6442 }
6443 Py_XDECREF(tmp);
6444 tmp = NULL;
6445 } else {
6446 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6447 return 1;
6448 }
6449 if (PyObject_HasAttrString(obj, "vararg")) {
6450 int res;
6451 tmp = PyObject_GetAttrString(obj, "vararg");
6452 if (tmp == NULL) goto failed;
6453 res = obj2ast_identifier(tmp, &vararg, arena);
6454 if (res != 0) goto failed;
6455 Py_XDECREF(tmp);
6456 tmp = NULL;
6457 } else {
6458 vararg = NULL;
6459 }
6460 if (PyObject_HasAttrString(obj, "kwarg")) {
6461 int res;
6462 tmp = PyObject_GetAttrString(obj, "kwarg");
6463 if (tmp == NULL) goto failed;
6464 res = obj2ast_identifier(tmp, &kwarg, arena);
6465 if (res != 0) goto failed;
6466 Py_XDECREF(tmp);
6467 tmp = NULL;
6468 } else {
6469 kwarg = NULL;
6470 }
6471 if (PyObject_HasAttrString(obj, "defaults")) {
6472 int res;
6473 Py_ssize_t len;
6474 Py_ssize_t i;
6475 tmp = PyObject_GetAttrString(obj, "defaults");
6476 if (tmp == NULL) goto failed;
6477 if (!PyList_Check(tmp)) {
6478 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6479 goto failed;
6480 }
6481 len = PyList_GET_SIZE(tmp);
6482 defaults = asdl_seq_new(len, arena);
6483 if (defaults == NULL) goto failed;
6484 for (i = 0; i < len; i++) {
6485 expr_ty value;
6486 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6487 if (res != 0) goto failed;
6488 asdl_seq_SET(defaults, i, value);
6489 }
6490 Py_XDECREF(tmp);
6491 tmp = NULL;
6492 } else {
6493 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6494 return 1;
6495 }
6496 *out = arguments(args, vararg, kwarg, defaults, arena);
6497 return 0;
6498failed:
6499 Py_XDECREF(tmp);
6500 return 1;
6501}
6502
6503int
6504obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6505{
6506 PyObject* tmp = NULL;
6507 identifier arg;
6508 expr_ty value;
6509
6510 if (PyObject_HasAttrString(obj, "arg")) {
6511 int res;
6512 tmp = PyObject_GetAttrString(obj, "arg");
6513 if (tmp == NULL) goto failed;
6514 res = obj2ast_identifier(tmp, &arg, arena);
6515 if (res != 0) goto failed;
6516 Py_XDECREF(tmp);
6517 tmp = NULL;
6518 } else {
6519 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6520 return 1;
6521 }
6522 if (PyObject_HasAttrString(obj, "value")) {
6523 int res;
6524 tmp = PyObject_GetAttrString(obj, "value");
6525 if (tmp == NULL) goto failed;
6526 res = obj2ast_expr(tmp, &value, arena);
6527 if (res != 0) goto failed;
6528 Py_XDECREF(tmp);
6529 tmp = NULL;
6530 } else {
6531 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6532 return 1;
6533 }
6534 *out = keyword(arg, value, arena);
6535 return 0;
6536failed:
6537 Py_XDECREF(tmp);
6538 return 1;
6539}
6540
6541int
6542obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6543{
6544 PyObject* tmp = NULL;
6545 identifier name;
6546 identifier asname;
6547
6548 if (PyObject_HasAttrString(obj, "name")) {
6549 int res;
6550 tmp = PyObject_GetAttrString(obj, "name");
6551 if (tmp == NULL) goto failed;
6552 res = obj2ast_identifier(tmp, &name, arena);
6553 if (res != 0) goto failed;
6554 Py_XDECREF(tmp);
6555 tmp = NULL;
6556 } else {
6557 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6558 return 1;
6559 }
6560 if (PyObject_HasAttrString(obj, "asname")) {
6561 int res;
6562 tmp = PyObject_GetAttrString(obj, "asname");
6563 if (tmp == NULL) goto failed;
6564 res = obj2ast_identifier(tmp, &asname, arena);
6565 if (res != 0) goto failed;
6566 Py_XDECREF(tmp);
6567 tmp = NULL;
6568 } else {
6569 asname = NULL;
6570 }
6571 *out = alias(name, asname, arena);
6572 return 0;
6573failed:
6574 Py_XDECREF(tmp);
6575 return 1;
6576}
6577
6578
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006579PyMODINIT_FUNC
6580init_ast(void)
6581{
6582 PyObject *m, *d;
6583 if (!init_types()) return;
6584 m = Py_InitModule3("_ast", NULL, NULL);
6585 if (!m) return;
6586 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00006587 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006588 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6589 return;
Benjamin Peterson5eed3062011-07-22 17:20:58 -05006590 if (PyModule_AddStringConstant(m, "__version__", "82160") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006591 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006592 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6593 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6594 return;
6595 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6596 < 0) return;
6597 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6598 0) return;
6599 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6600 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6601 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6602 < 0) return;
6603 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6604 return;
6605 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6606 return;
6607 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6608 return;
6609 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6610 return;
6611 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6612 0) return;
6613 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
6614 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6615 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6616 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6617 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6618 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6619 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6620 0) return;
6621 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6622 0) return;
6623 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6624 return;
6625 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6626 return;
6627 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6628 0) return;
6629 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
6630 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6631 return;
6632 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6633 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6634 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6635 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6636 return;
6637 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6638 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6639 return;
6640 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6641 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6642 return;
6643 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6644 return;
6645 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6646 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00006647 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006648 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6649 return;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00006650 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6651 return;
6652 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6653 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006654 if (PyDict_SetItemString(d, "GeneratorExp",
6655 (PyObject*)GeneratorExp_type) < 0) return;
6656 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6657 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6658 return;
6659 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6660 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6661 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6662 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6663 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6664 0) return;
6665 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6666 0) return;
6667 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6668 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6669 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6670 if (PyDict_SetItemString(d, "expr_context",
6671 (PyObject*)expr_context_type) < 0) return;
6672 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6673 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6674 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6675 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6676 return;
6677 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6678 return;
6679 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6680 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6681 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6682 return;
6683 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6684 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6685 return;
6686 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6687 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6688 return;
6689 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6690 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6691 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6692 return;
6693 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6694 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6695 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6696 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6697 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6698 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6699 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6700 return;
6701 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6702 return;
6703 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6704 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6705 return;
6706 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6707 return;
6708 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6709 return;
6710 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6711 return;
6712 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6713 return;
6714 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6715 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6716 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6717 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6718 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6719 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6720 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6721 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6722 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6723 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6724 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6725 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6726 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6727 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6728 if (PyDict_SetItemString(d, "comprehension",
6729 (PyObject*)comprehension_type) < 0) return;
6730 if (PyDict_SetItemString(d, "excepthandler",
6731 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006732 if (PyDict_SetItemString(d, "ExceptHandler",
6733 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006734 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6735 0) return;
6736 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6737 return;
6738 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006739}
6740
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006741
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006742PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006743{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006744 init_types();
6745 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006746}
6747
Georg Brandlf2bfd542008-03-29 13:24:23 +00006748/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6749mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006750{
6751 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006752 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6753 (PyObject*)Interactive_type};
6754 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006755 int isinstance;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006756 assert(0 <= mode && mode <= 2);
6757
Georg Brandlfc8eef32008-03-28 12:11:56 +00006758 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006759
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006760 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6761 if (isinstance == -1)
6762 return NULL;
6763 if (!isinstance) {
Georg Brandlf2bfd542008-03-29 13:24:23 +00006764 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6765 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006766 return NULL;
6767 }
6768 if (obj2ast_mod(ast, &res, arena) != 0)
6769 return NULL;
6770 else
6771 return res;
6772}
6773
6774int PyAST_Check(PyObject* obj)
6775{
6776 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006777 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006778}
6779
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006780