blob: 281e35490fa118bde15279231df0de77966c9221 [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Brett Cannon3ad57e22010-01-10 02:48:50 +00005 __version__ 77400.
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);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000530 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000531 for(i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000532 s = PyString_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000533 if (!s) {
534 Py_DECREF(l);
535 return 0;
536 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000537 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000538 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000539 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
540 Py_DECREF(l);
541 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000542}
543
Georg Brandlfc8eef32008-03-28 12:11:56 +0000544/* Conversion AST -> Python */
545
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000546static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
547{
548 int i, n = asdl_seq_LEN(seq);
549 PyObject *result = PyList_New(n);
550 PyObject *value;
551 if (!result)
552 return NULL;
553 for (i = 0; i < n; i++) {
554 value = func(asdl_seq_GET(seq, i));
555 if (!value) {
556 Py_DECREF(result);
557 return NULL;
558 }
559 PyList_SET_ITEM(result, i, value);
560 }
561 return result;
562}
563
564static PyObject* ast2obj_object(void *o)
565{
566 if (!o)
567 o = Py_None;
568 Py_INCREF((PyObject*)o);
569 return (PyObject*)o;
570}
571#define ast2obj_identifier ast2obj_object
572#define ast2obj_string ast2obj_object
573static PyObject* ast2obj_bool(bool b)
574{
575 return PyBool_FromLong(b);
576}
577
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000578static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000579{
580 return PyInt_FromLong(b);
581}
582
Georg Brandlfc8eef32008-03-28 12:11:56 +0000583/* Conversion Python -> AST */
584
585static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
586{
587 if (obj == Py_None)
588 obj = NULL;
589 if (obj)
590 PyArena_AddPyObject(arena, obj);
591 Py_XINCREF(obj);
592 *out = obj;
593 return 0;
594}
595
596#define obj2ast_identifier obj2ast_object
597#define obj2ast_string obj2ast_object
598
599static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
600{
601 int i;
602 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
603 PyObject *s = PyObject_Repr(obj);
604 if (s == NULL) return 1;
605 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000606 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000607 Py_DECREF(s);
608 return 1;
609 }
610
611 i = (int)PyLong_AsLong(obj);
612 if (i == -1 && PyErr_Occurred())
613 return 1;
614 *out = i;
615 return 0;
616}
617
618static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
619{
620 if (!PyBool_Check(obj)) {
621 PyObject *s = PyObject_Repr(obj);
622 if (s == NULL) return 1;
623 PyErr_Format(PyExc_ValueError, "invalid boolean 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 *out = (obj == Py_True);
630 return 0;
631}
632
Benjamin Petersonaff36f12008-10-19 13:59:01 +0000633static int add_ast_fields(void)
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000634{
635 PyObject *empty_tuple, *d;
636 if (PyType_Ready(&AST_type) < 0)
637 return -1;
638 d = AST_type.tp_dict;
639 empty_tuple = PyTuple_New(0);
640 if (!empty_tuple ||
641 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
642 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
643 Py_XDECREF(empty_tuple);
644 return -1;
645 }
646 Py_DECREF(empty_tuple);
647 return 0;
648}
649
Georg Brandlfc8eef32008-03-28 12:11:56 +0000650
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651static int init_types(void)
652{
Neal Norwitz19379f12006-04-03 04:50:58 +0000653 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 if (initialized) return 1;
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000655 if (add_ast_fields() < 0) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000656 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!mod_type) return 0;
658 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000659 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 Interactive_type = make_type("Interactive", mod_type,
662 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 Expression_type = make_type("Expression", mod_type, Expression_fields,
665 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000666 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000667 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000669 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000671 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 FunctionDef_type = make_type("FunctionDef", stmt_type,
673 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000674 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000675 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000676 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000677 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000680 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000683 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000684 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000687 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000689 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000693 With_type = make_type("With", stmt_type, With_fields, 3);
694 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
700 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000706 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000707 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000708 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000715 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000717 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000719 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000721 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000723 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000732 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Dict_type) return 0;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000736 Set_type = make_type("Set", expr_type, Set_fields, 1);
737 if (!Set_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!ListComp_type) return 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000740 SetComp_type = make_type("SetComp", expr_type, SetComp_fields, 2);
741 if (!SetComp_type) return 0;
742 DictComp_type = make_type("DictComp", expr_type, DictComp_fields, 3);
743 if (!DictComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 GeneratorExp_type = make_type("GeneratorExp", expr_type,
745 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000746 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000747 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000748 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000749 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000753 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000759 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000761 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000763 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000769 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!expr_context_type) return 0;
771 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000772 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000776 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000778 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000780 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000788 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000790 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000792 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000794 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000796 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!slice_type) return 0;
798 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000799 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000801 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000803 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000805 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000807 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000808 if (!boolop_type) return 0;
809 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000814 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000816 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000818 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!operator_type) return 0;
820 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000821 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000823 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000825 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000827 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000829 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000831 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000833 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000835 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000837 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000839 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000841 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000845 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000847 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000849 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000851 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000853 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000855 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000857 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000859 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000861 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000863 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000865 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000867 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000869 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000870 if (!unaryop_type) return 0;
871 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000872 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000874 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000876 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000878 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000880 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000884 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000886 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000888 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!cmpop_type) return 0;
890 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000891 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000892 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000893 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000895 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000897 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000898 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000899 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000900 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000901 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000902 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000903 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000904 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000905 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000906 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000907 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000908 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000909 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000910 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000911 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000913 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000915 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000916 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000917 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000918 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000919 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000920 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000921 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000922 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000923 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000924 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000925 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000926 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000927 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000928 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000929 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000930 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000931 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000932 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000933 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000934 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000935 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000936 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
937 return 0;
938 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
939 ExceptHandler_fields, 3);
940 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000941 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000942 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000943 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000944 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000945 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000946 if (!alias_type) return 0;
947 initialized = 1;
948 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000949}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000950
Georg Brandlfc8eef32008-03-28 12:11:56 +0000951static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
952static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
953static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
954static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
955 arena);
956static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
957static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
958static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
959static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
960static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
961static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
962 arena);
963static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
964 arena);
965static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
966static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
967static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000970Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971{
972 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000974 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 p->kind = Module_kind;
977 p->v.Module.body = body;
978 return p;
979}
980
981mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000982Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983{
984 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000985 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 p->kind = Interactive_kind;
989 p->v.Interactive.body = body;
990 return p;
991}
992
993mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000994Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995{
996 mod_ty p;
997 if (!body) {
998 PyErr_SetString(PyExc_ValueError,
999 "field body is required for Expression");
1000 return NULL;
1001 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001002 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001003 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 p->kind = Expression_kind;
1006 p->v.Expression.body = body;
1007 return p;
1008}
1009
1010mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001011Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012{
1013 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001014 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001015 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017 p->kind = Suite_kind;
1018 p->v.Suite.body = body;
1019 return p;
1020}
1021
1022stmt_ty
1023FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +00001024 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025{
1026 stmt_ty p;
1027 if (!name) {
1028 PyErr_SetString(PyExc_ValueError,
1029 "field name is required for FunctionDef");
1030 return NULL;
1031 }
1032 if (!args) {
1033 PyErr_SetString(PyExc_ValueError,
1034 "field args is required for FunctionDef");
1035 return NULL;
1036 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001037 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001038 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 p->kind = FunctionDef_kind;
1041 p->v.FunctionDef.name = name;
1042 p->v.FunctionDef.args = args;
1043 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001044 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001046 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 return p;
1048}
1049
1050stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001051ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1052 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053{
1054 stmt_ty p;
1055 if (!name) {
1056 PyErr_SetString(PyExc_ValueError,
1057 "field name is required for ClassDef");
1058 return NULL;
1059 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001060 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001061 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 p->kind = ClassDef_kind;
1064 p->v.ClassDef.name = name;
1065 p->v.ClassDef.bases = bases;
1066 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001067 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001069 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070 return p;
1071}
1072
1073stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001074Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075{
1076 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001077 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001078 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001079 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 p->kind = Return_kind;
1081 p->v.Return.value = value;
1082 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001083 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 return p;
1085}
1086
1087stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001088Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089{
1090 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001091 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001092 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 p->kind = Delete_kind;
1095 p->v.Delete.targets = targets;
1096 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001097 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 return p;
1099}
1100
1101stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001102Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1103 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104{
1105 stmt_ty p;
1106 if (!value) {
1107 PyErr_SetString(PyExc_ValueError,
1108 "field value is required for Assign");
1109 return NULL;
1110 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001112 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 p->kind = Assign_kind;
1115 p->v.Assign.targets = targets;
1116 p->v.Assign.value = value;
1117 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001118 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 return p;
1120}
1121
1122stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001123AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1124 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125{
1126 stmt_ty p;
1127 if (!target) {
1128 PyErr_SetString(PyExc_ValueError,
1129 "field target is required for AugAssign");
1130 return NULL;
1131 }
1132 if (!op) {
1133 PyErr_SetString(PyExc_ValueError,
1134 "field op is required for AugAssign");
1135 return NULL;
1136 }
1137 if (!value) {
1138 PyErr_SetString(PyExc_ValueError,
1139 "field value is required for AugAssign");
1140 return NULL;
1141 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001142 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001143 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 p->kind = AugAssign_kind;
1146 p->v.AugAssign.target = target;
1147 p->v.AugAssign.op = op;
1148 p->v.AugAssign.value = value;
1149 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001150 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return p;
1152}
1153
1154stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001155Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1156 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157{
1158 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001159 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001160 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 p->kind = Print_kind;
1163 p->v.Print.dest = dest;
1164 p->v.Print.values = values;
1165 p->v.Print.nl = nl;
1166 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001167 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 return p;
1169}
1170
1171stmt_ty
1172For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001173 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174{
1175 stmt_ty p;
1176 if (!target) {
1177 PyErr_SetString(PyExc_ValueError,
1178 "field target is required for For");
1179 return NULL;
1180 }
1181 if (!iter) {
1182 PyErr_SetString(PyExc_ValueError,
1183 "field iter is required for For");
1184 return NULL;
1185 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001186 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001187 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 p->kind = For_kind;
1190 p->v.For.target = target;
1191 p->v.For.iter = iter;
1192 p->v.For.body = body;
1193 p->v.For.orelse = orelse;
1194 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001195 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return p;
1197}
1198
1199stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1201 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202{
1203 stmt_ty p;
1204 if (!test) {
1205 PyErr_SetString(PyExc_ValueError,
1206 "field test is required for While");
1207 return NULL;
1208 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001209 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001210 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 p->kind = While_kind;
1213 p->v.While.test = test;
1214 p->v.While.body = body;
1215 p->v.While.orelse = orelse;
1216 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001217 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 return p;
1219}
1220
1221stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1223 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224{
1225 stmt_ty p;
1226 if (!test) {
1227 PyErr_SetString(PyExc_ValueError,
1228 "field test is required for If");
1229 return NULL;
1230 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001232 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 p->kind = If_kind;
1235 p->v.If.test = test;
1236 p->v.If.body = body;
1237 p->v.If.orelse = orelse;
1238 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return p;
1241}
1242
1243stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001244With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001245 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001246{
1247 stmt_ty p;
1248 if (!context_expr) {
1249 PyErr_SetString(PyExc_ValueError,
1250 "field context_expr is required for With");
1251 return NULL;
1252 }
1253 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001254 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001255 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001256 p->kind = With_kind;
1257 p->v.With.context_expr = context_expr;
1258 p->v.With.optional_vars = optional_vars;
1259 p->v.With.body = body;
1260 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001261 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001262 return p;
1263}
1264
1265stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001266Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1267 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268{
1269 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001270 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001271 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 p->kind = Raise_kind;
1274 p->v.Raise.type = type;
1275 p->v.Raise.inst = inst;
1276 p->v.Raise.tback = tback;
1277 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001278 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 return p;
1280}
1281
1282stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001283TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285{
1286 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001287 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001288 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 p->kind = TryExcept_kind;
1291 p->v.TryExcept.body = body;
1292 p->v.TryExcept.handlers = handlers;
1293 p->v.TryExcept.orelse = orelse;
1294 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001295 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 return p;
1297}
1298
1299stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001300TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1301 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302{
1303 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001304 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001305 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 p->kind = TryFinally_kind;
1308 p->v.TryFinally.body = body;
1309 p->v.TryFinally.finalbody = finalbody;
1310 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001311 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 return p;
1313}
1314
1315stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001316Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317{
1318 stmt_ty p;
1319 if (!test) {
1320 PyErr_SetString(PyExc_ValueError,
1321 "field test is required for Assert");
1322 return NULL;
1323 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001324 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001325 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 p->kind = Assert_kind;
1328 p->v.Assert.test = test;
1329 p->v.Assert.msg = msg;
1330 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001331 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 return p;
1333}
1334
1335stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001336Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001337{
1338 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001339 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001340 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 p->kind = Import_kind;
1343 p->v.Import.names = names;
1344 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001345 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 return p;
1347}
1348
1349stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001350ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1351 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352{
1353 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001354 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001355 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 p->kind = ImportFrom_kind;
1358 p->v.ImportFrom.module = module;
1359 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001360 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001362 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001363 return p;
1364}
1365
1366stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001367Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1368 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369{
1370 stmt_ty p;
1371 if (!body) {
1372 PyErr_SetString(PyExc_ValueError,
1373 "field body is required for Exec");
1374 return NULL;
1375 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001376 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001377 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 p->kind = Exec_kind;
1380 p->v.Exec.body = body;
1381 p->v.Exec.globals = globals;
1382 p->v.Exec.locals = locals;
1383 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001384 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 return p;
1386}
1387
1388stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001389Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390{
1391 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001393 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 p->kind = Global_kind;
1396 p->v.Global.names = names;
1397 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001398 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 return p;
1400}
1401
1402stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001403Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404{
1405 stmt_ty p;
1406 if (!value) {
1407 PyErr_SetString(PyExc_ValueError,
1408 "field value is required for Expr");
1409 return NULL;
1410 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001411 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001412 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 p->kind = Expr_kind;
1415 p->v.Expr.value = value;
1416 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001417 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 return p;
1419}
1420
1421stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001422Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423{
1424 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001425 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001426 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 p->kind = Pass_kind;
1429 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001430 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 return p;
1432}
1433
1434stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001435Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436{
1437 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001438 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001439 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441 p->kind = Break_kind;
1442 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 return p;
1445}
1446
1447stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001448Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449{
1450 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001451 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001452 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 p->kind = Continue_kind;
1455 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001456 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 return p;
1458}
1459
1460expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001461BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1462 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463{
1464 expr_ty p;
1465 if (!op) {
1466 PyErr_SetString(PyExc_ValueError,
1467 "field op is required for BoolOp");
1468 return NULL;
1469 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001470 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001471 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 p->kind = BoolOp_kind;
1474 p->v.BoolOp.op = op;
1475 p->v.BoolOp.values = values;
1476 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001477 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 return p;
1479}
1480
1481expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001482BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1483 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484{
1485 expr_ty p;
1486 if (!left) {
1487 PyErr_SetString(PyExc_ValueError,
1488 "field left is required for BinOp");
1489 return NULL;
1490 }
1491 if (!op) {
1492 PyErr_SetString(PyExc_ValueError,
1493 "field op is required for BinOp");
1494 return NULL;
1495 }
1496 if (!right) {
1497 PyErr_SetString(PyExc_ValueError,
1498 "field right is required for BinOp");
1499 return NULL;
1500 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001501 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001502 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 p->kind = BinOp_kind;
1505 p->v.BinOp.left = left;
1506 p->v.BinOp.op = op;
1507 p->v.BinOp.right = right;
1508 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001509 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 return p;
1511}
1512
1513expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1515 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516{
1517 expr_ty p;
1518 if (!op) {
1519 PyErr_SetString(PyExc_ValueError,
1520 "field op is required for UnaryOp");
1521 return NULL;
1522 }
1523 if (!operand) {
1524 PyErr_SetString(PyExc_ValueError,
1525 "field operand is required for UnaryOp");
1526 return NULL;
1527 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001528 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001529 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 p->kind = UnaryOp_kind;
1532 p->v.UnaryOp.op = op;
1533 p->v.UnaryOp.operand = operand;
1534 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001535 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 return p;
1537}
1538
1539expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001540Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1541 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542{
1543 expr_ty p;
1544 if (!args) {
1545 PyErr_SetString(PyExc_ValueError,
1546 "field args is required for Lambda");
1547 return NULL;
1548 }
1549 if (!body) {
1550 PyErr_SetString(PyExc_ValueError,
1551 "field body is required for Lambda");
1552 return NULL;
1553 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001554 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001555 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001556 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557 p->kind = Lambda_kind;
1558 p->v.Lambda.args = args;
1559 p->v.Lambda.body = body;
1560 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001561 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001562 return p;
1563}
1564
1565expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001566IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1567 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001568{
1569 expr_ty p;
1570 if (!test) {
1571 PyErr_SetString(PyExc_ValueError,
1572 "field test is required for IfExp");
1573 return NULL;
1574 }
1575 if (!body) {
1576 PyErr_SetString(PyExc_ValueError,
1577 "field body is required for IfExp");
1578 return NULL;
1579 }
1580 if (!orelse) {
1581 PyErr_SetString(PyExc_ValueError,
1582 "field orelse is required for IfExp");
1583 return NULL;
1584 }
1585 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001586 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001587 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001588 p->kind = IfExp_kind;
1589 p->v.IfExp.test = test;
1590 p->v.IfExp.body = body;
1591 p->v.IfExp.orelse = orelse;
1592 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001593 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001594 return p;
1595}
1596
1597expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001598Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1599 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001600{
1601 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001602 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001603 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 p->kind = Dict_kind;
1606 p->v.Dict.keys = keys;
1607 p->v.Dict.values = values;
1608 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001609 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 return p;
1611}
1612
1613expr_ty
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00001614Set(asdl_seq * elts, int lineno, int col_offset, PyArena *arena)
1615{
1616 expr_ty p;
1617 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1618 if (!p)
1619 return NULL;
1620 p->kind = Set_kind;
1621 p->v.Set.elts = elts;
1622 p->lineno = lineno;
1623 p->col_offset = col_offset;
1624 return p;
1625}
1626
1627expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001628ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1629 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001630{
1631 expr_ty p;
1632 if (!elt) {
1633 PyErr_SetString(PyExc_ValueError,
1634 "field elt is required for ListComp");
1635 return NULL;
1636 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001637 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001638 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 p->kind = ListComp_kind;
1641 p->v.ListComp.elt = elt;
1642 p->v.ListComp.generators = generators;
1643 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001644 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 return p;
1646}
1647
1648expr_ty
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001649SetComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset, PyArena
1650 *arena)
1651{
1652 expr_ty p;
1653 if (!elt) {
1654 PyErr_SetString(PyExc_ValueError,
1655 "field elt is required for SetComp");
1656 return NULL;
1657 }
1658 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1659 if (!p)
1660 return NULL;
1661 p->kind = SetComp_kind;
1662 p->v.SetComp.elt = elt;
1663 p->v.SetComp.generators = generators;
1664 p->lineno = lineno;
1665 p->col_offset = col_offset;
1666 return p;
1667}
1668
1669expr_ty
1670DictComp(expr_ty key, expr_ty value, asdl_seq * generators, int lineno, int
1671 col_offset, PyArena *arena)
1672{
1673 expr_ty p;
1674 if (!key) {
1675 PyErr_SetString(PyExc_ValueError,
1676 "field key is required for DictComp");
1677 return NULL;
1678 }
1679 if (!value) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field value is required for DictComp");
1682 return NULL;
1683 }
1684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
1685 if (!p)
1686 return NULL;
1687 p->kind = DictComp_kind;
1688 p->v.DictComp.key = key;
1689 p->v.DictComp.value = value;
1690 p->v.DictComp.generators = generators;
1691 p->lineno = lineno;
1692 p->col_offset = col_offset;
1693 return p;
1694}
1695
1696expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001697GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1698 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699{
1700 expr_ty p;
1701 if (!elt) {
1702 PyErr_SetString(PyExc_ValueError,
1703 "field elt is required for GeneratorExp");
1704 return NULL;
1705 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001707 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 p->kind = GeneratorExp_kind;
1710 p->v.GeneratorExp.elt = elt;
1711 p->v.GeneratorExp.generators = generators;
1712 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001713 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 return p;
1715}
1716
1717expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001718Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719{
1720 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001721 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001722 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 p->kind = Yield_kind;
1725 p->v.Yield.value = value;
1726 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001727 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728 return p;
1729}
1730
1731expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001732Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1733 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734{
1735 expr_ty p;
1736 if (!left) {
1737 PyErr_SetString(PyExc_ValueError,
1738 "field left is required for Compare");
1739 return NULL;
1740 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001741 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001742 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 p->kind = Compare_kind;
1745 p->v.Compare.left = left;
1746 p->v.Compare.ops = ops;
1747 p->v.Compare.comparators = comparators;
1748 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001749 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 return p;
1751}
1752
1753expr_ty
1754Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001755 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756{
1757 expr_ty p;
1758 if (!func) {
1759 PyErr_SetString(PyExc_ValueError,
1760 "field func is required for Call");
1761 return NULL;
1762 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001763 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001764 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001766 p->kind = Call_kind;
1767 p->v.Call.func = func;
1768 p->v.Call.args = args;
1769 p->v.Call.keywords = keywords;
1770 p->v.Call.starargs = starargs;
1771 p->v.Call.kwargs = kwargs;
1772 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 return p;
1775}
1776
1777expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001778Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779{
1780 expr_ty p;
1781 if (!value) {
1782 PyErr_SetString(PyExc_ValueError,
1783 "field value is required for Repr");
1784 return NULL;
1785 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001786 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001787 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 p->kind = Repr_kind;
1790 p->v.Repr.value = value;
1791 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001792 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 return p;
1794}
1795
1796expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001797Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798{
1799 expr_ty p;
1800 if (!n) {
1801 PyErr_SetString(PyExc_ValueError,
1802 "field n is required for Num");
1803 return NULL;
1804 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001806 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808 p->kind = Num_kind;
1809 p->v.Num.n = n;
1810 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001811 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812 return p;
1813}
1814
1815expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001816Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817{
1818 expr_ty p;
1819 if (!s) {
1820 PyErr_SetString(PyExc_ValueError,
1821 "field s is required for Str");
1822 return NULL;
1823 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001824 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001825 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 p->kind = Str_kind;
1828 p->v.Str.s = s;
1829 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001830 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831 return p;
1832}
1833
1834expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001835Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1836 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837{
1838 expr_ty p;
1839 if (!value) {
1840 PyErr_SetString(PyExc_ValueError,
1841 "field value is required for Attribute");
1842 return NULL;
1843 }
1844 if (!attr) {
1845 PyErr_SetString(PyExc_ValueError,
1846 "field attr is required for Attribute");
1847 return NULL;
1848 }
1849 if (!ctx) {
1850 PyErr_SetString(PyExc_ValueError,
1851 "field ctx is required for Attribute");
1852 return NULL;
1853 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001854 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001855 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 p->kind = Attribute_kind;
1858 p->v.Attribute.value = value;
1859 p->v.Attribute.attr = attr;
1860 p->v.Attribute.ctx = ctx;
1861 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001862 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 return p;
1864}
1865
1866expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001867Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1868 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869{
1870 expr_ty p;
1871 if (!value) {
1872 PyErr_SetString(PyExc_ValueError,
1873 "field value is required for Subscript");
1874 return NULL;
1875 }
1876 if (!slice) {
1877 PyErr_SetString(PyExc_ValueError,
1878 "field slice is required for Subscript");
1879 return NULL;
1880 }
1881 if (!ctx) {
1882 PyErr_SetString(PyExc_ValueError,
1883 "field ctx is required for Subscript");
1884 return NULL;
1885 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001887 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 p->kind = Subscript_kind;
1890 p->v.Subscript.value = value;
1891 p->v.Subscript.slice = slice;
1892 p->v.Subscript.ctx = ctx;
1893 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001894 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 return p;
1896}
1897
1898expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001899Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1900 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901{
1902 expr_ty p;
1903 if (!id) {
1904 PyErr_SetString(PyExc_ValueError,
1905 "field id is required for Name");
1906 return NULL;
1907 }
1908 if (!ctx) {
1909 PyErr_SetString(PyExc_ValueError,
1910 "field ctx is required for Name");
1911 return NULL;
1912 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001913 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001914 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 p->kind = Name_kind;
1917 p->v.Name.id = id;
1918 p->v.Name.ctx = ctx;
1919 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001920 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 return p;
1922}
1923
1924expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001925List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1926 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927{
1928 expr_ty p;
1929 if (!ctx) {
1930 PyErr_SetString(PyExc_ValueError,
1931 "field ctx is required for List");
1932 return NULL;
1933 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001934 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001935 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 p->kind = List_kind;
1938 p->v.List.elts = elts;
1939 p->v.List.ctx = ctx;
1940 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001941 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 return p;
1943}
1944
1945expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001946Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1947 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948{
1949 expr_ty p;
1950 if (!ctx) {
1951 PyErr_SetString(PyExc_ValueError,
1952 "field ctx is required for Tuple");
1953 return NULL;
1954 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001955 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001956 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 p->kind = Tuple_kind;
1959 p->v.Tuple.elts = elts;
1960 p->v.Tuple.ctx = ctx;
1961 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001962 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 return p;
1964}
1965
1966slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001967Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968{
1969 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001970 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001971 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 p->kind = Ellipsis_kind;
1974 return p;
1975}
1976
1977slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001978Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979{
1980 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001981 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001982 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 p->kind = Slice_kind;
1985 p->v.Slice.lower = lower;
1986 p->v.Slice.upper = upper;
1987 p->v.Slice.step = step;
1988 return p;
1989}
1990
1991slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001992ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993{
1994 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001995 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001996 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 p->kind = ExtSlice_kind;
1999 p->v.ExtSlice.dims = dims;
2000 return p;
2001}
2002
2003slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002004Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005{
2006 slice_ty p;
2007 if (!value) {
2008 PyErr_SetString(PyExc_ValueError,
2009 "field value is required for Index");
2010 return NULL;
2011 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002012 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002013 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 p->kind = Index_kind;
2016 p->v.Index.value = value;
2017 return p;
2018}
2019
2020comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002021comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022{
2023 comprehension_ty p;
2024 if (!target) {
2025 PyErr_SetString(PyExc_ValueError,
2026 "field target is required for comprehension");
2027 return NULL;
2028 }
2029 if (!iter) {
2030 PyErr_SetString(PyExc_ValueError,
2031 "field iter is required for comprehension");
2032 return NULL;
2033 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002034 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002035 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 p->target = target;
2038 p->iter = iter;
2039 p->ifs = ifs;
2040 return p;
2041}
2042
2043excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00002044ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002045 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046{
2047 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002048 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002049 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00002051 p->kind = ExceptHandler_kind;
2052 p->v.ExceptHandler.type = type;
2053 p->v.ExceptHandler.name = name;
2054 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002055 p->lineno = lineno;
2056 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 return p;
2058}
2059
2060arguments_ty
2061arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002062 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063{
2064 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002065 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002066 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 p->args = args;
2069 p->vararg = vararg;
2070 p->kwarg = kwarg;
2071 p->defaults = defaults;
2072 return p;
2073}
2074
2075keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002076keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002077{
2078 keyword_ty p;
2079 if (!arg) {
2080 PyErr_SetString(PyExc_ValueError,
2081 "field arg is required for keyword");
2082 return NULL;
2083 }
2084 if (!value) {
2085 PyErr_SetString(PyExc_ValueError,
2086 "field value is required for keyword");
2087 return NULL;
2088 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002089 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002090 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 p->arg = arg;
2093 p->value = value;
2094 return p;
2095}
2096
2097alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002098alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099{
2100 alias_ty p;
2101 if (!name) {
2102 PyErr_SetString(PyExc_ValueError,
2103 "field name is required for alias");
2104 return NULL;
2105 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002106 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002107 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 p->name = name;
2110 p->asname = asname;
2111 return p;
2112}
2113
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002114
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002115PyObject*
2116ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002117{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002118 mod_ty o = (mod_ty)_o;
2119 PyObject *result = NULL, *value = NULL;
2120 if (!o) {
2121 Py_INCREF(Py_None);
2122 return Py_None;
2123 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 switch (o->kind) {
2126 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002127 result = PyType_GenericNew(Module_type, NULL, NULL);
2128 if (!result) goto failed;
2129 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2130 if (!value) goto failed;
2131 if (PyObject_SetAttrString(result, "body", value) == -1)
2132 goto failed;
2133 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 break;
2135 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002136 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2137 if (!result) goto failed;
2138 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2139 if (!value) goto failed;
2140 if (PyObject_SetAttrString(result, "body", value) == -1)
2141 goto failed;
2142 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 break;
2144 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 result = PyType_GenericNew(Expression_type, NULL, NULL);
2146 if (!result) goto failed;
2147 value = ast2obj_expr(o->v.Expression.body);
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 Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002154 result = PyType_GenericNew(Suite_type, NULL, NULL);
2155 if (!result) goto failed;
2156 value = ast2obj_list(o->v.Suite.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 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002163 return result;
2164failed:
2165 Py_XDECREF(value);
2166 Py_XDECREF(result);
2167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168}
2169
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002170PyObject*
2171ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002173 stmt_ty o = (stmt_ty)_o;
2174 PyObject *result = NULL, *value = NULL;
2175 if (!o) {
2176 Py_INCREF(Py_None);
2177 return Py_None;
2178 }
2179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 switch (o->kind) {
2181 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002182 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2183 if (!result) goto failed;
2184 value = ast2obj_identifier(o->v.FunctionDef.name);
2185 if (!value) goto failed;
2186 if (PyObject_SetAttrString(result, "name", value) == -1)
2187 goto failed;
2188 Py_DECREF(value);
2189 value = ast2obj_arguments(o->v.FunctionDef.args);
2190 if (!value) goto failed;
2191 if (PyObject_SetAttrString(result, "args", value) == -1)
2192 goto failed;
2193 Py_DECREF(value);
2194 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2195 if (!value) goto failed;
2196 if (PyObject_SetAttrString(result, "body", value) == -1)
2197 goto failed;
2198 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002199 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2200 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002201 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002202 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2203 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002204 goto failed;
2205 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 break;
2207 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002208 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2209 if (!result) goto failed;
2210 value = ast2obj_identifier(o->v.ClassDef.name);
2211 if (!value) goto failed;
2212 if (PyObject_SetAttrString(result, "name", value) == -1)
2213 goto failed;
2214 Py_DECREF(value);
2215 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2216 if (!value) goto failed;
2217 if (PyObject_SetAttrString(result, "bases", value) == -1)
2218 goto failed;
2219 Py_DECREF(value);
2220 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2221 if (!value) goto failed;
2222 if (PyObject_SetAttrString(result, "body", value) == -1)
2223 goto failed;
2224 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002225 value = ast2obj_list(o->v.ClassDef.decorator_list,
2226 ast2obj_expr);
2227 if (!value) goto failed;
2228 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2229 -1)
2230 goto failed;
2231 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 break;
2233 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002234 result = PyType_GenericNew(Return_type, NULL, NULL);
2235 if (!result) goto failed;
2236 value = ast2obj_expr(o->v.Return.value);
2237 if (!value) goto failed;
2238 if (PyObject_SetAttrString(result, "value", value) == -1)
2239 goto failed;
2240 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 break;
2242 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002243 result = PyType_GenericNew(Delete_type, NULL, NULL);
2244 if (!result) goto failed;
2245 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2246 if (!value) goto failed;
2247 if (PyObject_SetAttrString(result, "targets", value) == -1)
2248 goto failed;
2249 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 break;
2251 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002252 result = PyType_GenericNew(Assign_type, NULL, NULL);
2253 if (!result) goto failed;
2254 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2255 if (!value) goto failed;
2256 if (PyObject_SetAttrString(result, "targets", value) == -1)
2257 goto failed;
2258 Py_DECREF(value);
2259 value = ast2obj_expr(o->v.Assign.value);
2260 if (!value) goto failed;
2261 if (PyObject_SetAttrString(result, "value", value) == -1)
2262 goto failed;
2263 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 break;
2265 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002266 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2267 if (!result) goto failed;
2268 value = ast2obj_expr(o->v.AugAssign.target);
2269 if (!value) goto failed;
2270 if (PyObject_SetAttrString(result, "target", value) == -1)
2271 goto failed;
2272 Py_DECREF(value);
2273 value = ast2obj_operator(o->v.AugAssign.op);
2274 if (!value) goto failed;
2275 if (PyObject_SetAttrString(result, "op", value) == -1)
2276 goto failed;
2277 Py_DECREF(value);
2278 value = ast2obj_expr(o->v.AugAssign.value);
2279 if (!value) goto failed;
2280 if (PyObject_SetAttrString(result, "value", value) == -1)
2281 goto failed;
2282 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 break;
2284 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002285 result = PyType_GenericNew(Print_type, NULL, NULL);
2286 if (!result) goto failed;
2287 value = ast2obj_expr(o->v.Print.dest);
2288 if (!value) goto failed;
2289 if (PyObject_SetAttrString(result, "dest", value) == -1)
2290 goto failed;
2291 Py_DECREF(value);
2292 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2293 if (!value) goto failed;
2294 if (PyObject_SetAttrString(result, "values", value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
2297 value = ast2obj_bool(o->v.Print.nl);
2298 if (!value) goto failed;
2299 if (PyObject_SetAttrString(result, "nl", value) == -1)
2300 goto failed;
2301 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 break;
2303 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002304 result = PyType_GenericNew(For_type, NULL, NULL);
2305 if (!result) goto failed;
2306 value = ast2obj_expr(o->v.For.target);
2307 if (!value) goto failed;
2308 if (PyObject_SetAttrString(result, "target", value) == -1)
2309 goto failed;
2310 Py_DECREF(value);
2311 value = ast2obj_expr(o->v.For.iter);
2312 if (!value) goto failed;
2313 if (PyObject_SetAttrString(result, "iter", value) == -1)
2314 goto failed;
2315 Py_DECREF(value);
2316 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2317 if (!value) goto failed;
2318 if (PyObject_SetAttrString(result, "body", value) == -1)
2319 goto failed;
2320 Py_DECREF(value);
2321 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2322 if (!value) goto failed;
2323 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2324 goto failed;
2325 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 break;
2327 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002328 result = PyType_GenericNew(While_type, NULL, NULL);
2329 if (!result) goto failed;
2330 value = ast2obj_expr(o->v.While.test);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "test", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "body", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
2340 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2341 if (!value) goto failed;
2342 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2343 goto failed;
2344 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 break;
2346 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002347 result = PyType_GenericNew(If_type, NULL, NULL);
2348 if (!result) goto failed;
2349 value = ast2obj_expr(o->v.If.test);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "test", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
2354 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2355 if (!value) goto failed;
2356 if (PyObject_SetAttrString(result, "body", value) == -1)
2357 goto failed;
2358 Py_DECREF(value);
2359 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002365 case With_kind:
2366 result = PyType_GenericNew(With_type, NULL, NULL);
2367 if (!result) goto failed;
2368 value = ast2obj_expr(o->v.With.context_expr);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
2373 value = ast2obj_expr(o->v.With.optional_vars);
2374 if (!value) goto failed;
2375 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2376 -1)
2377 goto failed;
2378 Py_DECREF(value);
2379 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2380 if (!value) goto failed;
2381 if (PyObject_SetAttrString(result, "body", value) == -1)
2382 goto failed;
2383 Py_DECREF(value);
2384 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002386 result = PyType_GenericNew(Raise_type, NULL, NULL);
2387 if (!result) goto failed;
2388 value = ast2obj_expr(o->v.Raise.type);
2389 if (!value) goto failed;
2390 if (PyObject_SetAttrString(result, "type", value) == -1)
2391 goto failed;
2392 Py_DECREF(value);
2393 value = ast2obj_expr(o->v.Raise.inst);
2394 if (!value) goto failed;
2395 if (PyObject_SetAttrString(result, "inst", value) == -1)
2396 goto failed;
2397 Py_DECREF(value);
2398 value = ast2obj_expr(o->v.Raise.tback);
2399 if (!value) goto failed;
2400 if (PyObject_SetAttrString(result, "tback", value) == -1)
2401 goto failed;
2402 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 break;
2404 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002405 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2406 if (!result) goto failed;
2407 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2408 if (!value) goto failed;
2409 if (PyObject_SetAttrString(result, "body", value) == -1)
2410 goto failed;
2411 Py_DECREF(value);
2412 value = ast2obj_list(o->v.TryExcept.handlers,
2413 ast2obj_excepthandler);
2414 if (!value) goto failed;
2415 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2416 goto failed;
2417 Py_DECREF(value);
2418 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2419 if (!value) goto failed;
2420 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2421 goto failed;
2422 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 break;
2424 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002425 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2426 if (!result) goto failed;
2427 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2428 if (!value) goto failed;
2429 if (PyObject_SetAttrString(result, "body", value) == -1)
2430 goto failed;
2431 Py_DECREF(value);
2432 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2433 if (!value) goto failed;
2434 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2435 goto failed;
2436 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 break;
2438 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002439 result = PyType_GenericNew(Assert_type, NULL, NULL);
2440 if (!result) goto failed;
2441 value = ast2obj_expr(o->v.Assert.test);
2442 if (!value) goto failed;
2443 if (PyObject_SetAttrString(result, "test", value) == -1)
2444 goto failed;
2445 Py_DECREF(value);
2446 value = ast2obj_expr(o->v.Assert.msg);
2447 if (!value) goto failed;
2448 if (PyObject_SetAttrString(result, "msg", value) == -1)
2449 goto failed;
2450 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 break;
2452 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002453 result = PyType_GenericNew(Import_type, NULL, NULL);
2454 if (!result) goto failed;
2455 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2456 if (!value) goto failed;
2457 if (PyObject_SetAttrString(result, "names", value) == -1)
2458 goto failed;
2459 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 break;
2461 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002462 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2463 if (!result) goto failed;
2464 value = ast2obj_identifier(o->v.ImportFrom.module);
2465 if (!value) goto failed;
2466 if (PyObject_SetAttrString(result, "module", value) == -1)
2467 goto failed;
2468 Py_DECREF(value);
2469 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2470 if (!value) goto failed;
2471 if (PyObject_SetAttrString(result, "names", value) == -1)
2472 goto failed;
2473 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002474 value = ast2obj_int(o->v.ImportFrom.level);
2475 if (!value) goto failed;
2476 if (PyObject_SetAttrString(result, "level", value) == -1)
2477 goto failed;
2478 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 break;
2480 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002481 result = PyType_GenericNew(Exec_type, NULL, NULL);
2482 if (!result) goto failed;
2483 value = ast2obj_expr(o->v.Exec.body);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "body", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
2488 value = ast2obj_expr(o->v.Exec.globals);
2489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "globals", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
2493 value = ast2obj_expr(o->v.Exec.locals);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "locals", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 break;
2499 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002500 result = PyType_GenericNew(Global_type, NULL, NULL);
2501 if (!result) goto failed;
2502 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "names", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 break;
2508 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002509 result = PyType_GenericNew(Expr_type, NULL, NULL);
2510 if (!result) goto failed;
2511 value = ast2obj_expr(o->v.Expr.value);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "value", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 break;
2517 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 result = PyType_GenericNew(Pass_type, NULL, NULL);
2519 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 break;
2521 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002522 result = PyType_GenericNew(Break_type, NULL, NULL);
2523 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 break;
2525 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002526 result = PyType_GenericNew(Continue_type, NULL, NULL);
2527 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 break;
2529 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002530 value = ast2obj_int(o->lineno);
2531 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002532 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2533 goto failed;
2534 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002535 value = ast2obj_int(o->col_offset);
2536 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002537 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2538 goto failed;
2539 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002540 return result;
2541failed:
2542 Py_XDECREF(value);
2543 Py_XDECREF(result);
2544 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545}
2546
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547PyObject*
2548ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002550 expr_ty o = (expr_ty)_o;
2551 PyObject *result = NULL, *value = NULL;
2552 if (!o) {
2553 Py_INCREF(Py_None);
2554 return Py_None;
2555 }
2556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 switch (o->kind) {
2558 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002559 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2560 if (!result) goto failed;
2561 value = ast2obj_boolop(o->v.BoolOp.op);
2562 if (!value) goto failed;
2563 if (PyObject_SetAttrString(result, "op", value) == -1)
2564 goto failed;
2565 Py_DECREF(value);
2566 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2567 if (!value) goto failed;
2568 if (PyObject_SetAttrString(result, "values", value) == -1)
2569 goto failed;
2570 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002571 break;
2572 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002573 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2574 if (!result) goto failed;
2575 value = ast2obj_expr(o->v.BinOp.left);
2576 if (!value) goto failed;
2577 if (PyObject_SetAttrString(result, "left", value) == -1)
2578 goto failed;
2579 Py_DECREF(value);
2580 value = ast2obj_operator(o->v.BinOp.op);
2581 if (!value) goto failed;
2582 if (PyObject_SetAttrString(result, "op", value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
2585 value = ast2obj_expr(o->v.BinOp.right);
2586 if (!value) goto failed;
2587 if (PyObject_SetAttrString(result, "right", value) == -1)
2588 goto failed;
2589 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 break;
2591 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002592 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2593 if (!result) goto failed;
2594 value = ast2obj_unaryop(o->v.UnaryOp.op);
2595 if (!value) goto failed;
2596 if (PyObject_SetAttrString(result, "op", value) == -1)
2597 goto failed;
2598 Py_DECREF(value);
2599 value = ast2obj_expr(o->v.UnaryOp.operand);
2600 if (!value) goto failed;
2601 if (PyObject_SetAttrString(result, "operand", value) == -1)
2602 goto failed;
2603 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 break;
2605 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002606 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2607 if (!result) goto failed;
2608 value = ast2obj_arguments(o->v.Lambda.args);
2609 if (!value) goto failed;
2610 if (PyObject_SetAttrString(result, "args", value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 value = ast2obj_expr(o->v.Lambda.body);
2614 if (!value) goto failed;
2615 if (PyObject_SetAttrString(result, "body", value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002619 case IfExp_kind:
2620 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2621 if (!result) goto failed;
2622 value = ast2obj_expr(o->v.IfExp.test);
2623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "test", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 value = ast2obj_expr(o->v.IfExp.body);
2628 if (!value) goto failed;
2629 if (PyObject_SetAttrString(result, "body", value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
2632 value = ast2obj_expr(o->v.IfExp.orelse);
2633 if (!value) goto failed;
2634 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2635 goto failed;
2636 Py_DECREF(value);
2637 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002639 result = PyType_GenericNew(Dict_type, NULL, NULL);
2640 if (!result) goto failed;
2641 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2642 if (!value) goto failed;
2643 if (PyObject_SetAttrString(result, "keys", value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "values", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002652 case Set_kind:
2653 result = PyType_GenericNew(Set_type, NULL, NULL);
2654 if (!result) goto failed;
2655 value = ast2obj_list(o->v.Set.elts, ast2obj_expr);
2656 if (!value) goto failed;
2657 if (PyObject_SetAttrString(result, "elts", value) == -1)
2658 goto failed;
2659 Py_DECREF(value);
2660 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002662 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2663 if (!result) goto failed;
2664 value = ast2obj_expr(o->v.ListComp.elt);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "elt", value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
2669 value = ast2obj_list(o->v.ListComp.generators,
2670 ast2obj_comprehension);
2671 if (!value) goto failed;
2672 if (PyObject_SetAttrString(result, "generators", value) == -1)
2673 goto failed;
2674 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002676 case SetComp_kind:
2677 result = PyType_GenericNew(SetComp_type, NULL, NULL);
2678 if (!result) goto failed;
2679 value = ast2obj_expr(o->v.SetComp.elt);
2680 if (!value) goto failed;
2681 if (PyObject_SetAttrString(result, "elt", value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
2684 value = ast2obj_list(o->v.SetComp.generators,
2685 ast2obj_comprehension);
2686 if (!value) goto failed;
2687 if (PyObject_SetAttrString(result, "generators", value) == -1)
2688 goto failed;
2689 Py_DECREF(value);
2690 break;
2691 case DictComp_kind:
2692 result = PyType_GenericNew(DictComp_type, NULL, NULL);
2693 if (!result) goto failed;
2694 value = ast2obj_expr(o->v.DictComp.key);
2695 if (!value) goto failed;
2696 if (PyObject_SetAttrString(result, "key", value) == -1)
2697 goto failed;
2698 Py_DECREF(value);
2699 value = ast2obj_expr(o->v.DictComp.value);
2700 if (!value) goto failed;
2701 if (PyObject_SetAttrString(result, "value", value) == -1)
2702 goto failed;
2703 Py_DECREF(value);
2704 value = ast2obj_list(o->v.DictComp.generators,
2705 ast2obj_comprehension);
2706 if (!value) goto failed;
2707 if (PyObject_SetAttrString(result, "generators", value) == -1)
2708 goto failed;
2709 Py_DECREF(value);
2710 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002712 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2713 if (!result) goto failed;
2714 value = ast2obj_expr(o->v.GeneratorExp.elt);
2715 if (!value) goto failed;
2716 if (PyObject_SetAttrString(result, "elt", value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
2719 value = ast2obj_list(o->v.GeneratorExp.generators,
2720 ast2obj_comprehension);
2721 if (!value) goto failed;
2722 if (PyObject_SetAttrString(result, "generators", value) == -1)
2723 goto failed;
2724 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002725 break;
2726 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002727 result = PyType_GenericNew(Yield_type, NULL, NULL);
2728 if (!result) goto failed;
2729 value = ast2obj_expr(o->v.Yield.value);
2730 if (!value) goto failed;
2731 if (PyObject_SetAttrString(result, "value", value) == -1)
2732 goto failed;
2733 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 break;
2735 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002736 result = PyType_GenericNew(Compare_type, NULL, NULL);
2737 if (!result) goto failed;
2738 value = ast2obj_expr(o->v.Compare.left);
2739 if (!value) goto failed;
2740 if (PyObject_SetAttrString(result, "left", value) == -1)
2741 goto failed;
2742 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002743 {
2744 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2745 value = PyList_New(n);
2746 if (!value) goto failed;
2747 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002748 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 +00002749 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002750 if (!value) goto failed;
2751 if (PyObject_SetAttrString(result, "ops", value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
2754 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2755 if (!value) goto failed;
2756 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2757 goto failed;
2758 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 break;
2760 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002761 result = PyType_GenericNew(Call_type, NULL, NULL);
2762 if (!result) goto failed;
2763 value = ast2obj_expr(o->v.Call.func);
2764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "func", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
2768 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2769 if (!value) goto failed;
2770 if (PyObject_SetAttrString(result, "args", value) == -1)
2771 goto failed;
2772 Py_DECREF(value);
2773 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2774 if (!value) goto failed;
2775 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2776 goto failed;
2777 Py_DECREF(value);
2778 value = ast2obj_expr(o->v.Call.starargs);
2779 if (!value) goto failed;
2780 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2781 goto failed;
2782 Py_DECREF(value);
2783 value = ast2obj_expr(o->v.Call.kwargs);
2784 if (!value) goto failed;
2785 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2786 goto failed;
2787 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 break;
2789 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002790 result = PyType_GenericNew(Repr_type, NULL, NULL);
2791 if (!result) goto failed;
2792 value = ast2obj_expr(o->v.Repr.value);
2793 if (!value) goto failed;
2794 if (PyObject_SetAttrString(result, "value", value) == -1)
2795 goto failed;
2796 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 break;
2798 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002799 result = PyType_GenericNew(Num_type, NULL, NULL);
2800 if (!result) goto failed;
2801 value = ast2obj_object(o->v.Num.n);
2802 if (!value) goto failed;
2803 if (PyObject_SetAttrString(result, "n", value) == -1)
2804 goto failed;
2805 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 break;
2807 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002808 result = PyType_GenericNew(Str_type, NULL, NULL);
2809 if (!result) goto failed;
2810 value = ast2obj_string(o->v.Str.s);
2811 if (!value) goto failed;
2812 if (PyObject_SetAttrString(result, "s", value) == -1)
2813 goto failed;
2814 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 break;
2816 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002817 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_expr(o->v.Attribute.value);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "value", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_identifier(o->v.Attribute.attr);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "attr", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 value = ast2obj_expr_context(o->v.Attribute.ctx);
2830 if (!value) goto failed;
2831 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 break;
2835 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002836 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2837 if (!result) goto failed;
2838 value = ast2obj_expr(o->v.Subscript.value);
2839 if (!value) goto failed;
2840 if (PyObject_SetAttrString(result, "value", value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
2843 value = ast2obj_slice(o->v.Subscript.slice);
2844 if (!value) goto failed;
2845 if (PyObject_SetAttrString(result, "slice", value) == -1)
2846 goto failed;
2847 Py_DECREF(value);
2848 value = ast2obj_expr_context(o->v.Subscript.ctx);
2849 if (!value) goto failed;
2850 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2851 goto failed;
2852 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853 break;
2854 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002855 result = PyType_GenericNew(Name_type, NULL, NULL);
2856 if (!result) goto failed;
2857 value = ast2obj_identifier(o->v.Name.id);
2858 if (!value) goto failed;
2859 if (PyObject_SetAttrString(result, "id", value) == -1)
2860 goto failed;
2861 Py_DECREF(value);
2862 value = ast2obj_expr_context(o->v.Name.ctx);
2863 if (!value) goto failed;
2864 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2865 goto failed;
2866 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 break;
2868 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002869 result = PyType_GenericNew(List_type, NULL, NULL);
2870 if (!result) goto failed;
2871 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2872 if (!value) goto failed;
2873 if (PyObject_SetAttrString(result, "elts", value) == -1)
2874 goto failed;
2875 Py_DECREF(value);
2876 value = ast2obj_expr_context(o->v.List.ctx);
2877 if (!value) goto failed;
2878 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2879 goto failed;
2880 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 break;
2882 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002883 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2884 if (!result) goto failed;
2885 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2886 if (!value) goto failed;
2887 if (PyObject_SetAttrString(result, "elts", value) == -1)
2888 goto failed;
2889 Py_DECREF(value);
2890 value = ast2obj_expr_context(o->v.Tuple.ctx);
2891 if (!value) goto failed;
2892 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2893 goto failed;
2894 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 break;
2896 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002897 value = ast2obj_int(o->lineno);
2898 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002899 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2900 goto failed;
2901 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002902 value = ast2obj_int(o->col_offset);
2903 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002904 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2905 goto failed;
2906 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002907 return result;
2908failed:
2909 Py_XDECREF(value);
2910 Py_XDECREF(result);
2911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912}
2913
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002914PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002916 switch(o) {
2917 case Load:
2918 Py_INCREF(Load_singleton);
2919 return Load_singleton;
2920 case Store:
2921 Py_INCREF(Store_singleton);
2922 return Store_singleton;
2923 case Del:
2924 Py_INCREF(Del_singleton);
2925 return Del_singleton;
2926 case AugLoad:
2927 Py_INCREF(AugLoad_singleton);
2928 return AugLoad_singleton;
2929 case AugStore:
2930 Py_INCREF(AugStore_singleton);
2931 return AugStore_singleton;
2932 case Param:
2933 Py_INCREF(Param_singleton);
2934 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002935 default:
2936 /* should never happen, but just in case ... */
2937 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2938 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941PyObject*
2942ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002944 slice_ty o = (slice_ty)_o;
2945 PyObject *result = NULL, *value = NULL;
2946 if (!o) {
2947 Py_INCREF(Py_None);
2948 return Py_None;
2949 }
2950
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002951 switch (o->kind) {
2952 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002953 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2954 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955 break;
2956 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002957 result = PyType_GenericNew(Slice_type, NULL, NULL);
2958 if (!result) goto failed;
2959 value = ast2obj_expr(o->v.Slice.lower);
2960 if (!value) goto failed;
2961 if (PyObject_SetAttrString(result, "lower", value) == -1)
2962 goto failed;
2963 Py_DECREF(value);
2964 value = ast2obj_expr(o->v.Slice.upper);
2965 if (!value) goto failed;
2966 if (PyObject_SetAttrString(result, "upper", value) == -1)
2967 goto failed;
2968 Py_DECREF(value);
2969 value = ast2obj_expr(o->v.Slice.step);
2970 if (!value) goto failed;
2971 if (PyObject_SetAttrString(result, "step", value) == -1)
2972 goto failed;
2973 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 break;
2975 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002976 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2977 if (!result) goto failed;
2978 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2979 if (!value) goto failed;
2980 if (PyObject_SetAttrString(result, "dims", value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 break;
2984 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002985 result = PyType_GenericNew(Index_type, NULL, NULL);
2986 if (!result) goto failed;
2987 value = ast2obj_expr(o->v.Index.value);
2988 if (!value) goto failed;
2989 if (PyObject_SetAttrString(result, "value", value) == -1)
2990 goto failed;
2991 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 break;
2993 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002994 return result;
2995failed:
2996 Py_XDECREF(value);
2997 Py_XDECREF(result);
2998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999}
3000
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003001PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003003 switch(o) {
3004 case And:
3005 Py_INCREF(And_singleton);
3006 return And_singleton;
3007 case Or:
3008 Py_INCREF(Or_singleton);
3009 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003010 default:
3011 /* should never happen, but just in case ... */
3012 PyErr_Format(PyExc_SystemError, "unknown boolop found");
3013 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003015}
3016PyObject* ast2obj_operator(operator_ty o)
3017{
3018 switch(o) {
3019 case Add:
3020 Py_INCREF(Add_singleton);
3021 return Add_singleton;
3022 case Sub:
3023 Py_INCREF(Sub_singleton);
3024 return Sub_singleton;
3025 case Mult:
3026 Py_INCREF(Mult_singleton);
3027 return Mult_singleton;
3028 case Div:
3029 Py_INCREF(Div_singleton);
3030 return Div_singleton;
3031 case Mod:
3032 Py_INCREF(Mod_singleton);
3033 return Mod_singleton;
3034 case Pow:
3035 Py_INCREF(Pow_singleton);
3036 return Pow_singleton;
3037 case LShift:
3038 Py_INCREF(LShift_singleton);
3039 return LShift_singleton;
3040 case RShift:
3041 Py_INCREF(RShift_singleton);
3042 return RShift_singleton;
3043 case BitOr:
3044 Py_INCREF(BitOr_singleton);
3045 return BitOr_singleton;
3046 case BitXor:
3047 Py_INCREF(BitXor_singleton);
3048 return BitXor_singleton;
3049 case BitAnd:
3050 Py_INCREF(BitAnd_singleton);
3051 return BitAnd_singleton;
3052 case FloorDiv:
3053 Py_INCREF(FloorDiv_singleton);
3054 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003055 default:
3056 /* should never happen, but just in case ... */
3057 PyErr_Format(PyExc_SystemError, "unknown operator found");
3058 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003059 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003060}
3061PyObject* ast2obj_unaryop(unaryop_ty o)
3062{
3063 switch(o) {
3064 case Invert:
3065 Py_INCREF(Invert_singleton);
3066 return Invert_singleton;
3067 case Not:
3068 Py_INCREF(Not_singleton);
3069 return Not_singleton;
3070 case UAdd:
3071 Py_INCREF(UAdd_singleton);
3072 return UAdd_singleton;
3073 case USub:
3074 Py_INCREF(USub_singleton);
3075 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003076 default:
3077 /* should never happen, but just in case ... */
3078 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
3079 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003080 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003081}
3082PyObject* ast2obj_cmpop(cmpop_ty o)
3083{
3084 switch(o) {
3085 case Eq:
3086 Py_INCREF(Eq_singleton);
3087 return Eq_singleton;
3088 case NotEq:
3089 Py_INCREF(NotEq_singleton);
3090 return NotEq_singleton;
3091 case Lt:
3092 Py_INCREF(Lt_singleton);
3093 return Lt_singleton;
3094 case LtE:
3095 Py_INCREF(LtE_singleton);
3096 return LtE_singleton;
3097 case Gt:
3098 Py_INCREF(Gt_singleton);
3099 return Gt_singleton;
3100 case GtE:
3101 Py_INCREF(GtE_singleton);
3102 return GtE_singleton;
3103 case Is:
3104 Py_INCREF(Is_singleton);
3105 return Is_singleton;
3106 case IsNot:
3107 Py_INCREF(IsNot_singleton);
3108 return IsNot_singleton;
3109 case In:
3110 Py_INCREF(In_singleton);
3111 return In_singleton;
3112 case NotIn:
3113 Py_INCREF(NotIn_singleton);
3114 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003115 default:
3116 /* should never happen, but just in case ... */
3117 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
3118 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003119 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003120}
3121PyObject*
3122ast2obj_comprehension(void* _o)
3123{
3124 comprehension_ty o = (comprehension_ty)_o;
3125 PyObject *result = NULL, *value = NULL;
3126 if (!o) {
3127 Py_INCREF(Py_None);
3128 return Py_None;
3129 }
3130
3131 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3132 if (!result) return NULL;
3133 value = ast2obj_expr(o->target);
3134 if (!value) goto failed;
3135 if (PyObject_SetAttrString(result, "target", value) == -1)
3136 goto failed;
3137 Py_DECREF(value);
3138 value = ast2obj_expr(o->iter);
3139 if (!value) goto failed;
3140 if (PyObject_SetAttrString(result, "iter", value) == -1)
3141 goto failed;
3142 Py_DECREF(value);
3143 value = ast2obj_list(o->ifs, ast2obj_expr);
3144 if (!value) goto failed;
3145 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003148 return result;
3149failed:
3150 Py_XDECREF(value);
3151 Py_XDECREF(result);
3152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153}
3154
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003155PyObject*
3156ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003158 excepthandler_ty o = (excepthandler_ty)_o;
3159 PyObject *result = NULL, *value = NULL;
3160 if (!o) {
3161 Py_INCREF(Py_None);
3162 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003164
Georg Brandla48f3ab2008-03-30 06:40:17 +00003165 switch (o->kind) {
3166 case ExceptHandler_kind:
3167 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3168 if (!result) goto failed;
3169 value = ast2obj_expr(o->v.ExceptHandler.type);
3170 if (!value) goto failed;
3171 if (PyObject_SetAttrString(result, "type", value) == -1)
3172 goto failed;
3173 Py_DECREF(value);
3174 value = ast2obj_expr(o->v.ExceptHandler.name);
3175 if (!value) goto failed;
3176 if (PyObject_SetAttrString(result, "name", value) == -1)
3177 goto failed;
3178 Py_DECREF(value);
3179 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3180 if (!value) goto failed;
3181 if (PyObject_SetAttrString(result, "body", value) == -1)
3182 goto failed;
3183 Py_DECREF(value);
3184 break;
3185 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003186 value = ast2obj_int(o->lineno);
3187 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003188 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003189 goto failed;
3190 Py_DECREF(value);
3191 value = ast2obj_int(o->col_offset);
3192 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003193 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003194 goto failed;
3195 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003196 return result;
3197failed:
3198 Py_XDECREF(value);
3199 Py_XDECREF(result);
3200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201}
3202
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003203PyObject*
3204ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003206 arguments_ty o = (arguments_ty)_o;
3207 PyObject *result = NULL, *value = NULL;
3208 if (!o) {
3209 Py_INCREF(Py_None);
3210 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003212
3213 result = PyType_GenericNew(arguments_type, NULL, NULL);
3214 if (!result) return NULL;
3215 value = ast2obj_list(o->args, ast2obj_expr);
3216 if (!value) goto failed;
3217 if (PyObject_SetAttrString(result, "args", value) == -1)
3218 goto failed;
3219 Py_DECREF(value);
3220 value = ast2obj_identifier(o->vararg);
3221 if (!value) goto failed;
3222 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3223 goto failed;
3224 Py_DECREF(value);
3225 value = ast2obj_identifier(o->kwarg);
3226 if (!value) goto failed;
3227 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3228 goto failed;
3229 Py_DECREF(value);
3230 value = ast2obj_list(o->defaults, ast2obj_expr);
3231 if (!value) goto failed;
3232 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3233 goto failed;
3234 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003235 return result;
3236failed:
3237 Py_XDECREF(value);
3238 Py_XDECREF(result);
3239 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240}
3241
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003242PyObject*
3243ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003245 keyword_ty o = (keyword_ty)_o;
3246 PyObject *result = NULL, *value = NULL;
3247 if (!o) {
3248 Py_INCREF(Py_None);
3249 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003251
3252 result = PyType_GenericNew(keyword_type, NULL, NULL);
3253 if (!result) return NULL;
3254 value = ast2obj_identifier(o->arg);
3255 if (!value) goto failed;
3256 if (PyObject_SetAttrString(result, "arg", value) == -1)
3257 goto failed;
3258 Py_DECREF(value);
3259 value = ast2obj_expr(o->value);
3260 if (!value) goto failed;
3261 if (PyObject_SetAttrString(result, "value", value) == -1)
3262 goto failed;
3263 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003264 return result;
3265failed:
3266 Py_XDECREF(value);
3267 Py_XDECREF(result);
3268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269}
3270
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003271PyObject*
3272ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003274 alias_ty o = (alias_ty)_o;
3275 PyObject *result = NULL, *value = NULL;
3276 if (!o) {
3277 Py_INCREF(Py_None);
3278 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003280
3281 result = PyType_GenericNew(alias_type, NULL, NULL);
3282 if (!result) return NULL;
3283 value = ast2obj_identifier(o->name);
3284 if (!value) goto failed;
3285 if (PyObject_SetAttrString(result, "name", value) == -1)
3286 goto failed;
3287 Py_DECREF(value);
3288 value = ast2obj_identifier(o->asname);
3289 if (!value) goto failed;
3290 if (PyObject_SetAttrString(result, "asname", value) == -1)
3291 goto failed;
3292 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003293 return result;
3294failed:
3295 Py_XDECREF(value);
3296 Py_XDECREF(result);
3297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298}
3299
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300
Georg Brandlfc8eef32008-03-28 12:11:56 +00003301int
3302obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3303{
3304 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003305 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003306
3307
3308 if (obj == Py_None) {
3309 *out = NULL;
3310 return 0;
3311 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003312 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3313 if (isinstance == -1) {
3314 return 1;
3315 }
3316 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003317 asdl_seq* body;
3318
3319 if (PyObject_HasAttrString(obj, "body")) {
3320 int res;
3321 Py_ssize_t len;
3322 Py_ssize_t i;
3323 tmp = PyObject_GetAttrString(obj, "body");
3324 if (tmp == NULL) goto failed;
3325 if (!PyList_Check(tmp)) {
3326 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3327 goto failed;
3328 }
3329 len = PyList_GET_SIZE(tmp);
3330 body = asdl_seq_new(len, arena);
3331 if (body == NULL) goto failed;
3332 for (i = 0; i < len; i++) {
3333 stmt_ty value;
3334 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3335 if (res != 0) goto failed;
3336 asdl_seq_SET(body, i, value);
3337 }
3338 Py_XDECREF(tmp);
3339 tmp = NULL;
3340 } else {
3341 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3342 return 1;
3343 }
3344 *out = Module(body, arena);
3345 if (*out == NULL) goto failed;
3346 return 0;
3347 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003348 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3349 if (isinstance == -1) {
3350 return 1;
3351 }
3352 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003353 asdl_seq* body;
3354
3355 if (PyObject_HasAttrString(obj, "body")) {
3356 int res;
3357 Py_ssize_t len;
3358 Py_ssize_t i;
3359 tmp = PyObject_GetAttrString(obj, "body");
3360 if (tmp == NULL) goto failed;
3361 if (!PyList_Check(tmp)) {
3362 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3363 goto failed;
3364 }
3365 len = PyList_GET_SIZE(tmp);
3366 body = asdl_seq_new(len, arena);
3367 if (body == NULL) goto failed;
3368 for (i = 0; i < len; i++) {
3369 stmt_ty value;
3370 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3371 if (res != 0) goto failed;
3372 asdl_seq_SET(body, i, value);
3373 }
3374 Py_XDECREF(tmp);
3375 tmp = NULL;
3376 } else {
3377 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3378 return 1;
3379 }
3380 *out = Interactive(body, arena);
3381 if (*out == NULL) goto failed;
3382 return 0;
3383 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003384 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3385 if (isinstance == -1) {
3386 return 1;
3387 }
3388 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003389 expr_ty body;
3390
3391 if (PyObject_HasAttrString(obj, "body")) {
3392 int res;
3393 tmp = PyObject_GetAttrString(obj, "body");
3394 if (tmp == NULL) goto failed;
3395 res = obj2ast_expr(tmp, &body, arena);
3396 if (res != 0) goto failed;
3397 Py_XDECREF(tmp);
3398 tmp = NULL;
3399 } else {
3400 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3401 return 1;
3402 }
3403 *out = Expression(body, arena);
3404 if (*out == NULL) goto failed;
3405 return 0;
3406 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003407 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3408 if (isinstance == -1) {
3409 return 1;
3410 }
3411 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003412 asdl_seq* body;
3413
3414 if (PyObject_HasAttrString(obj, "body")) {
3415 int res;
3416 Py_ssize_t len;
3417 Py_ssize_t i;
3418 tmp = PyObject_GetAttrString(obj, "body");
3419 if (tmp == NULL) goto failed;
3420 if (!PyList_Check(tmp)) {
3421 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3422 goto failed;
3423 }
3424 len = PyList_GET_SIZE(tmp);
3425 body = asdl_seq_new(len, arena);
3426 if (body == NULL) goto failed;
3427 for (i = 0; i < len; i++) {
3428 stmt_ty value;
3429 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3430 if (res != 0) goto failed;
3431 asdl_seq_SET(body, i, value);
3432 }
3433 Py_XDECREF(tmp);
3434 tmp = NULL;
3435 } else {
3436 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3437 return 1;
3438 }
3439 *out = Suite(body, arena);
3440 if (*out == NULL) goto failed;
3441 return 0;
3442 }
3443
3444 tmp = PyObject_Repr(obj);
3445 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003446 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00003447failed:
3448 Py_XDECREF(tmp);
3449 return 1;
3450}
3451
3452int
3453obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3454{
3455 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003456 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003457
3458 int lineno;
3459 int col_offset;
3460
3461 if (obj == Py_None) {
3462 *out = NULL;
3463 return 0;
3464 }
3465 if (PyObject_HasAttrString(obj, "lineno")) {
3466 int res;
3467 tmp = PyObject_GetAttrString(obj, "lineno");
3468 if (tmp == NULL) goto failed;
3469 res = obj2ast_int(tmp, &lineno, arena);
3470 if (res != 0) goto failed;
3471 Py_XDECREF(tmp);
3472 tmp = NULL;
3473 } else {
3474 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3475 return 1;
3476 }
3477 if (PyObject_HasAttrString(obj, "col_offset")) {
3478 int res;
3479 tmp = PyObject_GetAttrString(obj, "col_offset");
3480 if (tmp == NULL) goto failed;
3481 res = obj2ast_int(tmp, &col_offset, arena);
3482 if (res != 0) goto failed;
3483 Py_XDECREF(tmp);
3484 tmp = NULL;
3485 } else {
3486 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3487 return 1;
3488 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003489 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3490 if (isinstance == -1) {
3491 return 1;
3492 }
3493 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003494 identifier name;
3495 arguments_ty args;
3496 asdl_seq* body;
3497 asdl_seq* decorator_list;
3498
3499 if (PyObject_HasAttrString(obj, "name")) {
3500 int res;
3501 tmp = PyObject_GetAttrString(obj, "name");
3502 if (tmp == NULL) goto failed;
3503 res = obj2ast_identifier(tmp, &name, arena);
3504 if (res != 0) goto failed;
3505 Py_XDECREF(tmp);
3506 tmp = NULL;
3507 } else {
3508 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3509 return 1;
3510 }
3511 if (PyObject_HasAttrString(obj, "args")) {
3512 int res;
3513 tmp = PyObject_GetAttrString(obj, "args");
3514 if (tmp == NULL) goto failed;
3515 res = obj2ast_arguments(tmp, &args, arena);
3516 if (res != 0) goto failed;
3517 Py_XDECREF(tmp);
3518 tmp = NULL;
3519 } else {
3520 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3521 return 1;
3522 }
3523 if (PyObject_HasAttrString(obj, "body")) {
3524 int res;
3525 Py_ssize_t len;
3526 Py_ssize_t i;
3527 tmp = PyObject_GetAttrString(obj, "body");
3528 if (tmp == NULL) goto failed;
3529 if (!PyList_Check(tmp)) {
3530 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3531 goto failed;
3532 }
3533 len = PyList_GET_SIZE(tmp);
3534 body = asdl_seq_new(len, arena);
3535 if (body == NULL) goto failed;
3536 for (i = 0; i < len; i++) {
3537 stmt_ty value;
3538 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3539 if (res != 0) goto failed;
3540 asdl_seq_SET(body, i, value);
3541 }
3542 Py_XDECREF(tmp);
3543 tmp = NULL;
3544 } else {
3545 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3546 return 1;
3547 }
3548 if (PyObject_HasAttrString(obj, "decorator_list")) {
3549 int res;
3550 Py_ssize_t len;
3551 Py_ssize_t i;
3552 tmp = PyObject_GetAttrString(obj, "decorator_list");
3553 if (tmp == NULL) goto failed;
3554 if (!PyList_Check(tmp)) {
3555 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3556 goto failed;
3557 }
3558 len = PyList_GET_SIZE(tmp);
3559 decorator_list = asdl_seq_new(len, arena);
3560 if (decorator_list == NULL) goto failed;
3561 for (i = 0; i < len; i++) {
3562 expr_ty value;
3563 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3564 if (res != 0) goto failed;
3565 asdl_seq_SET(decorator_list, i, value);
3566 }
3567 Py_XDECREF(tmp);
3568 tmp = NULL;
3569 } else {
3570 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3571 return 1;
3572 }
3573 *out = FunctionDef(name, args, body, decorator_list, lineno,
3574 col_offset, arena);
3575 if (*out == NULL) goto failed;
3576 return 0;
3577 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003578 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3579 if (isinstance == -1) {
3580 return 1;
3581 }
3582 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003583 identifier name;
3584 asdl_seq* bases;
3585 asdl_seq* body;
3586 asdl_seq* decorator_list;
3587
3588 if (PyObject_HasAttrString(obj, "name")) {
3589 int res;
3590 tmp = PyObject_GetAttrString(obj, "name");
3591 if (tmp == NULL) goto failed;
3592 res = obj2ast_identifier(tmp, &name, arena);
3593 if (res != 0) goto failed;
3594 Py_XDECREF(tmp);
3595 tmp = NULL;
3596 } else {
3597 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3598 return 1;
3599 }
3600 if (PyObject_HasAttrString(obj, "bases")) {
3601 int res;
3602 Py_ssize_t len;
3603 Py_ssize_t i;
3604 tmp = PyObject_GetAttrString(obj, "bases");
3605 if (tmp == NULL) goto failed;
3606 if (!PyList_Check(tmp)) {
3607 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3608 goto failed;
3609 }
3610 len = PyList_GET_SIZE(tmp);
3611 bases = asdl_seq_new(len, arena);
3612 if (bases == NULL) goto failed;
3613 for (i = 0; i < len; i++) {
3614 expr_ty value;
3615 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3616 if (res != 0) goto failed;
3617 asdl_seq_SET(bases, i, value);
3618 }
3619 Py_XDECREF(tmp);
3620 tmp = NULL;
3621 } else {
3622 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3623 return 1;
3624 }
3625 if (PyObject_HasAttrString(obj, "body")) {
3626 int res;
3627 Py_ssize_t len;
3628 Py_ssize_t i;
3629 tmp = PyObject_GetAttrString(obj, "body");
3630 if (tmp == NULL) goto failed;
3631 if (!PyList_Check(tmp)) {
3632 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3633 goto failed;
3634 }
3635 len = PyList_GET_SIZE(tmp);
3636 body = asdl_seq_new(len, arena);
3637 if (body == NULL) goto failed;
3638 for (i = 0; i < len; i++) {
3639 stmt_ty value;
3640 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3641 if (res != 0) goto failed;
3642 asdl_seq_SET(body, i, value);
3643 }
3644 Py_XDECREF(tmp);
3645 tmp = NULL;
3646 } else {
3647 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3648 return 1;
3649 }
3650 if (PyObject_HasAttrString(obj, "decorator_list")) {
3651 int res;
3652 Py_ssize_t len;
3653 Py_ssize_t i;
3654 tmp = PyObject_GetAttrString(obj, "decorator_list");
3655 if (tmp == NULL) goto failed;
3656 if (!PyList_Check(tmp)) {
3657 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3658 goto failed;
3659 }
3660 len = PyList_GET_SIZE(tmp);
3661 decorator_list = asdl_seq_new(len, arena);
3662 if (decorator_list == NULL) goto failed;
3663 for (i = 0; i < len; i++) {
3664 expr_ty value;
3665 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3666 if (res != 0) goto failed;
3667 asdl_seq_SET(decorator_list, i, value);
3668 }
3669 Py_XDECREF(tmp);
3670 tmp = NULL;
3671 } else {
3672 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3673 return 1;
3674 }
3675 *out = ClassDef(name, bases, body, decorator_list, lineno,
3676 col_offset, arena);
3677 if (*out == NULL) goto failed;
3678 return 0;
3679 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003680 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3681 if (isinstance == -1) {
3682 return 1;
3683 }
3684 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003685 expr_ty value;
3686
3687 if (PyObject_HasAttrString(obj, "value")) {
3688 int res;
3689 tmp = PyObject_GetAttrString(obj, "value");
3690 if (tmp == NULL) goto failed;
3691 res = obj2ast_expr(tmp, &value, arena);
3692 if (res != 0) goto failed;
3693 Py_XDECREF(tmp);
3694 tmp = NULL;
3695 } else {
3696 value = NULL;
3697 }
3698 *out = Return(value, lineno, col_offset, arena);
3699 if (*out == NULL) goto failed;
3700 return 0;
3701 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003702 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3703 if (isinstance == -1) {
3704 return 1;
3705 }
3706 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003707 asdl_seq* targets;
3708
3709 if (PyObject_HasAttrString(obj, "targets")) {
3710 int res;
3711 Py_ssize_t len;
3712 Py_ssize_t i;
3713 tmp = PyObject_GetAttrString(obj, "targets");
3714 if (tmp == NULL) goto failed;
3715 if (!PyList_Check(tmp)) {
3716 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3717 goto failed;
3718 }
3719 len = PyList_GET_SIZE(tmp);
3720 targets = asdl_seq_new(len, arena);
3721 if (targets == NULL) goto failed;
3722 for (i = 0; i < len; i++) {
3723 expr_ty value;
3724 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3725 if (res != 0) goto failed;
3726 asdl_seq_SET(targets, i, value);
3727 }
3728 Py_XDECREF(tmp);
3729 tmp = NULL;
3730 } else {
3731 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3732 return 1;
3733 }
3734 *out = Delete(targets, lineno, col_offset, arena);
3735 if (*out == NULL) goto failed;
3736 return 0;
3737 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003738 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3739 if (isinstance == -1) {
3740 return 1;
3741 }
3742 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003743 asdl_seq* targets;
3744 expr_ty value;
3745
3746 if (PyObject_HasAttrString(obj, "targets")) {
3747 int res;
3748 Py_ssize_t len;
3749 Py_ssize_t i;
3750 tmp = PyObject_GetAttrString(obj, "targets");
3751 if (tmp == NULL) goto failed;
3752 if (!PyList_Check(tmp)) {
3753 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3754 goto failed;
3755 }
3756 len = PyList_GET_SIZE(tmp);
3757 targets = asdl_seq_new(len, arena);
3758 if (targets == NULL) goto failed;
3759 for (i = 0; i < len; i++) {
3760 expr_ty value;
3761 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3762 if (res != 0) goto failed;
3763 asdl_seq_SET(targets, i, value);
3764 }
3765 Py_XDECREF(tmp);
3766 tmp = NULL;
3767 } else {
3768 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3769 return 1;
3770 }
3771 if (PyObject_HasAttrString(obj, "value")) {
3772 int res;
3773 tmp = PyObject_GetAttrString(obj, "value");
3774 if (tmp == NULL) goto failed;
3775 res = obj2ast_expr(tmp, &value, arena);
3776 if (res != 0) goto failed;
3777 Py_XDECREF(tmp);
3778 tmp = NULL;
3779 } else {
3780 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3781 return 1;
3782 }
3783 *out = Assign(targets, value, lineno, col_offset, arena);
3784 if (*out == NULL) goto failed;
3785 return 0;
3786 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003787 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3788 if (isinstance == -1) {
3789 return 1;
3790 }
3791 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003792 expr_ty target;
3793 operator_ty op;
3794 expr_ty value;
3795
3796 if (PyObject_HasAttrString(obj, "target")) {
3797 int res;
3798 tmp = PyObject_GetAttrString(obj, "target");
3799 if (tmp == NULL) goto failed;
3800 res = obj2ast_expr(tmp, &target, arena);
3801 if (res != 0) goto failed;
3802 Py_XDECREF(tmp);
3803 tmp = NULL;
3804 } else {
3805 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3806 return 1;
3807 }
3808 if (PyObject_HasAttrString(obj, "op")) {
3809 int res;
3810 tmp = PyObject_GetAttrString(obj, "op");
3811 if (tmp == NULL) goto failed;
3812 res = obj2ast_operator(tmp, &op, arena);
3813 if (res != 0) goto failed;
3814 Py_XDECREF(tmp);
3815 tmp = NULL;
3816 } else {
3817 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3818 return 1;
3819 }
3820 if (PyObject_HasAttrString(obj, "value")) {
3821 int res;
3822 tmp = PyObject_GetAttrString(obj, "value");
3823 if (tmp == NULL) goto failed;
3824 res = obj2ast_expr(tmp, &value, arena);
3825 if (res != 0) goto failed;
3826 Py_XDECREF(tmp);
3827 tmp = NULL;
3828 } else {
3829 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3830 return 1;
3831 }
3832 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3833 if (*out == NULL) goto failed;
3834 return 0;
3835 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003836 isinstance = PyObject_IsInstance(obj, (PyObject*)Print_type);
3837 if (isinstance == -1) {
3838 return 1;
3839 }
3840 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003841 expr_ty dest;
3842 asdl_seq* values;
3843 bool nl;
3844
3845 if (PyObject_HasAttrString(obj, "dest")) {
3846 int res;
3847 tmp = PyObject_GetAttrString(obj, "dest");
3848 if (tmp == NULL) goto failed;
3849 res = obj2ast_expr(tmp, &dest, arena);
3850 if (res != 0) goto failed;
3851 Py_XDECREF(tmp);
3852 tmp = NULL;
3853 } else {
3854 dest = NULL;
3855 }
3856 if (PyObject_HasAttrString(obj, "values")) {
3857 int res;
3858 Py_ssize_t len;
3859 Py_ssize_t i;
3860 tmp = PyObject_GetAttrString(obj, "values");
3861 if (tmp == NULL) goto failed;
3862 if (!PyList_Check(tmp)) {
3863 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3864 goto failed;
3865 }
3866 len = PyList_GET_SIZE(tmp);
3867 values = asdl_seq_new(len, arena);
3868 if (values == NULL) goto failed;
3869 for (i = 0; i < len; i++) {
3870 expr_ty value;
3871 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3872 if (res != 0) goto failed;
3873 asdl_seq_SET(values, i, value);
3874 }
3875 Py_XDECREF(tmp);
3876 tmp = NULL;
3877 } else {
3878 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3879 return 1;
3880 }
3881 if (PyObject_HasAttrString(obj, "nl")) {
3882 int res;
3883 tmp = PyObject_GetAttrString(obj, "nl");
3884 if (tmp == NULL) goto failed;
3885 res = obj2ast_bool(tmp, &nl, arena);
3886 if (res != 0) goto failed;
3887 Py_XDECREF(tmp);
3888 tmp = NULL;
3889 } else {
3890 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3891 return 1;
3892 }
3893 *out = Print(dest, values, nl, lineno, col_offset, arena);
3894 if (*out == NULL) goto failed;
3895 return 0;
3896 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003897 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3898 if (isinstance == -1) {
3899 return 1;
3900 }
3901 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003902 expr_ty target;
3903 expr_ty iter;
3904 asdl_seq* body;
3905 asdl_seq* orelse;
3906
3907 if (PyObject_HasAttrString(obj, "target")) {
3908 int res;
3909 tmp = PyObject_GetAttrString(obj, "target");
3910 if (tmp == NULL) goto failed;
3911 res = obj2ast_expr(tmp, &target, arena);
3912 if (res != 0) goto failed;
3913 Py_XDECREF(tmp);
3914 tmp = NULL;
3915 } else {
3916 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3917 return 1;
3918 }
3919 if (PyObject_HasAttrString(obj, "iter")) {
3920 int res;
3921 tmp = PyObject_GetAttrString(obj, "iter");
3922 if (tmp == NULL) goto failed;
3923 res = obj2ast_expr(tmp, &iter, arena);
3924 if (res != 0) goto failed;
3925 Py_XDECREF(tmp);
3926 tmp = NULL;
3927 } else {
3928 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3929 return 1;
3930 }
3931 if (PyObject_HasAttrString(obj, "body")) {
3932 int res;
3933 Py_ssize_t len;
3934 Py_ssize_t i;
3935 tmp = PyObject_GetAttrString(obj, "body");
3936 if (tmp == NULL) goto failed;
3937 if (!PyList_Check(tmp)) {
3938 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3939 goto failed;
3940 }
3941 len = PyList_GET_SIZE(tmp);
3942 body = asdl_seq_new(len, arena);
3943 if (body == NULL) goto failed;
3944 for (i = 0; i < len; i++) {
3945 stmt_ty value;
3946 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3947 if (res != 0) goto failed;
3948 asdl_seq_SET(body, i, value);
3949 }
3950 Py_XDECREF(tmp);
3951 tmp = NULL;
3952 } else {
3953 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3954 return 1;
3955 }
3956 if (PyObject_HasAttrString(obj, "orelse")) {
3957 int res;
3958 Py_ssize_t len;
3959 Py_ssize_t i;
3960 tmp = PyObject_GetAttrString(obj, "orelse");
3961 if (tmp == NULL) goto failed;
3962 if (!PyList_Check(tmp)) {
3963 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3964 goto failed;
3965 }
3966 len = PyList_GET_SIZE(tmp);
3967 orelse = asdl_seq_new(len, arena);
3968 if (orelse == NULL) goto failed;
3969 for (i = 0; i < len; i++) {
3970 stmt_ty value;
3971 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3972 if (res != 0) goto failed;
3973 asdl_seq_SET(orelse, i, value);
3974 }
3975 Py_XDECREF(tmp);
3976 tmp = NULL;
3977 } else {
3978 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3979 return 1;
3980 }
3981 *out = For(target, iter, body, orelse, lineno, col_offset,
3982 arena);
3983 if (*out == NULL) goto failed;
3984 return 0;
3985 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003986 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
3987 if (isinstance == -1) {
3988 return 1;
3989 }
3990 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003991 expr_ty test;
3992 asdl_seq* body;
3993 asdl_seq* orelse;
3994
3995 if (PyObject_HasAttrString(obj, "test")) {
3996 int res;
3997 tmp = PyObject_GetAttrString(obj, "test");
3998 if (tmp == NULL) goto failed;
3999 res = obj2ast_expr(tmp, &test, arena);
4000 if (res != 0) goto failed;
4001 Py_XDECREF(tmp);
4002 tmp = NULL;
4003 } else {
4004 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
4005 return 1;
4006 }
4007 if (PyObject_HasAttrString(obj, "body")) {
4008 int res;
4009 Py_ssize_t len;
4010 Py_ssize_t i;
4011 tmp = PyObject_GetAttrString(obj, "body");
4012 if (tmp == NULL) goto failed;
4013 if (!PyList_Check(tmp)) {
4014 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4015 goto failed;
4016 }
4017 len = PyList_GET_SIZE(tmp);
4018 body = asdl_seq_new(len, arena);
4019 if (body == NULL) goto failed;
4020 for (i = 0; i < len; i++) {
4021 stmt_ty value;
4022 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4023 if (res != 0) goto failed;
4024 asdl_seq_SET(body, i, value);
4025 }
4026 Py_XDECREF(tmp);
4027 tmp = NULL;
4028 } else {
4029 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
4030 return 1;
4031 }
4032 if (PyObject_HasAttrString(obj, "orelse")) {
4033 int res;
4034 Py_ssize_t len;
4035 Py_ssize_t i;
4036 tmp = PyObject_GetAttrString(obj, "orelse");
4037 if (tmp == NULL) goto failed;
4038 if (!PyList_Check(tmp)) {
4039 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4040 goto failed;
4041 }
4042 len = PyList_GET_SIZE(tmp);
4043 orelse = asdl_seq_new(len, arena);
4044 if (orelse == NULL) goto failed;
4045 for (i = 0; i < len; i++) {
4046 stmt_ty value;
4047 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4048 if (res != 0) goto failed;
4049 asdl_seq_SET(orelse, i, value);
4050 }
4051 Py_XDECREF(tmp);
4052 tmp = NULL;
4053 } else {
4054 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
4055 return 1;
4056 }
4057 *out = While(test, body, orelse, lineno, col_offset, arena);
4058 if (*out == NULL) goto failed;
4059 return 0;
4060 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004061 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
4062 if (isinstance == -1) {
4063 return 1;
4064 }
4065 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004066 expr_ty test;
4067 asdl_seq* body;
4068 asdl_seq* orelse;
4069
4070 if (PyObject_HasAttrString(obj, "test")) {
4071 int res;
4072 tmp = PyObject_GetAttrString(obj, "test");
4073 if (tmp == NULL) goto failed;
4074 res = obj2ast_expr(tmp, &test, arena);
4075 if (res != 0) goto failed;
4076 Py_XDECREF(tmp);
4077 tmp = NULL;
4078 } else {
4079 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
4080 return 1;
4081 }
4082 if (PyObject_HasAttrString(obj, "body")) {
4083 int res;
4084 Py_ssize_t len;
4085 Py_ssize_t i;
4086 tmp = PyObject_GetAttrString(obj, "body");
4087 if (tmp == NULL) goto failed;
4088 if (!PyList_Check(tmp)) {
4089 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4090 goto failed;
4091 }
4092 len = PyList_GET_SIZE(tmp);
4093 body = asdl_seq_new(len, arena);
4094 if (body == NULL) goto failed;
4095 for (i = 0; i < len; i++) {
4096 stmt_ty value;
4097 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4098 if (res != 0) goto failed;
4099 asdl_seq_SET(body, i, value);
4100 }
4101 Py_XDECREF(tmp);
4102 tmp = NULL;
4103 } else {
4104 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
4105 return 1;
4106 }
4107 if (PyObject_HasAttrString(obj, "orelse")) {
4108 int res;
4109 Py_ssize_t len;
4110 Py_ssize_t i;
4111 tmp = PyObject_GetAttrString(obj, "orelse");
4112 if (tmp == NULL) goto failed;
4113 if (!PyList_Check(tmp)) {
4114 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4115 goto failed;
4116 }
4117 len = PyList_GET_SIZE(tmp);
4118 orelse = asdl_seq_new(len, arena);
4119 if (orelse == NULL) goto failed;
4120 for (i = 0; i < len; i++) {
4121 stmt_ty value;
4122 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4123 if (res != 0) goto failed;
4124 asdl_seq_SET(orelse, i, value);
4125 }
4126 Py_XDECREF(tmp);
4127 tmp = NULL;
4128 } else {
4129 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4130 return 1;
4131 }
4132 *out = If(test, body, orelse, lineno, col_offset, arena);
4133 if (*out == NULL) goto failed;
4134 return 0;
4135 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004136 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4137 if (isinstance == -1) {
4138 return 1;
4139 }
4140 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004141 expr_ty context_expr;
4142 expr_ty optional_vars;
4143 asdl_seq* body;
4144
4145 if (PyObject_HasAttrString(obj, "context_expr")) {
4146 int res;
4147 tmp = PyObject_GetAttrString(obj, "context_expr");
4148 if (tmp == NULL) goto failed;
4149 res = obj2ast_expr(tmp, &context_expr, arena);
4150 if (res != 0) goto failed;
4151 Py_XDECREF(tmp);
4152 tmp = NULL;
4153 } else {
4154 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4155 return 1;
4156 }
4157 if (PyObject_HasAttrString(obj, "optional_vars")) {
4158 int res;
4159 tmp = PyObject_GetAttrString(obj, "optional_vars");
4160 if (tmp == NULL) goto failed;
4161 res = obj2ast_expr(tmp, &optional_vars, arena);
4162 if (res != 0) goto failed;
4163 Py_XDECREF(tmp);
4164 tmp = NULL;
4165 } else {
4166 optional_vars = NULL;
4167 }
4168 if (PyObject_HasAttrString(obj, "body")) {
4169 int res;
4170 Py_ssize_t len;
4171 Py_ssize_t i;
4172 tmp = PyObject_GetAttrString(obj, "body");
4173 if (tmp == NULL) goto failed;
4174 if (!PyList_Check(tmp)) {
4175 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4176 goto failed;
4177 }
4178 len = PyList_GET_SIZE(tmp);
4179 body = asdl_seq_new(len, arena);
4180 if (body == NULL) goto failed;
4181 for (i = 0; i < len; i++) {
4182 stmt_ty value;
4183 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4184 if (res != 0) goto failed;
4185 asdl_seq_SET(body, i, value);
4186 }
4187 Py_XDECREF(tmp);
4188 tmp = NULL;
4189 } else {
4190 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4191 return 1;
4192 }
4193 *out = With(context_expr, optional_vars, body, lineno,
4194 col_offset, arena);
4195 if (*out == NULL) goto failed;
4196 return 0;
4197 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004198 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4199 if (isinstance == -1) {
4200 return 1;
4201 }
4202 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004203 expr_ty type;
4204 expr_ty inst;
4205 expr_ty tback;
4206
4207 if (PyObject_HasAttrString(obj, "type")) {
4208 int res;
4209 tmp = PyObject_GetAttrString(obj, "type");
4210 if (tmp == NULL) goto failed;
4211 res = obj2ast_expr(tmp, &type, arena);
4212 if (res != 0) goto failed;
4213 Py_XDECREF(tmp);
4214 tmp = NULL;
4215 } else {
4216 type = NULL;
4217 }
4218 if (PyObject_HasAttrString(obj, "inst")) {
4219 int res;
4220 tmp = PyObject_GetAttrString(obj, "inst");
4221 if (tmp == NULL) goto failed;
4222 res = obj2ast_expr(tmp, &inst, arena);
4223 if (res != 0) goto failed;
4224 Py_XDECREF(tmp);
4225 tmp = NULL;
4226 } else {
4227 inst = NULL;
4228 }
4229 if (PyObject_HasAttrString(obj, "tback")) {
4230 int res;
4231 tmp = PyObject_GetAttrString(obj, "tback");
4232 if (tmp == NULL) goto failed;
4233 res = obj2ast_expr(tmp, &tback, arena);
4234 if (res != 0) goto failed;
4235 Py_XDECREF(tmp);
4236 tmp = NULL;
4237 } else {
4238 tback = NULL;
4239 }
4240 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4241 if (*out == NULL) goto failed;
4242 return 0;
4243 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004244 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4245 if (isinstance == -1) {
4246 return 1;
4247 }
4248 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004249 asdl_seq* body;
4250 asdl_seq* handlers;
4251 asdl_seq* orelse;
4252
4253 if (PyObject_HasAttrString(obj, "body")) {
4254 int res;
4255 Py_ssize_t len;
4256 Py_ssize_t i;
4257 tmp = PyObject_GetAttrString(obj, "body");
4258 if (tmp == NULL) goto failed;
4259 if (!PyList_Check(tmp)) {
4260 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4261 goto failed;
4262 }
4263 len = PyList_GET_SIZE(tmp);
4264 body = asdl_seq_new(len, arena);
4265 if (body == NULL) goto failed;
4266 for (i = 0; i < len; i++) {
4267 stmt_ty value;
4268 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4269 if (res != 0) goto failed;
4270 asdl_seq_SET(body, i, value);
4271 }
4272 Py_XDECREF(tmp);
4273 tmp = NULL;
4274 } else {
4275 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4276 return 1;
4277 }
4278 if (PyObject_HasAttrString(obj, "handlers")) {
4279 int res;
4280 Py_ssize_t len;
4281 Py_ssize_t i;
4282 tmp = PyObject_GetAttrString(obj, "handlers");
4283 if (tmp == NULL) goto failed;
4284 if (!PyList_Check(tmp)) {
4285 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4286 goto failed;
4287 }
4288 len = PyList_GET_SIZE(tmp);
4289 handlers = asdl_seq_new(len, arena);
4290 if (handlers == NULL) goto failed;
4291 for (i = 0; i < len; i++) {
4292 excepthandler_ty value;
4293 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4294 if (res != 0) goto failed;
4295 asdl_seq_SET(handlers, i, value);
4296 }
4297 Py_XDECREF(tmp);
4298 tmp = NULL;
4299 } else {
4300 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4301 return 1;
4302 }
4303 if (PyObject_HasAttrString(obj, "orelse")) {
4304 int res;
4305 Py_ssize_t len;
4306 Py_ssize_t i;
4307 tmp = PyObject_GetAttrString(obj, "orelse");
4308 if (tmp == NULL) goto failed;
4309 if (!PyList_Check(tmp)) {
4310 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4311 goto failed;
4312 }
4313 len = PyList_GET_SIZE(tmp);
4314 orelse = asdl_seq_new(len, arena);
4315 if (orelse == NULL) goto failed;
4316 for (i = 0; i < len; i++) {
4317 stmt_ty value;
4318 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4319 if (res != 0) goto failed;
4320 asdl_seq_SET(orelse, i, value);
4321 }
4322 Py_XDECREF(tmp);
4323 tmp = NULL;
4324 } else {
4325 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4326 return 1;
4327 }
4328 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4329 arena);
4330 if (*out == NULL) goto failed;
4331 return 0;
4332 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004333 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4334 if (isinstance == -1) {
4335 return 1;
4336 }
4337 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004338 asdl_seq* body;
4339 asdl_seq* finalbody;
4340
4341 if (PyObject_HasAttrString(obj, "body")) {
4342 int res;
4343 Py_ssize_t len;
4344 Py_ssize_t i;
4345 tmp = PyObject_GetAttrString(obj, "body");
4346 if (tmp == NULL) goto failed;
4347 if (!PyList_Check(tmp)) {
4348 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4349 goto failed;
4350 }
4351 len = PyList_GET_SIZE(tmp);
4352 body = asdl_seq_new(len, arena);
4353 if (body == NULL) goto failed;
4354 for (i = 0; i < len; i++) {
4355 stmt_ty value;
4356 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4357 if (res != 0) goto failed;
4358 asdl_seq_SET(body, i, value);
4359 }
4360 Py_XDECREF(tmp);
4361 tmp = NULL;
4362 } else {
4363 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4364 return 1;
4365 }
4366 if (PyObject_HasAttrString(obj, "finalbody")) {
4367 int res;
4368 Py_ssize_t len;
4369 Py_ssize_t i;
4370 tmp = PyObject_GetAttrString(obj, "finalbody");
4371 if (tmp == NULL) goto failed;
4372 if (!PyList_Check(tmp)) {
4373 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4374 goto failed;
4375 }
4376 len = PyList_GET_SIZE(tmp);
4377 finalbody = asdl_seq_new(len, arena);
4378 if (finalbody == NULL) goto failed;
4379 for (i = 0; i < len; i++) {
4380 stmt_ty value;
4381 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4382 if (res != 0) goto failed;
4383 asdl_seq_SET(finalbody, i, value);
4384 }
4385 Py_XDECREF(tmp);
4386 tmp = NULL;
4387 } else {
4388 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4389 return 1;
4390 }
4391 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4392 if (*out == NULL) goto failed;
4393 return 0;
4394 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004395 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4396 if (isinstance == -1) {
4397 return 1;
4398 }
4399 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004400 expr_ty test;
4401 expr_ty msg;
4402
4403 if (PyObject_HasAttrString(obj, "test")) {
4404 int res;
4405 tmp = PyObject_GetAttrString(obj, "test");
4406 if (tmp == NULL) goto failed;
4407 res = obj2ast_expr(tmp, &test, arena);
4408 if (res != 0) goto failed;
4409 Py_XDECREF(tmp);
4410 tmp = NULL;
4411 } else {
4412 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4413 return 1;
4414 }
4415 if (PyObject_HasAttrString(obj, "msg")) {
4416 int res;
4417 tmp = PyObject_GetAttrString(obj, "msg");
4418 if (tmp == NULL) goto failed;
4419 res = obj2ast_expr(tmp, &msg, arena);
4420 if (res != 0) goto failed;
4421 Py_XDECREF(tmp);
4422 tmp = NULL;
4423 } else {
4424 msg = NULL;
4425 }
4426 *out = Assert(test, msg, lineno, col_offset, arena);
4427 if (*out == NULL) goto failed;
4428 return 0;
4429 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004430 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4431 if (isinstance == -1) {
4432 return 1;
4433 }
4434 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004435 asdl_seq* names;
4436
4437 if (PyObject_HasAttrString(obj, "names")) {
4438 int res;
4439 Py_ssize_t len;
4440 Py_ssize_t i;
4441 tmp = PyObject_GetAttrString(obj, "names");
4442 if (tmp == NULL) goto failed;
4443 if (!PyList_Check(tmp)) {
4444 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4445 goto failed;
4446 }
4447 len = PyList_GET_SIZE(tmp);
4448 names = asdl_seq_new(len, arena);
4449 if (names == NULL) goto failed;
4450 for (i = 0; i < len; i++) {
4451 alias_ty value;
4452 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4453 if (res != 0) goto failed;
4454 asdl_seq_SET(names, i, value);
4455 }
4456 Py_XDECREF(tmp);
4457 tmp = NULL;
4458 } else {
4459 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4460 return 1;
4461 }
4462 *out = Import(names, lineno, col_offset, arena);
4463 if (*out == NULL) goto failed;
4464 return 0;
4465 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004466 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4467 if (isinstance == -1) {
4468 return 1;
4469 }
4470 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004471 identifier module;
4472 asdl_seq* names;
4473 int level;
4474
4475 if (PyObject_HasAttrString(obj, "module")) {
4476 int res;
4477 tmp = PyObject_GetAttrString(obj, "module");
4478 if (tmp == NULL) goto failed;
4479 res = obj2ast_identifier(tmp, &module, arena);
4480 if (res != 0) goto failed;
4481 Py_XDECREF(tmp);
4482 tmp = NULL;
4483 } else {
Benjamin Petersona72be3b2009-06-13 20:23:33 +00004484 module = NULL;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004485 }
4486 if (PyObject_HasAttrString(obj, "names")) {
4487 int res;
4488 Py_ssize_t len;
4489 Py_ssize_t i;
4490 tmp = PyObject_GetAttrString(obj, "names");
4491 if (tmp == NULL) goto failed;
4492 if (!PyList_Check(tmp)) {
4493 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4494 goto failed;
4495 }
4496 len = PyList_GET_SIZE(tmp);
4497 names = asdl_seq_new(len, arena);
4498 if (names == NULL) goto failed;
4499 for (i = 0; i < len; i++) {
4500 alias_ty value;
4501 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4502 if (res != 0) goto failed;
4503 asdl_seq_SET(names, i, value);
4504 }
4505 Py_XDECREF(tmp);
4506 tmp = NULL;
4507 } else {
4508 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4509 return 1;
4510 }
4511 if (PyObject_HasAttrString(obj, "level")) {
4512 int res;
4513 tmp = PyObject_GetAttrString(obj, "level");
4514 if (tmp == NULL) goto failed;
4515 res = obj2ast_int(tmp, &level, arena);
4516 if (res != 0) goto failed;
4517 Py_XDECREF(tmp);
4518 tmp = NULL;
4519 } else {
4520 level = 0;
4521 }
4522 *out = ImportFrom(module, names, level, lineno, col_offset,
4523 arena);
4524 if (*out == NULL) goto failed;
4525 return 0;
4526 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004527 isinstance = PyObject_IsInstance(obj, (PyObject*)Exec_type);
4528 if (isinstance == -1) {
4529 return 1;
4530 }
4531 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004532 expr_ty body;
4533 expr_ty globals;
4534 expr_ty locals;
4535
4536 if (PyObject_HasAttrString(obj, "body")) {
4537 int res;
4538 tmp = PyObject_GetAttrString(obj, "body");
4539 if (tmp == NULL) goto failed;
4540 res = obj2ast_expr(tmp, &body, arena);
4541 if (res != 0) goto failed;
4542 Py_XDECREF(tmp);
4543 tmp = NULL;
4544 } else {
4545 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4546 return 1;
4547 }
4548 if (PyObject_HasAttrString(obj, "globals")) {
4549 int res;
4550 tmp = PyObject_GetAttrString(obj, "globals");
4551 if (tmp == NULL) goto failed;
4552 res = obj2ast_expr(tmp, &globals, arena);
4553 if (res != 0) goto failed;
4554 Py_XDECREF(tmp);
4555 tmp = NULL;
4556 } else {
4557 globals = NULL;
4558 }
4559 if (PyObject_HasAttrString(obj, "locals")) {
4560 int res;
4561 tmp = PyObject_GetAttrString(obj, "locals");
4562 if (tmp == NULL) goto failed;
4563 res = obj2ast_expr(tmp, &locals, arena);
4564 if (res != 0) goto failed;
4565 Py_XDECREF(tmp);
4566 tmp = NULL;
4567 } else {
4568 locals = NULL;
4569 }
4570 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4571 if (*out == NULL) goto failed;
4572 return 0;
4573 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004574 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4575 if (isinstance == -1) {
4576 return 1;
4577 }
4578 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004579 asdl_seq* names;
4580
4581 if (PyObject_HasAttrString(obj, "names")) {
4582 int res;
4583 Py_ssize_t len;
4584 Py_ssize_t i;
4585 tmp = PyObject_GetAttrString(obj, "names");
4586 if (tmp == NULL) goto failed;
4587 if (!PyList_Check(tmp)) {
4588 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4589 goto failed;
4590 }
4591 len = PyList_GET_SIZE(tmp);
4592 names = asdl_seq_new(len, arena);
4593 if (names == NULL) goto failed;
4594 for (i = 0; i < len; i++) {
4595 identifier value;
4596 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4597 if (res != 0) goto failed;
4598 asdl_seq_SET(names, i, value);
4599 }
4600 Py_XDECREF(tmp);
4601 tmp = NULL;
4602 } else {
4603 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4604 return 1;
4605 }
4606 *out = Global(names, lineno, col_offset, arena);
4607 if (*out == NULL) goto failed;
4608 return 0;
4609 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004610 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4611 if (isinstance == -1) {
4612 return 1;
4613 }
4614 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004615 expr_ty value;
4616
4617 if (PyObject_HasAttrString(obj, "value")) {
4618 int res;
4619 tmp = PyObject_GetAttrString(obj, "value");
4620 if (tmp == NULL) goto failed;
4621 res = obj2ast_expr(tmp, &value, arena);
4622 if (res != 0) goto failed;
4623 Py_XDECREF(tmp);
4624 tmp = NULL;
4625 } else {
4626 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4627 return 1;
4628 }
4629 *out = Expr(value, lineno, col_offset, arena);
4630 if (*out == NULL) goto failed;
4631 return 0;
4632 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004633 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4634 if (isinstance == -1) {
4635 return 1;
4636 }
4637 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004638
4639 *out = Pass(lineno, col_offset, arena);
4640 if (*out == NULL) goto failed;
4641 return 0;
4642 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004643 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4644 if (isinstance == -1) {
4645 return 1;
4646 }
4647 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004648
4649 *out = Break(lineno, col_offset, arena);
4650 if (*out == NULL) goto failed;
4651 return 0;
4652 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004653 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4654 if (isinstance == -1) {
4655 return 1;
4656 }
4657 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004658
4659 *out = Continue(lineno, col_offset, arena);
4660 if (*out == NULL) goto failed;
4661 return 0;
4662 }
4663
4664 tmp = PyObject_Repr(obj);
4665 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004666 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00004667failed:
4668 Py_XDECREF(tmp);
4669 return 1;
4670}
4671
4672int
4673obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4674{
4675 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004676 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004677
4678 int lineno;
4679 int col_offset;
4680
4681 if (obj == Py_None) {
4682 *out = NULL;
4683 return 0;
4684 }
4685 if (PyObject_HasAttrString(obj, "lineno")) {
4686 int res;
4687 tmp = PyObject_GetAttrString(obj, "lineno");
4688 if (tmp == NULL) goto failed;
4689 res = obj2ast_int(tmp, &lineno, arena);
4690 if (res != 0) goto failed;
4691 Py_XDECREF(tmp);
4692 tmp = NULL;
4693 } else {
4694 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4695 return 1;
4696 }
4697 if (PyObject_HasAttrString(obj, "col_offset")) {
4698 int res;
4699 tmp = PyObject_GetAttrString(obj, "col_offset");
4700 if (tmp == NULL) goto failed;
4701 res = obj2ast_int(tmp, &col_offset, arena);
4702 if (res != 0) goto failed;
4703 Py_XDECREF(tmp);
4704 tmp = NULL;
4705 } else {
4706 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4707 return 1;
4708 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004709 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4710 if (isinstance == -1) {
4711 return 1;
4712 }
4713 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004714 boolop_ty op;
4715 asdl_seq* values;
4716
4717 if (PyObject_HasAttrString(obj, "op")) {
4718 int res;
4719 tmp = PyObject_GetAttrString(obj, "op");
4720 if (tmp == NULL) goto failed;
4721 res = obj2ast_boolop(tmp, &op, arena);
4722 if (res != 0) goto failed;
4723 Py_XDECREF(tmp);
4724 tmp = NULL;
4725 } else {
4726 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4727 return 1;
4728 }
4729 if (PyObject_HasAttrString(obj, "values")) {
4730 int res;
4731 Py_ssize_t len;
4732 Py_ssize_t i;
4733 tmp = PyObject_GetAttrString(obj, "values");
4734 if (tmp == NULL) goto failed;
4735 if (!PyList_Check(tmp)) {
4736 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4737 goto failed;
4738 }
4739 len = PyList_GET_SIZE(tmp);
4740 values = asdl_seq_new(len, arena);
4741 if (values == NULL) goto failed;
4742 for (i = 0; i < len; i++) {
4743 expr_ty value;
4744 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4745 if (res != 0) goto failed;
4746 asdl_seq_SET(values, i, value);
4747 }
4748 Py_XDECREF(tmp);
4749 tmp = NULL;
4750 } else {
4751 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4752 return 1;
4753 }
4754 *out = BoolOp(op, values, lineno, col_offset, arena);
4755 if (*out == NULL) goto failed;
4756 return 0;
4757 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004758 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4759 if (isinstance == -1) {
4760 return 1;
4761 }
4762 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004763 expr_ty left;
4764 operator_ty op;
4765 expr_ty right;
4766
4767 if (PyObject_HasAttrString(obj, "left")) {
4768 int res;
4769 tmp = PyObject_GetAttrString(obj, "left");
4770 if (tmp == NULL) goto failed;
4771 res = obj2ast_expr(tmp, &left, arena);
4772 if (res != 0) goto failed;
4773 Py_XDECREF(tmp);
4774 tmp = NULL;
4775 } else {
4776 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4777 return 1;
4778 }
4779 if (PyObject_HasAttrString(obj, "op")) {
4780 int res;
4781 tmp = PyObject_GetAttrString(obj, "op");
4782 if (tmp == NULL) goto failed;
4783 res = obj2ast_operator(tmp, &op, arena);
4784 if (res != 0) goto failed;
4785 Py_XDECREF(tmp);
4786 tmp = NULL;
4787 } else {
4788 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4789 return 1;
4790 }
4791 if (PyObject_HasAttrString(obj, "right")) {
4792 int res;
4793 tmp = PyObject_GetAttrString(obj, "right");
4794 if (tmp == NULL) goto failed;
4795 res = obj2ast_expr(tmp, &right, arena);
4796 if (res != 0) goto failed;
4797 Py_XDECREF(tmp);
4798 tmp = NULL;
4799 } else {
4800 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4801 return 1;
4802 }
4803 *out = BinOp(left, op, right, lineno, col_offset, arena);
4804 if (*out == NULL) goto failed;
4805 return 0;
4806 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004807 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4808 if (isinstance == -1) {
4809 return 1;
4810 }
4811 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004812 unaryop_ty op;
4813 expr_ty operand;
4814
4815 if (PyObject_HasAttrString(obj, "op")) {
4816 int res;
4817 tmp = PyObject_GetAttrString(obj, "op");
4818 if (tmp == NULL) goto failed;
4819 res = obj2ast_unaryop(tmp, &op, arena);
4820 if (res != 0) goto failed;
4821 Py_XDECREF(tmp);
4822 tmp = NULL;
4823 } else {
4824 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4825 return 1;
4826 }
4827 if (PyObject_HasAttrString(obj, "operand")) {
4828 int res;
4829 tmp = PyObject_GetAttrString(obj, "operand");
4830 if (tmp == NULL) goto failed;
4831 res = obj2ast_expr(tmp, &operand, arena);
4832 if (res != 0) goto failed;
4833 Py_XDECREF(tmp);
4834 tmp = NULL;
4835 } else {
4836 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4837 return 1;
4838 }
4839 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4840 if (*out == NULL) goto failed;
4841 return 0;
4842 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004843 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4844 if (isinstance == -1) {
4845 return 1;
4846 }
4847 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004848 arguments_ty args;
4849 expr_ty body;
4850
4851 if (PyObject_HasAttrString(obj, "args")) {
4852 int res;
4853 tmp = PyObject_GetAttrString(obj, "args");
4854 if (tmp == NULL) goto failed;
4855 res = obj2ast_arguments(tmp, &args, arena);
4856 if (res != 0) goto failed;
4857 Py_XDECREF(tmp);
4858 tmp = NULL;
4859 } else {
4860 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4861 return 1;
4862 }
4863 if (PyObject_HasAttrString(obj, "body")) {
4864 int res;
4865 tmp = PyObject_GetAttrString(obj, "body");
4866 if (tmp == NULL) goto failed;
4867 res = obj2ast_expr(tmp, &body, arena);
4868 if (res != 0) goto failed;
4869 Py_XDECREF(tmp);
4870 tmp = NULL;
4871 } else {
4872 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4873 return 1;
4874 }
4875 *out = Lambda(args, body, lineno, col_offset, arena);
4876 if (*out == NULL) goto failed;
4877 return 0;
4878 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004879 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4880 if (isinstance == -1) {
4881 return 1;
4882 }
4883 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004884 expr_ty test;
4885 expr_ty body;
4886 expr_ty orelse;
4887
4888 if (PyObject_HasAttrString(obj, "test")) {
4889 int res;
4890 tmp = PyObject_GetAttrString(obj, "test");
4891 if (tmp == NULL) goto failed;
4892 res = obj2ast_expr(tmp, &test, arena);
4893 if (res != 0) goto failed;
4894 Py_XDECREF(tmp);
4895 tmp = NULL;
4896 } else {
4897 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4898 return 1;
4899 }
4900 if (PyObject_HasAttrString(obj, "body")) {
4901 int res;
4902 tmp = PyObject_GetAttrString(obj, "body");
4903 if (tmp == NULL) goto failed;
4904 res = obj2ast_expr(tmp, &body, arena);
4905 if (res != 0) goto failed;
4906 Py_XDECREF(tmp);
4907 tmp = NULL;
4908 } else {
4909 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4910 return 1;
4911 }
4912 if (PyObject_HasAttrString(obj, "orelse")) {
4913 int res;
4914 tmp = PyObject_GetAttrString(obj, "orelse");
4915 if (tmp == NULL) goto failed;
4916 res = obj2ast_expr(tmp, &orelse, arena);
4917 if (res != 0) goto failed;
4918 Py_XDECREF(tmp);
4919 tmp = NULL;
4920 } else {
4921 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4922 return 1;
4923 }
4924 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4925 if (*out == NULL) goto failed;
4926 return 0;
4927 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004928 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4929 if (isinstance == -1) {
4930 return 1;
4931 }
4932 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004933 asdl_seq* keys;
4934 asdl_seq* values;
4935
4936 if (PyObject_HasAttrString(obj, "keys")) {
4937 int res;
4938 Py_ssize_t len;
4939 Py_ssize_t i;
4940 tmp = PyObject_GetAttrString(obj, "keys");
4941 if (tmp == NULL) goto failed;
4942 if (!PyList_Check(tmp)) {
4943 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4944 goto failed;
4945 }
4946 len = PyList_GET_SIZE(tmp);
4947 keys = asdl_seq_new(len, arena);
4948 if (keys == NULL) goto failed;
4949 for (i = 0; i < len; i++) {
4950 expr_ty value;
4951 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4952 if (res != 0) goto failed;
4953 asdl_seq_SET(keys, i, value);
4954 }
4955 Py_XDECREF(tmp);
4956 tmp = NULL;
4957 } else {
4958 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4959 return 1;
4960 }
4961 if (PyObject_HasAttrString(obj, "values")) {
4962 int res;
4963 Py_ssize_t len;
4964 Py_ssize_t i;
4965 tmp = PyObject_GetAttrString(obj, "values");
4966 if (tmp == NULL) goto failed;
4967 if (!PyList_Check(tmp)) {
4968 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4969 goto failed;
4970 }
4971 len = PyList_GET_SIZE(tmp);
4972 values = asdl_seq_new(len, arena);
4973 if (values == NULL) goto failed;
4974 for (i = 0; i < len; i++) {
4975 expr_ty value;
4976 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4977 if (res != 0) goto failed;
4978 asdl_seq_SET(values, i, value);
4979 }
4980 Py_XDECREF(tmp);
4981 tmp = NULL;
4982 } else {
4983 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4984 return 1;
4985 }
4986 *out = Dict(keys, values, lineno, col_offset, arena);
4987 if (*out == NULL) goto failed;
4988 return 0;
4989 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00004990 isinstance = PyObject_IsInstance(obj, (PyObject*)Set_type);
4991 if (isinstance == -1) {
4992 return 1;
4993 }
4994 if (isinstance) {
4995 asdl_seq* elts;
4996
4997 if (PyObject_HasAttrString(obj, "elts")) {
4998 int res;
4999 Py_ssize_t len;
5000 Py_ssize_t i;
5001 tmp = PyObject_GetAttrString(obj, "elts");
5002 if (tmp == NULL) goto failed;
5003 if (!PyList_Check(tmp)) {
5004 PyErr_Format(PyExc_TypeError, "Set field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5005 goto failed;
5006 }
5007 len = PyList_GET_SIZE(tmp);
5008 elts = asdl_seq_new(len, arena);
5009 if (elts == NULL) goto failed;
5010 for (i = 0; i < len; i++) {
5011 expr_ty value;
5012 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5013 if (res != 0) goto failed;
5014 asdl_seq_SET(elts, i, value);
5015 }
5016 Py_XDECREF(tmp);
5017 tmp = NULL;
5018 } else {
5019 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Set");
5020 return 1;
5021 }
5022 *out = Set(elts, lineno, col_offset, arena);
5023 if (*out == NULL) goto failed;
5024 return 0;
5025 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005026 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
5027 if (isinstance == -1) {
5028 return 1;
5029 }
5030 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005031 expr_ty elt;
5032 asdl_seq* generators;
5033
5034 if (PyObject_HasAttrString(obj, "elt")) {
5035 int res;
5036 tmp = PyObject_GetAttrString(obj, "elt");
5037 if (tmp == NULL) goto failed;
5038 res = obj2ast_expr(tmp, &elt, arena);
5039 if (res != 0) goto failed;
5040 Py_XDECREF(tmp);
5041 tmp = NULL;
5042 } else {
5043 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
5044 return 1;
5045 }
5046 if (PyObject_HasAttrString(obj, "generators")) {
5047 int res;
5048 Py_ssize_t len;
5049 Py_ssize_t i;
5050 tmp = PyObject_GetAttrString(obj, "generators");
5051 if (tmp == NULL) goto failed;
5052 if (!PyList_Check(tmp)) {
5053 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5054 goto failed;
5055 }
5056 len = PyList_GET_SIZE(tmp);
5057 generators = asdl_seq_new(len, arena);
5058 if (generators == NULL) goto failed;
5059 for (i = 0; i < len; i++) {
5060 comprehension_ty value;
5061 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5062 if (res != 0) goto failed;
5063 asdl_seq_SET(generators, i, value);
5064 }
5065 Py_XDECREF(tmp);
5066 tmp = NULL;
5067 } else {
5068 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
5069 return 1;
5070 }
5071 *out = ListComp(elt, generators, lineno, col_offset, arena);
5072 if (*out == NULL) goto failed;
5073 return 0;
5074 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00005075 isinstance = PyObject_IsInstance(obj, (PyObject*)SetComp_type);
5076 if (isinstance == -1) {
5077 return 1;
5078 }
5079 if (isinstance) {
5080 expr_ty elt;
5081 asdl_seq* generators;
5082
5083 if (PyObject_HasAttrString(obj, "elt")) {
5084 int res;
5085 tmp = PyObject_GetAttrString(obj, "elt");
5086 if (tmp == NULL) goto failed;
5087 res = obj2ast_expr(tmp, &elt, arena);
5088 if (res != 0) goto failed;
5089 Py_XDECREF(tmp);
5090 tmp = NULL;
5091 } else {
5092 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from SetComp");
5093 return 1;
5094 }
5095 if (PyObject_HasAttrString(obj, "generators")) {
5096 int res;
5097 Py_ssize_t len;
5098 Py_ssize_t i;
5099 tmp = PyObject_GetAttrString(obj, "generators");
5100 if (tmp == NULL) goto failed;
5101 if (!PyList_Check(tmp)) {
5102 PyErr_Format(PyExc_TypeError, "SetComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5103 goto failed;
5104 }
5105 len = PyList_GET_SIZE(tmp);
5106 generators = asdl_seq_new(len, arena);
5107 if (generators == NULL) goto failed;
5108 for (i = 0; i < len; i++) {
5109 comprehension_ty value;
5110 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5111 if (res != 0) goto failed;
5112 asdl_seq_SET(generators, i, value);
5113 }
5114 Py_XDECREF(tmp);
5115 tmp = NULL;
5116 } else {
5117 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from SetComp");
5118 return 1;
5119 }
5120 *out = SetComp(elt, generators, lineno, col_offset, arena);
5121 if (*out == NULL) goto failed;
5122 return 0;
5123 }
5124 isinstance = PyObject_IsInstance(obj, (PyObject*)DictComp_type);
5125 if (isinstance == -1) {
5126 return 1;
5127 }
5128 if (isinstance) {
5129 expr_ty key;
5130 expr_ty value;
5131 asdl_seq* generators;
5132
5133 if (PyObject_HasAttrString(obj, "key")) {
5134 int res;
5135 tmp = PyObject_GetAttrString(obj, "key");
5136 if (tmp == NULL) goto failed;
5137 res = obj2ast_expr(tmp, &key, arena);
5138 if (res != 0) goto failed;
5139 Py_XDECREF(tmp);
5140 tmp = NULL;
5141 } else {
5142 PyErr_SetString(PyExc_TypeError, "required field \"key\" missing from DictComp");
5143 return 1;
5144 }
5145 if (PyObject_HasAttrString(obj, "value")) {
5146 int res;
5147 tmp = PyObject_GetAttrString(obj, "value");
5148 if (tmp == NULL) goto failed;
5149 res = obj2ast_expr(tmp, &value, arena);
5150 if (res != 0) goto failed;
5151 Py_XDECREF(tmp);
5152 tmp = NULL;
5153 } else {
5154 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from DictComp");
5155 return 1;
5156 }
5157 if (PyObject_HasAttrString(obj, "generators")) {
5158 int res;
5159 Py_ssize_t len;
5160 Py_ssize_t i;
5161 tmp = PyObject_GetAttrString(obj, "generators");
5162 if (tmp == NULL) goto failed;
5163 if (!PyList_Check(tmp)) {
5164 PyErr_Format(PyExc_TypeError, "DictComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5165 goto failed;
5166 }
5167 len = PyList_GET_SIZE(tmp);
5168 generators = asdl_seq_new(len, arena);
5169 if (generators == NULL) goto failed;
5170 for (i = 0; i < len; i++) {
5171 comprehension_ty value;
5172 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5173 if (res != 0) goto failed;
5174 asdl_seq_SET(generators, i, value);
5175 }
5176 Py_XDECREF(tmp);
5177 tmp = NULL;
5178 } else {
5179 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from DictComp");
5180 return 1;
5181 }
5182 *out = DictComp(key, value, generators, lineno, col_offset,
5183 arena);
5184 if (*out == NULL) goto failed;
5185 return 0;
5186 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005187 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
5188 if (isinstance == -1) {
5189 return 1;
5190 }
5191 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005192 expr_ty elt;
5193 asdl_seq* generators;
5194
5195 if (PyObject_HasAttrString(obj, "elt")) {
5196 int res;
5197 tmp = PyObject_GetAttrString(obj, "elt");
5198 if (tmp == NULL) goto failed;
5199 res = obj2ast_expr(tmp, &elt, arena);
5200 if (res != 0) goto failed;
5201 Py_XDECREF(tmp);
5202 tmp = NULL;
5203 } else {
5204 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
5205 return 1;
5206 }
5207 if (PyObject_HasAttrString(obj, "generators")) {
5208 int res;
5209 Py_ssize_t len;
5210 Py_ssize_t i;
5211 tmp = PyObject_GetAttrString(obj, "generators");
5212 if (tmp == NULL) goto failed;
5213 if (!PyList_Check(tmp)) {
5214 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5215 goto failed;
5216 }
5217 len = PyList_GET_SIZE(tmp);
5218 generators = asdl_seq_new(len, arena);
5219 if (generators == NULL) goto failed;
5220 for (i = 0; i < len; i++) {
5221 comprehension_ty value;
5222 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
5223 if (res != 0) goto failed;
5224 asdl_seq_SET(generators, i, value);
5225 }
5226 Py_XDECREF(tmp);
5227 tmp = NULL;
5228 } else {
5229 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
5230 return 1;
5231 }
5232 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
5233 if (*out == NULL) goto failed;
5234 return 0;
5235 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005236 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
5237 if (isinstance == -1) {
5238 return 1;
5239 }
5240 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005241 expr_ty value;
5242
5243 if (PyObject_HasAttrString(obj, "value")) {
5244 int res;
5245 tmp = PyObject_GetAttrString(obj, "value");
5246 if (tmp == NULL) goto failed;
5247 res = obj2ast_expr(tmp, &value, arena);
5248 if (res != 0) goto failed;
5249 Py_XDECREF(tmp);
5250 tmp = NULL;
5251 } else {
5252 value = NULL;
5253 }
5254 *out = Yield(value, lineno, col_offset, arena);
5255 if (*out == NULL) goto failed;
5256 return 0;
5257 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005258 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
5259 if (isinstance == -1) {
5260 return 1;
5261 }
5262 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005263 expr_ty left;
5264 asdl_int_seq* ops;
5265 asdl_seq* comparators;
5266
5267 if (PyObject_HasAttrString(obj, "left")) {
5268 int res;
5269 tmp = PyObject_GetAttrString(obj, "left");
5270 if (tmp == NULL) goto failed;
5271 res = obj2ast_expr(tmp, &left, arena);
5272 if (res != 0) goto failed;
5273 Py_XDECREF(tmp);
5274 tmp = NULL;
5275 } else {
5276 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5277 return 1;
5278 }
5279 if (PyObject_HasAttrString(obj, "ops")) {
5280 int res;
5281 Py_ssize_t len;
5282 Py_ssize_t i;
5283 tmp = PyObject_GetAttrString(obj, "ops");
5284 if (tmp == NULL) goto failed;
5285 if (!PyList_Check(tmp)) {
5286 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5287 goto failed;
5288 }
5289 len = PyList_GET_SIZE(tmp);
5290 ops = asdl_int_seq_new(len, arena);
5291 if (ops == NULL) goto failed;
5292 for (i = 0; i < len; i++) {
5293 cmpop_ty value;
5294 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5295 if (res != 0) goto failed;
5296 asdl_seq_SET(ops, i, value);
5297 }
5298 Py_XDECREF(tmp);
5299 tmp = NULL;
5300 } else {
5301 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5302 return 1;
5303 }
5304 if (PyObject_HasAttrString(obj, "comparators")) {
5305 int res;
5306 Py_ssize_t len;
5307 Py_ssize_t i;
5308 tmp = PyObject_GetAttrString(obj, "comparators");
5309 if (tmp == NULL) goto failed;
5310 if (!PyList_Check(tmp)) {
5311 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5312 goto failed;
5313 }
5314 len = PyList_GET_SIZE(tmp);
5315 comparators = asdl_seq_new(len, arena);
5316 if (comparators == NULL) goto failed;
5317 for (i = 0; i < len; i++) {
5318 expr_ty value;
5319 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5320 if (res != 0) goto failed;
5321 asdl_seq_SET(comparators, i, value);
5322 }
5323 Py_XDECREF(tmp);
5324 tmp = NULL;
5325 } else {
5326 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5327 return 1;
5328 }
5329 *out = Compare(left, ops, comparators, lineno, col_offset,
5330 arena);
5331 if (*out == NULL) goto failed;
5332 return 0;
5333 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005334 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5335 if (isinstance == -1) {
5336 return 1;
5337 }
5338 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005339 expr_ty func;
5340 asdl_seq* args;
5341 asdl_seq* keywords;
5342 expr_ty starargs;
5343 expr_ty kwargs;
5344
5345 if (PyObject_HasAttrString(obj, "func")) {
5346 int res;
5347 tmp = PyObject_GetAttrString(obj, "func");
5348 if (tmp == NULL) goto failed;
5349 res = obj2ast_expr(tmp, &func, arena);
5350 if (res != 0) goto failed;
5351 Py_XDECREF(tmp);
5352 tmp = NULL;
5353 } else {
5354 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5355 return 1;
5356 }
5357 if (PyObject_HasAttrString(obj, "args")) {
5358 int res;
5359 Py_ssize_t len;
5360 Py_ssize_t i;
5361 tmp = PyObject_GetAttrString(obj, "args");
5362 if (tmp == NULL) goto failed;
5363 if (!PyList_Check(tmp)) {
5364 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5365 goto failed;
5366 }
5367 len = PyList_GET_SIZE(tmp);
5368 args = asdl_seq_new(len, arena);
5369 if (args == NULL) goto failed;
5370 for (i = 0; i < len; i++) {
5371 expr_ty value;
5372 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5373 if (res != 0) goto failed;
5374 asdl_seq_SET(args, i, value);
5375 }
5376 Py_XDECREF(tmp);
5377 tmp = NULL;
5378 } else {
5379 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5380 return 1;
5381 }
5382 if (PyObject_HasAttrString(obj, "keywords")) {
5383 int res;
5384 Py_ssize_t len;
5385 Py_ssize_t i;
5386 tmp = PyObject_GetAttrString(obj, "keywords");
5387 if (tmp == NULL) goto failed;
5388 if (!PyList_Check(tmp)) {
5389 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5390 goto failed;
5391 }
5392 len = PyList_GET_SIZE(tmp);
5393 keywords = asdl_seq_new(len, arena);
5394 if (keywords == NULL) goto failed;
5395 for (i = 0; i < len; i++) {
5396 keyword_ty value;
5397 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5398 if (res != 0) goto failed;
5399 asdl_seq_SET(keywords, i, value);
5400 }
5401 Py_XDECREF(tmp);
5402 tmp = NULL;
5403 } else {
5404 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5405 return 1;
5406 }
5407 if (PyObject_HasAttrString(obj, "starargs")) {
5408 int res;
5409 tmp = PyObject_GetAttrString(obj, "starargs");
5410 if (tmp == NULL) goto failed;
5411 res = obj2ast_expr(tmp, &starargs, arena);
5412 if (res != 0) goto failed;
5413 Py_XDECREF(tmp);
5414 tmp = NULL;
5415 } else {
5416 starargs = NULL;
5417 }
5418 if (PyObject_HasAttrString(obj, "kwargs")) {
5419 int res;
5420 tmp = PyObject_GetAttrString(obj, "kwargs");
5421 if (tmp == NULL) goto failed;
5422 res = obj2ast_expr(tmp, &kwargs, arena);
5423 if (res != 0) goto failed;
5424 Py_XDECREF(tmp);
5425 tmp = NULL;
5426 } else {
5427 kwargs = NULL;
5428 }
5429 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5430 col_offset, arena);
5431 if (*out == NULL) goto failed;
5432 return 0;
5433 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005434 isinstance = PyObject_IsInstance(obj, (PyObject*)Repr_type);
5435 if (isinstance == -1) {
5436 return 1;
5437 }
5438 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005439 expr_ty value;
5440
5441 if (PyObject_HasAttrString(obj, "value")) {
5442 int res;
5443 tmp = PyObject_GetAttrString(obj, "value");
5444 if (tmp == NULL) goto failed;
5445 res = obj2ast_expr(tmp, &value, arena);
5446 if (res != 0) goto failed;
5447 Py_XDECREF(tmp);
5448 tmp = NULL;
5449 } else {
5450 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5451 return 1;
5452 }
5453 *out = Repr(value, lineno, col_offset, arena);
5454 if (*out == NULL) goto failed;
5455 return 0;
5456 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005457 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5458 if (isinstance == -1) {
5459 return 1;
5460 }
5461 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005462 object n;
5463
5464 if (PyObject_HasAttrString(obj, "n")) {
5465 int res;
5466 tmp = PyObject_GetAttrString(obj, "n");
5467 if (tmp == NULL) goto failed;
5468 res = obj2ast_object(tmp, &n, arena);
5469 if (res != 0) goto failed;
5470 Py_XDECREF(tmp);
5471 tmp = NULL;
5472 } else {
5473 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5474 return 1;
5475 }
5476 *out = Num(n, lineno, col_offset, arena);
5477 if (*out == NULL) goto failed;
5478 return 0;
5479 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005480 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5481 if (isinstance == -1) {
5482 return 1;
5483 }
5484 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005485 string s;
5486
5487 if (PyObject_HasAttrString(obj, "s")) {
5488 int res;
5489 tmp = PyObject_GetAttrString(obj, "s");
5490 if (tmp == NULL) goto failed;
5491 res = obj2ast_string(tmp, &s, arena);
5492 if (res != 0) goto failed;
5493 Py_XDECREF(tmp);
5494 tmp = NULL;
5495 } else {
5496 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5497 return 1;
5498 }
5499 *out = Str(s, lineno, col_offset, arena);
5500 if (*out == NULL) goto failed;
5501 return 0;
5502 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005503 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5504 if (isinstance == -1) {
5505 return 1;
5506 }
5507 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005508 expr_ty value;
5509 identifier attr;
5510 expr_context_ty ctx;
5511
5512 if (PyObject_HasAttrString(obj, "value")) {
5513 int res;
5514 tmp = PyObject_GetAttrString(obj, "value");
5515 if (tmp == NULL) goto failed;
5516 res = obj2ast_expr(tmp, &value, arena);
5517 if (res != 0) goto failed;
5518 Py_XDECREF(tmp);
5519 tmp = NULL;
5520 } else {
5521 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5522 return 1;
5523 }
5524 if (PyObject_HasAttrString(obj, "attr")) {
5525 int res;
5526 tmp = PyObject_GetAttrString(obj, "attr");
5527 if (tmp == NULL) goto failed;
5528 res = obj2ast_identifier(tmp, &attr, arena);
5529 if (res != 0) goto failed;
5530 Py_XDECREF(tmp);
5531 tmp = NULL;
5532 } else {
5533 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5534 return 1;
5535 }
5536 if (PyObject_HasAttrString(obj, "ctx")) {
5537 int res;
5538 tmp = PyObject_GetAttrString(obj, "ctx");
5539 if (tmp == NULL) goto failed;
5540 res = obj2ast_expr_context(tmp, &ctx, arena);
5541 if (res != 0) goto failed;
5542 Py_XDECREF(tmp);
5543 tmp = NULL;
5544 } else {
5545 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5546 return 1;
5547 }
5548 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5549 if (*out == NULL) goto failed;
5550 return 0;
5551 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005552 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5553 if (isinstance == -1) {
5554 return 1;
5555 }
5556 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005557 expr_ty value;
5558 slice_ty slice;
5559 expr_context_ty ctx;
5560
5561 if (PyObject_HasAttrString(obj, "value")) {
5562 int res;
5563 tmp = PyObject_GetAttrString(obj, "value");
5564 if (tmp == NULL) goto failed;
5565 res = obj2ast_expr(tmp, &value, arena);
5566 if (res != 0) goto failed;
5567 Py_XDECREF(tmp);
5568 tmp = NULL;
5569 } else {
5570 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5571 return 1;
5572 }
5573 if (PyObject_HasAttrString(obj, "slice")) {
5574 int res;
5575 tmp = PyObject_GetAttrString(obj, "slice");
5576 if (tmp == NULL) goto failed;
5577 res = obj2ast_slice(tmp, &slice, arena);
5578 if (res != 0) goto failed;
5579 Py_XDECREF(tmp);
5580 tmp = NULL;
5581 } else {
5582 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5583 return 1;
5584 }
5585 if (PyObject_HasAttrString(obj, "ctx")) {
5586 int res;
5587 tmp = PyObject_GetAttrString(obj, "ctx");
5588 if (tmp == NULL) goto failed;
5589 res = obj2ast_expr_context(tmp, &ctx, arena);
5590 if (res != 0) goto failed;
5591 Py_XDECREF(tmp);
5592 tmp = NULL;
5593 } else {
5594 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5595 return 1;
5596 }
5597 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5598 if (*out == NULL) goto failed;
5599 return 0;
5600 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005601 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5602 if (isinstance == -1) {
5603 return 1;
5604 }
5605 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005606 identifier id;
5607 expr_context_ty ctx;
5608
5609 if (PyObject_HasAttrString(obj, "id")) {
5610 int res;
5611 tmp = PyObject_GetAttrString(obj, "id");
5612 if (tmp == NULL) goto failed;
5613 res = obj2ast_identifier(tmp, &id, arena);
5614 if (res != 0) goto failed;
5615 Py_XDECREF(tmp);
5616 tmp = NULL;
5617 } else {
5618 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5619 return 1;
5620 }
5621 if (PyObject_HasAttrString(obj, "ctx")) {
5622 int res;
5623 tmp = PyObject_GetAttrString(obj, "ctx");
5624 if (tmp == NULL) goto failed;
5625 res = obj2ast_expr_context(tmp, &ctx, arena);
5626 if (res != 0) goto failed;
5627 Py_XDECREF(tmp);
5628 tmp = NULL;
5629 } else {
5630 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5631 return 1;
5632 }
5633 *out = Name(id, ctx, lineno, col_offset, arena);
5634 if (*out == NULL) goto failed;
5635 return 0;
5636 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005637 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5638 if (isinstance == -1) {
5639 return 1;
5640 }
5641 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005642 asdl_seq* elts;
5643 expr_context_ty ctx;
5644
5645 if (PyObject_HasAttrString(obj, "elts")) {
5646 int res;
5647 Py_ssize_t len;
5648 Py_ssize_t i;
5649 tmp = PyObject_GetAttrString(obj, "elts");
5650 if (tmp == NULL) goto failed;
5651 if (!PyList_Check(tmp)) {
5652 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5653 goto failed;
5654 }
5655 len = PyList_GET_SIZE(tmp);
5656 elts = asdl_seq_new(len, arena);
5657 if (elts == NULL) goto failed;
5658 for (i = 0; i < len; i++) {
5659 expr_ty value;
5660 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5661 if (res != 0) goto failed;
5662 asdl_seq_SET(elts, i, value);
5663 }
5664 Py_XDECREF(tmp);
5665 tmp = NULL;
5666 } else {
5667 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5668 return 1;
5669 }
5670 if (PyObject_HasAttrString(obj, "ctx")) {
5671 int res;
5672 tmp = PyObject_GetAttrString(obj, "ctx");
5673 if (tmp == NULL) goto failed;
5674 res = obj2ast_expr_context(tmp, &ctx, arena);
5675 if (res != 0) goto failed;
5676 Py_XDECREF(tmp);
5677 tmp = NULL;
5678 } else {
5679 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5680 return 1;
5681 }
5682 *out = List(elts, ctx, lineno, col_offset, arena);
5683 if (*out == NULL) goto failed;
5684 return 0;
5685 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005686 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5687 if (isinstance == -1) {
5688 return 1;
5689 }
5690 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005691 asdl_seq* elts;
5692 expr_context_ty ctx;
5693
5694 if (PyObject_HasAttrString(obj, "elts")) {
5695 int res;
5696 Py_ssize_t len;
5697 Py_ssize_t i;
5698 tmp = PyObject_GetAttrString(obj, "elts");
5699 if (tmp == NULL) goto failed;
5700 if (!PyList_Check(tmp)) {
5701 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5702 goto failed;
5703 }
5704 len = PyList_GET_SIZE(tmp);
5705 elts = asdl_seq_new(len, arena);
5706 if (elts == NULL) goto failed;
5707 for (i = 0; i < len; i++) {
5708 expr_ty value;
5709 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5710 if (res != 0) goto failed;
5711 asdl_seq_SET(elts, i, value);
5712 }
5713 Py_XDECREF(tmp);
5714 tmp = NULL;
5715 } else {
5716 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5717 return 1;
5718 }
5719 if (PyObject_HasAttrString(obj, "ctx")) {
5720 int res;
5721 tmp = PyObject_GetAttrString(obj, "ctx");
5722 if (tmp == NULL) goto failed;
5723 res = obj2ast_expr_context(tmp, &ctx, arena);
5724 if (res != 0) goto failed;
5725 Py_XDECREF(tmp);
5726 tmp = NULL;
5727 } else {
5728 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5729 return 1;
5730 }
5731 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5732 if (*out == NULL) goto failed;
5733 return 0;
5734 }
5735
5736 tmp = PyObject_Repr(obj);
5737 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005738 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005739failed:
5740 Py_XDECREF(tmp);
5741 return 1;
5742}
5743
5744int
5745obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5746{
5747 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005748 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005749
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005750 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5751 if (isinstance == -1) {
5752 return 1;
5753 }
5754 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005755 *out = Load;
5756 return 0;
5757 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005758 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5759 if (isinstance == -1) {
5760 return 1;
5761 }
5762 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005763 *out = Store;
5764 return 0;
5765 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005766 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5767 if (isinstance == -1) {
5768 return 1;
5769 }
5770 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005771 *out = Del;
5772 return 0;
5773 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005774 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5775 if (isinstance == -1) {
5776 return 1;
5777 }
5778 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005779 *out = AugLoad;
5780 return 0;
5781 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005782 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5783 if (isinstance == -1) {
5784 return 1;
5785 }
5786 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005787 *out = AugStore;
5788 return 0;
5789 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005790 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5791 if (isinstance == -1) {
5792 return 1;
5793 }
5794 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005795 *out = Param;
5796 return 0;
5797 }
5798
5799 tmp = PyObject_Repr(obj);
5800 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005801 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005802failed:
5803 Py_XDECREF(tmp);
5804 return 1;
5805}
5806
5807int
5808obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5809{
5810 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005811 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005812
5813
5814 if (obj == Py_None) {
5815 *out = NULL;
5816 return 0;
5817 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005818 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5819 if (isinstance == -1) {
5820 return 1;
5821 }
5822 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005823
5824 *out = Ellipsis(arena);
5825 if (*out == NULL) goto failed;
5826 return 0;
5827 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005828 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5829 if (isinstance == -1) {
5830 return 1;
5831 }
5832 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005833 expr_ty lower;
5834 expr_ty upper;
5835 expr_ty step;
5836
5837 if (PyObject_HasAttrString(obj, "lower")) {
5838 int res;
5839 tmp = PyObject_GetAttrString(obj, "lower");
5840 if (tmp == NULL) goto failed;
5841 res = obj2ast_expr(tmp, &lower, arena);
5842 if (res != 0) goto failed;
5843 Py_XDECREF(tmp);
5844 tmp = NULL;
5845 } else {
5846 lower = NULL;
5847 }
5848 if (PyObject_HasAttrString(obj, "upper")) {
5849 int res;
5850 tmp = PyObject_GetAttrString(obj, "upper");
5851 if (tmp == NULL) goto failed;
5852 res = obj2ast_expr(tmp, &upper, arena);
5853 if (res != 0) goto failed;
5854 Py_XDECREF(tmp);
5855 tmp = NULL;
5856 } else {
5857 upper = NULL;
5858 }
5859 if (PyObject_HasAttrString(obj, "step")) {
5860 int res;
5861 tmp = PyObject_GetAttrString(obj, "step");
5862 if (tmp == NULL) goto failed;
5863 res = obj2ast_expr(tmp, &step, arena);
5864 if (res != 0) goto failed;
5865 Py_XDECREF(tmp);
5866 tmp = NULL;
5867 } else {
5868 step = NULL;
5869 }
5870 *out = Slice(lower, upper, step, arena);
5871 if (*out == NULL) goto failed;
5872 return 0;
5873 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005874 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5875 if (isinstance == -1) {
5876 return 1;
5877 }
5878 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005879 asdl_seq* dims;
5880
5881 if (PyObject_HasAttrString(obj, "dims")) {
5882 int res;
5883 Py_ssize_t len;
5884 Py_ssize_t i;
5885 tmp = PyObject_GetAttrString(obj, "dims");
5886 if (tmp == NULL) goto failed;
5887 if (!PyList_Check(tmp)) {
5888 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5889 goto failed;
5890 }
5891 len = PyList_GET_SIZE(tmp);
5892 dims = asdl_seq_new(len, arena);
5893 if (dims == NULL) goto failed;
5894 for (i = 0; i < len; i++) {
5895 slice_ty value;
5896 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5897 if (res != 0) goto failed;
5898 asdl_seq_SET(dims, i, value);
5899 }
5900 Py_XDECREF(tmp);
5901 tmp = NULL;
5902 } else {
5903 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5904 return 1;
5905 }
5906 *out = ExtSlice(dims, arena);
5907 if (*out == NULL) goto failed;
5908 return 0;
5909 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005910 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5911 if (isinstance == -1) {
5912 return 1;
5913 }
5914 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005915 expr_ty value;
5916
5917 if (PyObject_HasAttrString(obj, "value")) {
5918 int res;
5919 tmp = PyObject_GetAttrString(obj, "value");
5920 if (tmp == NULL) goto failed;
5921 res = obj2ast_expr(tmp, &value, arena);
5922 if (res != 0) goto failed;
5923 Py_XDECREF(tmp);
5924 tmp = NULL;
5925 } else {
5926 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5927 return 1;
5928 }
5929 *out = Index(value, arena);
5930 if (*out == NULL) goto failed;
5931 return 0;
5932 }
5933
5934 tmp = PyObject_Repr(obj);
5935 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005936 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005937failed:
5938 Py_XDECREF(tmp);
5939 return 1;
5940}
5941
5942int
5943obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5944{
5945 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005946 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005947
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005948 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5949 if (isinstance == -1) {
5950 return 1;
5951 }
5952 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005953 *out = And;
5954 return 0;
5955 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005956 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5957 if (isinstance == -1) {
5958 return 1;
5959 }
5960 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005961 *out = Or;
5962 return 0;
5963 }
5964
5965 tmp = PyObject_Repr(obj);
5966 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005967 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005968failed:
5969 Py_XDECREF(tmp);
5970 return 1;
5971}
5972
5973int
5974obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5975{
5976 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005977 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005978
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005979 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
5980 if (isinstance == -1) {
5981 return 1;
5982 }
5983 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005984 *out = Add;
5985 return 0;
5986 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005987 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
5988 if (isinstance == -1) {
5989 return 1;
5990 }
5991 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005992 *out = Sub;
5993 return 0;
5994 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005995 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
5996 if (isinstance == -1) {
5997 return 1;
5998 }
5999 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006000 *out = Mult;
6001 return 0;
6002 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006003 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
6004 if (isinstance == -1) {
6005 return 1;
6006 }
6007 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006008 *out = Div;
6009 return 0;
6010 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006011 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
6012 if (isinstance == -1) {
6013 return 1;
6014 }
6015 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006016 *out = Mod;
6017 return 0;
6018 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006019 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
6020 if (isinstance == -1) {
6021 return 1;
6022 }
6023 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006024 *out = Pow;
6025 return 0;
6026 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006027 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
6028 if (isinstance == -1) {
6029 return 1;
6030 }
6031 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006032 *out = LShift;
6033 return 0;
6034 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006035 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
6036 if (isinstance == -1) {
6037 return 1;
6038 }
6039 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006040 *out = RShift;
6041 return 0;
6042 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006043 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
6044 if (isinstance == -1) {
6045 return 1;
6046 }
6047 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006048 *out = BitOr;
6049 return 0;
6050 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006051 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
6052 if (isinstance == -1) {
6053 return 1;
6054 }
6055 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006056 *out = BitXor;
6057 return 0;
6058 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006059 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
6060 if (isinstance == -1) {
6061 return 1;
6062 }
6063 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006064 *out = BitAnd;
6065 return 0;
6066 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006067 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
6068 if (isinstance == -1) {
6069 return 1;
6070 }
6071 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006072 *out = FloorDiv;
6073 return 0;
6074 }
6075
6076 tmp = PyObject_Repr(obj);
6077 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006078 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006079failed:
6080 Py_XDECREF(tmp);
6081 return 1;
6082}
6083
6084int
6085obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
6086{
6087 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006088 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006089
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006090 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
6091 if (isinstance == -1) {
6092 return 1;
6093 }
6094 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006095 *out = Invert;
6096 return 0;
6097 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006098 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
6099 if (isinstance == -1) {
6100 return 1;
6101 }
6102 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006103 *out = Not;
6104 return 0;
6105 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006106 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
6107 if (isinstance == -1) {
6108 return 1;
6109 }
6110 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006111 *out = UAdd;
6112 return 0;
6113 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006114 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
6115 if (isinstance == -1) {
6116 return 1;
6117 }
6118 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006119 *out = USub;
6120 return 0;
6121 }
6122
6123 tmp = PyObject_Repr(obj);
6124 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006125 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006126failed:
6127 Py_XDECREF(tmp);
6128 return 1;
6129}
6130
6131int
6132obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
6133{
6134 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006135 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006136
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006137 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
6138 if (isinstance == -1) {
6139 return 1;
6140 }
6141 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006142 *out = Eq;
6143 return 0;
6144 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006145 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
6146 if (isinstance == -1) {
6147 return 1;
6148 }
6149 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006150 *out = NotEq;
6151 return 0;
6152 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006153 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
6154 if (isinstance == -1) {
6155 return 1;
6156 }
6157 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006158 *out = Lt;
6159 return 0;
6160 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006161 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
6162 if (isinstance == -1) {
6163 return 1;
6164 }
6165 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006166 *out = LtE;
6167 return 0;
6168 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006169 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
6170 if (isinstance == -1) {
6171 return 1;
6172 }
6173 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006174 *out = Gt;
6175 return 0;
6176 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006177 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
6178 if (isinstance == -1) {
6179 return 1;
6180 }
6181 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006182 *out = GtE;
6183 return 0;
6184 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006185 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
6186 if (isinstance == -1) {
6187 return 1;
6188 }
6189 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006190 *out = Is;
6191 return 0;
6192 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006193 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
6194 if (isinstance == -1) {
6195 return 1;
6196 }
6197 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006198 *out = IsNot;
6199 return 0;
6200 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006201 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
6202 if (isinstance == -1) {
6203 return 1;
6204 }
6205 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006206 *out = In;
6207 return 0;
6208 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006209 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
6210 if (isinstance == -1) {
6211 return 1;
6212 }
6213 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00006214 *out = NotIn;
6215 return 0;
6216 }
6217
6218 tmp = PyObject_Repr(obj);
6219 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006220 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006221failed:
6222 Py_XDECREF(tmp);
6223 return 1;
6224}
6225
6226int
6227obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
6228{
6229 PyObject* tmp = NULL;
6230 expr_ty target;
6231 expr_ty iter;
6232 asdl_seq* ifs;
6233
6234 if (PyObject_HasAttrString(obj, "target")) {
6235 int res;
6236 tmp = PyObject_GetAttrString(obj, "target");
6237 if (tmp == NULL) goto failed;
6238 res = obj2ast_expr(tmp, &target, arena);
6239 if (res != 0) goto failed;
6240 Py_XDECREF(tmp);
6241 tmp = NULL;
6242 } else {
6243 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
6244 return 1;
6245 }
6246 if (PyObject_HasAttrString(obj, "iter")) {
6247 int res;
6248 tmp = PyObject_GetAttrString(obj, "iter");
6249 if (tmp == NULL) goto failed;
6250 res = obj2ast_expr(tmp, &iter, arena);
6251 if (res != 0) goto failed;
6252 Py_XDECREF(tmp);
6253 tmp = NULL;
6254 } else {
6255 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
6256 return 1;
6257 }
6258 if (PyObject_HasAttrString(obj, "ifs")) {
6259 int res;
6260 Py_ssize_t len;
6261 Py_ssize_t i;
6262 tmp = PyObject_GetAttrString(obj, "ifs");
6263 if (tmp == NULL) goto failed;
6264 if (!PyList_Check(tmp)) {
6265 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6266 goto failed;
6267 }
6268 len = PyList_GET_SIZE(tmp);
6269 ifs = asdl_seq_new(len, arena);
6270 if (ifs == NULL) goto failed;
6271 for (i = 0; i < len; i++) {
6272 expr_ty value;
6273 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6274 if (res != 0) goto failed;
6275 asdl_seq_SET(ifs, i, value);
6276 }
6277 Py_XDECREF(tmp);
6278 tmp = NULL;
6279 } else {
6280 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6281 return 1;
6282 }
6283 *out = comprehension(target, iter, ifs, arena);
6284 return 0;
6285failed:
6286 Py_XDECREF(tmp);
6287 return 1;
6288}
6289
6290int
6291obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6292{
6293 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006294 int isinstance;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006295
Georg Brandlfc8eef32008-03-28 12:11:56 +00006296 int lineno;
6297 int col_offset;
6298
Georg Brandla48f3ab2008-03-30 06:40:17 +00006299 if (obj == Py_None) {
6300 *out = NULL;
6301 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006302 }
6303 if (PyObject_HasAttrString(obj, "lineno")) {
6304 int res;
6305 tmp = PyObject_GetAttrString(obj, "lineno");
6306 if (tmp == NULL) goto failed;
6307 res = obj2ast_int(tmp, &lineno, arena);
6308 if (res != 0) goto failed;
6309 Py_XDECREF(tmp);
6310 tmp = NULL;
6311 } else {
6312 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6313 return 1;
6314 }
6315 if (PyObject_HasAttrString(obj, "col_offset")) {
6316 int res;
6317 tmp = PyObject_GetAttrString(obj, "col_offset");
6318 if (tmp == NULL) goto failed;
6319 res = obj2ast_int(tmp, &col_offset, arena);
6320 if (res != 0) goto failed;
6321 Py_XDECREF(tmp);
6322 tmp = NULL;
6323 } else {
6324 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6325 return 1;
6326 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006327 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6328 if (isinstance == -1) {
6329 return 1;
6330 }
6331 if (isinstance) {
Georg Brandla48f3ab2008-03-30 06:40:17 +00006332 expr_ty type;
6333 expr_ty name;
6334 asdl_seq* body;
6335
6336 if (PyObject_HasAttrString(obj, "type")) {
6337 int res;
6338 tmp = PyObject_GetAttrString(obj, "type");
6339 if (tmp == NULL) goto failed;
6340 res = obj2ast_expr(tmp, &type, arena);
6341 if (res != 0) goto failed;
6342 Py_XDECREF(tmp);
6343 tmp = NULL;
6344 } else {
6345 type = NULL;
6346 }
6347 if (PyObject_HasAttrString(obj, "name")) {
6348 int res;
6349 tmp = PyObject_GetAttrString(obj, "name");
6350 if (tmp == NULL) goto failed;
6351 res = obj2ast_expr(tmp, &name, arena);
6352 if (res != 0) goto failed;
6353 Py_XDECREF(tmp);
6354 tmp = NULL;
6355 } else {
6356 name = NULL;
6357 }
6358 if (PyObject_HasAttrString(obj, "body")) {
6359 int res;
6360 Py_ssize_t len;
6361 Py_ssize_t i;
6362 tmp = PyObject_GetAttrString(obj, "body");
6363 if (tmp == NULL) goto failed;
6364 if (!PyList_Check(tmp)) {
6365 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6366 goto failed;
6367 }
6368 len = PyList_GET_SIZE(tmp);
6369 body = asdl_seq_new(len, arena);
6370 if (body == NULL) goto failed;
6371 for (i = 0; i < len; i++) {
6372 stmt_ty value;
6373 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6374 if (res != 0) goto failed;
6375 asdl_seq_SET(body, i, value);
6376 }
6377 Py_XDECREF(tmp);
6378 tmp = NULL;
6379 } else {
6380 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6381 return 1;
6382 }
6383 *out = ExceptHandler(type, name, body, lineno, col_offset,
6384 arena);
6385 if (*out == NULL) goto failed;
6386 return 0;
6387 }
6388
6389 tmp = PyObject_Repr(obj);
6390 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006391 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006392failed:
6393 Py_XDECREF(tmp);
6394 return 1;
6395}
6396
6397int
6398obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6399{
6400 PyObject* tmp = NULL;
6401 asdl_seq* args;
6402 identifier vararg;
6403 identifier kwarg;
6404 asdl_seq* defaults;
6405
6406 if (PyObject_HasAttrString(obj, "args")) {
6407 int res;
6408 Py_ssize_t len;
6409 Py_ssize_t i;
6410 tmp = PyObject_GetAttrString(obj, "args");
6411 if (tmp == NULL) goto failed;
6412 if (!PyList_Check(tmp)) {
6413 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6414 goto failed;
6415 }
6416 len = PyList_GET_SIZE(tmp);
6417 args = asdl_seq_new(len, arena);
6418 if (args == NULL) goto failed;
6419 for (i = 0; i < len; i++) {
6420 expr_ty value;
6421 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6422 if (res != 0) goto failed;
6423 asdl_seq_SET(args, i, value);
6424 }
6425 Py_XDECREF(tmp);
6426 tmp = NULL;
6427 } else {
6428 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6429 return 1;
6430 }
6431 if (PyObject_HasAttrString(obj, "vararg")) {
6432 int res;
6433 tmp = PyObject_GetAttrString(obj, "vararg");
6434 if (tmp == NULL) goto failed;
6435 res = obj2ast_identifier(tmp, &vararg, arena);
6436 if (res != 0) goto failed;
6437 Py_XDECREF(tmp);
6438 tmp = NULL;
6439 } else {
6440 vararg = NULL;
6441 }
6442 if (PyObject_HasAttrString(obj, "kwarg")) {
6443 int res;
6444 tmp = PyObject_GetAttrString(obj, "kwarg");
6445 if (tmp == NULL) goto failed;
6446 res = obj2ast_identifier(tmp, &kwarg, arena);
6447 if (res != 0) goto failed;
6448 Py_XDECREF(tmp);
6449 tmp = NULL;
6450 } else {
6451 kwarg = NULL;
6452 }
6453 if (PyObject_HasAttrString(obj, "defaults")) {
6454 int res;
6455 Py_ssize_t len;
6456 Py_ssize_t i;
6457 tmp = PyObject_GetAttrString(obj, "defaults");
6458 if (tmp == NULL) goto failed;
6459 if (!PyList_Check(tmp)) {
6460 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6461 goto failed;
6462 }
6463 len = PyList_GET_SIZE(tmp);
6464 defaults = asdl_seq_new(len, arena);
6465 if (defaults == NULL) goto failed;
6466 for (i = 0; i < len; i++) {
6467 expr_ty value;
6468 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6469 if (res != 0) goto failed;
6470 asdl_seq_SET(defaults, i, value);
6471 }
6472 Py_XDECREF(tmp);
6473 tmp = NULL;
6474 } else {
6475 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6476 return 1;
6477 }
6478 *out = arguments(args, vararg, kwarg, defaults, arena);
6479 return 0;
6480failed:
6481 Py_XDECREF(tmp);
6482 return 1;
6483}
6484
6485int
6486obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6487{
6488 PyObject* tmp = NULL;
6489 identifier arg;
6490 expr_ty value;
6491
6492 if (PyObject_HasAttrString(obj, "arg")) {
6493 int res;
6494 tmp = PyObject_GetAttrString(obj, "arg");
6495 if (tmp == NULL) goto failed;
6496 res = obj2ast_identifier(tmp, &arg, arena);
6497 if (res != 0) goto failed;
6498 Py_XDECREF(tmp);
6499 tmp = NULL;
6500 } else {
6501 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6502 return 1;
6503 }
6504 if (PyObject_HasAttrString(obj, "value")) {
6505 int res;
6506 tmp = PyObject_GetAttrString(obj, "value");
6507 if (tmp == NULL) goto failed;
6508 res = obj2ast_expr(tmp, &value, arena);
6509 if (res != 0) goto failed;
6510 Py_XDECREF(tmp);
6511 tmp = NULL;
6512 } else {
6513 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6514 return 1;
6515 }
6516 *out = keyword(arg, value, arena);
6517 return 0;
6518failed:
6519 Py_XDECREF(tmp);
6520 return 1;
6521}
6522
6523int
6524obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6525{
6526 PyObject* tmp = NULL;
6527 identifier name;
6528 identifier asname;
6529
6530 if (PyObject_HasAttrString(obj, "name")) {
6531 int res;
6532 tmp = PyObject_GetAttrString(obj, "name");
6533 if (tmp == NULL) goto failed;
6534 res = obj2ast_identifier(tmp, &name, arena);
6535 if (res != 0) goto failed;
6536 Py_XDECREF(tmp);
6537 tmp = NULL;
6538 } else {
6539 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6540 return 1;
6541 }
6542 if (PyObject_HasAttrString(obj, "asname")) {
6543 int res;
6544 tmp = PyObject_GetAttrString(obj, "asname");
6545 if (tmp == NULL) goto failed;
6546 res = obj2ast_identifier(tmp, &asname, arena);
6547 if (res != 0) goto failed;
6548 Py_XDECREF(tmp);
6549 tmp = NULL;
6550 } else {
6551 asname = NULL;
6552 }
6553 *out = alias(name, asname, arena);
6554 return 0;
6555failed:
6556 Py_XDECREF(tmp);
6557 return 1;
6558}
6559
6560
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006561PyMODINIT_FUNC
6562init_ast(void)
6563{
6564 PyObject *m, *d;
6565 if (!init_types()) return;
6566 m = Py_InitModule3("_ast", NULL, NULL);
6567 if (!m) return;
6568 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00006569 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006570 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6571 return;
Brett Cannon3ad57e22010-01-10 02:48:50 +00006572 if (PyModule_AddStringConstant(m, "__version__", "77400") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006573 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006574 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6575 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6576 return;
6577 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6578 < 0) return;
6579 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6580 0) return;
6581 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6582 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6583 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6584 < 0) return;
6585 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6586 return;
6587 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6588 return;
6589 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6590 return;
6591 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6592 return;
6593 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6594 0) return;
6595 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
6596 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6597 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6598 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6599 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6600 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6601 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6602 0) return;
6603 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6604 0) return;
6605 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6606 return;
6607 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6608 return;
6609 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6610 0) return;
6611 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
6612 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6613 return;
6614 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6615 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6616 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6617 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6618 return;
6619 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6620 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6621 return;
6622 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6623 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6624 return;
6625 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6626 return;
6627 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6628 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00006629 if (PyDict_SetItemString(d, "Set", (PyObject*)Set_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006630 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6631 return;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00006632 if (PyDict_SetItemString(d, "SetComp", (PyObject*)SetComp_type) < 0)
6633 return;
6634 if (PyDict_SetItemString(d, "DictComp", (PyObject*)DictComp_type) < 0)
6635 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006636 if (PyDict_SetItemString(d, "GeneratorExp",
6637 (PyObject*)GeneratorExp_type) < 0) return;
6638 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6639 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6640 return;
6641 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6642 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6643 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6644 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6645 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6646 0) return;
6647 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6648 0) return;
6649 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6650 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6651 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6652 if (PyDict_SetItemString(d, "expr_context",
6653 (PyObject*)expr_context_type) < 0) return;
6654 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6655 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6656 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6657 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6658 return;
6659 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6660 return;
6661 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6662 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6663 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6664 return;
6665 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6666 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6667 return;
6668 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6669 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6670 return;
6671 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6672 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6673 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6674 return;
6675 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6676 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6677 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6678 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6679 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6680 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6681 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6682 return;
6683 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6684 return;
6685 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6686 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6687 return;
6688 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6689 return;
6690 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6691 return;
6692 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6693 return;
6694 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6695 return;
6696 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6697 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6698 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6699 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6700 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6701 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6702 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6703 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6704 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6705 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6706 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6707 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6708 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6709 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6710 if (PyDict_SetItemString(d, "comprehension",
6711 (PyObject*)comprehension_type) < 0) return;
6712 if (PyDict_SetItemString(d, "excepthandler",
6713 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006714 if (PyDict_SetItemString(d, "ExceptHandler",
6715 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006716 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6717 0) return;
6718 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6719 return;
6720 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006721}
6722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006723
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006724PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006725{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006726 init_types();
6727 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006728}
6729
Georg Brandlf2bfd542008-03-29 13:24:23 +00006730/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6731mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006732{
6733 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006734 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6735 (PyObject*)Interactive_type};
6736 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006737 int isinstance;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006738 assert(0 <= mode && mode <= 2);
6739
Georg Brandlfc8eef32008-03-28 12:11:56 +00006740 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006741
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006742 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6743 if (isinstance == -1)
6744 return NULL;
6745 if (!isinstance) {
Georg Brandlf2bfd542008-03-29 13:24:23 +00006746 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6747 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006748 return NULL;
6749 }
6750 if (obj2ast_mod(ast, &res, arena) != 0)
6751 return NULL;
6752 else
6753 return res;
6754}
6755
6756int PyAST_Check(PyObject* obj)
6757{
6758 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006759 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006760}
6761
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006762