blob: 61e7427380ff12097c77256ddaf2507bd3c6f2bf [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Benjamin Peterson8fa3e932009-06-13 20:30:48 +00005 __version__ 73421.
Brett Cannon0db62aa2007-02-12 03:51:02 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Georg Brandlc52ed592008-03-30 07:01:47 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000045 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000046};
Neal Norwitz53d960c2006-02-28 22:47:29 +000047static PyTypeObject *ClassDef_type;
48static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000049 "name",
50 "bases",
51 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000052 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000053};
Neal Norwitz53d960c2006-02-28 22:47:29 +000054static PyTypeObject *Return_type;
55static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000056 "value",
57};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Delete_type;
59static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "targets",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Assign_type;
63static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65 "value",
66};
Neal Norwitz53d960c2006-02-28 22:47:29 +000067static PyTypeObject *AugAssign_type;
68static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000069 "target",
70 "op",
71 "value",
72};
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static PyTypeObject *Print_type;
74static char *Print_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075 "dest",
76 "values",
77 "nl",
78};
Neal Norwitz53d960c2006-02-28 22:47:29 +000079static PyTypeObject *For_type;
80static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000081 "target",
82 "iter",
83 "body",
84 "orelse",
85};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *While_type;
87static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088 "test",
89 "body",
90 "orelse",
91};
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static PyTypeObject *If_type;
93static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000094 "test",
95 "body",
96 "orelse",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *With_type;
99static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100 "context_expr",
101 "optional_vars",
102 "body",
103};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000104static PyTypeObject *Raise_type;
105static char *Raise_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106 "type",
107 "inst",
108 "tback",
109};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000110static PyTypeObject *TryExcept_type;
111static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000112 "body",
113 "handlers",
114 "orelse",
115};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static PyTypeObject *TryFinally_type;
117static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000118 "body",
119 "finalbody",
120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *Assert_type;
122static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000123 "test",
124 "msg",
125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *Import_type;
127static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128 "names",
129};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000130static PyTypeObject *ImportFrom_type;
131static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132 "module",
133 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000134 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *Exec_type;
137static char *Exec_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138 "body",
139 "globals",
140 "locals",
141};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000142static PyTypeObject *Global_type;
143static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144 "names",
145};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000146static PyTypeObject *Expr_type;
147static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000148 "value",
149};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000150static PyTypeObject *Pass_type;
151static PyTypeObject *Break_type;
152static PyTypeObject *Continue_type;
153static PyTypeObject *expr_type;
154static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000155 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000156 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000157};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BoolOp_type;
160static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "op",
162 "values",
163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *BinOp_type;
165static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000166 "left",
167 "op",
168 "right",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *UnaryOp_type;
171static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "op",
173 "operand",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *Lambda_type;
176static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000177 "args",
178 "body",
179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *IfExp_type;
181static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000182 "test",
183 "body",
184 "orelse",
185};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000186static PyTypeObject *Dict_type;
187static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000188 "keys",
189 "values",
190};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000191static PyTypeObject *ListComp_type;
192static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193 "elt",
194 "generators",
195};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static PyTypeObject *GeneratorExp_type;
197static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198 "elt",
199 "generators",
200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *Yield_type;
202static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000203 "value",
204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Compare_type;
206static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207 "left",
208 "ops",
209 "comparators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Call_type;
212static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "func",
214 "args",
215 "keywords",
216 "starargs",
217 "kwargs",
218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *Repr_type;
220static char *Repr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221 "value",
222};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000223static PyTypeObject *Num_type;
224static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000225 "n",
226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *Str_type;
228static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000229 "s",
230};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000231static PyTypeObject *Attribute_type;
232static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233 "value",
234 "attr",
235 "ctx",
236};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000237static PyTypeObject *Subscript_type;
238static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000239 "value",
240 "slice",
241 "ctx",
242};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000243static PyTypeObject *Name_type;
244static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000245 "id",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *List_type;
249static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "elts",
251 "ctx",
252};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000253static PyTypeObject *Tuple_type;
254static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000255 "elts",
256 "ctx",
257};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000258static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
260*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
261static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000262static PyTypeObject *Load_type;
263static PyTypeObject *Store_type;
264static PyTypeObject *Del_type;
265static PyTypeObject *AugLoad_type;
266static PyTypeObject *AugStore_type;
267static PyTypeObject *Param_type;
268static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000269static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000270static PyTypeObject *Ellipsis_type;
271static PyTypeObject *Slice_type;
272static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000273 "lower",
274 "upper",
275 "step",
276};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *ExtSlice_type;
278static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000279 "dims",
280};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *Index_type;
282static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000283 "value",
284};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286static PyObject *And_singleton, *Or_singleton;
287static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000288static PyTypeObject *And_type;
289static PyTypeObject *Or_type;
290static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
292*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
293*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
294*FloorDiv_singleton;
295static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Add_type;
297static PyTypeObject *Sub_type;
298static PyTypeObject *Mult_type;
299static PyTypeObject *Div_type;
300static PyTypeObject *Mod_type;
301static PyTypeObject *Pow_type;
302static PyTypeObject *LShift_type;
303static PyTypeObject *RShift_type;
304static PyTypeObject *BitOr_type;
305static PyTypeObject *BitXor_type;
306static PyTypeObject *BitAnd_type;
307static PyTypeObject *FloorDiv_type;
308static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000309static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
310*USub_singleton;
311static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000312static PyTypeObject *Invert_type;
313static PyTypeObject *Not_type;
314static PyTypeObject *UAdd_type;
315static PyTypeObject *USub_type;
316static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000317static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
318*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
319*NotIn_singleton;
320static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000321static PyTypeObject *Eq_type;
322static PyTypeObject *NotEq_type;
323static PyTypeObject *Lt_type;
324static PyTypeObject *LtE_type;
325static PyTypeObject *Gt_type;
326static PyTypeObject *GtE_type;
327static PyTypeObject *Is_type;
328static PyTypeObject *IsNot_type;
329static PyTypeObject *In_type;
330static PyTypeObject *NotIn_type;
331static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334 "target",
335 "iter",
336 "ifs",
337};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000338static PyTypeObject *excepthandler_type;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000339static char *excepthandler_attributes[] = {
340 "lineno",
341 "col_offset",
342};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000343static PyObject* ast2obj_excepthandler(void*);
Georg Brandla48f3ab2008-03-30 06:40:17 +0000344static PyTypeObject *ExceptHandler_type;
345static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346 "type",
347 "name",
348 "body",
349};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000350static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000351static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353 "args",
354 "vararg",
355 "kwarg",
356 "defaults",
357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000359static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000360static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "arg",
362 "value",
363};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367 "name",
368 "asname",
369};
370
371
Georg Brandlc52ed592008-03-30 07:01:47 +0000372static int
373ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
374{
375 Py_ssize_t i, numfields = 0;
376 int res = -1;
377 PyObject *key, *value, *fields;
378 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
379 if (!fields)
380 PyErr_Clear();
381 if (fields) {
382 numfields = PySequence_Size(fields);
383 if (numfields == -1)
384 goto cleanup;
385 }
386 res = 0; /* if no error occurs, this stays 0 to the end */
387 if (PyTuple_GET_SIZE(args) > 0) {
388 if (numfields != PyTuple_GET_SIZE(args)) {
Georg Brandl1721e752008-03-30 19:43:27 +0000389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
Amaury Forgeot d'Arc05e34492008-09-10 22:04:45 +0000390 "%zd positional argument%s",
Georg Brandl1721e752008-03-30 19:43:27 +0000391 Py_TYPE(self)->tp_name,
392 numfields == 0 ? "" : "either 0 or ",
Georg Brandlc52ed592008-03-30 07:01:47 +0000393 numfields, numfields == 1 ? "" : "s");
394 res = -1;
395 goto cleanup;
396 }
397 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
398 /* cannot be reached when fields is NULL */
399 PyObject *name = PySequence_GetItem(fields, i);
400 if (!name) {
401 res = -1;
402 goto cleanup;
403 }
404 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
405 Py_DECREF(name);
406 if (res < 0)
407 goto cleanup;
408 }
409 }
410 if (kw) {
411 i = 0; /* needed by PyDict_Next */
412 while (PyDict_Next(kw, &i, &key, &value)) {
413 res = PyObject_SetAttr(self, key, value);
414 if (res < 0)
415 goto cleanup;
416 }
417 }
418 cleanup:
419 Py_XDECREF(fields);
420 return res;
421}
422
Georg Brandle34c21c2008-03-30 20:20:39 +0000423/* Pickling support */
424static PyObject *
425ast_type_reduce(PyObject *self, PyObject *unused)
426{
427 PyObject *res;
428 PyObject *dict = PyObject_GetAttrString(self, "__dict__");
429 if (dict == NULL) {
430 if (PyErr_ExceptionMatches(PyExc_AttributeError))
431 PyErr_Clear();
432 else
433 return NULL;
434 }
435 if (dict) {
436 res = Py_BuildValue("O()O", Py_TYPE(self), dict);
437 Py_DECREF(dict);
438 return res;
439 }
440 return Py_BuildValue("O()", Py_TYPE(self));
441}
442
443static PyMethodDef ast_type_methods[] = {
444 {"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
445 {NULL}
446};
447
Georg Brandlc52ed592008-03-30 07:01:47 +0000448static PyTypeObject AST_type = {
449 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Georg Brandle34c21c2008-03-30 20:20:39 +0000450 "_ast.AST",
Georg Brandlc52ed592008-03-30 07:01:47 +0000451 sizeof(PyObject),
452 0,
453 0, /* tp_dealloc */
454 0, /* tp_print */
455 0, /* tp_getattr */
456 0, /* tp_setattr */
457 0, /* tp_compare */
458 0, /* tp_repr */
459 0, /* tp_as_number */
460 0, /* tp_as_sequence */
461 0, /* tp_as_mapping */
462 0, /* tp_hash */
463 0, /* tp_call */
464 0, /* tp_str */
465 PyObject_GenericGetAttr, /* tp_getattro */
466 PyObject_GenericSetAttr, /* tp_setattro */
467 0, /* tp_as_buffer */
468 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
469 0, /* tp_doc */
470 0, /* tp_traverse */
471 0, /* tp_clear */
472 0, /* tp_richcompare */
473 0, /* tp_weaklistoffset */
474 0, /* tp_iter */
475 0, /* tp_iternext */
Georg Brandle34c21c2008-03-30 20:20:39 +0000476 ast_type_methods, /* tp_methods */
Georg Brandlc52ed592008-03-30 07:01:47 +0000477 0, /* tp_members */
478 0, /* tp_getset */
479 0, /* tp_base */
480 0, /* tp_dict */
481 0, /* tp_descr_get */
482 0, /* tp_descr_set */
483 0, /* tp_dictoffset */
484 (initproc)ast_type_init, /* tp_init */
485 PyType_GenericAlloc, /* tp_alloc */
486 PyType_GenericNew, /* tp_new */
487 PyObject_Del, /* tp_free */
488};
489
490
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000491static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
492{
493 PyObject *fnames, *result;
494 int i;
Georg Brandl2c55c592008-03-30 19:00:49 +0000495 fnames = PyTuple_New(num_fields);
496 if (!fnames) return NULL;
497 for (i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000498 PyObject *field = PyString_FromString(fields[i]);
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000499 if (!field) {
500 Py_DECREF(fnames);
501 return NULL;
502 }
503 PyTuple_SET_ITEM(fnames, i, field);
504 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000505 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000506 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000507 Py_DECREF(fnames);
508 return (PyTypeObject*)result;
509}
510
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000511static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
512{
Neal Norwitz19379f12006-04-03 04:50:58 +0000513 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000514 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000515 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000516 for(i = 0; i < num_fields; i++) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000517 s = PyString_FromString(attrs[i]);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000518 if (!s) {
519 Py_DECREF(l);
520 return 0;
521 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000522 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000523 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000524 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
525 Py_DECREF(l);
526 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000527}
528
Georg Brandlfc8eef32008-03-28 12:11:56 +0000529/* Conversion AST -> Python */
530
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000531static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
532{
533 int i, n = asdl_seq_LEN(seq);
534 PyObject *result = PyList_New(n);
535 PyObject *value;
536 if (!result)
537 return NULL;
538 for (i = 0; i < n; i++) {
539 value = func(asdl_seq_GET(seq, i));
540 if (!value) {
541 Py_DECREF(result);
542 return NULL;
543 }
544 PyList_SET_ITEM(result, i, value);
545 }
546 return result;
547}
548
549static PyObject* ast2obj_object(void *o)
550{
551 if (!o)
552 o = Py_None;
553 Py_INCREF((PyObject*)o);
554 return (PyObject*)o;
555}
556#define ast2obj_identifier ast2obj_object
557#define ast2obj_string ast2obj_object
558static PyObject* ast2obj_bool(bool b)
559{
560 return PyBool_FromLong(b);
561}
562
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000563static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000564{
565 return PyInt_FromLong(b);
566}
567
Georg Brandlfc8eef32008-03-28 12:11:56 +0000568/* Conversion Python -> AST */
569
570static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
571{
572 if (obj == Py_None)
573 obj = NULL;
574 if (obj)
575 PyArena_AddPyObject(arena, obj);
576 Py_XINCREF(obj);
577 *out = obj;
578 return 0;
579}
580
581#define obj2ast_identifier obj2ast_object
582#define obj2ast_string obj2ast_object
583
584static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
585{
586 int i;
587 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
588 PyObject *s = PyObject_Repr(obj);
589 if (s == NULL) return 1;
590 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000591 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000592 Py_DECREF(s);
593 return 1;
594 }
595
596 i = (int)PyLong_AsLong(obj);
597 if (i == -1 && PyErr_Occurred())
598 return 1;
599 *out = i;
600 return 0;
601}
602
603static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
604{
605 if (!PyBool_Check(obj)) {
606 PyObject *s = PyObject_Repr(obj);
607 if (s == NULL) return 1;
608 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000609 PyString_AS_STRING(s));
Georg Brandlfc8eef32008-03-28 12:11:56 +0000610 Py_DECREF(s);
611 return 1;
612 }
613
614 *out = (obj == Py_True);
615 return 0;
616}
617
Benjamin Petersonaff36f12008-10-19 13:59:01 +0000618static int add_ast_fields(void)
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000619{
620 PyObject *empty_tuple, *d;
621 if (PyType_Ready(&AST_type) < 0)
622 return -1;
623 d = AST_type.tp_dict;
624 empty_tuple = PyTuple_New(0);
625 if (!empty_tuple ||
626 PyDict_SetItemString(d, "_fields", empty_tuple) < 0 ||
627 PyDict_SetItemString(d, "_attributes", empty_tuple) < 0) {
628 Py_XDECREF(empty_tuple);
629 return -1;
630 }
631 Py_DECREF(empty_tuple);
632 return 0;
633}
634
Georg Brandlfc8eef32008-03-28 12:11:56 +0000635
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000636static int init_types(void)
637{
Neal Norwitz19379f12006-04-03 04:50:58 +0000638 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 if (initialized) return 1;
Armin Ronacher35e01fb2008-10-19 08:27:43 +0000640 if (add_ast_fields() < 0) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000641 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000642 if (!mod_type) return 0;
643 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000645 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000646 Interactive_type = make_type("Interactive", mod_type,
647 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000649 Expression_type = make_type("Expression", mod_type, Expression_fields,
650 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000653 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000654 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000655 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000656 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000657 FunctionDef_type = make_type("FunctionDef", stmt_type,
658 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000660 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000662 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000674 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000678 With_type = make_type("With", stmt_type, With_fields, 3);
679 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000682 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
685 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000687 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000689 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000692 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000700 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000702 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000704 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000706 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000708 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000709 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000710 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000711 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000715 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000717 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000721 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 GeneratorExp_type = make_type("GeneratorExp", expr_type,
724 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000732 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000734 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000735 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000736 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000740 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000748 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!expr_context_type) return 0;
750 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000751 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000753 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000755 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000757 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000759 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000761 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000763 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000765 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000767 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000769 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000775 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!slice_type) return 0;
777 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000778 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000780 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000784 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000786 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!boolop_type) return 0;
788 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000789 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000793 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000795 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000797 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!operator_type) return 0;
799 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000800 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000804 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000806 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000808 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000812 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000814 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000816 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000818 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000820 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000822 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000824 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000826 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000828 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000830 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000832 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000834 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000836 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000838 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000840 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000842 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000844 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000846 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000848 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!unaryop_type) return 0;
850 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000851 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000853 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000855 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000857 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000859 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000861 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000863 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000865 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000867 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000868 if (!cmpop_type) return 0;
869 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000870 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000872 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000874 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000876 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000878 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000880 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000882 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000884 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000886 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000888 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000890 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000892 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000893 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000894 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000895 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000896 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000897 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000898 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000899 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000900 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000901 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000902 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000904 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000906 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000908 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000909 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000910 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000911 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000912 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000913 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000914 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000915 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
916 return 0;
917 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
918 ExceptHandler_fields, 3);
919 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000920 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000921 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000922 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000923 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000924 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000925 if (!alias_type) return 0;
926 initialized = 1;
927 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000928}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000929
Georg Brandlfc8eef32008-03-28 12:11:56 +0000930static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
931static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
932static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
933static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
934 arena);
935static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
936static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
937static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
938static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
939static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
940static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
941 arena);
942static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
943 arena);
944static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
945static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
946static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000949Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950{
951 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000952 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000953 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 p->kind = Module_kind;
956 p->v.Module.body = body;
957 return p;
958}
959
960mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000961Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
963 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000964 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000965 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967 p->kind = Interactive_kind;
968 p->v.Interactive.body = body;
969 return p;
970}
971
972mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974{
975 mod_ty p;
976 if (!body) {
977 PyErr_SetString(PyExc_ValueError,
978 "field body is required for Expression");
979 return NULL;
980 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000981 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000982 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 p->kind = Expression_kind;
985 p->v.Expression.body = body;
986 return p;
987}
988
989mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000990Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991{
992 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000993 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000994 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 p->kind = Suite_kind;
997 p->v.Suite.body = body;
998 return p;
999}
1000
1001stmt_ty
1002FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +00001003 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004{
1005 stmt_ty p;
1006 if (!name) {
1007 PyErr_SetString(PyExc_ValueError,
1008 "field name is required for FunctionDef");
1009 return NULL;
1010 }
1011 if (!args) {
1012 PyErr_SetString(PyExc_ValueError,
1013 "field args is required for FunctionDef");
1014 return NULL;
1015 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001017 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 p->kind = FunctionDef_kind;
1020 p->v.FunctionDef.name = name;
1021 p->v.FunctionDef.args = args;
1022 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001023 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001025 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026 return p;
1027}
1028
1029stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001030ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1031 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032{
1033 stmt_ty p;
1034 if (!name) {
1035 PyErr_SetString(PyExc_ValueError,
1036 "field name is required for ClassDef");
1037 return NULL;
1038 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001039 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001040 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 p->kind = ClassDef_kind;
1043 p->v.ClassDef.name = name;
1044 p->v.ClassDef.bases = bases;
1045 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001046 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001048 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 return p;
1050}
1051
1052stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001053Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054{
1055 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001056 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001057 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 p->kind = Return_kind;
1060 p->v.Return.value = value;
1061 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001062 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063 return p;
1064}
1065
1066stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001067Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001068{
1069 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001070 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001071 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 p->kind = Delete_kind;
1074 p->v.Delete.targets = targets;
1075 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001076 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 return p;
1078}
1079
1080stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001081Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1082 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083{
1084 stmt_ty p;
1085 if (!value) {
1086 PyErr_SetString(PyExc_ValueError,
1087 "field value is required for Assign");
1088 return NULL;
1089 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001090 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001091 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093 p->kind = Assign_kind;
1094 p->v.Assign.targets = targets;
1095 p->v.Assign.value = value;
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 +00001102AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1103 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104{
1105 stmt_ty p;
1106 if (!target) {
1107 PyErr_SetString(PyExc_ValueError,
1108 "field target is required for AugAssign");
1109 return NULL;
1110 }
1111 if (!op) {
1112 PyErr_SetString(PyExc_ValueError,
1113 "field op is required for AugAssign");
1114 return NULL;
1115 }
1116 if (!value) {
1117 PyErr_SetString(PyExc_ValueError,
1118 "field value is required for AugAssign");
1119 return NULL;
1120 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001121 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001122 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 p->kind = AugAssign_kind;
1125 p->v.AugAssign.target = target;
1126 p->v.AugAssign.op = op;
1127 p->v.AugAssign.value = value;
1128 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001129 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 return p;
1131}
1132
1133stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001134Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1135 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136{
1137 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001138 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001139 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 p->kind = Print_kind;
1142 p->v.Print.dest = dest;
1143 p->v.Print.values = values;
1144 p->v.Print.nl = nl;
1145 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001146 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return p;
1148}
1149
1150stmt_ty
1151For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153{
1154 stmt_ty p;
1155 if (!target) {
1156 PyErr_SetString(PyExc_ValueError,
1157 "field target is required for For");
1158 return NULL;
1159 }
1160 if (!iter) {
1161 PyErr_SetString(PyExc_ValueError,
1162 "field iter is required for For");
1163 return NULL;
1164 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001166 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 p->kind = For_kind;
1169 p->v.For.target = target;
1170 p->v.For.iter = iter;
1171 p->v.For.body = body;
1172 p->v.For.orelse = orelse;
1173 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001174 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 return p;
1176}
1177
1178stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001179While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1180 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181{
1182 stmt_ty p;
1183 if (!test) {
1184 PyErr_SetString(PyExc_ValueError,
1185 "field test is required for While");
1186 return NULL;
1187 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001188 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001189 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 p->kind = While_kind;
1192 p->v.While.test = test;
1193 p->v.While.body = body;
1194 p->v.While.orelse = orelse;
1195 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001196 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 return p;
1198}
1199
1200stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001201If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1202 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203{
1204 stmt_ty p;
1205 if (!test) {
1206 PyErr_SetString(PyExc_ValueError,
1207 "field test is required for If");
1208 return NULL;
1209 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001210 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001211 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 p->kind = If_kind;
1214 p->v.If.test = test;
1215 p->v.If.body = body;
1216 p->v.If.orelse = orelse;
1217 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001218 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 return p;
1220}
1221
1222stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001223With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001224 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001225{
1226 stmt_ty p;
1227 if (!context_expr) {
1228 PyErr_SetString(PyExc_ValueError,
1229 "field context_expr is required for With");
1230 return NULL;
1231 }
1232 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001233 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001234 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001235 p->kind = With_kind;
1236 p->v.With.context_expr = context_expr;
1237 p->v.With.optional_vars = optional_vars;
1238 p->v.With.body = body;
1239 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001240 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001241 return p;
1242}
1243
1244stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001245Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1246 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247{
1248 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001249 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001250 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 p->kind = Raise_kind;
1253 p->v.Raise.type = type;
1254 p->v.Raise.inst = inst;
1255 p->v.Raise.tback = tback;
1256 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001257 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 return p;
1259}
1260
1261stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001262TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001263 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264{
1265 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001266 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001267 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 p->kind = TryExcept_kind;
1270 p->v.TryExcept.body = body;
1271 p->v.TryExcept.handlers = handlers;
1272 p->v.TryExcept.orelse = orelse;
1273 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001274 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 return p;
1276}
1277
1278stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001279TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1280 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281{
1282 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001283 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001284 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 p->kind = TryFinally_kind;
1287 p->v.TryFinally.body = body;
1288 p->v.TryFinally.finalbody = finalbody;
1289 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001290 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 return p;
1292}
1293
1294stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001295Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296{
1297 stmt_ty p;
1298 if (!test) {
1299 PyErr_SetString(PyExc_ValueError,
1300 "field test is required for Assert");
1301 return NULL;
1302 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001303 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001304 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 p->kind = Assert_kind;
1307 p->v.Assert.test = test;
1308 p->v.Assert.msg = msg;
1309 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001310 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 return p;
1312}
1313
1314stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001315Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316{
1317 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001318 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001319 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 p->kind = Import_kind;
1322 p->v.Import.names = names;
1323 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001324 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325 return p;
1326}
1327
1328stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001329ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1330 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331{
1332 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001334 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 p->kind = ImportFrom_kind;
1337 p->v.ImportFrom.module = module;
1338 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001339 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001341 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 return p;
1343}
1344
1345stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001346Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1347 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348{
1349 stmt_ty p;
1350 if (!body) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field body is required for Exec");
1353 return NULL;
1354 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001356 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 p->kind = Exec_kind;
1359 p->v.Exec.body = body;
1360 p->v.Exec.globals = globals;
1361 p->v.Exec.locals = locals;
1362 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 return p;
1365}
1366
1367stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001368Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369{
1370 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001371 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001372 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 p->kind = Global_kind;
1375 p->v.Global.names = names;
1376 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001377 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378 return p;
1379}
1380
1381stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001382Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383{
1384 stmt_ty p;
1385 if (!value) {
1386 PyErr_SetString(PyExc_ValueError,
1387 "field value is required for Expr");
1388 return NULL;
1389 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001390 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001391 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 p->kind = Expr_kind;
1394 p->v.Expr.value = value;
1395 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001396 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 return p;
1398}
1399
1400stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001401Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402{
1403 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001404 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001405 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 p->kind = Pass_kind;
1408 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001409 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 return p;
1411}
1412
1413stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001414Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415{
1416 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001417 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001418 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 p->kind = Break_kind;
1421 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001422 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 return p;
1424}
1425
1426stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428{
1429 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001430 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001431 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433 p->kind = Continue_kind;
1434 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001435 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 return p;
1437}
1438
1439expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001440BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1441 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442{
1443 expr_ty p;
1444 if (!op) {
1445 PyErr_SetString(PyExc_ValueError,
1446 "field op is required for BoolOp");
1447 return NULL;
1448 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001449 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001450 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452 p->kind = BoolOp_kind;
1453 p->v.BoolOp.op = op;
1454 p->v.BoolOp.values = values;
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 +00001461BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1462 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463{
1464 expr_ty p;
1465 if (!left) {
1466 PyErr_SetString(PyExc_ValueError,
1467 "field left is required for BinOp");
1468 return NULL;
1469 }
1470 if (!op) {
1471 PyErr_SetString(PyExc_ValueError,
1472 "field op is required for BinOp");
1473 return NULL;
1474 }
1475 if (!right) {
1476 PyErr_SetString(PyExc_ValueError,
1477 "field right is required for BinOp");
1478 return NULL;
1479 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001480 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001481 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483 p->kind = BinOp_kind;
1484 p->v.BinOp.left = left;
1485 p->v.BinOp.op = op;
1486 p->v.BinOp.right = right;
1487 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001488 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 return p;
1490}
1491
1492expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001493UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1494 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495{
1496 expr_ty p;
1497 if (!op) {
1498 PyErr_SetString(PyExc_ValueError,
1499 "field op is required for UnaryOp");
1500 return NULL;
1501 }
1502 if (!operand) {
1503 PyErr_SetString(PyExc_ValueError,
1504 "field operand is required for UnaryOp");
1505 return NULL;
1506 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001507 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001508 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 p->kind = UnaryOp_kind;
1511 p->v.UnaryOp.op = op;
1512 p->v.UnaryOp.operand = operand;
1513 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001514 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 return p;
1516}
1517
1518expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001519Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1520 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521{
1522 expr_ty p;
1523 if (!args) {
1524 PyErr_SetString(PyExc_ValueError,
1525 "field args is required for Lambda");
1526 return NULL;
1527 }
1528 if (!body) {
1529 PyErr_SetString(PyExc_ValueError,
1530 "field body is required for Lambda");
1531 return NULL;
1532 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001533 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001534 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 p->kind = Lambda_kind;
1537 p->v.Lambda.args = args;
1538 p->v.Lambda.body = body;
1539 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001540 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 return p;
1542}
1543
1544expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001545IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1546 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001547{
1548 expr_ty p;
1549 if (!test) {
1550 PyErr_SetString(PyExc_ValueError,
1551 "field test is required for IfExp");
1552 return NULL;
1553 }
1554 if (!body) {
1555 PyErr_SetString(PyExc_ValueError,
1556 "field body is required for IfExp");
1557 return NULL;
1558 }
1559 if (!orelse) {
1560 PyErr_SetString(PyExc_ValueError,
1561 "field orelse is required for IfExp");
1562 return NULL;
1563 }
1564 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001565 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001566 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001567 p->kind = IfExp_kind;
1568 p->v.IfExp.test = test;
1569 p->v.IfExp.body = body;
1570 p->v.IfExp.orelse = orelse;
1571 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001572 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001573 return p;
1574}
1575
1576expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001577Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1578 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579{
1580 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001581 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001582 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001584 p->kind = Dict_kind;
1585 p->v.Dict.keys = keys;
1586 p->v.Dict.values = values;
1587 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001588 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001589 return p;
1590}
1591
1592expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001593ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1594 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595{
1596 expr_ty p;
1597 if (!elt) {
1598 PyErr_SetString(PyExc_ValueError,
1599 "field elt is required for ListComp");
1600 return NULL;
1601 }
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 = ListComp_kind;
1606 p->v.ListComp.elt = elt;
1607 p->v.ListComp.generators = generators;
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
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001614GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1615 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616{
1617 expr_ty p;
1618 if (!elt) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "field elt is required for GeneratorExp");
1621 return NULL;
1622 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001623 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001624 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 p->kind = GeneratorExp_kind;
1627 p->v.GeneratorExp.elt = elt;
1628 p->v.GeneratorExp.generators = generators;
1629 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001630 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 return p;
1632}
1633
1634expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001635Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636{
1637 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001639 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 p->kind = Yield_kind;
1642 p->v.Yield.value = value;
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
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001649Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1650 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651{
1652 expr_ty p;
1653 if (!left) {
1654 PyErr_SetString(PyExc_ValueError,
1655 "field left is required for Compare");
1656 return NULL;
1657 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001658 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001659 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 p->kind = Compare_kind;
1662 p->v.Compare.left = left;
1663 p->v.Compare.ops = ops;
1664 p->v.Compare.comparators = comparators;
1665 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001666 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 return p;
1668}
1669
1670expr_ty
1671Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001672 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673{
1674 expr_ty p;
1675 if (!func) {
1676 PyErr_SetString(PyExc_ValueError,
1677 "field func is required for Call");
1678 return NULL;
1679 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001680 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001681 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 p->kind = Call_kind;
1684 p->v.Call.func = func;
1685 p->v.Call.args = args;
1686 p->v.Call.keywords = keywords;
1687 p->v.Call.starargs = starargs;
1688 p->v.Call.kwargs = kwargs;
1689 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001690 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 return p;
1692}
1693
1694expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001695Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696{
1697 expr_ty p;
1698 if (!value) {
1699 PyErr_SetString(PyExc_ValueError,
1700 "field value is required for Repr");
1701 return NULL;
1702 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001703 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001704 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 p->kind = Repr_kind;
1707 p->v.Repr.value = value;
1708 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 return p;
1711}
1712
1713expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001714Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715{
1716 expr_ty p;
1717 if (!n) {
1718 PyErr_SetString(PyExc_ValueError,
1719 "field n is required for Num");
1720 return NULL;
1721 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001722 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001723 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001724 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725 p->kind = Num_kind;
1726 p->v.Num.n = n;
1727 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001728 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 return p;
1730}
1731
1732expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001733Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734{
1735 expr_ty p;
1736 if (!s) {
1737 PyErr_SetString(PyExc_ValueError,
1738 "field s is required for Str");
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 = Str_kind;
1745 p->v.Str.s = s;
1746 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001747 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748 return p;
1749}
1750
1751expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001752Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1753 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754{
1755 expr_ty p;
1756 if (!value) {
1757 PyErr_SetString(PyExc_ValueError,
1758 "field value is required for Attribute");
1759 return NULL;
1760 }
1761 if (!attr) {
1762 PyErr_SetString(PyExc_ValueError,
1763 "field attr is required for Attribute");
1764 return NULL;
1765 }
1766 if (!ctx) {
1767 PyErr_SetString(PyExc_ValueError,
1768 "field ctx is required for Attribute");
1769 return NULL;
1770 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001771 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001772 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 p->kind = Attribute_kind;
1775 p->v.Attribute.value = value;
1776 p->v.Attribute.attr = attr;
1777 p->v.Attribute.ctx = ctx;
1778 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001779 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 return p;
1781}
1782
1783expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001784Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1785 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786{
1787 expr_ty p;
1788 if (!value) {
1789 PyErr_SetString(PyExc_ValueError,
1790 "field value is required for Subscript");
1791 return NULL;
1792 }
1793 if (!slice) {
1794 PyErr_SetString(PyExc_ValueError,
1795 "field slice is required for Subscript");
1796 return NULL;
1797 }
1798 if (!ctx) {
1799 PyErr_SetString(PyExc_ValueError,
1800 "field ctx is required for Subscript");
1801 return NULL;
1802 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001803 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001804 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 p->kind = Subscript_kind;
1807 p->v.Subscript.value = value;
1808 p->v.Subscript.slice = slice;
1809 p->v.Subscript.ctx = ctx;
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 +00001816Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1817 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818{
1819 expr_ty p;
1820 if (!id) {
1821 PyErr_SetString(PyExc_ValueError,
1822 "field id is required for Name");
1823 return NULL;
1824 }
1825 if (!ctx) {
1826 PyErr_SetString(PyExc_ValueError,
1827 "field ctx is required for Name");
1828 return NULL;
1829 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001830 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001831 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 p->kind = Name_kind;
1834 p->v.Name.id = id;
1835 p->v.Name.ctx = ctx;
1836 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001837 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 return p;
1839}
1840
1841expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001842List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1843 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844{
1845 expr_ty p;
1846 if (!ctx) {
1847 PyErr_SetString(PyExc_ValueError,
1848 "field ctx is required for List");
1849 return NULL;
1850 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001851 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001852 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 p->kind = List_kind;
1855 p->v.List.elts = elts;
1856 p->v.List.ctx = ctx;
1857 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001858 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 return p;
1860}
1861
1862expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001863Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1864 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865{
1866 expr_ty p;
1867 if (!ctx) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field ctx is required for Tuple");
1870 return NULL;
1871 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001873 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 p->kind = Tuple_kind;
1876 p->v.Tuple.elts = elts;
1877 p->v.Tuple.ctx = ctx;
1878 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001879 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 return p;
1881}
1882
1883slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001884Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885{
1886 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001887 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001888 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890 p->kind = Ellipsis_kind;
1891 return p;
1892}
1893
1894slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001895Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896{
1897 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001898 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001899 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 p->kind = Slice_kind;
1902 p->v.Slice.lower = lower;
1903 p->v.Slice.upper = upper;
1904 p->v.Slice.step = step;
1905 return p;
1906}
1907
1908slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001909ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910{
1911 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001912 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001913 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 p->kind = ExtSlice_kind;
1916 p->v.ExtSlice.dims = dims;
1917 return p;
1918}
1919
1920slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001921Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922{
1923 slice_ty p;
1924 if (!value) {
1925 PyErr_SetString(PyExc_ValueError,
1926 "field value is required for Index");
1927 return NULL;
1928 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001929 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001930 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 p->kind = Index_kind;
1933 p->v.Index.value = value;
1934 return p;
1935}
1936
1937comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001938comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939{
1940 comprehension_ty p;
1941 if (!target) {
1942 PyErr_SetString(PyExc_ValueError,
1943 "field target is required for comprehension");
1944 return NULL;
1945 }
1946 if (!iter) {
1947 PyErr_SetString(PyExc_ValueError,
1948 "field iter is required for comprehension");
1949 return NULL;
1950 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001951 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001952 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 p->target = target;
1955 p->iter = iter;
1956 p->ifs = ifs;
1957 return p;
1958}
1959
1960excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001961ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001962 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963{
1964 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001965 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001966 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001968 p->kind = ExceptHandler_kind;
1969 p->v.ExceptHandler.type = type;
1970 p->v.ExceptHandler.name = name;
1971 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001972 p->lineno = lineno;
1973 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 return p;
1975}
1976
1977arguments_ty
1978arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001979 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980{
1981 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001982 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001983 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 p->args = args;
1986 p->vararg = vararg;
1987 p->kwarg = kwarg;
1988 p->defaults = defaults;
1989 return p;
1990}
1991
1992keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994{
1995 keyword_ty p;
1996 if (!arg) {
1997 PyErr_SetString(PyExc_ValueError,
1998 "field arg is required for keyword");
1999 return NULL;
2000 }
2001 if (!value) {
2002 PyErr_SetString(PyExc_ValueError,
2003 "field value is required for keyword");
2004 return NULL;
2005 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002006 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002007 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 p->arg = arg;
2010 p->value = value;
2011 return p;
2012}
2013
2014alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002015alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016{
2017 alias_ty p;
2018 if (!name) {
2019 PyErr_SetString(PyExc_ValueError,
2020 "field name is required for alias");
2021 return NULL;
2022 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002023 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002024 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 p->name = name;
2027 p->asname = asname;
2028 return p;
2029}
2030
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002031
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002032PyObject*
2033ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002034{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002035 mod_ty o = (mod_ty)_o;
2036 PyObject *result = NULL, *value = NULL;
2037 if (!o) {
2038 Py_INCREF(Py_None);
2039 return Py_None;
2040 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 switch (o->kind) {
2043 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002044 result = PyType_GenericNew(Module_type, NULL, NULL);
2045 if (!result) goto failed;
2046 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2047 if (!value) goto failed;
2048 if (PyObject_SetAttrString(result, "body", value) == -1)
2049 goto failed;
2050 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 break;
2052 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002053 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2054 if (!result) goto failed;
2055 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2056 if (!value) goto failed;
2057 if (PyObject_SetAttrString(result, "body", value) == -1)
2058 goto failed;
2059 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 break;
2061 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002062 result = PyType_GenericNew(Expression_type, NULL, NULL);
2063 if (!result) goto failed;
2064 value = ast2obj_expr(o->v.Expression.body);
2065 if (!value) goto failed;
2066 if (PyObject_SetAttrString(result, "body", value) == -1)
2067 goto failed;
2068 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 break;
2070 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002071 result = PyType_GenericNew(Suite_type, NULL, NULL);
2072 if (!result) goto failed;
2073 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2074 if (!value) goto failed;
2075 if (PyObject_SetAttrString(result, "body", value) == -1)
2076 goto failed;
2077 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 break;
2079 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002080 return result;
2081failed:
2082 Py_XDECREF(value);
2083 Py_XDECREF(result);
2084 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085}
2086
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002087PyObject*
2088ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002090 stmt_ty o = (stmt_ty)_o;
2091 PyObject *result = NULL, *value = NULL;
2092 if (!o) {
2093 Py_INCREF(Py_None);
2094 return Py_None;
2095 }
2096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 switch (o->kind) {
2098 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002099 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2100 if (!result) goto failed;
2101 value = ast2obj_identifier(o->v.FunctionDef.name);
2102 if (!value) goto failed;
2103 if (PyObject_SetAttrString(result, "name", value) == -1)
2104 goto failed;
2105 Py_DECREF(value);
2106 value = ast2obj_arguments(o->v.FunctionDef.args);
2107 if (!value) goto failed;
2108 if (PyObject_SetAttrString(result, "args", value) == -1)
2109 goto failed;
2110 Py_DECREF(value);
2111 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2112 if (!value) goto failed;
2113 if (PyObject_SetAttrString(result, "body", value) == -1)
2114 goto failed;
2115 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002116 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2117 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002118 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002119 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2120 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002121 goto failed;
2122 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 break;
2124 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002125 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2126 if (!result) goto failed;
2127 value = ast2obj_identifier(o->v.ClassDef.name);
2128 if (!value) goto failed;
2129 if (PyObject_SetAttrString(result, "name", value) == -1)
2130 goto failed;
2131 Py_DECREF(value);
2132 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2133 if (!value) goto failed;
2134 if (PyObject_SetAttrString(result, "bases", value) == -1)
2135 goto failed;
2136 Py_DECREF(value);
2137 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2138 if (!value) goto failed;
2139 if (PyObject_SetAttrString(result, "body", value) == -1)
2140 goto failed;
2141 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002142 value = ast2obj_list(o->v.ClassDef.decorator_list,
2143 ast2obj_expr);
2144 if (!value) goto failed;
2145 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2146 -1)
2147 goto failed;
2148 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 break;
2150 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002151 result = PyType_GenericNew(Return_type, NULL, NULL);
2152 if (!result) goto failed;
2153 value = ast2obj_expr(o->v.Return.value);
2154 if (!value) goto failed;
2155 if (PyObject_SetAttrString(result, "value", value) == -1)
2156 goto failed;
2157 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 break;
2159 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002160 result = PyType_GenericNew(Delete_type, NULL, NULL);
2161 if (!result) goto failed;
2162 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2163 if (!value) goto failed;
2164 if (PyObject_SetAttrString(result, "targets", value) == -1)
2165 goto failed;
2166 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 break;
2168 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002169 result = PyType_GenericNew(Assign_type, NULL, NULL);
2170 if (!result) goto failed;
2171 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2172 if (!value) goto failed;
2173 if (PyObject_SetAttrString(result, "targets", value) == -1)
2174 goto failed;
2175 Py_DECREF(value);
2176 value = ast2obj_expr(o->v.Assign.value);
2177 if (!value) goto failed;
2178 if (PyObject_SetAttrString(result, "value", value) == -1)
2179 goto failed;
2180 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 break;
2182 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002183 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2184 if (!result) goto failed;
2185 value = ast2obj_expr(o->v.AugAssign.target);
2186 if (!value) goto failed;
2187 if (PyObject_SetAttrString(result, "target", value) == -1)
2188 goto failed;
2189 Py_DECREF(value);
2190 value = ast2obj_operator(o->v.AugAssign.op);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "op", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 value = ast2obj_expr(o->v.AugAssign.value);
2196 if (!value) goto failed;
2197 if (PyObject_SetAttrString(result, "value", value) == -1)
2198 goto failed;
2199 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 break;
2201 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002202 result = PyType_GenericNew(Print_type, NULL, NULL);
2203 if (!result) goto failed;
2204 value = ast2obj_expr(o->v.Print.dest);
2205 if (!value) goto failed;
2206 if (PyObject_SetAttrString(result, "dest", value) == -1)
2207 goto failed;
2208 Py_DECREF(value);
2209 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2210 if (!value) goto failed;
2211 if (PyObject_SetAttrString(result, "values", value) == -1)
2212 goto failed;
2213 Py_DECREF(value);
2214 value = ast2obj_bool(o->v.Print.nl);
2215 if (!value) goto failed;
2216 if (PyObject_SetAttrString(result, "nl", value) == -1)
2217 goto failed;
2218 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 break;
2220 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002221 result = PyType_GenericNew(For_type, NULL, NULL);
2222 if (!result) goto failed;
2223 value = ast2obj_expr(o->v.For.target);
2224 if (!value) goto failed;
2225 if (PyObject_SetAttrString(result, "target", value) == -1)
2226 goto failed;
2227 Py_DECREF(value);
2228 value = ast2obj_expr(o->v.For.iter);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "iter", value) == -1)
2231 goto failed;
2232 Py_DECREF(value);
2233 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "body", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
2238 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 break;
2244 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002245 result = PyType_GenericNew(While_type, NULL, NULL);
2246 if (!result) goto failed;
2247 value = ast2obj_expr(o->v.While.test);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "test", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
2252 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "body", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
2257 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 break;
2263 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002264 result = PyType_GenericNew(If_type, NULL, NULL);
2265 if (!result) goto failed;
2266 value = ast2obj_expr(o->v.If.test);
2267 if (!value) goto failed;
2268 if (PyObject_SetAttrString(result, "test", value) == -1)
2269 goto failed;
2270 Py_DECREF(value);
2271 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2272 if (!value) goto failed;
2273 if (PyObject_SetAttrString(result, "body", value) == -1)
2274 goto failed;
2275 Py_DECREF(value);
2276 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2277 if (!value) goto failed;
2278 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2279 goto failed;
2280 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002282 case With_kind:
2283 result = PyType_GenericNew(With_type, NULL, NULL);
2284 if (!result) goto failed;
2285 value = ast2obj_expr(o->v.With.context_expr);
2286 if (!value) goto failed;
2287 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2288 goto failed;
2289 Py_DECREF(value);
2290 value = ast2obj_expr(o->v.With.optional_vars);
2291 if (!value) goto failed;
2292 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2293 -1)
2294 goto failed;
2295 Py_DECREF(value);
2296 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2297 if (!value) goto failed;
2298 if (PyObject_SetAttrString(result, "body", value) == -1)
2299 goto failed;
2300 Py_DECREF(value);
2301 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002303 result = PyType_GenericNew(Raise_type, NULL, NULL);
2304 if (!result) goto failed;
2305 value = ast2obj_expr(o->v.Raise.type);
2306 if (!value) goto failed;
2307 if (PyObject_SetAttrString(result, "type", value) == -1)
2308 goto failed;
2309 Py_DECREF(value);
2310 value = ast2obj_expr(o->v.Raise.inst);
2311 if (!value) goto failed;
2312 if (PyObject_SetAttrString(result, "inst", value) == -1)
2313 goto failed;
2314 Py_DECREF(value);
2315 value = ast2obj_expr(o->v.Raise.tback);
2316 if (!value) goto failed;
2317 if (PyObject_SetAttrString(result, "tback", value) == -1)
2318 goto failed;
2319 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 break;
2321 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002322 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2323 if (!result) goto failed;
2324 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2325 if (!value) goto failed;
2326 if (PyObject_SetAttrString(result, "body", value) == -1)
2327 goto failed;
2328 Py_DECREF(value);
2329 value = ast2obj_list(o->v.TryExcept.handlers,
2330 ast2obj_excepthandler);
2331 if (!value) goto failed;
2332 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2333 goto failed;
2334 Py_DECREF(value);
2335 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2336 if (!value) goto failed;
2337 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2338 goto failed;
2339 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 break;
2341 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002342 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2343 if (!result) goto failed;
2344 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "body", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 break;
2355 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002356 result = PyType_GenericNew(Assert_type, NULL, NULL);
2357 if (!result) goto failed;
2358 value = ast2obj_expr(o->v.Assert.test);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "test", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 value = ast2obj_expr(o->v.Assert.msg);
2364 if (!value) goto failed;
2365 if (PyObject_SetAttrString(result, "msg", value) == -1)
2366 goto failed;
2367 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 break;
2369 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002370 result = PyType_GenericNew(Import_type, NULL, NULL);
2371 if (!result) goto failed;
2372 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "names", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 break;
2378 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002379 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2380 if (!result) goto failed;
2381 value = ast2obj_identifier(o->v.ImportFrom.module);
2382 if (!value) goto failed;
2383 if (PyObject_SetAttrString(result, "module", value) == -1)
2384 goto failed;
2385 Py_DECREF(value);
2386 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2387 if (!value) goto failed;
2388 if (PyObject_SetAttrString(result, "names", value) == -1)
2389 goto failed;
2390 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002391 value = ast2obj_int(o->v.ImportFrom.level);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "level", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 break;
2397 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002398 result = PyType_GenericNew(Exec_type, NULL, NULL);
2399 if (!result) goto failed;
2400 value = ast2obj_expr(o->v.Exec.body);
2401 if (!value) goto failed;
2402 if (PyObject_SetAttrString(result, "body", value) == -1)
2403 goto failed;
2404 Py_DECREF(value);
2405 value = ast2obj_expr(o->v.Exec.globals);
2406 if (!value) goto failed;
2407 if (PyObject_SetAttrString(result, "globals", value) == -1)
2408 goto failed;
2409 Py_DECREF(value);
2410 value = ast2obj_expr(o->v.Exec.locals);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "locals", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 break;
2416 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002417 result = PyType_GenericNew(Global_type, NULL, NULL);
2418 if (!result) goto failed;
2419 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2420 if (!value) goto failed;
2421 if (PyObject_SetAttrString(result, "names", value) == -1)
2422 goto failed;
2423 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 break;
2425 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426 result = PyType_GenericNew(Expr_type, NULL, NULL);
2427 if (!result) goto failed;
2428 value = ast2obj_expr(o->v.Expr.value);
2429 if (!value) goto failed;
2430 if (PyObject_SetAttrString(result, "value", value) == -1)
2431 goto failed;
2432 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 break;
2434 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002435 result = PyType_GenericNew(Pass_type, NULL, NULL);
2436 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 break;
2438 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002439 result = PyType_GenericNew(Break_type, NULL, NULL);
2440 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 break;
2442 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002443 result = PyType_GenericNew(Continue_type, NULL, NULL);
2444 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 break;
2446 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002447 value = ast2obj_int(o->lineno);
2448 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002449 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2450 goto failed;
2451 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002452 value = ast2obj_int(o->col_offset);
2453 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002454 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2455 goto failed;
2456 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002457 return result;
2458failed:
2459 Py_XDECREF(value);
2460 Py_XDECREF(result);
2461 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462}
2463
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002464PyObject*
2465ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002467 expr_ty o = (expr_ty)_o;
2468 PyObject *result = NULL, *value = NULL;
2469 if (!o) {
2470 Py_INCREF(Py_None);
2471 return Py_None;
2472 }
2473
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 switch (o->kind) {
2475 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002476 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2477 if (!result) goto failed;
2478 value = ast2obj_boolop(o->v.BoolOp.op);
2479 if (!value) goto failed;
2480 if (PyObject_SetAttrString(result, "op", value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
2483 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2484 if (!value) goto failed;
2485 if (PyObject_SetAttrString(result, "values", value) == -1)
2486 goto failed;
2487 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 break;
2489 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002490 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2491 if (!result) goto failed;
2492 value = ast2obj_expr(o->v.BinOp.left);
2493 if (!value) goto failed;
2494 if (PyObject_SetAttrString(result, "left", value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
2497 value = ast2obj_operator(o->v.BinOp.op);
2498 if (!value) goto failed;
2499 if (PyObject_SetAttrString(result, "op", value) == -1)
2500 goto failed;
2501 Py_DECREF(value);
2502 value = ast2obj_expr(o->v.BinOp.right);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "right", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 break;
2508 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002509 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2510 if (!result) goto failed;
2511 value = ast2obj_unaryop(o->v.UnaryOp.op);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "op", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 value = ast2obj_expr(o->v.UnaryOp.operand);
2517 if (!value) goto failed;
2518 if (PyObject_SetAttrString(result, "operand", value) == -1)
2519 goto failed;
2520 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 break;
2522 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002523 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2524 if (!result) goto failed;
2525 value = ast2obj_arguments(o->v.Lambda.args);
2526 if (!value) goto failed;
2527 if (PyObject_SetAttrString(result, "args", value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
2530 value = ast2obj_expr(o->v.Lambda.body);
2531 if (!value) goto failed;
2532 if (PyObject_SetAttrString(result, "body", value) == -1)
2533 goto failed;
2534 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002536 case IfExp_kind:
2537 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2538 if (!result) goto failed;
2539 value = ast2obj_expr(o->v.IfExp.test);
2540 if (!value) goto failed;
2541 if (PyObject_SetAttrString(result, "test", value) == -1)
2542 goto failed;
2543 Py_DECREF(value);
2544 value = ast2obj_expr(o->v.IfExp.body);
2545 if (!value) goto failed;
2546 if (PyObject_SetAttrString(result, "body", value) == -1)
2547 goto failed;
2548 Py_DECREF(value);
2549 value = ast2obj_expr(o->v.IfExp.orelse);
2550 if (!value) goto failed;
2551 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002556 result = PyType_GenericNew(Dict_type, NULL, NULL);
2557 if (!result) goto failed;
2558 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2559 if (!value) goto failed;
2560 if (PyObject_SetAttrString(result, "keys", value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
2563 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2564 if (!value) goto failed;
2565 if (PyObject_SetAttrString(result, "values", value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 break;
2569 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002570 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2571 if (!result) goto failed;
2572 value = ast2obj_expr(o->v.ListComp.elt);
2573 if (!value) goto failed;
2574 if (PyObject_SetAttrString(result, "elt", value) == -1)
2575 goto failed;
2576 Py_DECREF(value);
2577 value = ast2obj_list(o->v.ListComp.generators,
2578 ast2obj_comprehension);
2579 if (!value) goto failed;
2580 if (PyObject_SetAttrString(result, "generators", value) == -1)
2581 goto failed;
2582 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 break;
2584 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002585 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2586 if (!result) goto failed;
2587 value = ast2obj_expr(o->v.GeneratorExp.elt);
2588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "elt", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 value = ast2obj_list(o->v.GeneratorExp.generators,
2593 ast2obj_comprehension);
2594 if (!value) goto failed;
2595 if (PyObject_SetAttrString(result, "generators", value) == -1)
2596 goto failed;
2597 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598 break;
2599 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002600 result = PyType_GenericNew(Yield_type, NULL, NULL);
2601 if (!result) goto failed;
2602 value = ast2obj_expr(o->v.Yield.value);
2603 if (!value) goto failed;
2604 if (PyObject_SetAttrString(result, "value", value) == -1)
2605 goto failed;
2606 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 break;
2608 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002609 result = PyType_GenericNew(Compare_type, NULL, NULL);
2610 if (!result) goto failed;
2611 value = ast2obj_expr(o->v.Compare.left);
2612 if (!value) goto failed;
2613 if (PyObject_SetAttrString(result, "left", value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002616 {
2617 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2618 value = PyList_New(n);
2619 if (!value) goto failed;
2620 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002621 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 +00002622 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002623 if (!value) goto failed;
2624 if (PyObject_SetAttrString(result, "ops", value) == -1)
2625 goto failed;
2626 Py_DECREF(value);
2627 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2628 if (!value) goto failed;
2629 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 break;
2633 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002634 result = PyType_GenericNew(Call_type, NULL, NULL);
2635 if (!result) goto failed;
2636 value = ast2obj_expr(o->v.Call.func);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "func", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
2641 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2642 if (!value) goto failed;
2643 if (PyObject_SetAttrString(result, "args", value) == -1)
2644 goto failed;
2645 Py_DECREF(value);
2646 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2647 if (!value) goto failed;
2648 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2649 goto failed;
2650 Py_DECREF(value);
2651 value = ast2obj_expr(o->v.Call.starargs);
2652 if (!value) goto failed;
2653 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2654 goto failed;
2655 Py_DECREF(value);
2656 value = ast2obj_expr(o->v.Call.kwargs);
2657 if (!value) goto failed;
2658 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2659 goto failed;
2660 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 break;
2662 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002663 result = PyType_GenericNew(Repr_type, NULL, NULL);
2664 if (!result) goto failed;
2665 value = ast2obj_expr(o->v.Repr.value);
2666 if (!value) goto failed;
2667 if (PyObject_SetAttrString(result, "value", value) == -1)
2668 goto failed;
2669 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 break;
2671 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002672 result = PyType_GenericNew(Num_type, NULL, NULL);
2673 if (!result) goto failed;
2674 value = ast2obj_object(o->v.Num.n);
2675 if (!value) goto failed;
2676 if (PyObject_SetAttrString(result, "n", value) == -1)
2677 goto failed;
2678 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 break;
2680 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002681 result = PyType_GenericNew(Str_type, NULL, NULL);
2682 if (!result) goto failed;
2683 value = ast2obj_string(o->v.Str.s);
2684 if (!value) goto failed;
2685 if (PyObject_SetAttrString(result, "s", value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 break;
2689 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002690 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2691 if (!result) goto failed;
2692 value = ast2obj_expr(o->v.Attribute.value);
2693 if (!value) goto failed;
2694 if (PyObject_SetAttrString(result, "value", value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
2697 value = ast2obj_identifier(o->v.Attribute.attr);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "attr", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
2702 value = ast2obj_expr_context(o->v.Attribute.ctx);
2703 if (!value) goto failed;
2704 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2705 goto failed;
2706 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 break;
2708 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002709 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2710 if (!result) goto failed;
2711 value = ast2obj_expr(o->v.Subscript.value);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "value", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
2716 value = ast2obj_slice(o->v.Subscript.slice);
2717 if (!value) goto failed;
2718 if (PyObject_SetAttrString(result, "slice", value) == -1)
2719 goto failed;
2720 Py_DECREF(value);
2721 value = ast2obj_expr_context(o->v.Subscript.ctx);
2722 if (!value) goto failed;
2723 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2724 goto failed;
2725 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 break;
2727 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002728 result = PyType_GenericNew(Name_type, NULL, NULL);
2729 if (!result) goto failed;
2730 value = ast2obj_identifier(o->v.Name.id);
2731 if (!value) goto failed;
2732 if (PyObject_SetAttrString(result, "id", value) == -1)
2733 goto failed;
2734 Py_DECREF(value);
2735 value = ast2obj_expr_context(o->v.Name.ctx);
2736 if (!value) goto failed;
2737 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2738 goto failed;
2739 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740 break;
2741 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002742 result = PyType_GenericNew(List_type, NULL, NULL);
2743 if (!result) goto failed;
2744 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2745 if (!value) goto failed;
2746 if (PyObject_SetAttrString(result, "elts", value) == -1)
2747 goto failed;
2748 Py_DECREF(value);
2749 value = ast2obj_expr_context(o->v.List.ctx);
2750 if (!value) goto failed;
2751 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2752 goto failed;
2753 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 break;
2755 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002756 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2757 if (!result) goto failed;
2758 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2759 if (!value) goto failed;
2760 if (PyObject_SetAttrString(result, "elts", value) == -1)
2761 goto failed;
2762 Py_DECREF(value);
2763 value = ast2obj_expr_context(o->v.Tuple.ctx);
2764 if (!value) goto failed;
2765 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2766 goto failed;
2767 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 break;
2769 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002770 value = ast2obj_int(o->lineno);
2771 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002772 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2773 goto failed;
2774 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002775 value = ast2obj_int(o->col_offset);
2776 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002777 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2778 goto failed;
2779 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002780 return result;
2781failed:
2782 Py_XDECREF(value);
2783 Py_XDECREF(result);
2784 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785}
2786
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002787PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002789 switch(o) {
2790 case Load:
2791 Py_INCREF(Load_singleton);
2792 return Load_singleton;
2793 case Store:
2794 Py_INCREF(Store_singleton);
2795 return Store_singleton;
2796 case Del:
2797 Py_INCREF(Del_singleton);
2798 return Del_singleton;
2799 case AugLoad:
2800 Py_INCREF(AugLoad_singleton);
2801 return AugLoad_singleton;
2802 case AugStore:
2803 Py_INCREF(AugStore_singleton);
2804 return AugStore_singleton;
2805 case Param:
2806 Py_INCREF(Param_singleton);
2807 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002808 default:
2809 /* should never happen, but just in case ... */
2810 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2811 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002814PyObject*
2815ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002817 slice_ty o = (slice_ty)_o;
2818 PyObject *result = NULL, *value = NULL;
2819 if (!o) {
2820 Py_INCREF(Py_None);
2821 return Py_None;
2822 }
2823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 switch (o->kind) {
2825 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002826 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2827 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 break;
2829 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002830 result = PyType_GenericNew(Slice_type, NULL, NULL);
2831 if (!result) goto failed;
2832 value = ast2obj_expr(o->v.Slice.lower);
2833 if (!value) goto failed;
2834 if (PyObject_SetAttrString(result, "lower", value) == -1)
2835 goto failed;
2836 Py_DECREF(value);
2837 value = ast2obj_expr(o->v.Slice.upper);
2838 if (!value) goto failed;
2839 if (PyObject_SetAttrString(result, "upper", value) == -1)
2840 goto failed;
2841 Py_DECREF(value);
2842 value = ast2obj_expr(o->v.Slice.step);
2843 if (!value) goto failed;
2844 if (PyObject_SetAttrString(result, "step", value) == -1)
2845 goto failed;
2846 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 break;
2848 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002849 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2850 if (!result) goto failed;
2851 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2852 if (!value) goto failed;
2853 if (PyObject_SetAttrString(result, "dims", value) == -1)
2854 goto failed;
2855 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 break;
2857 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002858 result = PyType_GenericNew(Index_type, NULL, NULL);
2859 if (!result) goto failed;
2860 value = ast2obj_expr(o->v.Index.value);
2861 if (!value) goto failed;
2862 if (PyObject_SetAttrString(result, "value", value) == -1)
2863 goto failed;
2864 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865 break;
2866 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002867 return result;
2868failed:
2869 Py_XDECREF(value);
2870 Py_XDECREF(result);
2871 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002872}
2873
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002874PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002876 switch(o) {
2877 case And:
2878 Py_INCREF(And_singleton);
2879 return And_singleton;
2880 case Or:
2881 Py_INCREF(Or_singleton);
2882 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002883 default:
2884 /* should never happen, but just in case ... */
2885 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2886 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002888}
2889PyObject* ast2obj_operator(operator_ty o)
2890{
2891 switch(o) {
2892 case Add:
2893 Py_INCREF(Add_singleton);
2894 return Add_singleton;
2895 case Sub:
2896 Py_INCREF(Sub_singleton);
2897 return Sub_singleton;
2898 case Mult:
2899 Py_INCREF(Mult_singleton);
2900 return Mult_singleton;
2901 case Div:
2902 Py_INCREF(Div_singleton);
2903 return Div_singleton;
2904 case Mod:
2905 Py_INCREF(Mod_singleton);
2906 return Mod_singleton;
2907 case Pow:
2908 Py_INCREF(Pow_singleton);
2909 return Pow_singleton;
2910 case LShift:
2911 Py_INCREF(LShift_singleton);
2912 return LShift_singleton;
2913 case RShift:
2914 Py_INCREF(RShift_singleton);
2915 return RShift_singleton;
2916 case BitOr:
2917 Py_INCREF(BitOr_singleton);
2918 return BitOr_singleton;
2919 case BitXor:
2920 Py_INCREF(BitXor_singleton);
2921 return BitXor_singleton;
2922 case BitAnd:
2923 Py_INCREF(BitAnd_singleton);
2924 return BitAnd_singleton;
2925 case FloorDiv:
2926 Py_INCREF(FloorDiv_singleton);
2927 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002928 default:
2929 /* should never happen, but just in case ... */
2930 PyErr_Format(PyExc_SystemError, "unknown operator found");
2931 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002932 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002933}
2934PyObject* ast2obj_unaryop(unaryop_ty o)
2935{
2936 switch(o) {
2937 case Invert:
2938 Py_INCREF(Invert_singleton);
2939 return Invert_singleton;
2940 case Not:
2941 Py_INCREF(Not_singleton);
2942 return Not_singleton;
2943 case UAdd:
2944 Py_INCREF(UAdd_singleton);
2945 return UAdd_singleton;
2946 case USub:
2947 Py_INCREF(USub_singleton);
2948 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002949 default:
2950 /* should never happen, but just in case ... */
2951 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2952 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002953 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002954}
2955PyObject* ast2obj_cmpop(cmpop_ty o)
2956{
2957 switch(o) {
2958 case Eq:
2959 Py_INCREF(Eq_singleton);
2960 return Eq_singleton;
2961 case NotEq:
2962 Py_INCREF(NotEq_singleton);
2963 return NotEq_singleton;
2964 case Lt:
2965 Py_INCREF(Lt_singleton);
2966 return Lt_singleton;
2967 case LtE:
2968 Py_INCREF(LtE_singleton);
2969 return LtE_singleton;
2970 case Gt:
2971 Py_INCREF(Gt_singleton);
2972 return Gt_singleton;
2973 case GtE:
2974 Py_INCREF(GtE_singleton);
2975 return GtE_singleton;
2976 case Is:
2977 Py_INCREF(Is_singleton);
2978 return Is_singleton;
2979 case IsNot:
2980 Py_INCREF(IsNot_singleton);
2981 return IsNot_singleton;
2982 case In:
2983 Py_INCREF(In_singleton);
2984 return In_singleton;
2985 case NotIn:
2986 Py_INCREF(NotIn_singleton);
2987 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002988 default:
2989 /* should never happen, but just in case ... */
2990 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2991 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002992 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002993}
2994PyObject*
2995ast2obj_comprehension(void* _o)
2996{
2997 comprehension_ty o = (comprehension_ty)_o;
2998 PyObject *result = NULL, *value = NULL;
2999 if (!o) {
3000 Py_INCREF(Py_None);
3001 return Py_None;
3002 }
3003
3004 result = PyType_GenericNew(comprehension_type, NULL, NULL);
3005 if (!result) return NULL;
3006 value = ast2obj_expr(o->target);
3007 if (!value) goto failed;
3008 if (PyObject_SetAttrString(result, "target", value) == -1)
3009 goto failed;
3010 Py_DECREF(value);
3011 value = ast2obj_expr(o->iter);
3012 if (!value) goto failed;
3013 if (PyObject_SetAttrString(result, "iter", value) == -1)
3014 goto failed;
3015 Py_DECREF(value);
3016 value = ast2obj_list(o->ifs, ast2obj_expr);
3017 if (!value) goto failed;
3018 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3019 goto failed;
3020 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003021 return result;
3022failed:
3023 Py_XDECREF(value);
3024 Py_XDECREF(result);
3025 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026}
3027
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003028PyObject*
3029ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003031 excepthandler_ty o = (excepthandler_ty)_o;
3032 PyObject *result = NULL, *value = NULL;
3033 if (!o) {
3034 Py_INCREF(Py_None);
3035 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003037
Georg Brandla48f3ab2008-03-30 06:40:17 +00003038 switch (o->kind) {
3039 case ExceptHandler_kind:
3040 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3041 if (!result) goto failed;
3042 value = ast2obj_expr(o->v.ExceptHandler.type);
3043 if (!value) goto failed;
3044 if (PyObject_SetAttrString(result, "type", value) == -1)
3045 goto failed;
3046 Py_DECREF(value);
3047 value = ast2obj_expr(o->v.ExceptHandler.name);
3048 if (!value) goto failed;
3049 if (PyObject_SetAttrString(result, "name", value) == -1)
3050 goto failed;
3051 Py_DECREF(value);
3052 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3053 if (!value) goto failed;
3054 if (PyObject_SetAttrString(result, "body", value) == -1)
3055 goto failed;
3056 Py_DECREF(value);
3057 break;
3058 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003059 value = ast2obj_int(o->lineno);
3060 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003061 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003062 goto failed;
3063 Py_DECREF(value);
3064 value = ast2obj_int(o->col_offset);
3065 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003066 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003067 goto failed;
3068 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003069 return result;
3070failed:
3071 Py_XDECREF(value);
3072 Py_XDECREF(result);
3073 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074}
3075
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003076PyObject*
3077ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003079 arguments_ty o = (arguments_ty)_o;
3080 PyObject *result = NULL, *value = NULL;
3081 if (!o) {
3082 Py_INCREF(Py_None);
3083 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003085
3086 result = PyType_GenericNew(arguments_type, NULL, NULL);
3087 if (!result) return NULL;
3088 value = ast2obj_list(o->args, ast2obj_expr);
3089 if (!value) goto failed;
3090 if (PyObject_SetAttrString(result, "args", value) == -1)
3091 goto failed;
3092 Py_DECREF(value);
3093 value = ast2obj_identifier(o->vararg);
3094 if (!value) goto failed;
3095 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3096 goto failed;
3097 Py_DECREF(value);
3098 value = ast2obj_identifier(o->kwarg);
3099 if (!value) goto failed;
3100 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3101 goto failed;
3102 Py_DECREF(value);
3103 value = ast2obj_list(o->defaults, ast2obj_expr);
3104 if (!value) goto failed;
3105 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3106 goto failed;
3107 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003108 return result;
3109failed:
3110 Py_XDECREF(value);
3111 Py_XDECREF(result);
3112 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113}
3114
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003115PyObject*
3116ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003118 keyword_ty o = (keyword_ty)_o;
3119 PyObject *result = NULL, *value = NULL;
3120 if (!o) {
3121 Py_INCREF(Py_None);
3122 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003124
3125 result = PyType_GenericNew(keyword_type, NULL, NULL);
3126 if (!result) return NULL;
3127 value = ast2obj_identifier(o->arg);
3128 if (!value) goto failed;
3129 if (PyObject_SetAttrString(result, "arg", value) == -1)
3130 goto failed;
3131 Py_DECREF(value);
3132 value = ast2obj_expr(o->value);
3133 if (!value) goto failed;
3134 if (PyObject_SetAttrString(result, "value", value) == -1)
3135 goto failed;
3136 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003137 return result;
3138failed:
3139 Py_XDECREF(value);
3140 Py_XDECREF(result);
3141 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142}
3143
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003144PyObject*
3145ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003147 alias_ty o = (alias_ty)_o;
3148 PyObject *result = NULL, *value = NULL;
3149 if (!o) {
3150 Py_INCREF(Py_None);
3151 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003153
3154 result = PyType_GenericNew(alias_type, NULL, NULL);
3155 if (!result) return NULL;
3156 value = ast2obj_identifier(o->name);
3157 if (!value) goto failed;
3158 if (PyObject_SetAttrString(result, "name", value) == -1)
3159 goto failed;
3160 Py_DECREF(value);
3161 value = ast2obj_identifier(o->asname);
3162 if (!value) goto failed;
3163 if (PyObject_SetAttrString(result, "asname", value) == -1)
3164 goto failed;
3165 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003166 return result;
3167failed:
3168 Py_XDECREF(value);
3169 Py_XDECREF(result);
3170 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171}
3172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173
Georg Brandlfc8eef32008-03-28 12:11:56 +00003174int
3175obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3176{
3177 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003178 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003179
3180
3181 if (obj == Py_None) {
3182 *out = NULL;
3183 return 0;
3184 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003185 isinstance = PyObject_IsInstance(obj, (PyObject*)Module_type);
3186 if (isinstance == -1) {
3187 return 1;
3188 }
3189 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003190 asdl_seq* body;
3191
3192 if (PyObject_HasAttrString(obj, "body")) {
3193 int res;
3194 Py_ssize_t len;
3195 Py_ssize_t i;
3196 tmp = PyObject_GetAttrString(obj, "body");
3197 if (tmp == NULL) goto failed;
3198 if (!PyList_Check(tmp)) {
3199 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3200 goto failed;
3201 }
3202 len = PyList_GET_SIZE(tmp);
3203 body = asdl_seq_new(len, arena);
3204 if (body == NULL) goto failed;
3205 for (i = 0; i < len; i++) {
3206 stmt_ty value;
3207 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3208 if (res != 0) goto failed;
3209 asdl_seq_SET(body, i, value);
3210 }
3211 Py_XDECREF(tmp);
3212 tmp = NULL;
3213 } else {
3214 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3215 return 1;
3216 }
3217 *out = Module(body, arena);
3218 if (*out == NULL) goto failed;
3219 return 0;
3220 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003221 isinstance = PyObject_IsInstance(obj, (PyObject*)Interactive_type);
3222 if (isinstance == -1) {
3223 return 1;
3224 }
3225 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003226 asdl_seq* body;
3227
3228 if (PyObject_HasAttrString(obj, "body")) {
3229 int res;
3230 Py_ssize_t len;
3231 Py_ssize_t i;
3232 tmp = PyObject_GetAttrString(obj, "body");
3233 if (tmp == NULL) goto failed;
3234 if (!PyList_Check(tmp)) {
3235 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3236 goto failed;
3237 }
3238 len = PyList_GET_SIZE(tmp);
3239 body = asdl_seq_new(len, arena);
3240 if (body == NULL) goto failed;
3241 for (i = 0; i < len; i++) {
3242 stmt_ty value;
3243 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3244 if (res != 0) goto failed;
3245 asdl_seq_SET(body, i, value);
3246 }
3247 Py_XDECREF(tmp);
3248 tmp = NULL;
3249 } else {
3250 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3251 return 1;
3252 }
3253 *out = Interactive(body, arena);
3254 if (*out == NULL) goto failed;
3255 return 0;
3256 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003257 isinstance = PyObject_IsInstance(obj, (PyObject*)Expression_type);
3258 if (isinstance == -1) {
3259 return 1;
3260 }
3261 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003262 expr_ty body;
3263
3264 if (PyObject_HasAttrString(obj, "body")) {
3265 int res;
3266 tmp = PyObject_GetAttrString(obj, "body");
3267 if (tmp == NULL) goto failed;
3268 res = obj2ast_expr(tmp, &body, arena);
3269 if (res != 0) goto failed;
3270 Py_XDECREF(tmp);
3271 tmp = NULL;
3272 } else {
3273 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3274 return 1;
3275 }
3276 *out = Expression(body, arena);
3277 if (*out == NULL) goto failed;
3278 return 0;
3279 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003280 isinstance = PyObject_IsInstance(obj, (PyObject*)Suite_type);
3281 if (isinstance == -1) {
3282 return 1;
3283 }
3284 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003285 asdl_seq* body;
3286
3287 if (PyObject_HasAttrString(obj, "body")) {
3288 int res;
3289 Py_ssize_t len;
3290 Py_ssize_t i;
3291 tmp = PyObject_GetAttrString(obj, "body");
3292 if (tmp == NULL) goto failed;
3293 if (!PyList_Check(tmp)) {
3294 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3295 goto failed;
3296 }
3297 len = PyList_GET_SIZE(tmp);
3298 body = asdl_seq_new(len, arena);
3299 if (body == NULL) goto failed;
3300 for (i = 0; i < len; i++) {
3301 stmt_ty value;
3302 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3303 if (res != 0) goto failed;
3304 asdl_seq_SET(body, i, value);
3305 }
3306 Py_XDECREF(tmp);
3307 tmp = NULL;
3308 } else {
3309 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3310 return 1;
3311 }
3312 *out = Suite(body, arena);
3313 if (*out == NULL) goto failed;
3314 return 0;
3315 }
3316
3317 tmp = PyObject_Repr(obj);
3318 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003319 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00003320failed:
3321 Py_XDECREF(tmp);
3322 return 1;
3323}
3324
3325int
3326obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3327{
3328 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003329 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00003330
3331 int lineno;
3332 int col_offset;
3333
3334 if (obj == Py_None) {
3335 *out = NULL;
3336 return 0;
3337 }
3338 if (PyObject_HasAttrString(obj, "lineno")) {
3339 int res;
3340 tmp = PyObject_GetAttrString(obj, "lineno");
3341 if (tmp == NULL) goto failed;
3342 res = obj2ast_int(tmp, &lineno, arena);
3343 if (res != 0) goto failed;
3344 Py_XDECREF(tmp);
3345 tmp = NULL;
3346 } else {
3347 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3348 return 1;
3349 }
3350 if (PyObject_HasAttrString(obj, "col_offset")) {
3351 int res;
3352 tmp = PyObject_GetAttrString(obj, "col_offset");
3353 if (tmp == NULL) goto failed;
3354 res = obj2ast_int(tmp, &col_offset, arena);
3355 if (res != 0) goto failed;
3356 Py_XDECREF(tmp);
3357 tmp = NULL;
3358 } else {
3359 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3360 return 1;
3361 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003362 isinstance = PyObject_IsInstance(obj, (PyObject*)FunctionDef_type);
3363 if (isinstance == -1) {
3364 return 1;
3365 }
3366 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003367 identifier name;
3368 arguments_ty args;
3369 asdl_seq* body;
3370 asdl_seq* decorator_list;
3371
3372 if (PyObject_HasAttrString(obj, "name")) {
3373 int res;
3374 tmp = PyObject_GetAttrString(obj, "name");
3375 if (tmp == NULL) goto failed;
3376 res = obj2ast_identifier(tmp, &name, arena);
3377 if (res != 0) goto failed;
3378 Py_XDECREF(tmp);
3379 tmp = NULL;
3380 } else {
3381 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3382 return 1;
3383 }
3384 if (PyObject_HasAttrString(obj, "args")) {
3385 int res;
3386 tmp = PyObject_GetAttrString(obj, "args");
3387 if (tmp == NULL) goto failed;
3388 res = obj2ast_arguments(tmp, &args, arena);
3389 if (res != 0) goto failed;
3390 Py_XDECREF(tmp);
3391 tmp = NULL;
3392 } else {
3393 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3394 return 1;
3395 }
3396 if (PyObject_HasAttrString(obj, "body")) {
3397 int res;
3398 Py_ssize_t len;
3399 Py_ssize_t i;
3400 tmp = PyObject_GetAttrString(obj, "body");
3401 if (tmp == NULL) goto failed;
3402 if (!PyList_Check(tmp)) {
3403 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3404 goto failed;
3405 }
3406 len = PyList_GET_SIZE(tmp);
3407 body = asdl_seq_new(len, arena);
3408 if (body == NULL) goto failed;
3409 for (i = 0; i < len; i++) {
3410 stmt_ty value;
3411 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3412 if (res != 0) goto failed;
3413 asdl_seq_SET(body, i, value);
3414 }
3415 Py_XDECREF(tmp);
3416 tmp = NULL;
3417 } else {
3418 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3419 return 1;
3420 }
3421 if (PyObject_HasAttrString(obj, "decorator_list")) {
3422 int res;
3423 Py_ssize_t len;
3424 Py_ssize_t i;
3425 tmp = PyObject_GetAttrString(obj, "decorator_list");
3426 if (tmp == NULL) goto failed;
3427 if (!PyList_Check(tmp)) {
3428 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3429 goto failed;
3430 }
3431 len = PyList_GET_SIZE(tmp);
3432 decorator_list = asdl_seq_new(len, arena);
3433 if (decorator_list == NULL) goto failed;
3434 for (i = 0; i < len; i++) {
3435 expr_ty value;
3436 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3437 if (res != 0) goto failed;
3438 asdl_seq_SET(decorator_list, i, value);
3439 }
3440 Py_XDECREF(tmp);
3441 tmp = NULL;
3442 } else {
3443 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3444 return 1;
3445 }
3446 *out = FunctionDef(name, args, body, decorator_list, lineno,
3447 col_offset, arena);
3448 if (*out == NULL) goto failed;
3449 return 0;
3450 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003451 isinstance = PyObject_IsInstance(obj, (PyObject*)ClassDef_type);
3452 if (isinstance == -1) {
3453 return 1;
3454 }
3455 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003456 identifier name;
3457 asdl_seq* bases;
3458 asdl_seq* body;
3459 asdl_seq* decorator_list;
3460
3461 if (PyObject_HasAttrString(obj, "name")) {
3462 int res;
3463 tmp = PyObject_GetAttrString(obj, "name");
3464 if (tmp == NULL) goto failed;
3465 res = obj2ast_identifier(tmp, &name, arena);
3466 if (res != 0) goto failed;
3467 Py_XDECREF(tmp);
3468 tmp = NULL;
3469 } else {
3470 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3471 return 1;
3472 }
3473 if (PyObject_HasAttrString(obj, "bases")) {
3474 int res;
3475 Py_ssize_t len;
3476 Py_ssize_t i;
3477 tmp = PyObject_GetAttrString(obj, "bases");
3478 if (tmp == NULL) goto failed;
3479 if (!PyList_Check(tmp)) {
3480 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3481 goto failed;
3482 }
3483 len = PyList_GET_SIZE(tmp);
3484 bases = asdl_seq_new(len, arena);
3485 if (bases == NULL) goto failed;
3486 for (i = 0; i < len; i++) {
3487 expr_ty value;
3488 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3489 if (res != 0) goto failed;
3490 asdl_seq_SET(bases, i, value);
3491 }
3492 Py_XDECREF(tmp);
3493 tmp = NULL;
3494 } else {
3495 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3496 return 1;
3497 }
3498 if (PyObject_HasAttrString(obj, "body")) {
3499 int res;
3500 Py_ssize_t len;
3501 Py_ssize_t i;
3502 tmp = PyObject_GetAttrString(obj, "body");
3503 if (tmp == NULL) goto failed;
3504 if (!PyList_Check(tmp)) {
3505 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3506 goto failed;
3507 }
3508 len = PyList_GET_SIZE(tmp);
3509 body = asdl_seq_new(len, arena);
3510 if (body == NULL) goto failed;
3511 for (i = 0; i < len; i++) {
3512 stmt_ty value;
3513 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3514 if (res != 0) goto failed;
3515 asdl_seq_SET(body, i, value);
3516 }
3517 Py_XDECREF(tmp);
3518 tmp = NULL;
3519 } else {
3520 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3521 return 1;
3522 }
3523 if (PyObject_HasAttrString(obj, "decorator_list")) {
3524 int res;
3525 Py_ssize_t len;
3526 Py_ssize_t i;
3527 tmp = PyObject_GetAttrString(obj, "decorator_list");
3528 if (tmp == NULL) goto failed;
3529 if (!PyList_Check(tmp)) {
3530 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3531 goto failed;
3532 }
3533 len = PyList_GET_SIZE(tmp);
3534 decorator_list = asdl_seq_new(len, arena);
3535 if (decorator_list == NULL) goto failed;
3536 for (i = 0; i < len; i++) {
3537 expr_ty value;
3538 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3539 if (res != 0) goto failed;
3540 asdl_seq_SET(decorator_list, i, value);
3541 }
3542 Py_XDECREF(tmp);
3543 tmp = NULL;
3544 } else {
3545 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3546 return 1;
3547 }
3548 *out = ClassDef(name, bases, body, decorator_list, lineno,
3549 col_offset, arena);
3550 if (*out == NULL) goto failed;
3551 return 0;
3552 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003553 isinstance = PyObject_IsInstance(obj, (PyObject*)Return_type);
3554 if (isinstance == -1) {
3555 return 1;
3556 }
3557 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003558 expr_ty value;
3559
3560 if (PyObject_HasAttrString(obj, "value")) {
3561 int res;
3562 tmp = PyObject_GetAttrString(obj, "value");
3563 if (tmp == NULL) goto failed;
3564 res = obj2ast_expr(tmp, &value, arena);
3565 if (res != 0) goto failed;
3566 Py_XDECREF(tmp);
3567 tmp = NULL;
3568 } else {
3569 value = NULL;
3570 }
3571 *out = Return(value, lineno, col_offset, arena);
3572 if (*out == NULL) goto failed;
3573 return 0;
3574 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003575 isinstance = PyObject_IsInstance(obj, (PyObject*)Delete_type);
3576 if (isinstance == -1) {
3577 return 1;
3578 }
3579 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003580 asdl_seq* targets;
3581
3582 if (PyObject_HasAttrString(obj, "targets")) {
3583 int res;
3584 Py_ssize_t len;
3585 Py_ssize_t i;
3586 tmp = PyObject_GetAttrString(obj, "targets");
3587 if (tmp == NULL) goto failed;
3588 if (!PyList_Check(tmp)) {
3589 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3590 goto failed;
3591 }
3592 len = PyList_GET_SIZE(tmp);
3593 targets = asdl_seq_new(len, arena);
3594 if (targets == NULL) goto failed;
3595 for (i = 0; i < len; i++) {
3596 expr_ty value;
3597 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3598 if (res != 0) goto failed;
3599 asdl_seq_SET(targets, i, value);
3600 }
3601 Py_XDECREF(tmp);
3602 tmp = NULL;
3603 } else {
3604 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3605 return 1;
3606 }
3607 *out = Delete(targets, lineno, col_offset, arena);
3608 if (*out == NULL) goto failed;
3609 return 0;
3610 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003611 isinstance = PyObject_IsInstance(obj, (PyObject*)Assign_type);
3612 if (isinstance == -1) {
3613 return 1;
3614 }
3615 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003616 asdl_seq* targets;
3617 expr_ty value;
3618
3619 if (PyObject_HasAttrString(obj, "targets")) {
3620 int res;
3621 Py_ssize_t len;
3622 Py_ssize_t i;
3623 tmp = PyObject_GetAttrString(obj, "targets");
3624 if (tmp == NULL) goto failed;
3625 if (!PyList_Check(tmp)) {
3626 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3627 goto failed;
3628 }
3629 len = PyList_GET_SIZE(tmp);
3630 targets = asdl_seq_new(len, arena);
3631 if (targets == NULL) goto failed;
3632 for (i = 0; i < len; i++) {
3633 expr_ty value;
3634 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3635 if (res != 0) goto failed;
3636 asdl_seq_SET(targets, i, value);
3637 }
3638 Py_XDECREF(tmp);
3639 tmp = NULL;
3640 } else {
3641 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3642 return 1;
3643 }
3644 if (PyObject_HasAttrString(obj, "value")) {
3645 int res;
3646 tmp = PyObject_GetAttrString(obj, "value");
3647 if (tmp == NULL) goto failed;
3648 res = obj2ast_expr(tmp, &value, arena);
3649 if (res != 0) goto failed;
3650 Py_XDECREF(tmp);
3651 tmp = NULL;
3652 } else {
3653 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3654 return 1;
3655 }
3656 *out = Assign(targets, value, lineno, col_offset, arena);
3657 if (*out == NULL) goto failed;
3658 return 0;
3659 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003660 isinstance = PyObject_IsInstance(obj, (PyObject*)AugAssign_type);
3661 if (isinstance == -1) {
3662 return 1;
3663 }
3664 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003665 expr_ty target;
3666 operator_ty op;
3667 expr_ty value;
3668
3669 if (PyObject_HasAttrString(obj, "target")) {
3670 int res;
3671 tmp = PyObject_GetAttrString(obj, "target");
3672 if (tmp == NULL) goto failed;
3673 res = obj2ast_expr(tmp, &target, arena);
3674 if (res != 0) goto failed;
3675 Py_XDECREF(tmp);
3676 tmp = NULL;
3677 } else {
3678 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3679 return 1;
3680 }
3681 if (PyObject_HasAttrString(obj, "op")) {
3682 int res;
3683 tmp = PyObject_GetAttrString(obj, "op");
3684 if (tmp == NULL) goto failed;
3685 res = obj2ast_operator(tmp, &op, arena);
3686 if (res != 0) goto failed;
3687 Py_XDECREF(tmp);
3688 tmp = NULL;
3689 } else {
3690 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3691 return 1;
3692 }
3693 if (PyObject_HasAttrString(obj, "value")) {
3694 int res;
3695 tmp = PyObject_GetAttrString(obj, "value");
3696 if (tmp == NULL) goto failed;
3697 res = obj2ast_expr(tmp, &value, arena);
3698 if (res != 0) goto failed;
3699 Py_XDECREF(tmp);
3700 tmp = NULL;
3701 } else {
3702 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3703 return 1;
3704 }
3705 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3706 if (*out == NULL) goto failed;
3707 return 0;
3708 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003709 isinstance = PyObject_IsInstance(obj, (PyObject*)Print_type);
3710 if (isinstance == -1) {
3711 return 1;
3712 }
3713 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003714 expr_ty dest;
3715 asdl_seq* values;
3716 bool nl;
3717
3718 if (PyObject_HasAttrString(obj, "dest")) {
3719 int res;
3720 tmp = PyObject_GetAttrString(obj, "dest");
3721 if (tmp == NULL) goto failed;
3722 res = obj2ast_expr(tmp, &dest, arena);
3723 if (res != 0) goto failed;
3724 Py_XDECREF(tmp);
3725 tmp = NULL;
3726 } else {
3727 dest = NULL;
3728 }
3729 if (PyObject_HasAttrString(obj, "values")) {
3730 int res;
3731 Py_ssize_t len;
3732 Py_ssize_t i;
3733 tmp = PyObject_GetAttrString(obj, "values");
3734 if (tmp == NULL) goto failed;
3735 if (!PyList_Check(tmp)) {
3736 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3737 goto failed;
3738 }
3739 len = PyList_GET_SIZE(tmp);
3740 values = asdl_seq_new(len, arena);
3741 if (values == NULL) goto failed;
3742 for (i = 0; i < len; i++) {
3743 expr_ty value;
3744 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3745 if (res != 0) goto failed;
3746 asdl_seq_SET(values, i, value);
3747 }
3748 Py_XDECREF(tmp);
3749 tmp = NULL;
3750 } else {
3751 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3752 return 1;
3753 }
3754 if (PyObject_HasAttrString(obj, "nl")) {
3755 int res;
3756 tmp = PyObject_GetAttrString(obj, "nl");
3757 if (tmp == NULL) goto failed;
3758 res = obj2ast_bool(tmp, &nl, arena);
3759 if (res != 0) goto failed;
3760 Py_XDECREF(tmp);
3761 tmp = NULL;
3762 } else {
3763 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3764 return 1;
3765 }
3766 *out = Print(dest, values, nl, lineno, col_offset, arena);
3767 if (*out == NULL) goto failed;
3768 return 0;
3769 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003770 isinstance = PyObject_IsInstance(obj, (PyObject*)For_type);
3771 if (isinstance == -1) {
3772 return 1;
3773 }
3774 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003775 expr_ty target;
3776 expr_ty iter;
3777 asdl_seq* body;
3778 asdl_seq* orelse;
3779
3780 if (PyObject_HasAttrString(obj, "target")) {
3781 int res;
3782 tmp = PyObject_GetAttrString(obj, "target");
3783 if (tmp == NULL) goto failed;
3784 res = obj2ast_expr(tmp, &target, arena);
3785 if (res != 0) goto failed;
3786 Py_XDECREF(tmp);
3787 tmp = NULL;
3788 } else {
3789 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3790 return 1;
3791 }
3792 if (PyObject_HasAttrString(obj, "iter")) {
3793 int res;
3794 tmp = PyObject_GetAttrString(obj, "iter");
3795 if (tmp == NULL) goto failed;
3796 res = obj2ast_expr(tmp, &iter, arena);
3797 if (res != 0) goto failed;
3798 Py_XDECREF(tmp);
3799 tmp = NULL;
3800 } else {
3801 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3802 return 1;
3803 }
3804 if (PyObject_HasAttrString(obj, "body")) {
3805 int res;
3806 Py_ssize_t len;
3807 Py_ssize_t i;
3808 tmp = PyObject_GetAttrString(obj, "body");
3809 if (tmp == NULL) goto failed;
3810 if (!PyList_Check(tmp)) {
3811 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3812 goto failed;
3813 }
3814 len = PyList_GET_SIZE(tmp);
3815 body = asdl_seq_new(len, arena);
3816 if (body == NULL) goto failed;
3817 for (i = 0; i < len; i++) {
3818 stmt_ty value;
3819 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3820 if (res != 0) goto failed;
3821 asdl_seq_SET(body, i, value);
3822 }
3823 Py_XDECREF(tmp);
3824 tmp = NULL;
3825 } else {
3826 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3827 return 1;
3828 }
3829 if (PyObject_HasAttrString(obj, "orelse")) {
3830 int res;
3831 Py_ssize_t len;
3832 Py_ssize_t i;
3833 tmp = PyObject_GetAttrString(obj, "orelse");
3834 if (tmp == NULL) goto failed;
3835 if (!PyList_Check(tmp)) {
3836 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3837 goto failed;
3838 }
3839 len = PyList_GET_SIZE(tmp);
3840 orelse = asdl_seq_new(len, arena);
3841 if (orelse == NULL) goto failed;
3842 for (i = 0; i < len; i++) {
3843 stmt_ty value;
3844 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3845 if (res != 0) goto failed;
3846 asdl_seq_SET(orelse, i, value);
3847 }
3848 Py_XDECREF(tmp);
3849 tmp = NULL;
3850 } else {
3851 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3852 return 1;
3853 }
3854 *out = For(target, iter, body, orelse, lineno, col_offset,
3855 arena);
3856 if (*out == NULL) goto failed;
3857 return 0;
3858 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003859 isinstance = PyObject_IsInstance(obj, (PyObject*)While_type);
3860 if (isinstance == -1) {
3861 return 1;
3862 }
3863 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003864 expr_ty test;
3865 asdl_seq* body;
3866 asdl_seq* orelse;
3867
3868 if (PyObject_HasAttrString(obj, "test")) {
3869 int res;
3870 tmp = PyObject_GetAttrString(obj, "test");
3871 if (tmp == NULL) goto failed;
3872 res = obj2ast_expr(tmp, &test, arena);
3873 if (res != 0) goto failed;
3874 Py_XDECREF(tmp);
3875 tmp = NULL;
3876 } else {
3877 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3878 return 1;
3879 }
3880 if (PyObject_HasAttrString(obj, "body")) {
3881 int res;
3882 Py_ssize_t len;
3883 Py_ssize_t i;
3884 tmp = PyObject_GetAttrString(obj, "body");
3885 if (tmp == NULL) goto failed;
3886 if (!PyList_Check(tmp)) {
3887 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3888 goto failed;
3889 }
3890 len = PyList_GET_SIZE(tmp);
3891 body = asdl_seq_new(len, arena);
3892 if (body == NULL) goto failed;
3893 for (i = 0; i < len; i++) {
3894 stmt_ty value;
3895 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3896 if (res != 0) goto failed;
3897 asdl_seq_SET(body, i, value);
3898 }
3899 Py_XDECREF(tmp);
3900 tmp = NULL;
3901 } else {
3902 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3903 return 1;
3904 }
3905 if (PyObject_HasAttrString(obj, "orelse")) {
3906 int res;
3907 Py_ssize_t len;
3908 Py_ssize_t i;
3909 tmp = PyObject_GetAttrString(obj, "orelse");
3910 if (tmp == NULL) goto failed;
3911 if (!PyList_Check(tmp)) {
3912 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3913 goto failed;
3914 }
3915 len = PyList_GET_SIZE(tmp);
3916 orelse = asdl_seq_new(len, arena);
3917 if (orelse == NULL) goto failed;
3918 for (i = 0; i < len; i++) {
3919 stmt_ty value;
3920 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3921 if (res != 0) goto failed;
3922 asdl_seq_SET(orelse, i, value);
3923 }
3924 Py_XDECREF(tmp);
3925 tmp = NULL;
3926 } else {
3927 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3928 return 1;
3929 }
3930 *out = While(test, body, orelse, lineno, col_offset, arena);
3931 if (*out == NULL) goto failed;
3932 return 0;
3933 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00003934 isinstance = PyObject_IsInstance(obj, (PyObject*)If_type);
3935 if (isinstance == -1) {
3936 return 1;
3937 }
3938 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00003939 expr_ty test;
3940 asdl_seq* body;
3941 asdl_seq* orelse;
3942
3943 if (PyObject_HasAttrString(obj, "test")) {
3944 int res;
3945 tmp = PyObject_GetAttrString(obj, "test");
3946 if (tmp == NULL) goto failed;
3947 res = obj2ast_expr(tmp, &test, arena);
3948 if (res != 0) goto failed;
3949 Py_XDECREF(tmp);
3950 tmp = NULL;
3951 } else {
3952 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3953 return 1;
3954 }
3955 if (PyObject_HasAttrString(obj, "body")) {
3956 int res;
3957 Py_ssize_t len;
3958 Py_ssize_t i;
3959 tmp = PyObject_GetAttrString(obj, "body");
3960 if (tmp == NULL) goto failed;
3961 if (!PyList_Check(tmp)) {
3962 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3963 goto failed;
3964 }
3965 len = PyList_GET_SIZE(tmp);
3966 body = asdl_seq_new(len, arena);
3967 if (body == NULL) goto failed;
3968 for (i = 0; i < len; i++) {
3969 stmt_ty value;
3970 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3971 if (res != 0) goto failed;
3972 asdl_seq_SET(body, i, value);
3973 }
3974 Py_XDECREF(tmp);
3975 tmp = NULL;
3976 } else {
3977 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3978 return 1;
3979 }
3980 if (PyObject_HasAttrString(obj, "orelse")) {
3981 int res;
3982 Py_ssize_t len;
3983 Py_ssize_t i;
3984 tmp = PyObject_GetAttrString(obj, "orelse");
3985 if (tmp == NULL) goto failed;
3986 if (!PyList_Check(tmp)) {
3987 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3988 goto failed;
3989 }
3990 len = PyList_GET_SIZE(tmp);
3991 orelse = asdl_seq_new(len, arena);
3992 if (orelse == NULL) goto failed;
3993 for (i = 0; i < len; i++) {
3994 stmt_ty value;
3995 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3996 if (res != 0) goto failed;
3997 asdl_seq_SET(orelse, i, value);
3998 }
3999 Py_XDECREF(tmp);
4000 tmp = NULL;
4001 } else {
4002 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
4003 return 1;
4004 }
4005 *out = If(test, body, orelse, lineno, col_offset, arena);
4006 if (*out == NULL) goto failed;
4007 return 0;
4008 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004009 isinstance = PyObject_IsInstance(obj, (PyObject*)With_type);
4010 if (isinstance == -1) {
4011 return 1;
4012 }
4013 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004014 expr_ty context_expr;
4015 expr_ty optional_vars;
4016 asdl_seq* body;
4017
4018 if (PyObject_HasAttrString(obj, "context_expr")) {
4019 int res;
4020 tmp = PyObject_GetAttrString(obj, "context_expr");
4021 if (tmp == NULL) goto failed;
4022 res = obj2ast_expr(tmp, &context_expr, arena);
4023 if (res != 0) goto failed;
4024 Py_XDECREF(tmp);
4025 tmp = NULL;
4026 } else {
4027 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
4028 return 1;
4029 }
4030 if (PyObject_HasAttrString(obj, "optional_vars")) {
4031 int res;
4032 tmp = PyObject_GetAttrString(obj, "optional_vars");
4033 if (tmp == NULL) goto failed;
4034 res = obj2ast_expr(tmp, &optional_vars, arena);
4035 if (res != 0) goto failed;
4036 Py_XDECREF(tmp);
4037 tmp = NULL;
4038 } else {
4039 optional_vars = NULL;
4040 }
4041 if (PyObject_HasAttrString(obj, "body")) {
4042 int res;
4043 Py_ssize_t len;
4044 Py_ssize_t i;
4045 tmp = PyObject_GetAttrString(obj, "body");
4046 if (tmp == NULL) goto failed;
4047 if (!PyList_Check(tmp)) {
4048 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4049 goto failed;
4050 }
4051 len = PyList_GET_SIZE(tmp);
4052 body = asdl_seq_new(len, arena);
4053 if (body == NULL) goto failed;
4054 for (i = 0; i < len; i++) {
4055 stmt_ty value;
4056 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4057 if (res != 0) goto failed;
4058 asdl_seq_SET(body, i, value);
4059 }
4060 Py_XDECREF(tmp);
4061 tmp = NULL;
4062 } else {
4063 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
4064 return 1;
4065 }
4066 *out = With(context_expr, optional_vars, body, lineno,
4067 col_offset, arena);
4068 if (*out == NULL) goto failed;
4069 return 0;
4070 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004071 isinstance = PyObject_IsInstance(obj, (PyObject*)Raise_type);
4072 if (isinstance == -1) {
4073 return 1;
4074 }
4075 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004076 expr_ty type;
4077 expr_ty inst;
4078 expr_ty tback;
4079
4080 if (PyObject_HasAttrString(obj, "type")) {
4081 int res;
4082 tmp = PyObject_GetAttrString(obj, "type");
4083 if (tmp == NULL) goto failed;
4084 res = obj2ast_expr(tmp, &type, arena);
4085 if (res != 0) goto failed;
4086 Py_XDECREF(tmp);
4087 tmp = NULL;
4088 } else {
4089 type = NULL;
4090 }
4091 if (PyObject_HasAttrString(obj, "inst")) {
4092 int res;
4093 tmp = PyObject_GetAttrString(obj, "inst");
4094 if (tmp == NULL) goto failed;
4095 res = obj2ast_expr(tmp, &inst, arena);
4096 if (res != 0) goto failed;
4097 Py_XDECREF(tmp);
4098 tmp = NULL;
4099 } else {
4100 inst = NULL;
4101 }
4102 if (PyObject_HasAttrString(obj, "tback")) {
4103 int res;
4104 tmp = PyObject_GetAttrString(obj, "tback");
4105 if (tmp == NULL) goto failed;
4106 res = obj2ast_expr(tmp, &tback, arena);
4107 if (res != 0) goto failed;
4108 Py_XDECREF(tmp);
4109 tmp = NULL;
4110 } else {
4111 tback = NULL;
4112 }
4113 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4114 if (*out == NULL) goto failed;
4115 return 0;
4116 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004117 isinstance = PyObject_IsInstance(obj, (PyObject*)TryExcept_type);
4118 if (isinstance == -1) {
4119 return 1;
4120 }
4121 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004122 asdl_seq* body;
4123 asdl_seq* handlers;
4124 asdl_seq* orelse;
4125
4126 if (PyObject_HasAttrString(obj, "body")) {
4127 int res;
4128 Py_ssize_t len;
4129 Py_ssize_t i;
4130 tmp = PyObject_GetAttrString(obj, "body");
4131 if (tmp == NULL) goto failed;
4132 if (!PyList_Check(tmp)) {
4133 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4134 goto failed;
4135 }
4136 len = PyList_GET_SIZE(tmp);
4137 body = asdl_seq_new(len, arena);
4138 if (body == NULL) goto failed;
4139 for (i = 0; i < len; i++) {
4140 stmt_ty value;
4141 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4142 if (res != 0) goto failed;
4143 asdl_seq_SET(body, i, value);
4144 }
4145 Py_XDECREF(tmp);
4146 tmp = NULL;
4147 } else {
4148 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4149 return 1;
4150 }
4151 if (PyObject_HasAttrString(obj, "handlers")) {
4152 int res;
4153 Py_ssize_t len;
4154 Py_ssize_t i;
4155 tmp = PyObject_GetAttrString(obj, "handlers");
4156 if (tmp == NULL) goto failed;
4157 if (!PyList_Check(tmp)) {
4158 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4159 goto failed;
4160 }
4161 len = PyList_GET_SIZE(tmp);
4162 handlers = asdl_seq_new(len, arena);
4163 if (handlers == NULL) goto failed;
4164 for (i = 0; i < len; i++) {
4165 excepthandler_ty value;
4166 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4167 if (res != 0) goto failed;
4168 asdl_seq_SET(handlers, i, value);
4169 }
4170 Py_XDECREF(tmp);
4171 tmp = NULL;
4172 } else {
4173 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4174 return 1;
4175 }
4176 if (PyObject_HasAttrString(obj, "orelse")) {
4177 int res;
4178 Py_ssize_t len;
4179 Py_ssize_t i;
4180 tmp = PyObject_GetAttrString(obj, "orelse");
4181 if (tmp == NULL) goto failed;
4182 if (!PyList_Check(tmp)) {
4183 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4184 goto failed;
4185 }
4186 len = PyList_GET_SIZE(tmp);
4187 orelse = asdl_seq_new(len, arena);
4188 if (orelse == NULL) goto failed;
4189 for (i = 0; i < len; i++) {
4190 stmt_ty value;
4191 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4192 if (res != 0) goto failed;
4193 asdl_seq_SET(orelse, i, value);
4194 }
4195 Py_XDECREF(tmp);
4196 tmp = NULL;
4197 } else {
4198 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4199 return 1;
4200 }
4201 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4202 arena);
4203 if (*out == NULL) goto failed;
4204 return 0;
4205 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004206 isinstance = PyObject_IsInstance(obj, (PyObject*)TryFinally_type);
4207 if (isinstance == -1) {
4208 return 1;
4209 }
4210 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004211 asdl_seq* body;
4212 asdl_seq* finalbody;
4213
4214 if (PyObject_HasAttrString(obj, "body")) {
4215 int res;
4216 Py_ssize_t len;
4217 Py_ssize_t i;
4218 tmp = PyObject_GetAttrString(obj, "body");
4219 if (tmp == NULL) goto failed;
4220 if (!PyList_Check(tmp)) {
4221 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4222 goto failed;
4223 }
4224 len = PyList_GET_SIZE(tmp);
4225 body = asdl_seq_new(len, arena);
4226 if (body == NULL) goto failed;
4227 for (i = 0; i < len; i++) {
4228 stmt_ty value;
4229 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4230 if (res != 0) goto failed;
4231 asdl_seq_SET(body, i, value);
4232 }
4233 Py_XDECREF(tmp);
4234 tmp = NULL;
4235 } else {
4236 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4237 return 1;
4238 }
4239 if (PyObject_HasAttrString(obj, "finalbody")) {
4240 int res;
4241 Py_ssize_t len;
4242 Py_ssize_t i;
4243 tmp = PyObject_GetAttrString(obj, "finalbody");
4244 if (tmp == NULL) goto failed;
4245 if (!PyList_Check(tmp)) {
4246 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4247 goto failed;
4248 }
4249 len = PyList_GET_SIZE(tmp);
4250 finalbody = asdl_seq_new(len, arena);
4251 if (finalbody == NULL) goto failed;
4252 for (i = 0; i < len; i++) {
4253 stmt_ty value;
4254 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4255 if (res != 0) goto failed;
4256 asdl_seq_SET(finalbody, i, value);
4257 }
4258 Py_XDECREF(tmp);
4259 tmp = NULL;
4260 } else {
4261 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4262 return 1;
4263 }
4264 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4265 if (*out == NULL) goto failed;
4266 return 0;
4267 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004268 isinstance = PyObject_IsInstance(obj, (PyObject*)Assert_type);
4269 if (isinstance == -1) {
4270 return 1;
4271 }
4272 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004273 expr_ty test;
4274 expr_ty msg;
4275
4276 if (PyObject_HasAttrString(obj, "test")) {
4277 int res;
4278 tmp = PyObject_GetAttrString(obj, "test");
4279 if (tmp == NULL) goto failed;
4280 res = obj2ast_expr(tmp, &test, arena);
4281 if (res != 0) goto failed;
4282 Py_XDECREF(tmp);
4283 tmp = NULL;
4284 } else {
4285 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4286 return 1;
4287 }
4288 if (PyObject_HasAttrString(obj, "msg")) {
4289 int res;
4290 tmp = PyObject_GetAttrString(obj, "msg");
4291 if (tmp == NULL) goto failed;
4292 res = obj2ast_expr(tmp, &msg, arena);
4293 if (res != 0) goto failed;
4294 Py_XDECREF(tmp);
4295 tmp = NULL;
4296 } else {
4297 msg = NULL;
4298 }
4299 *out = Assert(test, msg, lineno, col_offset, arena);
4300 if (*out == NULL) goto failed;
4301 return 0;
4302 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004303 isinstance = PyObject_IsInstance(obj, (PyObject*)Import_type);
4304 if (isinstance == -1) {
4305 return 1;
4306 }
4307 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004308 asdl_seq* names;
4309
4310 if (PyObject_HasAttrString(obj, "names")) {
4311 int res;
4312 Py_ssize_t len;
4313 Py_ssize_t i;
4314 tmp = PyObject_GetAttrString(obj, "names");
4315 if (tmp == NULL) goto failed;
4316 if (!PyList_Check(tmp)) {
4317 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4318 goto failed;
4319 }
4320 len = PyList_GET_SIZE(tmp);
4321 names = asdl_seq_new(len, arena);
4322 if (names == NULL) goto failed;
4323 for (i = 0; i < len; i++) {
4324 alias_ty value;
4325 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4326 if (res != 0) goto failed;
4327 asdl_seq_SET(names, i, value);
4328 }
4329 Py_XDECREF(tmp);
4330 tmp = NULL;
4331 } else {
4332 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4333 return 1;
4334 }
4335 *out = Import(names, lineno, col_offset, arena);
4336 if (*out == NULL) goto failed;
4337 return 0;
4338 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004339 isinstance = PyObject_IsInstance(obj, (PyObject*)ImportFrom_type);
4340 if (isinstance == -1) {
4341 return 1;
4342 }
4343 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004344 identifier module;
4345 asdl_seq* names;
4346 int level;
4347
4348 if (PyObject_HasAttrString(obj, "module")) {
4349 int res;
4350 tmp = PyObject_GetAttrString(obj, "module");
4351 if (tmp == NULL) goto failed;
4352 res = obj2ast_identifier(tmp, &module, arena);
4353 if (res != 0) goto failed;
4354 Py_XDECREF(tmp);
4355 tmp = NULL;
4356 } else {
Benjamin Petersona72be3b2009-06-13 20:23:33 +00004357 module = NULL;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004358 }
4359 if (PyObject_HasAttrString(obj, "names")) {
4360 int res;
4361 Py_ssize_t len;
4362 Py_ssize_t i;
4363 tmp = PyObject_GetAttrString(obj, "names");
4364 if (tmp == NULL) goto failed;
4365 if (!PyList_Check(tmp)) {
4366 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4367 goto failed;
4368 }
4369 len = PyList_GET_SIZE(tmp);
4370 names = asdl_seq_new(len, arena);
4371 if (names == NULL) goto failed;
4372 for (i = 0; i < len; i++) {
4373 alias_ty value;
4374 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4375 if (res != 0) goto failed;
4376 asdl_seq_SET(names, i, value);
4377 }
4378 Py_XDECREF(tmp);
4379 tmp = NULL;
4380 } else {
4381 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4382 return 1;
4383 }
4384 if (PyObject_HasAttrString(obj, "level")) {
4385 int res;
4386 tmp = PyObject_GetAttrString(obj, "level");
4387 if (tmp == NULL) goto failed;
4388 res = obj2ast_int(tmp, &level, arena);
4389 if (res != 0) goto failed;
4390 Py_XDECREF(tmp);
4391 tmp = NULL;
4392 } else {
4393 level = 0;
4394 }
4395 *out = ImportFrom(module, names, level, lineno, col_offset,
4396 arena);
4397 if (*out == NULL) goto failed;
4398 return 0;
4399 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004400 isinstance = PyObject_IsInstance(obj, (PyObject*)Exec_type);
4401 if (isinstance == -1) {
4402 return 1;
4403 }
4404 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004405 expr_ty body;
4406 expr_ty globals;
4407 expr_ty locals;
4408
4409 if (PyObject_HasAttrString(obj, "body")) {
4410 int res;
4411 tmp = PyObject_GetAttrString(obj, "body");
4412 if (tmp == NULL) goto failed;
4413 res = obj2ast_expr(tmp, &body, arena);
4414 if (res != 0) goto failed;
4415 Py_XDECREF(tmp);
4416 tmp = NULL;
4417 } else {
4418 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4419 return 1;
4420 }
4421 if (PyObject_HasAttrString(obj, "globals")) {
4422 int res;
4423 tmp = PyObject_GetAttrString(obj, "globals");
4424 if (tmp == NULL) goto failed;
4425 res = obj2ast_expr(tmp, &globals, arena);
4426 if (res != 0) goto failed;
4427 Py_XDECREF(tmp);
4428 tmp = NULL;
4429 } else {
4430 globals = NULL;
4431 }
4432 if (PyObject_HasAttrString(obj, "locals")) {
4433 int res;
4434 tmp = PyObject_GetAttrString(obj, "locals");
4435 if (tmp == NULL) goto failed;
4436 res = obj2ast_expr(tmp, &locals, arena);
4437 if (res != 0) goto failed;
4438 Py_XDECREF(tmp);
4439 tmp = NULL;
4440 } else {
4441 locals = NULL;
4442 }
4443 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4444 if (*out == NULL) goto failed;
4445 return 0;
4446 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004447 isinstance = PyObject_IsInstance(obj, (PyObject*)Global_type);
4448 if (isinstance == -1) {
4449 return 1;
4450 }
4451 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004452 asdl_seq* names;
4453
4454 if (PyObject_HasAttrString(obj, "names")) {
4455 int res;
4456 Py_ssize_t len;
4457 Py_ssize_t i;
4458 tmp = PyObject_GetAttrString(obj, "names");
4459 if (tmp == NULL) goto failed;
4460 if (!PyList_Check(tmp)) {
4461 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4462 goto failed;
4463 }
4464 len = PyList_GET_SIZE(tmp);
4465 names = asdl_seq_new(len, arena);
4466 if (names == NULL) goto failed;
4467 for (i = 0; i < len; i++) {
4468 identifier value;
4469 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4470 if (res != 0) goto failed;
4471 asdl_seq_SET(names, i, value);
4472 }
4473 Py_XDECREF(tmp);
4474 tmp = NULL;
4475 } else {
4476 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4477 return 1;
4478 }
4479 *out = Global(names, lineno, col_offset, arena);
4480 if (*out == NULL) goto failed;
4481 return 0;
4482 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004483 isinstance = PyObject_IsInstance(obj, (PyObject*)Expr_type);
4484 if (isinstance == -1) {
4485 return 1;
4486 }
4487 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004488 expr_ty value;
4489
4490 if (PyObject_HasAttrString(obj, "value")) {
4491 int res;
4492 tmp = PyObject_GetAttrString(obj, "value");
4493 if (tmp == NULL) goto failed;
4494 res = obj2ast_expr(tmp, &value, arena);
4495 if (res != 0) goto failed;
4496 Py_XDECREF(tmp);
4497 tmp = NULL;
4498 } else {
4499 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4500 return 1;
4501 }
4502 *out = Expr(value, lineno, col_offset, arena);
4503 if (*out == NULL) goto failed;
4504 return 0;
4505 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004506 isinstance = PyObject_IsInstance(obj, (PyObject*)Pass_type);
4507 if (isinstance == -1) {
4508 return 1;
4509 }
4510 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004511
4512 *out = Pass(lineno, col_offset, arena);
4513 if (*out == NULL) goto failed;
4514 return 0;
4515 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004516 isinstance = PyObject_IsInstance(obj, (PyObject*)Break_type);
4517 if (isinstance == -1) {
4518 return 1;
4519 }
4520 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004521
4522 *out = Break(lineno, col_offset, arena);
4523 if (*out == NULL) goto failed;
4524 return 0;
4525 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004526 isinstance = PyObject_IsInstance(obj, (PyObject*)Continue_type);
4527 if (isinstance == -1) {
4528 return 1;
4529 }
4530 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004531
4532 *out = Continue(lineno, col_offset, arena);
4533 if (*out == NULL) goto failed;
4534 return 0;
4535 }
4536
4537 tmp = PyObject_Repr(obj);
4538 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00004539 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00004540failed:
4541 Py_XDECREF(tmp);
4542 return 1;
4543}
4544
4545int
4546obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4547{
4548 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004549 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00004550
4551 int lineno;
4552 int col_offset;
4553
4554 if (obj == Py_None) {
4555 *out = NULL;
4556 return 0;
4557 }
4558 if (PyObject_HasAttrString(obj, "lineno")) {
4559 int res;
4560 tmp = PyObject_GetAttrString(obj, "lineno");
4561 if (tmp == NULL) goto failed;
4562 res = obj2ast_int(tmp, &lineno, arena);
4563 if (res != 0) goto failed;
4564 Py_XDECREF(tmp);
4565 tmp = NULL;
4566 } else {
4567 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4568 return 1;
4569 }
4570 if (PyObject_HasAttrString(obj, "col_offset")) {
4571 int res;
4572 tmp = PyObject_GetAttrString(obj, "col_offset");
4573 if (tmp == NULL) goto failed;
4574 res = obj2ast_int(tmp, &col_offset, arena);
4575 if (res != 0) goto failed;
4576 Py_XDECREF(tmp);
4577 tmp = NULL;
4578 } else {
4579 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4580 return 1;
4581 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004582 isinstance = PyObject_IsInstance(obj, (PyObject*)BoolOp_type);
4583 if (isinstance == -1) {
4584 return 1;
4585 }
4586 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004587 boolop_ty op;
4588 asdl_seq* values;
4589
4590 if (PyObject_HasAttrString(obj, "op")) {
4591 int res;
4592 tmp = PyObject_GetAttrString(obj, "op");
4593 if (tmp == NULL) goto failed;
4594 res = obj2ast_boolop(tmp, &op, arena);
4595 if (res != 0) goto failed;
4596 Py_XDECREF(tmp);
4597 tmp = NULL;
4598 } else {
4599 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4600 return 1;
4601 }
4602 if (PyObject_HasAttrString(obj, "values")) {
4603 int res;
4604 Py_ssize_t len;
4605 Py_ssize_t i;
4606 tmp = PyObject_GetAttrString(obj, "values");
4607 if (tmp == NULL) goto failed;
4608 if (!PyList_Check(tmp)) {
4609 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4610 goto failed;
4611 }
4612 len = PyList_GET_SIZE(tmp);
4613 values = asdl_seq_new(len, arena);
4614 if (values == NULL) goto failed;
4615 for (i = 0; i < len; i++) {
4616 expr_ty value;
4617 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4618 if (res != 0) goto failed;
4619 asdl_seq_SET(values, i, value);
4620 }
4621 Py_XDECREF(tmp);
4622 tmp = NULL;
4623 } else {
4624 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4625 return 1;
4626 }
4627 *out = BoolOp(op, values, lineno, col_offset, arena);
4628 if (*out == NULL) goto failed;
4629 return 0;
4630 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004631 isinstance = PyObject_IsInstance(obj, (PyObject*)BinOp_type);
4632 if (isinstance == -1) {
4633 return 1;
4634 }
4635 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004636 expr_ty left;
4637 operator_ty op;
4638 expr_ty right;
4639
4640 if (PyObject_HasAttrString(obj, "left")) {
4641 int res;
4642 tmp = PyObject_GetAttrString(obj, "left");
4643 if (tmp == NULL) goto failed;
4644 res = obj2ast_expr(tmp, &left, arena);
4645 if (res != 0) goto failed;
4646 Py_XDECREF(tmp);
4647 tmp = NULL;
4648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4650 return 1;
4651 }
4652 if (PyObject_HasAttrString(obj, "op")) {
4653 int res;
4654 tmp = PyObject_GetAttrString(obj, "op");
4655 if (tmp == NULL) goto failed;
4656 res = obj2ast_operator(tmp, &op, arena);
4657 if (res != 0) goto failed;
4658 Py_XDECREF(tmp);
4659 tmp = NULL;
4660 } else {
4661 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4662 return 1;
4663 }
4664 if (PyObject_HasAttrString(obj, "right")) {
4665 int res;
4666 tmp = PyObject_GetAttrString(obj, "right");
4667 if (tmp == NULL) goto failed;
4668 res = obj2ast_expr(tmp, &right, arena);
4669 if (res != 0) goto failed;
4670 Py_XDECREF(tmp);
4671 tmp = NULL;
4672 } else {
4673 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4674 return 1;
4675 }
4676 *out = BinOp(left, op, right, lineno, col_offset, arena);
4677 if (*out == NULL) goto failed;
4678 return 0;
4679 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004680 isinstance = PyObject_IsInstance(obj, (PyObject*)UnaryOp_type);
4681 if (isinstance == -1) {
4682 return 1;
4683 }
4684 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004685 unaryop_ty op;
4686 expr_ty operand;
4687
4688 if (PyObject_HasAttrString(obj, "op")) {
4689 int res;
4690 tmp = PyObject_GetAttrString(obj, "op");
4691 if (tmp == NULL) goto failed;
4692 res = obj2ast_unaryop(tmp, &op, arena);
4693 if (res != 0) goto failed;
4694 Py_XDECREF(tmp);
4695 tmp = NULL;
4696 } else {
4697 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4698 return 1;
4699 }
4700 if (PyObject_HasAttrString(obj, "operand")) {
4701 int res;
4702 tmp = PyObject_GetAttrString(obj, "operand");
4703 if (tmp == NULL) goto failed;
4704 res = obj2ast_expr(tmp, &operand, arena);
4705 if (res != 0) goto failed;
4706 Py_XDECREF(tmp);
4707 tmp = NULL;
4708 } else {
4709 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4710 return 1;
4711 }
4712 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4713 if (*out == NULL) goto failed;
4714 return 0;
4715 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004716 isinstance = PyObject_IsInstance(obj, (PyObject*)Lambda_type);
4717 if (isinstance == -1) {
4718 return 1;
4719 }
4720 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004721 arguments_ty args;
4722 expr_ty body;
4723
4724 if (PyObject_HasAttrString(obj, "args")) {
4725 int res;
4726 tmp = PyObject_GetAttrString(obj, "args");
4727 if (tmp == NULL) goto failed;
4728 res = obj2ast_arguments(tmp, &args, arena);
4729 if (res != 0) goto failed;
4730 Py_XDECREF(tmp);
4731 tmp = NULL;
4732 } else {
4733 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4734 return 1;
4735 }
4736 if (PyObject_HasAttrString(obj, "body")) {
4737 int res;
4738 tmp = PyObject_GetAttrString(obj, "body");
4739 if (tmp == NULL) goto failed;
4740 res = obj2ast_expr(tmp, &body, arena);
4741 if (res != 0) goto failed;
4742 Py_XDECREF(tmp);
4743 tmp = NULL;
4744 } else {
4745 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4746 return 1;
4747 }
4748 *out = Lambda(args, body, lineno, col_offset, arena);
4749 if (*out == NULL) goto failed;
4750 return 0;
4751 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004752 isinstance = PyObject_IsInstance(obj, (PyObject*)IfExp_type);
4753 if (isinstance == -1) {
4754 return 1;
4755 }
4756 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004757 expr_ty test;
4758 expr_ty body;
4759 expr_ty orelse;
4760
4761 if (PyObject_HasAttrString(obj, "test")) {
4762 int res;
4763 tmp = PyObject_GetAttrString(obj, "test");
4764 if (tmp == NULL) goto failed;
4765 res = obj2ast_expr(tmp, &test, arena);
4766 if (res != 0) goto failed;
4767 Py_XDECREF(tmp);
4768 tmp = NULL;
4769 } else {
4770 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4771 return 1;
4772 }
4773 if (PyObject_HasAttrString(obj, "body")) {
4774 int res;
4775 tmp = PyObject_GetAttrString(obj, "body");
4776 if (tmp == NULL) goto failed;
4777 res = obj2ast_expr(tmp, &body, arena);
4778 if (res != 0) goto failed;
4779 Py_XDECREF(tmp);
4780 tmp = NULL;
4781 } else {
4782 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4783 return 1;
4784 }
4785 if (PyObject_HasAttrString(obj, "orelse")) {
4786 int res;
4787 tmp = PyObject_GetAttrString(obj, "orelse");
4788 if (tmp == NULL) goto failed;
4789 res = obj2ast_expr(tmp, &orelse, arena);
4790 if (res != 0) goto failed;
4791 Py_XDECREF(tmp);
4792 tmp = NULL;
4793 } else {
4794 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4795 return 1;
4796 }
4797 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4798 if (*out == NULL) goto failed;
4799 return 0;
4800 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004801 isinstance = PyObject_IsInstance(obj, (PyObject*)Dict_type);
4802 if (isinstance == -1) {
4803 return 1;
4804 }
4805 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004806 asdl_seq* keys;
4807 asdl_seq* values;
4808
4809 if (PyObject_HasAttrString(obj, "keys")) {
4810 int res;
4811 Py_ssize_t len;
4812 Py_ssize_t i;
4813 tmp = PyObject_GetAttrString(obj, "keys");
4814 if (tmp == NULL) goto failed;
4815 if (!PyList_Check(tmp)) {
4816 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4817 goto failed;
4818 }
4819 len = PyList_GET_SIZE(tmp);
4820 keys = asdl_seq_new(len, arena);
4821 if (keys == NULL) goto failed;
4822 for (i = 0; i < len; i++) {
4823 expr_ty value;
4824 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4825 if (res != 0) goto failed;
4826 asdl_seq_SET(keys, i, value);
4827 }
4828 Py_XDECREF(tmp);
4829 tmp = NULL;
4830 } else {
4831 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4832 return 1;
4833 }
4834 if (PyObject_HasAttrString(obj, "values")) {
4835 int res;
4836 Py_ssize_t len;
4837 Py_ssize_t i;
4838 tmp = PyObject_GetAttrString(obj, "values");
4839 if (tmp == NULL) goto failed;
4840 if (!PyList_Check(tmp)) {
4841 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4842 goto failed;
4843 }
4844 len = PyList_GET_SIZE(tmp);
4845 values = asdl_seq_new(len, arena);
4846 if (values == NULL) goto failed;
4847 for (i = 0; i < len; i++) {
4848 expr_ty value;
4849 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4850 if (res != 0) goto failed;
4851 asdl_seq_SET(values, i, value);
4852 }
4853 Py_XDECREF(tmp);
4854 tmp = NULL;
4855 } else {
4856 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4857 return 1;
4858 }
4859 *out = Dict(keys, values, lineno, col_offset, arena);
4860 if (*out == NULL) goto failed;
4861 return 0;
4862 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004863 isinstance = PyObject_IsInstance(obj, (PyObject*)ListComp_type);
4864 if (isinstance == -1) {
4865 return 1;
4866 }
4867 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004868 expr_ty elt;
4869 asdl_seq* generators;
4870
4871 if (PyObject_HasAttrString(obj, "elt")) {
4872 int res;
4873 tmp = PyObject_GetAttrString(obj, "elt");
4874 if (tmp == NULL) goto failed;
4875 res = obj2ast_expr(tmp, &elt, arena);
4876 if (res != 0) goto failed;
4877 Py_XDECREF(tmp);
4878 tmp = NULL;
4879 } else {
4880 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4881 return 1;
4882 }
4883 if (PyObject_HasAttrString(obj, "generators")) {
4884 int res;
4885 Py_ssize_t len;
4886 Py_ssize_t i;
4887 tmp = PyObject_GetAttrString(obj, "generators");
4888 if (tmp == NULL) goto failed;
4889 if (!PyList_Check(tmp)) {
4890 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4891 goto failed;
4892 }
4893 len = PyList_GET_SIZE(tmp);
4894 generators = asdl_seq_new(len, arena);
4895 if (generators == NULL) goto failed;
4896 for (i = 0; i < len; i++) {
4897 comprehension_ty value;
4898 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4899 if (res != 0) goto failed;
4900 asdl_seq_SET(generators, i, value);
4901 }
4902 Py_XDECREF(tmp);
4903 tmp = NULL;
4904 } else {
4905 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4906 return 1;
4907 }
4908 *out = ListComp(elt, generators, lineno, col_offset, arena);
4909 if (*out == NULL) goto failed;
4910 return 0;
4911 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004912 isinstance = PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type);
4913 if (isinstance == -1) {
4914 return 1;
4915 }
4916 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004917 expr_ty elt;
4918 asdl_seq* generators;
4919
4920 if (PyObject_HasAttrString(obj, "elt")) {
4921 int res;
4922 tmp = PyObject_GetAttrString(obj, "elt");
4923 if (tmp == NULL) goto failed;
4924 res = obj2ast_expr(tmp, &elt, arena);
4925 if (res != 0) goto failed;
4926 Py_XDECREF(tmp);
4927 tmp = NULL;
4928 } else {
4929 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4930 return 1;
4931 }
4932 if (PyObject_HasAttrString(obj, "generators")) {
4933 int res;
4934 Py_ssize_t len;
4935 Py_ssize_t i;
4936 tmp = PyObject_GetAttrString(obj, "generators");
4937 if (tmp == NULL) goto failed;
4938 if (!PyList_Check(tmp)) {
4939 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4940 goto failed;
4941 }
4942 len = PyList_GET_SIZE(tmp);
4943 generators = asdl_seq_new(len, arena);
4944 if (generators == NULL) goto failed;
4945 for (i = 0; i < len; i++) {
4946 comprehension_ty value;
4947 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4948 if (res != 0) goto failed;
4949 asdl_seq_SET(generators, i, value);
4950 }
4951 Py_XDECREF(tmp);
4952 tmp = NULL;
4953 } else {
4954 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4955 return 1;
4956 }
4957 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4958 if (*out == NULL) goto failed;
4959 return 0;
4960 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004961 isinstance = PyObject_IsInstance(obj, (PyObject*)Yield_type);
4962 if (isinstance == -1) {
4963 return 1;
4964 }
4965 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004966 expr_ty value;
4967
4968 if (PyObject_HasAttrString(obj, "value")) {
4969 int res;
4970 tmp = PyObject_GetAttrString(obj, "value");
4971 if (tmp == NULL) goto failed;
4972 res = obj2ast_expr(tmp, &value, arena);
4973 if (res != 0) goto failed;
4974 Py_XDECREF(tmp);
4975 tmp = NULL;
4976 } else {
4977 value = NULL;
4978 }
4979 *out = Yield(value, lineno, col_offset, arena);
4980 if (*out == NULL) goto failed;
4981 return 0;
4982 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00004983 isinstance = PyObject_IsInstance(obj, (PyObject*)Compare_type);
4984 if (isinstance == -1) {
4985 return 1;
4986 }
4987 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00004988 expr_ty left;
4989 asdl_int_seq* ops;
4990 asdl_seq* comparators;
4991
4992 if (PyObject_HasAttrString(obj, "left")) {
4993 int res;
4994 tmp = PyObject_GetAttrString(obj, "left");
4995 if (tmp == NULL) goto failed;
4996 res = obj2ast_expr(tmp, &left, arena);
4997 if (res != 0) goto failed;
4998 Py_XDECREF(tmp);
4999 tmp = NULL;
5000 } else {
5001 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
5002 return 1;
5003 }
5004 if (PyObject_HasAttrString(obj, "ops")) {
5005 int res;
5006 Py_ssize_t len;
5007 Py_ssize_t i;
5008 tmp = PyObject_GetAttrString(obj, "ops");
5009 if (tmp == NULL) goto failed;
5010 if (!PyList_Check(tmp)) {
5011 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5012 goto failed;
5013 }
5014 len = PyList_GET_SIZE(tmp);
5015 ops = asdl_int_seq_new(len, arena);
5016 if (ops == NULL) goto failed;
5017 for (i = 0; i < len; i++) {
5018 cmpop_ty value;
5019 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
5020 if (res != 0) goto failed;
5021 asdl_seq_SET(ops, i, value);
5022 }
5023 Py_XDECREF(tmp);
5024 tmp = NULL;
5025 } else {
5026 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
5027 return 1;
5028 }
5029 if (PyObject_HasAttrString(obj, "comparators")) {
5030 int res;
5031 Py_ssize_t len;
5032 Py_ssize_t i;
5033 tmp = PyObject_GetAttrString(obj, "comparators");
5034 if (tmp == NULL) goto failed;
5035 if (!PyList_Check(tmp)) {
5036 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5037 goto failed;
5038 }
5039 len = PyList_GET_SIZE(tmp);
5040 comparators = asdl_seq_new(len, arena);
5041 if (comparators == NULL) goto failed;
5042 for (i = 0; i < len; i++) {
5043 expr_ty value;
5044 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5045 if (res != 0) goto failed;
5046 asdl_seq_SET(comparators, i, value);
5047 }
5048 Py_XDECREF(tmp);
5049 tmp = NULL;
5050 } else {
5051 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
5052 return 1;
5053 }
5054 *out = Compare(left, ops, comparators, lineno, col_offset,
5055 arena);
5056 if (*out == NULL) goto failed;
5057 return 0;
5058 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005059 isinstance = PyObject_IsInstance(obj, (PyObject*)Call_type);
5060 if (isinstance == -1) {
5061 return 1;
5062 }
5063 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005064 expr_ty func;
5065 asdl_seq* args;
5066 asdl_seq* keywords;
5067 expr_ty starargs;
5068 expr_ty kwargs;
5069
5070 if (PyObject_HasAttrString(obj, "func")) {
5071 int res;
5072 tmp = PyObject_GetAttrString(obj, "func");
5073 if (tmp == NULL) goto failed;
5074 res = obj2ast_expr(tmp, &func, arena);
5075 if (res != 0) goto failed;
5076 Py_XDECREF(tmp);
5077 tmp = NULL;
5078 } else {
5079 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
5080 return 1;
5081 }
5082 if (PyObject_HasAttrString(obj, "args")) {
5083 int res;
5084 Py_ssize_t len;
5085 Py_ssize_t i;
5086 tmp = PyObject_GetAttrString(obj, "args");
5087 if (tmp == NULL) goto failed;
5088 if (!PyList_Check(tmp)) {
5089 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5090 goto failed;
5091 }
5092 len = PyList_GET_SIZE(tmp);
5093 args = asdl_seq_new(len, arena);
5094 if (args == NULL) goto failed;
5095 for (i = 0; i < len; i++) {
5096 expr_ty value;
5097 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5098 if (res != 0) goto failed;
5099 asdl_seq_SET(args, i, value);
5100 }
5101 Py_XDECREF(tmp);
5102 tmp = NULL;
5103 } else {
5104 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
5105 return 1;
5106 }
5107 if (PyObject_HasAttrString(obj, "keywords")) {
5108 int res;
5109 Py_ssize_t len;
5110 Py_ssize_t i;
5111 tmp = PyObject_GetAttrString(obj, "keywords");
5112 if (tmp == NULL) goto failed;
5113 if (!PyList_Check(tmp)) {
5114 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5115 goto failed;
5116 }
5117 len = PyList_GET_SIZE(tmp);
5118 keywords = asdl_seq_new(len, arena);
5119 if (keywords == NULL) goto failed;
5120 for (i = 0; i < len; i++) {
5121 keyword_ty value;
5122 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
5123 if (res != 0) goto failed;
5124 asdl_seq_SET(keywords, i, value);
5125 }
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
5130 return 1;
5131 }
5132 if (PyObject_HasAttrString(obj, "starargs")) {
5133 int res;
5134 tmp = PyObject_GetAttrString(obj, "starargs");
5135 if (tmp == NULL) goto failed;
5136 res = obj2ast_expr(tmp, &starargs, arena);
5137 if (res != 0) goto failed;
5138 Py_XDECREF(tmp);
5139 tmp = NULL;
5140 } else {
5141 starargs = NULL;
5142 }
5143 if (PyObject_HasAttrString(obj, "kwargs")) {
5144 int res;
5145 tmp = PyObject_GetAttrString(obj, "kwargs");
5146 if (tmp == NULL) goto failed;
5147 res = obj2ast_expr(tmp, &kwargs, arena);
5148 if (res != 0) goto failed;
5149 Py_XDECREF(tmp);
5150 tmp = NULL;
5151 } else {
5152 kwargs = NULL;
5153 }
5154 *out = Call(func, args, keywords, starargs, kwargs, lineno,
5155 col_offset, arena);
5156 if (*out == NULL) goto failed;
5157 return 0;
5158 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005159 isinstance = PyObject_IsInstance(obj, (PyObject*)Repr_type);
5160 if (isinstance == -1) {
5161 return 1;
5162 }
5163 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005164 expr_ty value;
5165
5166 if (PyObject_HasAttrString(obj, "value")) {
5167 int res;
5168 tmp = PyObject_GetAttrString(obj, "value");
5169 if (tmp == NULL) goto failed;
5170 res = obj2ast_expr(tmp, &value, arena);
5171 if (res != 0) goto failed;
5172 Py_XDECREF(tmp);
5173 tmp = NULL;
5174 } else {
5175 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5176 return 1;
5177 }
5178 *out = Repr(value, lineno, col_offset, arena);
5179 if (*out == NULL) goto failed;
5180 return 0;
5181 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005182 isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type);
5183 if (isinstance == -1) {
5184 return 1;
5185 }
5186 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005187 object n;
5188
5189 if (PyObject_HasAttrString(obj, "n")) {
5190 int res;
5191 tmp = PyObject_GetAttrString(obj, "n");
5192 if (tmp == NULL) goto failed;
5193 res = obj2ast_object(tmp, &n, arena);
5194 if (res != 0) goto failed;
5195 Py_XDECREF(tmp);
5196 tmp = NULL;
5197 } else {
5198 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5199 return 1;
5200 }
5201 *out = Num(n, lineno, col_offset, arena);
5202 if (*out == NULL) goto failed;
5203 return 0;
5204 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005205 isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type);
5206 if (isinstance == -1) {
5207 return 1;
5208 }
5209 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005210 string s;
5211
5212 if (PyObject_HasAttrString(obj, "s")) {
5213 int res;
5214 tmp = PyObject_GetAttrString(obj, "s");
5215 if (tmp == NULL) goto failed;
5216 res = obj2ast_string(tmp, &s, arena);
5217 if (res != 0) goto failed;
5218 Py_XDECREF(tmp);
5219 tmp = NULL;
5220 } else {
5221 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5222 return 1;
5223 }
5224 *out = Str(s, lineno, col_offset, arena);
5225 if (*out == NULL) goto failed;
5226 return 0;
5227 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005228 isinstance = PyObject_IsInstance(obj, (PyObject*)Attribute_type);
5229 if (isinstance == -1) {
5230 return 1;
5231 }
5232 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005233 expr_ty value;
5234 identifier attr;
5235 expr_context_ty ctx;
5236
5237 if (PyObject_HasAttrString(obj, "value")) {
5238 int res;
5239 tmp = PyObject_GetAttrString(obj, "value");
5240 if (tmp == NULL) goto failed;
5241 res = obj2ast_expr(tmp, &value, arena);
5242 if (res != 0) goto failed;
5243 Py_XDECREF(tmp);
5244 tmp = NULL;
5245 } else {
5246 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5247 return 1;
5248 }
5249 if (PyObject_HasAttrString(obj, "attr")) {
5250 int res;
5251 tmp = PyObject_GetAttrString(obj, "attr");
5252 if (tmp == NULL) goto failed;
5253 res = obj2ast_identifier(tmp, &attr, arena);
5254 if (res != 0) goto failed;
5255 Py_XDECREF(tmp);
5256 tmp = NULL;
5257 } else {
5258 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5259 return 1;
5260 }
5261 if (PyObject_HasAttrString(obj, "ctx")) {
5262 int res;
5263 tmp = PyObject_GetAttrString(obj, "ctx");
5264 if (tmp == NULL) goto failed;
5265 res = obj2ast_expr_context(tmp, &ctx, arena);
5266 if (res != 0) goto failed;
5267 Py_XDECREF(tmp);
5268 tmp = NULL;
5269 } else {
5270 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5271 return 1;
5272 }
5273 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5274 if (*out == NULL) goto failed;
5275 return 0;
5276 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005277 isinstance = PyObject_IsInstance(obj, (PyObject*)Subscript_type);
5278 if (isinstance == -1) {
5279 return 1;
5280 }
5281 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005282 expr_ty value;
5283 slice_ty slice;
5284 expr_context_ty ctx;
5285
5286 if (PyObject_HasAttrString(obj, "value")) {
5287 int res;
5288 tmp = PyObject_GetAttrString(obj, "value");
5289 if (tmp == NULL) goto failed;
5290 res = obj2ast_expr(tmp, &value, arena);
5291 if (res != 0) goto failed;
5292 Py_XDECREF(tmp);
5293 tmp = NULL;
5294 } else {
5295 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5296 return 1;
5297 }
5298 if (PyObject_HasAttrString(obj, "slice")) {
5299 int res;
5300 tmp = PyObject_GetAttrString(obj, "slice");
5301 if (tmp == NULL) goto failed;
5302 res = obj2ast_slice(tmp, &slice, arena);
5303 if (res != 0) goto failed;
5304 Py_XDECREF(tmp);
5305 tmp = NULL;
5306 } else {
5307 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5308 return 1;
5309 }
5310 if (PyObject_HasAttrString(obj, "ctx")) {
5311 int res;
5312 tmp = PyObject_GetAttrString(obj, "ctx");
5313 if (tmp == NULL) goto failed;
5314 res = obj2ast_expr_context(tmp, &ctx, arena);
5315 if (res != 0) goto failed;
5316 Py_XDECREF(tmp);
5317 tmp = NULL;
5318 } else {
5319 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5320 return 1;
5321 }
5322 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5323 if (*out == NULL) goto failed;
5324 return 0;
5325 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005326 isinstance = PyObject_IsInstance(obj, (PyObject*)Name_type);
5327 if (isinstance == -1) {
5328 return 1;
5329 }
5330 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005331 identifier id;
5332 expr_context_ty ctx;
5333
5334 if (PyObject_HasAttrString(obj, "id")) {
5335 int res;
5336 tmp = PyObject_GetAttrString(obj, "id");
5337 if (tmp == NULL) goto failed;
5338 res = obj2ast_identifier(tmp, &id, arena);
5339 if (res != 0) goto failed;
5340 Py_XDECREF(tmp);
5341 tmp = NULL;
5342 } else {
5343 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5344 return 1;
5345 }
5346 if (PyObject_HasAttrString(obj, "ctx")) {
5347 int res;
5348 tmp = PyObject_GetAttrString(obj, "ctx");
5349 if (tmp == NULL) goto failed;
5350 res = obj2ast_expr_context(tmp, &ctx, arena);
5351 if (res != 0) goto failed;
5352 Py_XDECREF(tmp);
5353 tmp = NULL;
5354 } else {
5355 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5356 return 1;
5357 }
5358 *out = Name(id, ctx, lineno, col_offset, arena);
5359 if (*out == NULL) goto failed;
5360 return 0;
5361 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005362 isinstance = PyObject_IsInstance(obj, (PyObject*)List_type);
5363 if (isinstance == -1) {
5364 return 1;
5365 }
5366 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005367 asdl_seq* elts;
5368 expr_context_ty ctx;
5369
5370 if (PyObject_HasAttrString(obj, "elts")) {
5371 int res;
5372 Py_ssize_t len;
5373 Py_ssize_t i;
5374 tmp = PyObject_GetAttrString(obj, "elts");
5375 if (tmp == NULL) goto failed;
5376 if (!PyList_Check(tmp)) {
5377 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5378 goto failed;
5379 }
5380 len = PyList_GET_SIZE(tmp);
5381 elts = asdl_seq_new(len, arena);
5382 if (elts == NULL) goto failed;
5383 for (i = 0; i < len; i++) {
5384 expr_ty value;
5385 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5386 if (res != 0) goto failed;
5387 asdl_seq_SET(elts, i, value);
5388 }
5389 Py_XDECREF(tmp);
5390 tmp = NULL;
5391 } else {
5392 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5393 return 1;
5394 }
5395 if (PyObject_HasAttrString(obj, "ctx")) {
5396 int res;
5397 tmp = PyObject_GetAttrString(obj, "ctx");
5398 if (tmp == NULL) goto failed;
5399 res = obj2ast_expr_context(tmp, &ctx, arena);
5400 if (res != 0) goto failed;
5401 Py_XDECREF(tmp);
5402 tmp = NULL;
5403 } else {
5404 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5405 return 1;
5406 }
5407 *out = List(elts, ctx, lineno, col_offset, arena);
5408 if (*out == NULL) goto failed;
5409 return 0;
5410 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005411 isinstance = PyObject_IsInstance(obj, (PyObject*)Tuple_type);
5412 if (isinstance == -1) {
5413 return 1;
5414 }
5415 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005416 asdl_seq* elts;
5417 expr_context_ty ctx;
5418
5419 if (PyObject_HasAttrString(obj, "elts")) {
5420 int res;
5421 Py_ssize_t len;
5422 Py_ssize_t i;
5423 tmp = PyObject_GetAttrString(obj, "elts");
5424 if (tmp == NULL) goto failed;
5425 if (!PyList_Check(tmp)) {
5426 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5427 goto failed;
5428 }
5429 len = PyList_GET_SIZE(tmp);
5430 elts = asdl_seq_new(len, arena);
5431 if (elts == NULL) goto failed;
5432 for (i = 0; i < len; i++) {
5433 expr_ty value;
5434 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5435 if (res != 0) goto failed;
5436 asdl_seq_SET(elts, i, value);
5437 }
5438 Py_XDECREF(tmp);
5439 tmp = NULL;
5440 } else {
5441 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5442 return 1;
5443 }
5444 if (PyObject_HasAttrString(obj, "ctx")) {
5445 int res;
5446 tmp = PyObject_GetAttrString(obj, "ctx");
5447 if (tmp == NULL) goto failed;
5448 res = obj2ast_expr_context(tmp, &ctx, arena);
5449 if (res != 0) goto failed;
5450 Py_XDECREF(tmp);
5451 tmp = NULL;
5452 } else {
5453 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5454 return 1;
5455 }
5456 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5457 if (*out == NULL) goto failed;
5458 return 0;
5459 }
5460
5461 tmp = PyObject_Repr(obj);
5462 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005463 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005464failed:
5465 Py_XDECREF(tmp);
5466 return 1;
5467}
5468
5469int
5470obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5471{
5472 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005473 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005474
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005475 isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
5476 if (isinstance == -1) {
5477 return 1;
5478 }
5479 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005480 *out = Load;
5481 return 0;
5482 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005483 isinstance = PyObject_IsInstance(obj, (PyObject *)Store_type);
5484 if (isinstance == -1) {
5485 return 1;
5486 }
5487 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005488 *out = Store;
5489 return 0;
5490 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005491 isinstance = PyObject_IsInstance(obj, (PyObject *)Del_type);
5492 if (isinstance == -1) {
5493 return 1;
5494 }
5495 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005496 *out = Del;
5497 return 0;
5498 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005499 isinstance = PyObject_IsInstance(obj, (PyObject *)AugLoad_type);
5500 if (isinstance == -1) {
5501 return 1;
5502 }
5503 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005504 *out = AugLoad;
5505 return 0;
5506 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005507 isinstance = PyObject_IsInstance(obj, (PyObject *)AugStore_type);
5508 if (isinstance == -1) {
5509 return 1;
5510 }
5511 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005512 *out = AugStore;
5513 return 0;
5514 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005515 isinstance = PyObject_IsInstance(obj, (PyObject *)Param_type);
5516 if (isinstance == -1) {
5517 return 1;
5518 }
5519 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005520 *out = Param;
5521 return 0;
5522 }
5523
5524 tmp = PyObject_Repr(obj);
5525 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005526 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005527failed:
5528 Py_XDECREF(tmp);
5529 return 1;
5530}
5531
5532int
5533obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5534{
5535 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005536 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005537
5538
5539 if (obj == Py_None) {
5540 *out = NULL;
5541 return 0;
5542 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005543 isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);
5544 if (isinstance == -1) {
5545 return 1;
5546 }
5547 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005548
5549 *out = Ellipsis(arena);
5550 if (*out == NULL) goto failed;
5551 return 0;
5552 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005553 isinstance = PyObject_IsInstance(obj, (PyObject*)Slice_type);
5554 if (isinstance == -1) {
5555 return 1;
5556 }
5557 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005558 expr_ty lower;
5559 expr_ty upper;
5560 expr_ty step;
5561
5562 if (PyObject_HasAttrString(obj, "lower")) {
5563 int res;
5564 tmp = PyObject_GetAttrString(obj, "lower");
5565 if (tmp == NULL) goto failed;
5566 res = obj2ast_expr(tmp, &lower, arena);
5567 if (res != 0) goto failed;
5568 Py_XDECREF(tmp);
5569 tmp = NULL;
5570 } else {
5571 lower = NULL;
5572 }
5573 if (PyObject_HasAttrString(obj, "upper")) {
5574 int res;
5575 tmp = PyObject_GetAttrString(obj, "upper");
5576 if (tmp == NULL) goto failed;
5577 res = obj2ast_expr(tmp, &upper, arena);
5578 if (res != 0) goto failed;
5579 Py_XDECREF(tmp);
5580 tmp = NULL;
5581 } else {
5582 upper = NULL;
5583 }
5584 if (PyObject_HasAttrString(obj, "step")) {
5585 int res;
5586 tmp = PyObject_GetAttrString(obj, "step");
5587 if (tmp == NULL) goto failed;
5588 res = obj2ast_expr(tmp, &step, arena);
5589 if (res != 0) goto failed;
5590 Py_XDECREF(tmp);
5591 tmp = NULL;
5592 } else {
5593 step = NULL;
5594 }
5595 *out = Slice(lower, upper, step, arena);
5596 if (*out == NULL) goto failed;
5597 return 0;
5598 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005599 isinstance = PyObject_IsInstance(obj, (PyObject*)ExtSlice_type);
5600 if (isinstance == -1) {
5601 return 1;
5602 }
5603 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005604 asdl_seq* dims;
5605
5606 if (PyObject_HasAttrString(obj, "dims")) {
5607 int res;
5608 Py_ssize_t len;
5609 Py_ssize_t i;
5610 tmp = PyObject_GetAttrString(obj, "dims");
5611 if (tmp == NULL) goto failed;
5612 if (!PyList_Check(tmp)) {
5613 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5614 goto failed;
5615 }
5616 len = PyList_GET_SIZE(tmp);
5617 dims = asdl_seq_new(len, arena);
5618 if (dims == NULL) goto failed;
5619 for (i = 0; i < len; i++) {
5620 slice_ty value;
5621 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5622 if (res != 0) goto failed;
5623 asdl_seq_SET(dims, i, value);
5624 }
5625 Py_XDECREF(tmp);
5626 tmp = NULL;
5627 } else {
5628 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5629 return 1;
5630 }
5631 *out = ExtSlice(dims, arena);
5632 if (*out == NULL) goto failed;
5633 return 0;
5634 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005635 isinstance = PyObject_IsInstance(obj, (PyObject*)Index_type);
5636 if (isinstance == -1) {
5637 return 1;
5638 }
5639 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005640 expr_ty value;
5641
5642 if (PyObject_HasAttrString(obj, "value")) {
5643 int res;
5644 tmp = PyObject_GetAttrString(obj, "value");
5645 if (tmp == NULL) goto failed;
5646 res = obj2ast_expr(tmp, &value, arena);
5647 if (res != 0) goto failed;
5648 Py_XDECREF(tmp);
5649 tmp = NULL;
5650 } else {
5651 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5652 return 1;
5653 }
5654 *out = Index(value, arena);
5655 if (*out == NULL) goto failed;
5656 return 0;
5657 }
5658
5659 tmp = PyObject_Repr(obj);
5660 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005661 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005662failed:
5663 Py_XDECREF(tmp);
5664 return 1;
5665}
5666
5667int
5668obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5669{
5670 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005671 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005672
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005673 isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
5674 if (isinstance == -1) {
5675 return 1;
5676 }
5677 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005678 *out = And;
5679 return 0;
5680 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005681 isinstance = PyObject_IsInstance(obj, (PyObject *)Or_type);
5682 if (isinstance == -1) {
5683 return 1;
5684 }
5685 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005686 *out = Or;
5687 return 0;
5688 }
5689
5690 tmp = PyObject_Repr(obj);
5691 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005692 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005693failed:
5694 Py_XDECREF(tmp);
5695 return 1;
5696}
5697
5698int
5699obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5700{
5701 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005702 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005703
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005704 isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
5705 if (isinstance == -1) {
5706 return 1;
5707 }
5708 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005709 *out = Add;
5710 return 0;
5711 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005712 isinstance = PyObject_IsInstance(obj, (PyObject *)Sub_type);
5713 if (isinstance == -1) {
5714 return 1;
5715 }
5716 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005717 *out = Sub;
5718 return 0;
5719 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005720 isinstance = PyObject_IsInstance(obj, (PyObject *)Mult_type);
5721 if (isinstance == -1) {
5722 return 1;
5723 }
5724 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005725 *out = Mult;
5726 return 0;
5727 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005728 isinstance = PyObject_IsInstance(obj, (PyObject *)Div_type);
5729 if (isinstance == -1) {
5730 return 1;
5731 }
5732 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005733 *out = Div;
5734 return 0;
5735 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005736 isinstance = PyObject_IsInstance(obj, (PyObject *)Mod_type);
5737 if (isinstance == -1) {
5738 return 1;
5739 }
5740 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005741 *out = Mod;
5742 return 0;
5743 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005744 isinstance = PyObject_IsInstance(obj, (PyObject *)Pow_type);
5745 if (isinstance == -1) {
5746 return 1;
5747 }
5748 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005749 *out = Pow;
5750 return 0;
5751 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005752 isinstance = PyObject_IsInstance(obj, (PyObject *)LShift_type);
5753 if (isinstance == -1) {
5754 return 1;
5755 }
5756 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005757 *out = LShift;
5758 return 0;
5759 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005760 isinstance = PyObject_IsInstance(obj, (PyObject *)RShift_type);
5761 if (isinstance == -1) {
5762 return 1;
5763 }
5764 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005765 *out = RShift;
5766 return 0;
5767 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005768 isinstance = PyObject_IsInstance(obj, (PyObject *)BitOr_type);
5769 if (isinstance == -1) {
5770 return 1;
5771 }
5772 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005773 *out = BitOr;
5774 return 0;
5775 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005776 isinstance = PyObject_IsInstance(obj, (PyObject *)BitXor_type);
5777 if (isinstance == -1) {
5778 return 1;
5779 }
5780 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005781 *out = BitXor;
5782 return 0;
5783 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005784 isinstance = PyObject_IsInstance(obj, (PyObject *)BitAnd_type);
5785 if (isinstance == -1) {
5786 return 1;
5787 }
5788 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005789 *out = BitAnd;
5790 return 0;
5791 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005792 isinstance = PyObject_IsInstance(obj, (PyObject *)FloorDiv_type);
5793 if (isinstance == -1) {
5794 return 1;
5795 }
5796 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005797 *out = FloorDiv;
5798 return 0;
5799 }
5800
5801 tmp = PyObject_Repr(obj);
5802 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005803 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005804failed:
5805 Py_XDECREF(tmp);
5806 return 1;
5807}
5808
5809int
5810obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5811{
5812 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005813 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005814
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005815 isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
5816 if (isinstance == -1) {
5817 return 1;
5818 }
5819 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005820 *out = Invert;
5821 return 0;
5822 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005823 isinstance = PyObject_IsInstance(obj, (PyObject *)Not_type);
5824 if (isinstance == -1) {
5825 return 1;
5826 }
5827 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005828 *out = Not;
5829 return 0;
5830 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005831 isinstance = PyObject_IsInstance(obj, (PyObject *)UAdd_type);
5832 if (isinstance == -1) {
5833 return 1;
5834 }
5835 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005836 *out = UAdd;
5837 return 0;
5838 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005839 isinstance = PyObject_IsInstance(obj, (PyObject *)USub_type);
5840 if (isinstance == -1) {
5841 return 1;
5842 }
5843 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005844 *out = USub;
5845 return 0;
5846 }
5847
5848 tmp = PyObject_Repr(obj);
5849 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005850 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005851failed:
5852 Py_XDECREF(tmp);
5853 return 1;
5854}
5855
5856int
5857obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5858{
5859 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005860 int isinstance;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005861
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005862 isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
5863 if (isinstance == -1) {
5864 return 1;
5865 }
5866 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005867 *out = Eq;
5868 return 0;
5869 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005870 isinstance = PyObject_IsInstance(obj, (PyObject *)NotEq_type);
5871 if (isinstance == -1) {
5872 return 1;
5873 }
5874 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005875 *out = NotEq;
5876 return 0;
5877 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005878 isinstance = PyObject_IsInstance(obj, (PyObject *)Lt_type);
5879 if (isinstance == -1) {
5880 return 1;
5881 }
5882 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005883 *out = Lt;
5884 return 0;
5885 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005886 isinstance = PyObject_IsInstance(obj, (PyObject *)LtE_type);
5887 if (isinstance == -1) {
5888 return 1;
5889 }
5890 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005891 *out = LtE;
5892 return 0;
5893 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005894 isinstance = PyObject_IsInstance(obj, (PyObject *)Gt_type);
5895 if (isinstance == -1) {
5896 return 1;
5897 }
5898 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005899 *out = Gt;
5900 return 0;
5901 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005902 isinstance = PyObject_IsInstance(obj, (PyObject *)GtE_type);
5903 if (isinstance == -1) {
5904 return 1;
5905 }
5906 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005907 *out = GtE;
5908 return 0;
5909 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005910 isinstance = PyObject_IsInstance(obj, (PyObject *)Is_type);
5911 if (isinstance == -1) {
5912 return 1;
5913 }
5914 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005915 *out = Is;
5916 return 0;
5917 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005918 isinstance = PyObject_IsInstance(obj, (PyObject *)IsNot_type);
5919 if (isinstance == -1) {
5920 return 1;
5921 }
5922 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005923 *out = IsNot;
5924 return 0;
5925 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005926 isinstance = PyObject_IsInstance(obj, (PyObject *)In_type);
5927 if (isinstance == -1) {
5928 return 1;
5929 }
5930 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005931 *out = In;
5932 return 0;
5933 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00005934 isinstance = PyObject_IsInstance(obj, (PyObject *)NotIn_type);
5935 if (isinstance == -1) {
5936 return 1;
5937 }
5938 if (isinstance) {
Georg Brandlfc8eef32008-03-28 12:11:56 +00005939 *out = NotIn;
5940 return 0;
5941 }
5942
5943 tmp = PyObject_Repr(obj);
5944 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00005945 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005946failed:
5947 Py_XDECREF(tmp);
5948 return 1;
5949}
5950
5951int
5952obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5953{
5954 PyObject* tmp = NULL;
5955 expr_ty target;
5956 expr_ty iter;
5957 asdl_seq* ifs;
5958
5959 if (PyObject_HasAttrString(obj, "target")) {
5960 int res;
5961 tmp = PyObject_GetAttrString(obj, "target");
5962 if (tmp == NULL) goto failed;
5963 res = obj2ast_expr(tmp, &target, arena);
5964 if (res != 0) goto failed;
5965 Py_XDECREF(tmp);
5966 tmp = NULL;
5967 } else {
5968 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5969 return 1;
5970 }
5971 if (PyObject_HasAttrString(obj, "iter")) {
5972 int res;
5973 tmp = PyObject_GetAttrString(obj, "iter");
5974 if (tmp == NULL) goto failed;
5975 res = obj2ast_expr(tmp, &iter, arena);
5976 if (res != 0) goto failed;
5977 Py_XDECREF(tmp);
5978 tmp = NULL;
5979 } else {
5980 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5981 return 1;
5982 }
5983 if (PyObject_HasAttrString(obj, "ifs")) {
5984 int res;
5985 Py_ssize_t len;
5986 Py_ssize_t i;
5987 tmp = PyObject_GetAttrString(obj, "ifs");
5988 if (tmp == NULL) goto failed;
5989 if (!PyList_Check(tmp)) {
5990 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5991 goto failed;
5992 }
5993 len = PyList_GET_SIZE(tmp);
5994 ifs = asdl_seq_new(len, arena);
5995 if (ifs == NULL) goto failed;
5996 for (i = 0; i < len; i++) {
5997 expr_ty value;
5998 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5999 if (res != 0) goto failed;
6000 asdl_seq_SET(ifs, i, value);
6001 }
6002 Py_XDECREF(tmp);
6003 tmp = NULL;
6004 } else {
6005 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
6006 return 1;
6007 }
6008 *out = comprehension(target, iter, ifs, arena);
6009 return 0;
6010failed:
6011 Py_XDECREF(tmp);
6012 return 1;
6013}
6014
6015int
6016obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
6017{
6018 PyObject* tmp = NULL;
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006019 int isinstance;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006020
Georg Brandlfc8eef32008-03-28 12:11:56 +00006021 int lineno;
6022 int col_offset;
6023
Georg Brandla48f3ab2008-03-30 06:40:17 +00006024 if (obj == Py_None) {
6025 *out = NULL;
6026 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00006027 }
6028 if (PyObject_HasAttrString(obj, "lineno")) {
6029 int res;
6030 tmp = PyObject_GetAttrString(obj, "lineno");
6031 if (tmp == NULL) goto failed;
6032 res = obj2ast_int(tmp, &lineno, arena);
6033 if (res != 0) goto failed;
6034 Py_XDECREF(tmp);
6035 tmp = NULL;
6036 } else {
6037 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
6038 return 1;
6039 }
6040 if (PyObject_HasAttrString(obj, "col_offset")) {
6041 int res;
6042 tmp = PyObject_GetAttrString(obj, "col_offset");
6043 if (tmp == NULL) goto failed;
6044 res = obj2ast_int(tmp, &col_offset, arena);
6045 if (res != 0) goto failed;
6046 Py_XDECREF(tmp);
6047 tmp = NULL;
6048 } else {
6049 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
6050 return 1;
6051 }
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006052 isinstance = PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type);
6053 if (isinstance == -1) {
6054 return 1;
6055 }
6056 if (isinstance) {
Georg Brandla48f3ab2008-03-30 06:40:17 +00006057 expr_ty type;
6058 expr_ty name;
6059 asdl_seq* body;
6060
6061 if (PyObject_HasAttrString(obj, "type")) {
6062 int res;
6063 tmp = PyObject_GetAttrString(obj, "type");
6064 if (tmp == NULL) goto failed;
6065 res = obj2ast_expr(tmp, &type, arena);
6066 if (res != 0) goto failed;
6067 Py_XDECREF(tmp);
6068 tmp = NULL;
6069 } else {
6070 type = NULL;
6071 }
6072 if (PyObject_HasAttrString(obj, "name")) {
6073 int res;
6074 tmp = PyObject_GetAttrString(obj, "name");
6075 if (tmp == NULL) goto failed;
6076 res = obj2ast_expr(tmp, &name, arena);
6077 if (res != 0) goto failed;
6078 Py_XDECREF(tmp);
6079 tmp = NULL;
6080 } else {
6081 name = NULL;
6082 }
6083 if (PyObject_HasAttrString(obj, "body")) {
6084 int res;
6085 Py_ssize_t len;
6086 Py_ssize_t i;
6087 tmp = PyObject_GetAttrString(obj, "body");
6088 if (tmp == NULL) goto failed;
6089 if (!PyList_Check(tmp)) {
6090 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6091 goto failed;
6092 }
6093 len = PyList_GET_SIZE(tmp);
6094 body = asdl_seq_new(len, arena);
6095 if (body == NULL) goto failed;
6096 for (i = 0; i < len; i++) {
6097 stmt_ty value;
6098 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
6099 if (res != 0) goto failed;
6100 asdl_seq_SET(body, i, value);
6101 }
6102 Py_XDECREF(tmp);
6103 tmp = NULL;
6104 } else {
6105 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
6106 return 1;
6107 }
6108 *out = ExceptHandler(type, name, body, lineno, col_offset,
6109 arena);
6110 if (*out == NULL) goto failed;
6111 return 0;
6112 }
6113
6114 tmp = PyObject_Repr(obj);
6115 if (tmp == NULL) goto failed;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00006116 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00006117failed:
6118 Py_XDECREF(tmp);
6119 return 1;
6120}
6121
6122int
6123obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
6124{
6125 PyObject* tmp = NULL;
6126 asdl_seq* args;
6127 identifier vararg;
6128 identifier kwarg;
6129 asdl_seq* defaults;
6130
6131 if (PyObject_HasAttrString(obj, "args")) {
6132 int res;
6133 Py_ssize_t len;
6134 Py_ssize_t i;
6135 tmp = PyObject_GetAttrString(obj, "args");
6136 if (tmp == NULL) goto failed;
6137 if (!PyList_Check(tmp)) {
6138 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6139 goto failed;
6140 }
6141 len = PyList_GET_SIZE(tmp);
6142 args = asdl_seq_new(len, arena);
6143 if (args == NULL) goto failed;
6144 for (i = 0; i < len; i++) {
6145 expr_ty value;
6146 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6147 if (res != 0) goto failed;
6148 asdl_seq_SET(args, i, value);
6149 }
6150 Py_XDECREF(tmp);
6151 tmp = NULL;
6152 } else {
6153 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
6154 return 1;
6155 }
6156 if (PyObject_HasAttrString(obj, "vararg")) {
6157 int res;
6158 tmp = PyObject_GetAttrString(obj, "vararg");
6159 if (tmp == NULL) goto failed;
6160 res = obj2ast_identifier(tmp, &vararg, arena);
6161 if (res != 0) goto failed;
6162 Py_XDECREF(tmp);
6163 tmp = NULL;
6164 } else {
6165 vararg = NULL;
6166 }
6167 if (PyObject_HasAttrString(obj, "kwarg")) {
6168 int res;
6169 tmp = PyObject_GetAttrString(obj, "kwarg");
6170 if (tmp == NULL) goto failed;
6171 res = obj2ast_identifier(tmp, &kwarg, arena);
6172 if (res != 0) goto failed;
6173 Py_XDECREF(tmp);
6174 tmp = NULL;
6175 } else {
6176 kwarg = NULL;
6177 }
6178 if (PyObject_HasAttrString(obj, "defaults")) {
6179 int res;
6180 Py_ssize_t len;
6181 Py_ssize_t i;
6182 tmp = PyObject_GetAttrString(obj, "defaults");
6183 if (tmp == NULL) goto failed;
6184 if (!PyList_Check(tmp)) {
6185 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
6186 goto failed;
6187 }
6188 len = PyList_GET_SIZE(tmp);
6189 defaults = asdl_seq_new(len, arena);
6190 if (defaults == NULL) goto failed;
6191 for (i = 0; i < len; i++) {
6192 expr_ty value;
6193 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
6194 if (res != 0) goto failed;
6195 asdl_seq_SET(defaults, i, value);
6196 }
6197 Py_XDECREF(tmp);
6198 tmp = NULL;
6199 } else {
6200 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
6201 return 1;
6202 }
6203 *out = arguments(args, vararg, kwarg, defaults, arena);
6204 return 0;
6205failed:
6206 Py_XDECREF(tmp);
6207 return 1;
6208}
6209
6210int
6211obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
6212{
6213 PyObject* tmp = NULL;
6214 identifier arg;
6215 expr_ty value;
6216
6217 if (PyObject_HasAttrString(obj, "arg")) {
6218 int res;
6219 tmp = PyObject_GetAttrString(obj, "arg");
6220 if (tmp == NULL) goto failed;
6221 res = obj2ast_identifier(tmp, &arg, arena);
6222 if (res != 0) goto failed;
6223 Py_XDECREF(tmp);
6224 tmp = NULL;
6225 } else {
6226 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
6227 return 1;
6228 }
6229 if (PyObject_HasAttrString(obj, "value")) {
6230 int res;
6231 tmp = PyObject_GetAttrString(obj, "value");
6232 if (tmp == NULL) goto failed;
6233 res = obj2ast_expr(tmp, &value, arena);
6234 if (res != 0) goto failed;
6235 Py_XDECREF(tmp);
6236 tmp = NULL;
6237 } else {
6238 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
6239 return 1;
6240 }
6241 *out = keyword(arg, value, arena);
6242 return 0;
6243failed:
6244 Py_XDECREF(tmp);
6245 return 1;
6246}
6247
6248int
6249obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
6250{
6251 PyObject* tmp = NULL;
6252 identifier name;
6253 identifier asname;
6254
6255 if (PyObject_HasAttrString(obj, "name")) {
6256 int res;
6257 tmp = PyObject_GetAttrString(obj, "name");
6258 if (tmp == NULL) goto failed;
6259 res = obj2ast_identifier(tmp, &name, arena);
6260 if (res != 0) goto failed;
6261 Py_XDECREF(tmp);
6262 tmp = NULL;
6263 } else {
6264 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
6265 return 1;
6266 }
6267 if (PyObject_HasAttrString(obj, "asname")) {
6268 int res;
6269 tmp = PyObject_GetAttrString(obj, "asname");
6270 if (tmp == NULL) goto failed;
6271 res = obj2ast_identifier(tmp, &asname, arena);
6272 if (res != 0) goto failed;
6273 Py_XDECREF(tmp);
6274 tmp = NULL;
6275 } else {
6276 asname = NULL;
6277 }
6278 *out = alias(name, asname, arena);
6279 return 0;
6280failed:
6281 Py_XDECREF(tmp);
6282 return 1;
6283}
6284
6285
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006286PyMODINIT_FUNC
6287init_ast(void)
6288{
6289 PyObject *m, *d;
6290 if (!init_types()) return;
6291 m = Py_InitModule3("_ast", NULL, NULL);
6292 if (!m) return;
6293 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00006294 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006295 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
6296 return;
Benjamin Peterson8fa3e932009-06-13 20:30:48 +00006297 if (PyModule_AddStringConstant(m, "__version__", "73421") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00006298 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006299 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
6300 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
6301 return;
6302 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
6303 < 0) return;
6304 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
6305 0) return;
6306 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
6307 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
6308 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
6309 < 0) return;
6310 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
6311 return;
6312 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
6313 return;
6314 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
6315 return;
6316 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
6317 return;
6318 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
6319 0) return;
6320 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
6321 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
6322 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
6323 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
6324 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
6325 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
6326 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
6327 0) return;
6328 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
6329 0) return;
6330 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
6331 return;
6332 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
6333 return;
6334 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
6335 0) return;
6336 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
6337 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
6338 return;
6339 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
6340 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
6341 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
6342 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
6343 return;
6344 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
6345 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
6346 return;
6347 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
6348 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
6349 return;
6350 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
6351 return;
6352 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
6353 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
6354 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
6355 return;
6356 if (PyDict_SetItemString(d, "GeneratorExp",
6357 (PyObject*)GeneratorExp_type) < 0) return;
6358 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
6359 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
6360 return;
6361 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6362 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6363 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6364 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6365 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6366 0) return;
6367 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6368 0) return;
6369 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6370 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6371 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6372 if (PyDict_SetItemString(d, "expr_context",
6373 (PyObject*)expr_context_type) < 0) return;
6374 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6375 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6376 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6377 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6378 return;
6379 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6380 return;
6381 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6382 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6383 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6384 return;
6385 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6386 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6387 return;
6388 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6389 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6390 return;
6391 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6392 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6393 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6394 return;
6395 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6396 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6397 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6398 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6399 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6400 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6401 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6402 return;
6403 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6404 return;
6405 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6406 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6407 return;
6408 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6409 return;
6410 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6411 return;
6412 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6413 return;
6414 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6415 return;
6416 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6417 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6418 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6419 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6420 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6421 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6422 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6423 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6424 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6425 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6426 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6427 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6428 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6429 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6430 if (PyDict_SetItemString(d, "comprehension",
6431 (PyObject*)comprehension_type) < 0) return;
6432 if (PyDict_SetItemString(d, "excepthandler",
6433 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006434 if (PyDict_SetItemString(d, "ExceptHandler",
6435 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006436 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6437 0) return;
6438 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6439 return;
6440 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006441}
6442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006443
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006444PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006445{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006446 init_types();
6447 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006448}
6449
Georg Brandlf2bfd542008-03-29 13:24:23 +00006450/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6451mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006452{
6453 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006454 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6455 (PyObject*)Interactive_type};
6456 char *req_name[] = {"Module", "Expression", "Interactive"};
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006457 int isinstance;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006458 assert(0 <= mode && mode <= 2);
6459
Georg Brandlfc8eef32008-03-28 12:11:56 +00006460 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006461
Benjamin Peterson5f429e02009-12-13 00:54:15 +00006462 isinstance = PyObject_IsInstance(ast, req_type[mode]);
6463 if (isinstance == -1)
6464 return NULL;
6465 if (!isinstance) {
Georg Brandlf2bfd542008-03-29 13:24:23 +00006466 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6467 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006468 return NULL;
6469 }
6470 if (obj2ast_mod(ast, &res, arena) != 0)
6471 return NULL;
6472 else
6473 return res;
6474}
6475
6476int PyAST_Check(PyObject* obj)
6477{
6478 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006479 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006480}
6481
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006482