blob: f958145a23317903b2cdab062b85c47b4fb368b5 [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Georg Brandlc52ed592008-03-30 07:01:47 +00005 __version__ 62047.
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"
390 "%" PY_FORMAT_SIZE_T "d positional argument%s",
391 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++) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000498 PyObject *field = PyString_FromString(fields[i]);
499 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++) {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000517 s = PyString_FromString(attrs[i]);
518 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",
591 PyString_AS_STRING(s));
592 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",
609 PyString_AS_STRING(s));
610 Py_DECREF(s);
611 return 1;
612 }
613
614 *out = (obj == Py_True);
615 return 0;
616}
617
618
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000619static int init_types(void)
620{
Neal Norwitz19379f12006-04-03 04:50:58 +0000621 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000622 if (initialized) return 1;
Georg Brandlc52ed592008-03-30 07:01:47 +0000623 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 if (!mod_type) return 0;
625 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000626 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000627 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000628 Interactive_type = make_type("Interactive", mod_type,
629 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000630 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000631 Expression_type = make_type("Expression", mod_type, Expression_fields,
632 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000633 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000634 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000635 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000636 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000637 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000638 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 FunctionDef_type = make_type("FunctionDef", stmt_type,
640 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000641 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000642 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000645 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000646 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000649 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000652 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000653 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000655 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000660 With_type = make_type("With", stmt_type, With_fields, 3);
661 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000662 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000664 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
667 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000668 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000671 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000674 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000682 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000684 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000686 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000688 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000690 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000699 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000705 GeneratorExp_type = make_type("GeneratorExp", expr_type,
706 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000710 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000716 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000718 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000720 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000724 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000728 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000730 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!expr_context_type) return 0;
732 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000733 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000735 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000737 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000741 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000743 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000745 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000746 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000747 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000748 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000749 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000753 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000757 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!slice_type) return 0;
759 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000760 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000764 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000768 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!boolop_type) return 0;
770 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000771 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000773 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000775 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000779 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!operator_type) return 0;
781 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000782 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000784 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000786 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000788 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000790 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000792 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000794 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000796 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000798 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000800 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000802 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000804 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000806 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000808 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000810 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000812 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000814 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000816 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000818 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000820 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000822 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000824 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000826 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000827 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000828 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000829 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000830 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!unaryop_type) return 0;
832 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000833 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000835 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000837 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000839 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000841 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000843 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000845 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000847 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000849 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!cmpop_type) return 0;
851 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000852 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000854 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000856 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000858 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000860 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000862 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000864 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000866 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000868 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000870 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000872 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000873 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000874 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000875 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000876 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000877 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000878 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000879 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000880 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000882 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000884 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000886 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000887 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000888 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000889 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000890 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000891 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000892 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000893 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000894 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000895 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000896 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000897 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
898 return 0;
899 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
900 ExceptHandler_fields, 3);
901 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000902 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000903 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000904 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000905 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000906 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000907 if (!alias_type) return 0;
908 initialized = 1;
909 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000910}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000911
Georg Brandlfc8eef32008-03-28 12:11:56 +0000912static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
913static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
914static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
915static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
916 arena);
917static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
918static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
919static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
920static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
921static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
922static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
923 arena);
924static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
925 arena);
926static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
927static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
928static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000931Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932{
933 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000934 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000935 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 p->kind = Module_kind;
938 p->v.Module.body = body;
939 return p;
940}
941
942mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000943Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944{
945 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000946 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000947 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949 p->kind = Interactive_kind;
950 p->v.Interactive.body = body;
951 return p;
952}
953
954mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000955Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956{
957 mod_ty p;
958 if (!body) {
959 PyErr_SetString(PyExc_ValueError,
960 "field body is required for Expression");
961 return NULL;
962 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000963 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000964 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966 p->kind = Expression_kind;
967 p->v.Expression.body = body;
968 return p;
969}
970
971mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000972Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973{
974 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000975 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000976 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 p->kind = Suite_kind;
979 p->v.Suite.body = body;
980 return p;
981}
982
983stmt_ty
984FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +0000985 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986{
987 stmt_ty p;
988 if (!name) {
989 PyErr_SetString(PyExc_ValueError,
990 "field name is required for FunctionDef");
991 return NULL;
992 }
993 if (!args) {
994 PyErr_SetString(PyExc_ValueError,
995 "field args is required for FunctionDef");
996 return NULL;
997 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000998 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000999 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 p->kind = FunctionDef_kind;
1002 p->v.FunctionDef.name = name;
1003 p->v.FunctionDef.args = args;
1004 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001005 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001007 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 return p;
1009}
1010
1011stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +00001012ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
1013 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014{
1015 stmt_ty p;
1016 if (!name) {
1017 PyErr_SetString(PyExc_ValueError,
1018 "field name is required for ClassDef");
1019 return NULL;
1020 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001021 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001022 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024 p->kind = ClassDef_kind;
1025 p->v.ClassDef.name = name;
1026 p->v.ClassDef.bases = bases;
1027 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001028 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001030 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031 return p;
1032}
1033
1034stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001035Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036{
1037 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001038 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001039 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 p->kind = Return_kind;
1042 p->v.Return.value = value;
1043 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001044 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 return p;
1046}
1047
1048stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001049Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050{
1051 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001052 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001053 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 p->kind = Delete_kind;
1056 p->v.Delete.targets = targets;
1057 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001058 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 return p;
1060}
1061
1062stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001063Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1064 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065{
1066 stmt_ty p;
1067 if (!value) {
1068 PyErr_SetString(PyExc_ValueError,
1069 "field value is required for Assign");
1070 return NULL;
1071 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001072 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001073 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 p->kind = Assign_kind;
1076 p->v.Assign.targets = targets;
1077 p->v.Assign.value = value;
1078 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001079 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 return p;
1081}
1082
1083stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001084AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1085 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086{
1087 stmt_ty p;
1088 if (!target) {
1089 PyErr_SetString(PyExc_ValueError,
1090 "field target is required for AugAssign");
1091 return NULL;
1092 }
1093 if (!op) {
1094 PyErr_SetString(PyExc_ValueError,
1095 "field op is required for AugAssign");
1096 return NULL;
1097 }
1098 if (!value) {
1099 PyErr_SetString(PyExc_ValueError,
1100 "field value is required for AugAssign");
1101 return NULL;
1102 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001103 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001104 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 p->kind = AugAssign_kind;
1107 p->v.AugAssign.target = target;
1108 p->v.AugAssign.op = op;
1109 p->v.AugAssign.value = value;
1110 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001111 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 return p;
1113}
1114
1115stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001116Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1117 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118{
1119 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001120 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001121 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 p->kind = Print_kind;
1124 p->v.Print.dest = dest;
1125 p->v.Print.values = values;
1126 p->v.Print.nl = nl;
1127 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001128 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 return p;
1130}
1131
1132stmt_ty
1133For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001134 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135{
1136 stmt_ty p;
1137 if (!target) {
1138 PyErr_SetString(PyExc_ValueError,
1139 "field target is required for For");
1140 return NULL;
1141 }
1142 if (!iter) {
1143 PyErr_SetString(PyExc_ValueError,
1144 "field iter is required for For");
1145 return NULL;
1146 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001147 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001148 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 p->kind = For_kind;
1151 p->v.For.target = target;
1152 p->v.For.iter = iter;
1153 p->v.For.body = body;
1154 p->v.For.orelse = orelse;
1155 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001156 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return p;
1158}
1159
1160stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001161While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1162 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163{
1164 stmt_ty p;
1165 if (!test) {
1166 PyErr_SetString(PyExc_ValueError,
1167 "field test is required for While");
1168 return NULL;
1169 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001170 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001171 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 p->kind = While_kind;
1174 p->v.While.test = test;
1175 p->v.While.body = body;
1176 p->v.While.orelse = orelse;
1177 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001178 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 return p;
1180}
1181
1182stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001183If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1184 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185{
1186 stmt_ty p;
1187 if (!test) {
1188 PyErr_SetString(PyExc_ValueError,
1189 "field test is required for If");
1190 return NULL;
1191 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001192 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001193 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 p->kind = If_kind;
1196 p->v.If.test = test;
1197 p->v.If.body = body;
1198 p->v.If.orelse = orelse;
1199 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 return p;
1202}
1203
1204stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001205With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001206 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001207{
1208 stmt_ty p;
1209 if (!context_expr) {
1210 PyErr_SetString(PyExc_ValueError,
1211 "field context_expr is required for With");
1212 return NULL;
1213 }
1214 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001215 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001216 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001217 p->kind = With_kind;
1218 p->v.With.context_expr = context_expr;
1219 p->v.With.optional_vars = optional_vars;
1220 p->v.With.body = body;
1221 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001222 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001223 return p;
1224}
1225
1226stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001227Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1228 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229{
1230 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001231 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001232 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234 p->kind = Raise_kind;
1235 p->v.Raise.type = type;
1236 p->v.Raise.inst = inst;
1237 p->v.Raise.tback = tback;
1238 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return p;
1241}
1242
1243stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001244TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001245 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246{
1247 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001248 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001249 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 p->kind = TryExcept_kind;
1252 p->v.TryExcept.body = body;
1253 p->v.TryExcept.handlers = handlers;
1254 p->v.TryExcept.orelse = orelse;
1255 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001256 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 return p;
1258}
1259
1260stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001261TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1262 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263{
1264 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001265 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001266 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 p->kind = TryFinally_kind;
1269 p->v.TryFinally.body = body;
1270 p->v.TryFinally.finalbody = finalbody;
1271 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001272 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return p;
1274}
1275
1276stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001277Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278{
1279 stmt_ty p;
1280 if (!test) {
1281 PyErr_SetString(PyExc_ValueError,
1282 "field test is required for Assert");
1283 return NULL;
1284 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001285 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001286 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288 p->kind = Assert_kind;
1289 p->v.Assert.test = test;
1290 p->v.Assert.msg = msg;
1291 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001292 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293 return p;
1294}
1295
1296stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001297Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298{
1299 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001300 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001301 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 p->kind = Import_kind;
1304 p->v.Import.names = names;
1305 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001306 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 return p;
1308}
1309
1310stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001311ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1312 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313{
1314 stmt_ty p;
1315 if (!module) {
1316 PyErr_SetString(PyExc_ValueError,
1317 "field module is required for ImportFrom");
1318 return NULL;
1319 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001320 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001321 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 p->kind = ImportFrom_kind;
1324 p->v.ImportFrom.module = module;
1325 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001326 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001328 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 return p;
1330}
1331
1332stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001333Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1334 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335{
1336 stmt_ty p;
1337 if (!body) {
1338 PyErr_SetString(PyExc_ValueError,
1339 "field body is required for Exec");
1340 return NULL;
1341 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001342 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001343 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 p->kind = Exec_kind;
1346 p->v.Exec.body = body;
1347 p->v.Exec.globals = globals;
1348 p->v.Exec.locals = locals;
1349 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001350 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return p;
1352}
1353
1354stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001355Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356{
1357 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001358 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001359 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001360 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001361 p->kind = Global_kind;
1362 p->v.Global.names = names;
1363 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001364 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 return p;
1366}
1367
1368stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001369Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370{
1371 stmt_ty p;
1372 if (!value) {
1373 PyErr_SetString(PyExc_ValueError,
1374 "field value is required for Expr");
1375 return NULL;
1376 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001377 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001378 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 p->kind = Expr_kind;
1381 p->v.Expr.value = value;
1382 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001383 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 return p;
1385}
1386
1387stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001388Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389{
1390 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001391 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001392 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 p->kind = Pass_kind;
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 +00001401Break(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 = Break_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 +00001414Continue(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 = Continue_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
1426expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001427BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1428 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429{
1430 expr_ty p;
1431 if (!op) {
1432 PyErr_SetString(PyExc_ValueError,
1433 "field op is required for BoolOp");
1434 return NULL;
1435 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001436 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001437 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 p->kind = BoolOp_kind;
1440 p->v.BoolOp.op = op;
1441 p->v.BoolOp.values = values;
1442 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001443 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 return p;
1445}
1446
1447expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001448BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1449 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450{
1451 expr_ty p;
1452 if (!left) {
1453 PyErr_SetString(PyExc_ValueError,
1454 "field left is required for BinOp");
1455 return NULL;
1456 }
1457 if (!op) {
1458 PyErr_SetString(PyExc_ValueError,
1459 "field op is required for BinOp");
1460 return NULL;
1461 }
1462 if (!right) {
1463 PyErr_SetString(PyExc_ValueError,
1464 "field right is required for BinOp");
1465 return NULL;
1466 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001467 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001468 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 p->kind = BinOp_kind;
1471 p->v.BinOp.left = left;
1472 p->v.BinOp.op = op;
1473 p->v.BinOp.right = right;
1474 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001475 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 return p;
1477}
1478
1479expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001480UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1481 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482{
1483 expr_ty p;
1484 if (!op) {
1485 PyErr_SetString(PyExc_ValueError,
1486 "field op is required for UnaryOp");
1487 return NULL;
1488 }
1489 if (!operand) {
1490 PyErr_SetString(PyExc_ValueError,
1491 "field operand is required for UnaryOp");
1492 return NULL;
1493 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001494 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001495 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 p->kind = UnaryOp_kind;
1498 p->v.UnaryOp.op = op;
1499 p->v.UnaryOp.operand = operand;
1500 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001501 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 return p;
1503}
1504
1505expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001506Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1507 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508{
1509 expr_ty p;
1510 if (!args) {
1511 PyErr_SetString(PyExc_ValueError,
1512 "field args is required for Lambda");
1513 return NULL;
1514 }
1515 if (!body) {
1516 PyErr_SetString(PyExc_ValueError,
1517 "field body is required for Lambda");
1518 return NULL;
1519 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001520 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001521 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523 p->kind = Lambda_kind;
1524 p->v.Lambda.args = args;
1525 p->v.Lambda.body = body;
1526 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001527 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 return p;
1529}
1530
1531expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001532IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1533 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001534{
1535 expr_ty p;
1536 if (!test) {
1537 PyErr_SetString(PyExc_ValueError,
1538 "field test is required for IfExp");
1539 return NULL;
1540 }
1541 if (!body) {
1542 PyErr_SetString(PyExc_ValueError,
1543 "field body is required for IfExp");
1544 return NULL;
1545 }
1546 if (!orelse) {
1547 PyErr_SetString(PyExc_ValueError,
1548 "field orelse is required for IfExp");
1549 return NULL;
1550 }
1551 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001552 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001553 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001554 p->kind = IfExp_kind;
1555 p->v.IfExp.test = test;
1556 p->v.IfExp.body = body;
1557 p->v.IfExp.orelse = orelse;
1558 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001559 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001560 return p;
1561}
1562
1563expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001564Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1565 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566{
1567 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001568 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001569 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571 p->kind = Dict_kind;
1572 p->v.Dict.keys = keys;
1573 p->v.Dict.values = values;
1574 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001575 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576 return p;
1577}
1578
1579expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001580ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1581 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582{
1583 expr_ty p;
1584 if (!elt) {
1585 PyErr_SetString(PyExc_ValueError,
1586 "field elt is required for ListComp");
1587 return NULL;
1588 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001589 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001590 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 p->kind = ListComp_kind;
1593 p->v.ListComp.elt = elt;
1594 p->v.ListComp.generators = generators;
1595 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001596 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001597 return p;
1598}
1599
1600expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001601GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1602 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603{
1604 expr_ty p;
1605 if (!elt) {
1606 PyErr_SetString(PyExc_ValueError,
1607 "field elt is required for GeneratorExp");
1608 return NULL;
1609 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001610 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001611 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 p->kind = GeneratorExp_kind;
1614 p->v.GeneratorExp.elt = elt;
1615 p->v.GeneratorExp.generators = generators;
1616 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001617 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 return p;
1619}
1620
1621expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001622Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623{
1624 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001625 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001626 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001627 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 p->kind = Yield_kind;
1629 p->v.Yield.value = value;
1630 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001631 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001632 return p;
1633}
1634
1635expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001636Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1637 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638{
1639 expr_ty p;
1640 if (!left) {
1641 PyErr_SetString(PyExc_ValueError,
1642 "field left is required for Compare");
1643 return NULL;
1644 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001645 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001646 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 p->kind = Compare_kind;
1649 p->v.Compare.left = left;
1650 p->v.Compare.ops = ops;
1651 p->v.Compare.comparators = comparators;
1652 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001653 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 return p;
1655}
1656
1657expr_ty
1658Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001659 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660{
1661 expr_ty p;
1662 if (!func) {
1663 PyErr_SetString(PyExc_ValueError,
1664 "field func is required for Call");
1665 return NULL;
1666 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001667 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001668 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 p->kind = Call_kind;
1671 p->v.Call.func = func;
1672 p->v.Call.args = args;
1673 p->v.Call.keywords = keywords;
1674 p->v.Call.starargs = starargs;
1675 p->v.Call.kwargs = kwargs;
1676 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001677 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 return p;
1679}
1680
1681expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001682Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683{
1684 expr_ty p;
1685 if (!value) {
1686 PyErr_SetString(PyExc_ValueError,
1687 "field value is required for Repr");
1688 return NULL;
1689 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001690 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001691 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 p->kind = Repr_kind;
1694 p->v.Repr.value = value;
1695 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001696 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697 return p;
1698}
1699
1700expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001701Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702{
1703 expr_ty p;
1704 if (!n) {
1705 PyErr_SetString(PyExc_ValueError,
1706 "field n is required for Num");
1707 return NULL;
1708 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001709 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001710 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 p->kind = Num_kind;
1713 p->v.Num.n = n;
1714 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001715 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 return p;
1717}
1718
1719expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001720Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721{
1722 expr_ty p;
1723 if (!s) {
1724 PyErr_SetString(PyExc_ValueError,
1725 "field s is required for Str");
1726 return NULL;
1727 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001728 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001729 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 p->kind = Str_kind;
1732 p->v.Str.s = s;
1733 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001734 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 return p;
1736}
1737
1738expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001739Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1740 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741{
1742 expr_ty p;
1743 if (!value) {
1744 PyErr_SetString(PyExc_ValueError,
1745 "field value is required for Attribute");
1746 return NULL;
1747 }
1748 if (!attr) {
1749 PyErr_SetString(PyExc_ValueError,
1750 "field attr is required for Attribute");
1751 return NULL;
1752 }
1753 if (!ctx) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "field ctx is required for Attribute");
1756 return NULL;
1757 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001758 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001759 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001760 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761 p->kind = Attribute_kind;
1762 p->v.Attribute.value = value;
1763 p->v.Attribute.attr = attr;
1764 p->v.Attribute.ctx = ctx;
1765 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001766 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return p;
1768}
1769
1770expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001771Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1772 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773{
1774 expr_ty p;
1775 if (!value) {
1776 PyErr_SetString(PyExc_ValueError,
1777 "field value is required for Subscript");
1778 return NULL;
1779 }
1780 if (!slice) {
1781 PyErr_SetString(PyExc_ValueError,
1782 "field slice is required for Subscript");
1783 return NULL;
1784 }
1785 if (!ctx) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field ctx is required for Subscript");
1788 return NULL;
1789 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001790 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001791 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 p->kind = Subscript_kind;
1794 p->v.Subscript.value = value;
1795 p->v.Subscript.slice = slice;
1796 p->v.Subscript.ctx = ctx;
1797 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001798 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 return p;
1800}
1801
1802expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001803Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1804 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805{
1806 expr_ty p;
1807 if (!id) {
1808 PyErr_SetString(PyExc_ValueError,
1809 "field id is required for Name");
1810 return NULL;
1811 }
1812 if (!ctx) {
1813 PyErr_SetString(PyExc_ValueError,
1814 "field ctx is required for Name");
1815 return NULL;
1816 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001817 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001818 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 p->kind = Name_kind;
1821 p->v.Name.id = id;
1822 p->v.Name.ctx = ctx;
1823 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001824 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 return p;
1826}
1827
1828expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001829List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1830 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831{
1832 expr_ty p;
1833 if (!ctx) {
1834 PyErr_SetString(PyExc_ValueError,
1835 "field ctx is required for List");
1836 return NULL;
1837 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001838 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001839 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841 p->kind = List_kind;
1842 p->v.List.elts = elts;
1843 p->v.List.ctx = ctx;
1844 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001845 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 return p;
1847}
1848
1849expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001850Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1851 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852{
1853 expr_ty p;
1854 if (!ctx) {
1855 PyErr_SetString(PyExc_ValueError,
1856 "field ctx is required for Tuple");
1857 return NULL;
1858 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001860 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 p->kind = Tuple_kind;
1863 p->v.Tuple.elts = elts;
1864 p->v.Tuple.ctx = ctx;
1865 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001866 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 return p;
1868}
1869
1870slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872{
1873 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001875 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 p->kind = Ellipsis_kind;
1878 return p;
1879}
1880
1881slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001882Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883{
1884 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001885 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001886 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 p->kind = Slice_kind;
1889 p->v.Slice.lower = lower;
1890 p->v.Slice.upper = upper;
1891 p->v.Slice.step = step;
1892 return p;
1893}
1894
1895slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001896ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897{
1898 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001899 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001900 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 p->kind = ExtSlice_kind;
1903 p->v.ExtSlice.dims = dims;
1904 return p;
1905}
1906
1907slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001908Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909{
1910 slice_ty p;
1911 if (!value) {
1912 PyErr_SetString(PyExc_ValueError,
1913 "field value is required for Index");
1914 return NULL;
1915 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001917 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 p->kind = Index_kind;
1920 p->v.Index.value = value;
1921 return p;
1922}
1923
1924comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001925comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926{
1927 comprehension_ty p;
1928 if (!target) {
1929 PyErr_SetString(PyExc_ValueError,
1930 "field target is required for comprehension");
1931 return NULL;
1932 }
1933 if (!iter) {
1934 PyErr_SetString(PyExc_ValueError,
1935 "field iter is required for comprehension");
1936 return NULL;
1937 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001938 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001939 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 p->target = target;
1942 p->iter = iter;
1943 p->ifs = ifs;
1944 return p;
1945}
1946
1947excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001948ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001949 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
1951 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001952 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001953 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001955 p->kind = ExceptHandler_kind;
1956 p->v.ExceptHandler.type = type;
1957 p->v.ExceptHandler.name = name;
1958 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001959 p->lineno = lineno;
1960 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961 return p;
1962}
1963
1964arguments_ty
1965arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001966 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967{
1968 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001969 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001970 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 p->args = args;
1973 p->vararg = vararg;
1974 p->kwarg = kwarg;
1975 p->defaults = defaults;
1976 return p;
1977}
1978
1979keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001980keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
1982 keyword_ty p;
1983 if (!arg) {
1984 PyErr_SetString(PyExc_ValueError,
1985 "field arg is required for keyword");
1986 return NULL;
1987 }
1988 if (!value) {
1989 PyErr_SetString(PyExc_ValueError,
1990 "field value is required for keyword");
1991 return NULL;
1992 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001993 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001994 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 p->arg = arg;
1997 p->value = value;
1998 return p;
1999}
2000
2001alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002002alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003{
2004 alias_ty p;
2005 if (!name) {
2006 PyErr_SetString(PyExc_ValueError,
2007 "field name is required for alias");
2008 return NULL;
2009 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00002010 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00002011 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 p->name = name;
2014 p->asname = asname;
2015 return p;
2016}
2017
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002018
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002019PyObject*
2020ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002021{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002022 mod_ty o = (mod_ty)_o;
2023 PyObject *result = NULL, *value = NULL;
2024 if (!o) {
2025 Py_INCREF(Py_None);
2026 return Py_None;
2027 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002028
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 switch (o->kind) {
2030 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002031 result = PyType_GenericNew(Module_type, NULL, NULL);
2032 if (!result) goto failed;
2033 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2034 if (!value) goto failed;
2035 if (PyObject_SetAttrString(result, "body", value) == -1)
2036 goto failed;
2037 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 break;
2039 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002040 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2041 if (!result) goto failed;
2042 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2043 if (!value) goto failed;
2044 if (PyObject_SetAttrString(result, "body", value) == -1)
2045 goto failed;
2046 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 break;
2048 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002049 result = PyType_GenericNew(Expression_type, NULL, NULL);
2050 if (!result) goto failed;
2051 value = ast2obj_expr(o->v.Expression.body);
2052 if (!value) goto failed;
2053 if (PyObject_SetAttrString(result, "body", value) == -1)
2054 goto failed;
2055 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 break;
2057 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002058 result = PyType_GenericNew(Suite_type, NULL, NULL);
2059 if (!result) goto failed;
2060 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2061 if (!value) goto failed;
2062 if (PyObject_SetAttrString(result, "body", value) == -1)
2063 goto failed;
2064 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 break;
2066 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002067 return result;
2068failed:
2069 Py_XDECREF(value);
2070 Py_XDECREF(result);
2071 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072}
2073
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002074PyObject*
2075ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002077 stmt_ty o = (stmt_ty)_o;
2078 PyObject *result = NULL, *value = NULL;
2079 if (!o) {
2080 Py_INCREF(Py_None);
2081 return Py_None;
2082 }
2083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 switch (o->kind) {
2085 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002086 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2087 if (!result) goto failed;
2088 value = ast2obj_identifier(o->v.FunctionDef.name);
2089 if (!value) goto failed;
2090 if (PyObject_SetAttrString(result, "name", value) == -1)
2091 goto failed;
2092 Py_DECREF(value);
2093 value = ast2obj_arguments(o->v.FunctionDef.args);
2094 if (!value) goto failed;
2095 if (PyObject_SetAttrString(result, "args", value) == -1)
2096 goto failed;
2097 Py_DECREF(value);
2098 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2099 if (!value) goto failed;
2100 if (PyObject_SetAttrString(result, "body", value) == -1)
2101 goto failed;
2102 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002103 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2104 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002105 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002106 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2107 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002108 goto failed;
2109 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 break;
2111 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002112 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2113 if (!result) goto failed;
2114 value = ast2obj_identifier(o->v.ClassDef.name);
2115 if (!value) goto failed;
2116 if (PyObject_SetAttrString(result, "name", value) == -1)
2117 goto failed;
2118 Py_DECREF(value);
2119 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2120 if (!value) goto failed;
2121 if (PyObject_SetAttrString(result, "bases", value) == -1)
2122 goto failed;
2123 Py_DECREF(value);
2124 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2125 if (!value) goto failed;
2126 if (PyObject_SetAttrString(result, "body", value) == -1)
2127 goto failed;
2128 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002129 value = ast2obj_list(o->v.ClassDef.decorator_list,
2130 ast2obj_expr);
2131 if (!value) goto failed;
2132 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2133 -1)
2134 goto failed;
2135 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136 break;
2137 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002138 result = PyType_GenericNew(Return_type, NULL, NULL);
2139 if (!result) goto failed;
2140 value = ast2obj_expr(o->v.Return.value);
2141 if (!value) goto failed;
2142 if (PyObject_SetAttrString(result, "value", value) == -1)
2143 goto failed;
2144 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 break;
2146 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002147 result = PyType_GenericNew(Delete_type, NULL, NULL);
2148 if (!result) goto failed;
2149 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2150 if (!value) goto failed;
2151 if (PyObject_SetAttrString(result, "targets", value) == -1)
2152 goto failed;
2153 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 break;
2155 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002156 result = PyType_GenericNew(Assign_type, NULL, NULL);
2157 if (!result) goto failed;
2158 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2159 if (!value) goto failed;
2160 if (PyObject_SetAttrString(result, "targets", value) == -1)
2161 goto failed;
2162 Py_DECREF(value);
2163 value = ast2obj_expr(o->v.Assign.value);
2164 if (!value) goto failed;
2165 if (PyObject_SetAttrString(result, "value", value) == -1)
2166 goto failed;
2167 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 break;
2169 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002170 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2171 if (!result) goto failed;
2172 value = ast2obj_expr(o->v.AugAssign.target);
2173 if (!value) goto failed;
2174 if (PyObject_SetAttrString(result, "target", value) == -1)
2175 goto failed;
2176 Py_DECREF(value);
2177 value = ast2obj_operator(o->v.AugAssign.op);
2178 if (!value) goto failed;
2179 if (PyObject_SetAttrString(result, "op", value) == -1)
2180 goto failed;
2181 Py_DECREF(value);
2182 value = ast2obj_expr(o->v.AugAssign.value);
2183 if (!value) goto failed;
2184 if (PyObject_SetAttrString(result, "value", value) == -1)
2185 goto failed;
2186 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 break;
2188 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002189 result = PyType_GenericNew(Print_type, NULL, NULL);
2190 if (!result) goto failed;
2191 value = ast2obj_expr(o->v.Print.dest);
2192 if (!value) goto failed;
2193 if (PyObject_SetAttrString(result, "dest", value) == -1)
2194 goto failed;
2195 Py_DECREF(value);
2196 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2197 if (!value) goto failed;
2198 if (PyObject_SetAttrString(result, "values", value) == -1)
2199 goto failed;
2200 Py_DECREF(value);
2201 value = ast2obj_bool(o->v.Print.nl);
2202 if (!value) goto failed;
2203 if (PyObject_SetAttrString(result, "nl", value) == -1)
2204 goto failed;
2205 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 break;
2207 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002208 result = PyType_GenericNew(For_type, NULL, NULL);
2209 if (!result) goto failed;
2210 value = ast2obj_expr(o->v.For.target);
2211 if (!value) goto failed;
2212 if (PyObject_SetAttrString(result, "target", value) == -1)
2213 goto failed;
2214 Py_DECREF(value);
2215 value = ast2obj_expr(o->v.For.iter);
2216 if (!value) goto failed;
2217 if (PyObject_SetAttrString(result, "iter", value) == -1)
2218 goto failed;
2219 Py_DECREF(value);
2220 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2221 if (!value) goto failed;
2222 if (PyObject_SetAttrString(result, "body", value) == -1)
2223 goto failed;
2224 Py_DECREF(value);
2225 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2226 if (!value) goto failed;
2227 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2228 goto failed;
2229 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 break;
2231 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002232 result = PyType_GenericNew(While_type, NULL, NULL);
2233 if (!result) goto failed;
2234 value = ast2obj_expr(o->v.While.test);
2235 if (!value) goto failed;
2236 if (PyObject_SetAttrString(result, "test", value) == -1)
2237 goto failed;
2238 Py_DECREF(value);
2239 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2240 if (!value) goto failed;
2241 if (PyObject_SetAttrString(result, "body", value) == -1)
2242 goto failed;
2243 Py_DECREF(value);
2244 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2245 if (!value) goto failed;
2246 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2247 goto failed;
2248 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 break;
2250 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002251 result = PyType_GenericNew(If_type, NULL, NULL);
2252 if (!result) goto failed;
2253 value = ast2obj_expr(o->v.If.test);
2254 if (!value) goto failed;
2255 if (PyObject_SetAttrString(result, "test", value) == -1)
2256 goto failed;
2257 Py_DECREF(value);
2258 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "body", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
2263 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2264 if (!value) goto failed;
2265 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2266 goto failed;
2267 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002269 case With_kind:
2270 result = PyType_GenericNew(With_type, NULL, NULL);
2271 if (!result) goto failed;
2272 value = ast2obj_expr(o->v.With.context_expr);
2273 if (!value) goto failed;
2274 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2275 goto failed;
2276 Py_DECREF(value);
2277 value = ast2obj_expr(o->v.With.optional_vars);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2280 -1)
2281 goto failed;
2282 Py_DECREF(value);
2283 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2284 if (!value) goto failed;
2285 if (PyObject_SetAttrString(result, "body", value) == -1)
2286 goto failed;
2287 Py_DECREF(value);
2288 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002290 result = PyType_GenericNew(Raise_type, NULL, NULL);
2291 if (!result) goto failed;
2292 value = ast2obj_expr(o->v.Raise.type);
2293 if (!value) goto failed;
2294 if (PyObject_SetAttrString(result, "type", value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
2297 value = ast2obj_expr(o->v.Raise.inst);
2298 if (!value) goto failed;
2299 if (PyObject_SetAttrString(result, "inst", value) == -1)
2300 goto failed;
2301 Py_DECREF(value);
2302 value = ast2obj_expr(o->v.Raise.tback);
2303 if (!value) goto failed;
2304 if (PyObject_SetAttrString(result, "tback", value) == -1)
2305 goto failed;
2306 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 break;
2308 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002309 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2310 if (!result) goto failed;
2311 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2312 if (!value) goto failed;
2313 if (PyObject_SetAttrString(result, "body", value) == -1)
2314 goto failed;
2315 Py_DECREF(value);
2316 value = ast2obj_list(o->v.TryExcept.handlers,
2317 ast2obj_excepthandler);
2318 if (!value) goto failed;
2319 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2320 goto failed;
2321 Py_DECREF(value);
2322 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2323 if (!value) goto failed;
2324 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2325 goto failed;
2326 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 break;
2328 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002329 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2330 if (!result) goto failed;
2331 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2332 if (!value) goto failed;
2333 if (PyObject_SetAttrString(result, "body", value) == -1)
2334 goto failed;
2335 Py_DECREF(value);
2336 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2337 if (!value) goto failed;
2338 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2339 goto failed;
2340 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 break;
2342 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002343 result = PyType_GenericNew(Assert_type, NULL, NULL);
2344 if (!result) goto failed;
2345 value = ast2obj_expr(o->v.Assert.test);
2346 if (!value) goto failed;
2347 if (PyObject_SetAttrString(result, "test", value) == -1)
2348 goto failed;
2349 Py_DECREF(value);
2350 value = ast2obj_expr(o->v.Assert.msg);
2351 if (!value) goto failed;
2352 if (PyObject_SetAttrString(result, "msg", value) == -1)
2353 goto failed;
2354 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 break;
2356 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002357 result = PyType_GenericNew(Import_type, NULL, NULL);
2358 if (!result) goto failed;
2359 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2360 if (!value) goto failed;
2361 if (PyObject_SetAttrString(result, "names", value) == -1)
2362 goto failed;
2363 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 break;
2365 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002366 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2367 if (!result) goto failed;
2368 value = ast2obj_identifier(o->v.ImportFrom.module);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "module", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
2373 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2374 if (!value) goto failed;
2375 if (PyObject_SetAttrString(result, "names", value) == -1)
2376 goto failed;
2377 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002378 value = ast2obj_int(o->v.ImportFrom.level);
2379 if (!value) goto failed;
2380 if (PyObject_SetAttrString(result, "level", value) == -1)
2381 goto failed;
2382 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 break;
2384 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002385 result = PyType_GenericNew(Exec_type, NULL, NULL);
2386 if (!result) goto failed;
2387 value = ast2obj_expr(o->v.Exec.body);
2388 if (!value) goto failed;
2389 if (PyObject_SetAttrString(result, "body", value) == -1)
2390 goto failed;
2391 Py_DECREF(value);
2392 value = ast2obj_expr(o->v.Exec.globals);
2393 if (!value) goto failed;
2394 if (PyObject_SetAttrString(result, "globals", value) == -1)
2395 goto failed;
2396 Py_DECREF(value);
2397 value = ast2obj_expr(o->v.Exec.locals);
2398 if (!value) goto failed;
2399 if (PyObject_SetAttrString(result, "locals", value) == -1)
2400 goto failed;
2401 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 break;
2403 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002404 result = PyType_GenericNew(Global_type, NULL, NULL);
2405 if (!result) goto failed;
2406 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2407 if (!value) goto failed;
2408 if (PyObject_SetAttrString(result, "names", value) == -1)
2409 goto failed;
2410 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 break;
2412 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002413 result = PyType_GenericNew(Expr_type, NULL, NULL);
2414 if (!result) goto failed;
2415 value = ast2obj_expr(o->v.Expr.value);
2416 if (!value) goto failed;
2417 if (PyObject_SetAttrString(result, "value", value) == -1)
2418 goto failed;
2419 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 break;
2421 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002422 result = PyType_GenericNew(Pass_type, NULL, NULL);
2423 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 break;
2425 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426 result = PyType_GenericNew(Break_type, NULL, NULL);
2427 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 break;
2429 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002430 result = PyType_GenericNew(Continue_type, NULL, NULL);
2431 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 break;
2433 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002434 value = ast2obj_int(o->lineno);
2435 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002436 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2437 goto failed;
2438 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002439 value = ast2obj_int(o->col_offset);
2440 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002441 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2442 goto failed;
2443 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002444 return result;
2445failed:
2446 Py_XDECREF(value);
2447 Py_XDECREF(result);
2448 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449}
2450
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002451PyObject*
2452ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002454 expr_ty o = (expr_ty)_o;
2455 PyObject *result = NULL, *value = NULL;
2456 if (!o) {
2457 Py_INCREF(Py_None);
2458 return Py_None;
2459 }
2460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 switch (o->kind) {
2462 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002463 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2464 if (!result) goto failed;
2465 value = ast2obj_boolop(o->v.BoolOp.op);
2466 if (!value) goto failed;
2467 if (PyObject_SetAttrString(result, "op", value) == -1)
2468 goto failed;
2469 Py_DECREF(value);
2470 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2471 if (!value) goto failed;
2472 if (PyObject_SetAttrString(result, "values", value) == -1)
2473 goto failed;
2474 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 break;
2476 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002477 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2478 if (!result) goto failed;
2479 value = ast2obj_expr(o->v.BinOp.left);
2480 if (!value) goto failed;
2481 if (PyObject_SetAttrString(result, "left", value) == -1)
2482 goto failed;
2483 Py_DECREF(value);
2484 value = ast2obj_operator(o->v.BinOp.op);
2485 if (!value) goto failed;
2486 if (PyObject_SetAttrString(result, "op", value) == -1)
2487 goto failed;
2488 Py_DECREF(value);
2489 value = ast2obj_expr(o->v.BinOp.right);
2490 if (!value) goto failed;
2491 if (PyObject_SetAttrString(result, "right", value) == -1)
2492 goto failed;
2493 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 break;
2495 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002496 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2497 if (!result) goto failed;
2498 value = ast2obj_unaryop(o->v.UnaryOp.op);
2499 if (!value) goto failed;
2500 if (PyObject_SetAttrString(result, "op", value) == -1)
2501 goto failed;
2502 Py_DECREF(value);
2503 value = ast2obj_expr(o->v.UnaryOp.operand);
2504 if (!value) goto failed;
2505 if (PyObject_SetAttrString(result, "operand", value) == -1)
2506 goto failed;
2507 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 break;
2509 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002510 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2511 if (!result) goto failed;
2512 value = ast2obj_arguments(o->v.Lambda.args);
2513 if (!value) goto failed;
2514 if (PyObject_SetAttrString(result, "args", value) == -1)
2515 goto failed;
2516 Py_DECREF(value);
2517 value = ast2obj_expr(o->v.Lambda.body);
2518 if (!value) goto failed;
2519 if (PyObject_SetAttrString(result, "body", value) == -1)
2520 goto failed;
2521 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002523 case IfExp_kind:
2524 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2525 if (!result) goto failed;
2526 value = ast2obj_expr(o->v.IfExp.test);
2527 if (!value) goto failed;
2528 if (PyObject_SetAttrString(result, "test", value) == -1)
2529 goto failed;
2530 Py_DECREF(value);
2531 value = ast2obj_expr(o->v.IfExp.body);
2532 if (!value) goto failed;
2533 if (PyObject_SetAttrString(result, "body", value) == -1)
2534 goto failed;
2535 Py_DECREF(value);
2536 value = ast2obj_expr(o->v.IfExp.orelse);
2537 if (!value) goto failed;
2538 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2539 goto failed;
2540 Py_DECREF(value);
2541 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002543 result = PyType_GenericNew(Dict_type, NULL, NULL);
2544 if (!result) goto failed;
2545 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2546 if (!value) goto failed;
2547 if (PyObject_SetAttrString(result, "keys", value) == -1)
2548 goto failed;
2549 Py_DECREF(value);
2550 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2551 if (!value) goto failed;
2552 if (PyObject_SetAttrString(result, "values", value) == -1)
2553 goto failed;
2554 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 break;
2556 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002557 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2558 if (!result) goto failed;
2559 value = ast2obj_expr(o->v.ListComp.elt);
2560 if (!value) goto failed;
2561 if (PyObject_SetAttrString(result, "elt", value) == -1)
2562 goto failed;
2563 Py_DECREF(value);
2564 value = ast2obj_list(o->v.ListComp.generators,
2565 ast2obj_comprehension);
2566 if (!value) goto failed;
2567 if (PyObject_SetAttrString(result, "generators", value) == -1)
2568 goto failed;
2569 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 break;
2571 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002572 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2573 if (!result) goto failed;
2574 value = ast2obj_expr(o->v.GeneratorExp.elt);
2575 if (!value) goto failed;
2576 if (PyObject_SetAttrString(result, "elt", value) == -1)
2577 goto failed;
2578 Py_DECREF(value);
2579 value = ast2obj_list(o->v.GeneratorExp.generators,
2580 ast2obj_comprehension);
2581 if (!value) goto failed;
2582 if (PyObject_SetAttrString(result, "generators", value) == -1)
2583 goto failed;
2584 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 break;
2586 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002587 result = PyType_GenericNew(Yield_type, NULL, NULL);
2588 if (!result) goto failed;
2589 value = ast2obj_expr(o->v.Yield.value);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "value", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 break;
2595 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002596 result = PyType_GenericNew(Compare_type, NULL, NULL);
2597 if (!result) goto failed;
2598 value = ast2obj_expr(o->v.Compare.left);
2599 if (!value) goto failed;
2600 if (PyObject_SetAttrString(result, "left", value) == -1)
2601 goto failed;
2602 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002603 {
2604 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2605 value = PyList_New(n);
2606 if (!value) goto failed;
2607 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002608 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 +00002609 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002610 if (!value) goto failed;
2611 if (PyObject_SetAttrString(result, "ops", value) == -1)
2612 goto failed;
2613 Py_DECREF(value);
2614 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2615 if (!value) goto failed;
2616 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2617 goto failed;
2618 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 break;
2620 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002621 result = PyType_GenericNew(Call_type, NULL, NULL);
2622 if (!result) goto failed;
2623 value = ast2obj_expr(o->v.Call.func);
2624 if (!value) goto failed;
2625 if (PyObject_SetAttrString(result, "func", value) == -1)
2626 goto failed;
2627 Py_DECREF(value);
2628 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2629 if (!value) goto failed;
2630 if (PyObject_SetAttrString(result, "args", value) == -1)
2631 goto failed;
2632 Py_DECREF(value);
2633 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2634 if (!value) goto failed;
2635 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2636 goto failed;
2637 Py_DECREF(value);
2638 value = ast2obj_expr(o->v.Call.starargs);
2639 if (!value) goto failed;
2640 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2641 goto failed;
2642 Py_DECREF(value);
2643 value = ast2obj_expr(o->v.Call.kwargs);
2644 if (!value) goto failed;
2645 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2646 goto failed;
2647 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 break;
2649 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002650 result = PyType_GenericNew(Repr_type, NULL, NULL);
2651 if (!result) goto failed;
2652 value = ast2obj_expr(o->v.Repr.value);
2653 if (!value) goto failed;
2654 if (PyObject_SetAttrString(result, "value", value) == -1)
2655 goto failed;
2656 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 break;
2658 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002659 result = PyType_GenericNew(Num_type, NULL, NULL);
2660 if (!result) goto failed;
2661 value = ast2obj_object(o->v.Num.n);
2662 if (!value) goto failed;
2663 if (PyObject_SetAttrString(result, "n", value) == -1)
2664 goto failed;
2665 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 break;
2667 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002668 result = PyType_GenericNew(Str_type, NULL, NULL);
2669 if (!result) goto failed;
2670 value = ast2obj_string(o->v.Str.s);
2671 if (!value) goto failed;
2672 if (PyObject_SetAttrString(result, "s", value) == -1)
2673 goto failed;
2674 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 break;
2676 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002677 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2678 if (!result) goto failed;
2679 value = ast2obj_expr(o->v.Attribute.value);
2680 if (!value) goto failed;
2681 if (PyObject_SetAttrString(result, "value", value) == -1)
2682 goto failed;
2683 Py_DECREF(value);
2684 value = ast2obj_identifier(o->v.Attribute.attr);
2685 if (!value) goto failed;
2686 if (PyObject_SetAttrString(result, "attr", value) == -1)
2687 goto failed;
2688 Py_DECREF(value);
2689 value = ast2obj_expr_context(o->v.Attribute.ctx);
2690 if (!value) goto failed;
2691 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2692 goto failed;
2693 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 break;
2695 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002696 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2697 if (!result) goto failed;
2698 value = ast2obj_expr(o->v.Subscript.value);
2699 if (!value) goto failed;
2700 if (PyObject_SetAttrString(result, "value", value) == -1)
2701 goto failed;
2702 Py_DECREF(value);
2703 value = ast2obj_slice(o->v.Subscript.slice);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "slice", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 value = ast2obj_expr_context(o->v.Subscript.ctx);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 break;
2714 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002715 result = PyType_GenericNew(Name_type, NULL, NULL);
2716 if (!result) goto failed;
2717 value = ast2obj_identifier(o->v.Name.id);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "id", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
2722 value = ast2obj_expr_context(o->v.Name.ctx);
2723 if (!value) goto failed;
2724 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2725 goto failed;
2726 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002727 break;
2728 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002729 result = PyType_GenericNew(List_type, NULL, NULL);
2730 if (!result) goto failed;
2731 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2732 if (!value) goto failed;
2733 if (PyObject_SetAttrString(result, "elts", value) == -1)
2734 goto failed;
2735 Py_DECREF(value);
2736 value = ast2obj_expr_context(o->v.List.ctx);
2737 if (!value) goto failed;
2738 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2739 goto failed;
2740 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 break;
2742 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002743 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2744 if (!result) goto failed;
2745 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2746 if (!value) goto failed;
2747 if (PyObject_SetAttrString(result, "elts", value) == -1)
2748 goto failed;
2749 Py_DECREF(value);
2750 value = ast2obj_expr_context(o->v.Tuple.ctx);
2751 if (!value) goto failed;
2752 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2753 goto failed;
2754 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 break;
2756 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002757 value = ast2obj_int(o->lineno);
2758 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002759 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2760 goto failed;
2761 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002762 value = ast2obj_int(o->col_offset);
2763 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002764 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2765 goto failed;
2766 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002767 return result;
2768failed:
2769 Py_XDECREF(value);
2770 Py_XDECREF(result);
2771 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772}
2773
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002774PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002776 switch(o) {
2777 case Load:
2778 Py_INCREF(Load_singleton);
2779 return Load_singleton;
2780 case Store:
2781 Py_INCREF(Store_singleton);
2782 return Store_singleton;
2783 case Del:
2784 Py_INCREF(Del_singleton);
2785 return Del_singleton;
2786 case AugLoad:
2787 Py_INCREF(AugLoad_singleton);
2788 return AugLoad_singleton;
2789 case AugStore:
2790 Py_INCREF(AugStore_singleton);
2791 return AugStore_singleton;
2792 case Param:
2793 Py_INCREF(Param_singleton);
2794 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002795 default:
2796 /* should never happen, but just in case ... */
2797 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002801PyObject*
2802ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002804 slice_ty o = (slice_ty)_o;
2805 PyObject *result = NULL, *value = NULL;
2806 if (!o) {
2807 Py_INCREF(Py_None);
2808 return Py_None;
2809 }
2810
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 switch (o->kind) {
2812 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002813 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2814 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815 break;
2816 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002817 result = PyType_GenericNew(Slice_type, NULL, NULL);
2818 if (!result) goto failed;
2819 value = ast2obj_expr(o->v.Slice.lower);
2820 if (!value) goto failed;
2821 if (PyObject_SetAttrString(result, "lower", value) == -1)
2822 goto failed;
2823 Py_DECREF(value);
2824 value = ast2obj_expr(o->v.Slice.upper);
2825 if (!value) goto failed;
2826 if (PyObject_SetAttrString(result, "upper", value) == -1)
2827 goto failed;
2828 Py_DECREF(value);
2829 value = ast2obj_expr(o->v.Slice.step);
2830 if (!value) goto failed;
2831 if (PyObject_SetAttrString(result, "step", value) == -1)
2832 goto failed;
2833 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 break;
2835 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002836 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2837 if (!result) goto failed;
2838 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2839 if (!value) goto failed;
2840 if (PyObject_SetAttrString(result, "dims", value) == -1)
2841 goto failed;
2842 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843 break;
2844 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002845 result = PyType_GenericNew(Index_type, NULL, NULL);
2846 if (!result) goto failed;
2847 value = ast2obj_expr(o->v.Index.value);
2848 if (!value) goto failed;
2849 if (PyObject_SetAttrString(result, "value", value) == -1)
2850 goto failed;
2851 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 break;
2853 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002854 return result;
2855failed:
2856 Py_XDECREF(value);
2857 Py_XDECREF(result);
2858 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859}
2860
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002861PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002863 switch(o) {
2864 case And:
2865 Py_INCREF(And_singleton);
2866 return And_singleton;
2867 case Or:
2868 Py_INCREF(Or_singleton);
2869 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002870 default:
2871 /* should never happen, but just in case ... */
2872 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2873 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002875}
2876PyObject* ast2obj_operator(operator_ty o)
2877{
2878 switch(o) {
2879 case Add:
2880 Py_INCREF(Add_singleton);
2881 return Add_singleton;
2882 case Sub:
2883 Py_INCREF(Sub_singleton);
2884 return Sub_singleton;
2885 case Mult:
2886 Py_INCREF(Mult_singleton);
2887 return Mult_singleton;
2888 case Div:
2889 Py_INCREF(Div_singleton);
2890 return Div_singleton;
2891 case Mod:
2892 Py_INCREF(Mod_singleton);
2893 return Mod_singleton;
2894 case Pow:
2895 Py_INCREF(Pow_singleton);
2896 return Pow_singleton;
2897 case LShift:
2898 Py_INCREF(LShift_singleton);
2899 return LShift_singleton;
2900 case RShift:
2901 Py_INCREF(RShift_singleton);
2902 return RShift_singleton;
2903 case BitOr:
2904 Py_INCREF(BitOr_singleton);
2905 return BitOr_singleton;
2906 case BitXor:
2907 Py_INCREF(BitXor_singleton);
2908 return BitXor_singleton;
2909 case BitAnd:
2910 Py_INCREF(BitAnd_singleton);
2911 return BitAnd_singleton;
2912 case FloorDiv:
2913 Py_INCREF(FloorDiv_singleton);
2914 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002915 default:
2916 /* should never happen, but just in case ... */
2917 PyErr_Format(PyExc_SystemError, "unknown operator found");
2918 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002919 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002920}
2921PyObject* ast2obj_unaryop(unaryop_ty o)
2922{
2923 switch(o) {
2924 case Invert:
2925 Py_INCREF(Invert_singleton);
2926 return Invert_singleton;
2927 case Not:
2928 Py_INCREF(Not_singleton);
2929 return Not_singleton;
2930 case UAdd:
2931 Py_INCREF(UAdd_singleton);
2932 return UAdd_singleton;
2933 case USub:
2934 Py_INCREF(USub_singleton);
2935 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002936 default:
2937 /* should never happen, but just in case ... */
2938 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2939 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002940 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941}
2942PyObject* ast2obj_cmpop(cmpop_ty o)
2943{
2944 switch(o) {
2945 case Eq:
2946 Py_INCREF(Eq_singleton);
2947 return Eq_singleton;
2948 case NotEq:
2949 Py_INCREF(NotEq_singleton);
2950 return NotEq_singleton;
2951 case Lt:
2952 Py_INCREF(Lt_singleton);
2953 return Lt_singleton;
2954 case LtE:
2955 Py_INCREF(LtE_singleton);
2956 return LtE_singleton;
2957 case Gt:
2958 Py_INCREF(Gt_singleton);
2959 return Gt_singleton;
2960 case GtE:
2961 Py_INCREF(GtE_singleton);
2962 return GtE_singleton;
2963 case Is:
2964 Py_INCREF(Is_singleton);
2965 return Is_singleton;
2966 case IsNot:
2967 Py_INCREF(IsNot_singleton);
2968 return IsNot_singleton;
2969 case In:
2970 Py_INCREF(In_singleton);
2971 return In_singleton;
2972 case NotIn:
2973 Py_INCREF(NotIn_singleton);
2974 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002975 default:
2976 /* should never happen, but just in case ... */
2977 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2978 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002979 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002980}
2981PyObject*
2982ast2obj_comprehension(void* _o)
2983{
2984 comprehension_ty o = (comprehension_ty)_o;
2985 PyObject *result = NULL, *value = NULL;
2986 if (!o) {
2987 Py_INCREF(Py_None);
2988 return Py_None;
2989 }
2990
2991 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2992 if (!result) return NULL;
2993 value = ast2obj_expr(o->target);
2994 if (!value) goto failed;
2995 if (PyObject_SetAttrString(result, "target", value) == -1)
2996 goto failed;
2997 Py_DECREF(value);
2998 value = ast2obj_expr(o->iter);
2999 if (!value) goto failed;
3000 if (PyObject_SetAttrString(result, "iter", value) == -1)
3001 goto failed;
3002 Py_DECREF(value);
3003 value = ast2obj_list(o->ifs, ast2obj_expr);
3004 if (!value) goto failed;
3005 if (PyObject_SetAttrString(result, "ifs", value) == -1)
3006 goto failed;
3007 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003008 return result;
3009failed:
3010 Py_XDECREF(value);
3011 Py_XDECREF(result);
3012 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013}
3014
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003015PyObject*
3016ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003018 excepthandler_ty o = (excepthandler_ty)_o;
3019 PyObject *result = NULL, *value = NULL;
3020 if (!o) {
3021 Py_INCREF(Py_None);
3022 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003024
Georg Brandla48f3ab2008-03-30 06:40:17 +00003025 switch (o->kind) {
3026 case ExceptHandler_kind:
3027 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3028 if (!result) goto failed;
3029 value = ast2obj_expr(o->v.ExceptHandler.type);
3030 if (!value) goto failed;
3031 if (PyObject_SetAttrString(result, "type", value) == -1)
3032 goto failed;
3033 Py_DECREF(value);
3034 value = ast2obj_expr(o->v.ExceptHandler.name);
3035 if (!value) goto failed;
3036 if (PyObject_SetAttrString(result, "name", value) == -1)
3037 goto failed;
3038 Py_DECREF(value);
3039 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3040 if (!value) goto failed;
3041 if (PyObject_SetAttrString(result, "body", value) == -1)
3042 goto failed;
3043 Py_DECREF(value);
3044 break;
3045 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003046 value = ast2obj_int(o->lineno);
3047 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003048 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003049 goto failed;
3050 Py_DECREF(value);
3051 value = ast2obj_int(o->col_offset);
3052 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003053 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003054 goto failed;
3055 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003056 return result;
3057failed:
3058 Py_XDECREF(value);
3059 Py_XDECREF(result);
3060 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061}
3062
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003063PyObject*
3064ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003066 arguments_ty o = (arguments_ty)_o;
3067 PyObject *result = NULL, *value = NULL;
3068 if (!o) {
3069 Py_INCREF(Py_None);
3070 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003071 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003072
3073 result = PyType_GenericNew(arguments_type, NULL, NULL);
3074 if (!result) return NULL;
3075 value = ast2obj_list(o->args, ast2obj_expr);
3076 if (!value) goto failed;
3077 if (PyObject_SetAttrString(result, "args", value) == -1)
3078 goto failed;
3079 Py_DECREF(value);
3080 value = ast2obj_identifier(o->vararg);
3081 if (!value) goto failed;
3082 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3083 goto failed;
3084 Py_DECREF(value);
3085 value = ast2obj_identifier(o->kwarg);
3086 if (!value) goto failed;
3087 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3088 goto failed;
3089 Py_DECREF(value);
3090 value = ast2obj_list(o->defaults, ast2obj_expr);
3091 if (!value) goto failed;
3092 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3093 goto failed;
3094 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003095 return result;
3096failed:
3097 Py_XDECREF(value);
3098 Py_XDECREF(result);
3099 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100}
3101
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003102PyObject*
3103ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003105 keyword_ty o = (keyword_ty)_o;
3106 PyObject *result = NULL, *value = NULL;
3107 if (!o) {
3108 Py_INCREF(Py_None);
3109 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003111
3112 result = PyType_GenericNew(keyword_type, NULL, NULL);
3113 if (!result) return NULL;
3114 value = ast2obj_identifier(o->arg);
3115 if (!value) goto failed;
3116 if (PyObject_SetAttrString(result, "arg", value) == -1)
3117 goto failed;
3118 Py_DECREF(value);
3119 value = ast2obj_expr(o->value);
3120 if (!value) goto failed;
3121 if (PyObject_SetAttrString(result, "value", value) == -1)
3122 goto failed;
3123 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003124 return result;
3125failed:
3126 Py_XDECREF(value);
3127 Py_XDECREF(result);
3128 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129}
3130
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003131PyObject*
3132ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003134 alias_ty o = (alias_ty)_o;
3135 PyObject *result = NULL, *value = NULL;
3136 if (!o) {
3137 Py_INCREF(Py_None);
3138 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003140
3141 result = PyType_GenericNew(alias_type, NULL, NULL);
3142 if (!result) return NULL;
3143 value = ast2obj_identifier(o->name);
3144 if (!value) goto failed;
3145 if (PyObject_SetAttrString(result, "name", value) == -1)
3146 goto failed;
3147 Py_DECREF(value);
3148 value = ast2obj_identifier(o->asname);
3149 if (!value) goto failed;
3150 if (PyObject_SetAttrString(result, "asname", value) == -1)
3151 goto failed;
3152 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003153 return result;
3154failed:
3155 Py_XDECREF(value);
3156 Py_XDECREF(result);
3157 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158}
3159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160
Georg Brandlfc8eef32008-03-28 12:11:56 +00003161int
3162obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3163{
3164 PyObject* tmp = NULL;
3165
3166
3167 if (obj == Py_None) {
3168 *out = NULL;
3169 return 0;
3170 }
3171 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3172 asdl_seq* body;
3173
3174 if (PyObject_HasAttrString(obj, "body")) {
3175 int res;
3176 Py_ssize_t len;
3177 Py_ssize_t i;
3178 tmp = PyObject_GetAttrString(obj, "body");
3179 if (tmp == NULL) goto failed;
3180 if (!PyList_Check(tmp)) {
3181 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3182 goto failed;
3183 }
3184 len = PyList_GET_SIZE(tmp);
3185 body = asdl_seq_new(len, arena);
3186 if (body == NULL) goto failed;
3187 for (i = 0; i < len; i++) {
3188 stmt_ty value;
3189 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3190 if (res != 0) goto failed;
3191 asdl_seq_SET(body, i, value);
3192 }
3193 Py_XDECREF(tmp);
3194 tmp = NULL;
3195 } else {
3196 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3197 return 1;
3198 }
3199 *out = Module(body, arena);
3200 if (*out == NULL) goto failed;
3201 return 0;
3202 }
3203 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3204 asdl_seq* body;
3205
3206 if (PyObject_HasAttrString(obj, "body")) {
3207 int res;
3208 Py_ssize_t len;
3209 Py_ssize_t i;
3210 tmp = PyObject_GetAttrString(obj, "body");
3211 if (tmp == NULL) goto failed;
3212 if (!PyList_Check(tmp)) {
3213 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3214 goto failed;
3215 }
3216 len = PyList_GET_SIZE(tmp);
3217 body = asdl_seq_new(len, arena);
3218 if (body == NULL) goto failed;
3219 for (i = 0; i < len; i++) {
3220 stmt_ty value;
3221 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3222 if (res != 0) goto failed;
3223 asdl_seq_SET(body, i, value);
3224 }
3225 Py_XDECREF(tmp);
3226 tmp = NULL;
3227 } else {
3228 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3229 return 1;
3230 }
3231 *out = Interactive(body, arena);
3232 if (*out == NULL) goto failed;
3233 return 0;
3234 }
3235 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3236 expr_ty body;
3237
3238 if (PyObject_HasAttrString(obj, "body")) {
3239 int res;
3240 tmp = PyObject_GetAttrString(obj, "body");
3241 if (tmp == NULL) goto failed;
3242 res = obj2ast_expr(tmp, &body, arena);
3243 if (res != 0) goto failed;
3244 Py_XDECREF(tmp);
3245 tmp = NULL;
3246 } else {
3247 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3248 return 1;
3249 }
3250 *out = Expression(body, arena);
3251 if (*out == NULL) goto failed;
3252 return 0;
3253 }
3254 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3255 asdl_seq* body;
3256
3257 if (PyObject_HasAttrString(obj, "body")) {
3258 int res;
3259 Py_ssize_t len;
3260 Py_ssize_t i;
3261 tmp = PyObject_GetAttrString(obj, "body");
3262 if (tmp == NULL) goto failed;
3263 if (!PyList_Check(tmp)) {
3264 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3265 goto failed;
3266 }
3267 len = PyList_GET_SIZE(tmp);
3268 body = asdl_seq_new(len, arena);
3269 if (body == NULL) goto failed;
3270 for (i = 0; i < len; i++) {
3271 stmt_ty value;
3272 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3273 if (res != 0) goto failed;
3274 asdl_seq_SET(body, i, value);
3275 }
3276 Py_XDECREF(tmp);
3277 tmp = NULL;
3278 } else {
3279 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3280 return 1;
3281 }
3282 *out = Suite(body, arena);
3283 if (*out == NULL) goto failed;
3284 return 0;
3285 }
3286
3287 tmp = PyObject_Repr(obj);
3288 if (tmp == NULL) goto failed;
3289 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3290failed:
3291 Py_XDECREF(tmp);
3292 return 1;
3293}
3294
3295int
3296obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3297{
3298 PyObject* tmp = NULL;
3299
3300 int lineno;
3301 int col_offset;
3302
3303 if (obj == Py_None) {
3304 *out = NULL;
3305 return 0;
3306 }
3307 if (PyObject_HasAttrString(obj, "lineno")) {
3308 int res;
3309 tmp = PyObject_GetAttrString(obj, "lineno");
3310 if (tmp == NULL) goto failed;
3311 res = obj2ast_int(tmp, &lineno, arena);
3312 if (res != 0) goto failed;
3313 Py_XDECREF(tmp);
3314 tmp = NULL;
3315 } else {
3316 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3317 return 1;
3318 }
3319 if (PyObject_HasAttrString(obj, "col_offset")) {
3320 int res;
3321 tmp = PyObject_GetAttrString(obj, "col_offset");
3322 if (tmp == NULL) goto failed;
3323 res = obj2ast_int(tmp, &col_offset, arena);
3324 if (res != 0) goto failed;
3325 Py_XDECREF(tmp);
3326 tmp = NULL;
3327 } else {
3328 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3329 return 1;
3330 }
3331 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3332 identifier name;
3333 arguments_ty args;
3334 asdl_seq* body;
3335 asdl_seq* decorator_list;
3336
3337 if (PyObject_HasAttrString(obj, "name")) {
3338 int res;
3339 tmp = PyObject_GetAttrString(obj, "name");
3340 if (tmp == NULL) goto failed;
3341 res = obj2ast_identifier(tmp, &name, arena);
3342 if (res != 0) goto failed;
3343 Py_XDECREF(tmp);
3344 tmp = NULL;
3345 } else {
3346 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3347 return 1;
3348 }
3349 if (PyObject_HasAttrString(obj, "args")) {
3350 int res;
3351 tmp = PyObject_GetAttrString(obj, "args");
3352 if (tmp == NULL) goto failed;
3353 res = obj2ast_arguments(tmp, &args, arena);
3354 if (res != 0) goto failed;
3355 Py_XDECREF(tmp);
3356 tmp = NULL;
3357 } else {
3358 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3359 return 1;
3360 }
3361 if (PyObject_HasAttrString(obj, "body")) {
3362 int res;
3363 Py_ssize_t len;
3364 Py_ssize_t i;
3365 tmp = PyObject_GetAttrString(obj, "body");
3366 if (tmp == NULL) goto failed;
3367 if (!PyList_Check(tmp)) {
3368 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3369 goto failed;
3370 }
3371 len = PyList_GET_SIZE(tmp);
3372 body = asdl_seq_new(len, arena);
3373 if (body == NULL) goto failed;
3374 for (i = 0; i < len; i++) {
3375 stmt_ty value;
3376 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3377 if (res != 0) goto failed;
3378 asdl_seq_SET(body, i, value);
3379 }
3380 Py_XDECREF(tmp);
3381 tmp = NULL;
3382 } else {
3383 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3384 return 1;
3385 }
3386 if (PyObject_HasAttrString(obj, "decorator_list")) {
3387 int res;
3388 Py_ssize_t len;
3389 Py_ssize_t i;
3390 tmp = PyObject_GetAttrString(obj, "decorator_list");
3391 if (tmp == NULL) goto failed;
3392 if (!PyList_Check(tmp)) {
3393 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3394 goto failed;
3395 }
3396 len = PyList_GET_SIZE(tmp);
3397 decorator_list = asdl_seq_new(len, arena);
3398 if (decorator_list == NULL) goto failed;
3399 for (i = 0; i < len; i++) {
3400 expr_ty value;
3401 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3402 if (res != 0) goto failed;
3403 asdl_seq_SET(decorator_list, i, value);
3404 }
3405 Py_XDECREF(tmp);
3406 tmp = NULL;
3407 } else {
3408 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3409 return 1;
3410 }
3411 *out = FunctionDef(name, args, body, decorator_list, lineno,
3412 col_offset, arena);
3413 if (*out == NULL) goto failed;
3414 return 0;
3415 }
3416 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3417 identifier name;
3418 asdl_seq* bases;
3419 asdl_seq* body;
3420 asdl_seq* decorator_list;
3421
3422 if (PyObject_HasAttrString(obj, "name")) {
3423 int res;
3424 tmp = PyObject_GetAttrString(obj, "name");
3425 if (tmp == NULL) goto failed;
3426 res = obj2ast_identifier(tmp, &name, arena);
3427 if (res != 0) goto failed;
3428 Py_XDECREF(tmp);
3429 tmp = NULL;
3430 } else {
3431 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3432 return 1;
3433 }
3434 if (PyObject_HasAttrString(obj, "bases")) {
3435 int res;
3436 Py_ssize_t len;
3437 Py_ssize_t i;
3438 tmp = PyObject_GetAttrString(obj, "bases");
3439 if (tmp == NULL) goto failed;
3440 if (!PyList_Check(tmp)) {
3441 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3442 goto failed;
3443 }
3444 len = PyList_GET_SIZE(tmp);
3445 bases = asdl_seq_new(len, arena);
3446 if (bases == NULL) goto failed;
3447 for (i = 0; i < len; i++) {
3448 expr_ty value;
3449 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3450 if (res != 0) goto failed;
3451 asdl_seq_SET(bases, i, value);
3452 }
3453 Py_XDECREF(tmp);
3454 tmp = NULL;
3455 } else {
3456 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3457 return 1;
3458 }
3459 if (PyObject_HasAttrString(obj, "body")) {
3460 int res;
3461 Py_ssize_t len;
3462 Py_ssize_t i;
3463 tmp = PyObject_GetAttrString(obj, "body");
3464 if (tmp == NULL) goto failed;
3465 if (!PyList_Check(tmp)) {
3466 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3467 goto failed;
3468 }
3469 len = PyList_GET_SIZE(tmp);
3470 body = asdl_seq_new(len, arena);
3471 if (body == NULL) goto failed;
3472 for (i = 0; i < len; i++) {
3473 stmt_ty value;
3474 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3475 if (res != 0) goto failed;
3476 asdl_seq_SET(body, i, value);
3477 }
3478 Py_XDECREF(tmp);
3479 tmp = NULL;
3480 } else {
3481 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3482 return 1;
3483 }
3484 if (PyObject_HasAttrString(obj, "decorator_list")) {
3485 int res;
3486 Py_ssize_t len;
3487 Py_ssize_t i;
3488 tmp = PyObject_GetAttrString(obj, "decorator_list");
3489 if (tmp == NULL) goto failed;
3490 if (!PyList_Check(tmp)) {
3491 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3492 goto failed;
3493 }
3494 len = PyList_GET_SIZE(tmp);
3495 decorator_list = asdl_seq_new(len, arena);
3496 if (decorator_list == NULL) goto failed;
3497 for (i = 0; i < len; i++) {
3498 expr_ty value;
3499 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3500 if (res != 0) goto failed;
3501 asdl_seq_SET(decorator_list, i, value);
3502 }
3503 Py_XDECREF(tmp);
3504 tmp = NULL;
3505 } else {
3506 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3507 return 1;
3508 }
3509 *out = ClassDef(name, bases, body, decorator_list, lineno,
3510 col_offset, arena);
3511 if (*out == NULL) goto failed;
3512 return 0;
3513 }
3514 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3515 expr_ty value;
3516
3517 if (PyObject_HasAttrString(obj, "value")) {
3518 int res;
3519 tmp = PyObject_GetAttrString(obj, "value");
3520 if (tmp == NULL) goto failed;
3521 res = obj2ast_expr(tmp, &value, arena);
3522 if (res != 0) goto failed;
3523 Py_XDECREF(tmp);
3524 tmp = NULL;
3525 } else {
3526 value = NULL;
3527 }
3528 *out = Return(value, lineno, col_offset, arena);
3529 if (*out == NULL) goto failed;
3530 return 0;
3531 }
3532 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3533 asdl_seq* targets;
3534
3535 if (PyObject_HasAttrString(obj, "targets")) {
3536 int res;
3537 Py_ssize_t len;
3538 Py_ssize_t i;
3539 tmp = PyObject_GetAttrString(obj, "targets");
3540 if (tmp == NULL) goto failed;
3541 if (!PyList_Check(tmp)) {
3542 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3543 goto failed;
3544 }
3545 len = PyList_GET_SIZE(tmp);
3546 targets = asdl_seq_new(len, arena);
3547 if (targets == NULL) goto failed;
3548 for (i = 0; i < len; i++) {
3549 expr_ty value;
3550 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3551 if (res != 0) goto failed;
3552 asdl_seq_SET(targets, i, value);
3553 }
3554 Py_XDECREF(tmp);
3555 tmp = NULL;
3556 } else {
3557 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3558 return 1;
3559 }
3560 *out = Delete(targets, lineno, col_offset, arena);
3561 if (*out == NULL) goto failed;
3562 return 0;
3563 }
3564 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3565 asdl_seq* targets;
3566 expr_ty value;
3567
3568 if (PyObject_HasAttrString(obj, "targets")) {
3569 int res;
3570 Py_ssize_t len;
3571 Py_ssize_t i;
3572 tmp = PyObject_GetAttrString(obj, "targets");
3573 if (tmp == NULL) goto failed;
3574 if (!PyList_Check(tmp)) {
3575 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3576 goto failed;
3577 }
3578 len = PyList_GET_SIZE(tmp);
3579 targets = asdl_seq_new(len, arena);
3580 if (targets == NULL) goto failed;
3581 for (i = 0; i < len; i++) {
3582 expr_ty value;
3583 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3584 if (res != 0) goto failed;
3585 asdl_seq_SET(targets, i, value);
3586 }
3587 Py_XDECREF(tmp);
3588 tmp = NULL;
3589 } else {
3590 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3591 return 1;
3592 }
3593 if (PyObject_HasAttrString(obj, "value")) {
3594 int res;
3595 tmp = PyObject_GetAttrString(obj, "value");
3596 if (tmp == NULL) goto failed;
3597 res = obj2ast_expr(tmp, &value, arena);
3598 if (res != 0) goto failed;
3599 Py_XDECREF(tmp);
3600 tmp = NULL;
3601 } else {
3602 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3603 return 1;
3604 }
3605 *out = Assign(targets, value, lineno, col_offset, arena);
3606 if (*out == NULL) goto failed;
3607 return 0;
3608 }
3609 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3610 expr_ty target;
3611 operator_ty op;
3612 expr_ty value;
3613
3614 if (PyObject_HasAttrString(obj, "target")) {
3615 int res;
3616 tmp = PyObject_GetAttrString(obj, "target");
3617 if (tmp == NULL) goto failed;
3618 res = obj2ast_expr(tmp, &target, arena);
3619 if (res != 0) goto failed;
3620 Py_XDECREF(tmp);
3621 tmp = NULL;
3622 } else {
3623 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3624 return 1;
3625 }
3626 if (PyObject_HasAttrString(obj, "op")) {
3627 int res;
3628 tmp = PyObject_GetAttrString(obj, "op");
3629 if (tmp == NULL) goto failed;
3630 res = obj2ast_operator(tmp, &op, arena);
3631 if (res != 0) goto failed;
3632 Py_XDECREF(tmp);
3633 tmp = NULL;
3634 } else {
3635 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3636 return 1;
3637 }
3638 if (PyObject_HasAttrString(obj, "value")) {
3639 int res;
3640 tmp = PyObject_GetAttrString(obj, "value");
3641 if (tmp == NULL) goto failed;
3642 res = obj2ast_expr(tmp, &value, arena);
3643 if (res != 0) goto failed;
3644 Py_XDECREF(tmp);
3645 tmp = NULL;
3646 } else {
3647 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3648 return 1;
3649 }
3650 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3651 if (*out == NULL) goto failed;
3652 return 0;
3653 }
3654 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3655 expr_ty dest;
3656 asdl_seq* values;
3657 bool nl;
3658
3659 if (PyObject_HasAttrString(obj, "dest")) {
3660 int res;
3661 tmp = PyObject_GetAttrString(obj, "dest");
3662 if (tmp == NULL) goto failed;
3663 res = obj2ast_expr(tmp, &dest, arena);
3664 if (res != 0) goto failed;
3665 Py_XDECREF(tmp);
3666 tmp = NULL;
3667 } else {
3668 dest = NULL;
3669 }
3670 if (PyObject_HasAttrString(obj, "values")) {
3671 int res;
3672 Py_ssize_t len;
3673 Py_ssize_t i;
3674 tmp = PyObject_GetAttrString(obj, "values");
3675 if (tmp == NULL) goto failed;
3676 if (!PyList_Check(tmp)) {
3677 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3678 goto failed;
3679 }
3680 len = PyList_GET_SIZE(tmp);
3681 values = asdl_seq_new(len, arena);
3682 if (values == NULL) goto failed;
3683 for (i = 0; i < len; i++) {
3684 expr_ty value;
3685 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3686 if (res != 0) goto failed;
3687 asdl_seq_SET(values, i, value);
3688 }
3689 Py_XDECREF(tmp);
3690 tmp = NULL;
3691 } else {
3692 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3693 return 1;
3694 }
3695 if (PyObject_HasAttrString(obj, "nl")) {
3696 int res;
3697 tmp = PyObject_GetAttrString(obj, "nl");
3698 if (tmp == NULL) goto failed;
3699 res = obj2ast_bool(tmp, &nl, arena);
3700 if (res != 0) goto failed;
3701 Py_XDECREF(tmp);
3702 tmp = NULL;
3703 } else {
3704 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3705 return 1;
3706 }
3707 *out = Print(dest, values, nl, lineno, col_offset, arena);
3708 if (*out == NULL) goto failed;
3709 return 0;
3710 }
3711 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3712 expr_ty target;
3713 expr_ty iter;
3714 asdl_seq* body;
3715 asdl_seq* orelse;
3716
3717 if (PyObject_HasAttrString(obj, "target")) {
3718 int res;
3719 tmp = PyObject_GetAttrString(obj, "target");
3720 if (tmp == NULL) goto failed;
3721 res = obj2ast_expr(tmp, &target, arena);
3722 if (res != 0) goto failed;
3723 Py_XDECREF(tmp);
3724 tmp = NULL;
3725 } else {
3726 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3727 return 1;
3728 }
3729 if (PyObject_HasAttrString(obj, "iter")) {
3730 int res;
3731 tmp = PyObject_GetAttrString(obj, "iter");
3732 if (tmp == NULL) goto failed;
3733 res = obj2ast_expr(tmp, &iter, arena);
3734 if (res != 0) goto failed;
3735 Py_XDECREF(tmp);
3736 tmp = NULL;
3737 } else {
3738 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3739 return 1;
3740 }
3741 if (PyObject_HasAttrString(obj, "body")) {
3742 int res;
3743 Py_ssize_t len;
3744 Py_ssize_t i;
3745 tmp = PyObject_GetAttrString(obj, "body");
3746 if (tmp == NULL) goto failed;
3747 if (!PyList_Check(tmp)) {
3748 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3749 goto failed;
3750 }
3751 len = PyList_GET_SIZE(tmp);
3752 body = asdl_seq_new(len, arena);
3753 if (body == NULL) goto failed;
3754 for (i = 0; i < len; i++) {
3755 stmt_ty value;
3756 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3757 if (res != 0) goto failed;
3758 asdl_seq_SET(body, i, value);
3759 }
3760 Py_XDECREF(tmp);
3761 tmp = NULL;
3762 } else {
3763 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3764 return 1;
3765 }
3766 if (PyObject_HasAttrString(obj, "orelse")) {
3767 int res;
3768 Py_ssize_t len;
3769 Py_ssize_t i;
3770 tmp = PyObject_GetAttrString(obj, "orelse");
3771 if (tmp == NULL) goto failed;
3772 if (!PyList_Check(tmp)) {
3773 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3774 goto failed;
3775 }
3776 len = PyList_GET_SIZE(tmp);
3777 orelse = asdl_seq_new(len, arena);
3778 if (orelse == NULL) goto failed;
3779 for (i = 0; i < len; i++) {
3780 stmt_ty value;
3781 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3782 if (res != 0) goto failed;
3783 asdl_seq_SET(orelse, i, value);
3784 }
3785 Py_XDECREF(tmp);
3786 tmp = NULL;
3787 } else {
3788 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3789 return 1;
3790 }
3791 *out = For(target, iter, body, orelse, lineno, col_offset,
3792 arena);
3793 if (*out == NULL) goto failed;
3794 return 0;
3795 }
3796 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3797 expr_ty test;
3798 asdl_seq* body;
3799 asdl_seq* orelse;
3800
3801 if (PyObject_HasAttrString(obj, "test")) {
3802 int res;
3803 tmp = PyObject_GetAttrString(obj, "test");
3804 if (tmp == NULL) goto failed;
3805 res = obj2ast_expr(tmp, &test, arena);
3806 if (res != 0) goto failed;
3807 Py_XDECREF(tmp);
3808 tmp = NULL;
3809 } else {
3810 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3811 return 1;
3812 }
3813 if (PyObject_HasAttrString(obj, "body")) {
3814 int res;
3815 Py_ssize_t len;
3816 Py_ssize_t i;
3817 tmp = PyObject_GetAttrString(obj, "body");
3818 if (tmp == NULL) goto failed;
3819 if (!PyList_Check(tmp)) {
3820 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3821 goto failed;
3822 }
3823 len = PyList_GET_SIZE(tmp);
3824 body = asdl_seq_new(len, arena);
3825 if (body == NULL) goto failed;
3826 for (i = 0; i < len; i++) {
3827 stmt_ty value;
3828 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3829 if (res != 0) goto failed;
3830 asdl_seq_SET(body, i, value);
3831 }
3832 Py_XDECREF(tmp);
3833 tmp = NULL;
3834 } else {
3835 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3836 return 1;
3837 }
3838 if (PyObject_HasAttrString(obj, "orelse")) {
3839 int res;
3840 Py_ssize_t len;
3841 Py_ssize_t i;
3842 tmp = PyObject_GetAttrString(obj, "orelse");
3843 if (tmp == NULL) goto failed;
3844 if (!PyList_Check(tmp)) {
3845 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3846 goto failed;
3847 }
3848 len = PyList_GET_SIZE(tmp);
3849 orelse = asdl_seq_new(len, arena);
3850 if (orelse == NULL) goto failed;
3851 for (i = 0; i < len; i++) {
3852 stmt_ty value;
3853 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3854 if (res != 0) goto failed;
3855 asdl_seq_SET(orelse, i, value);
3856 }
3857 Py_XDECREF(tmp);
3858 tmp = NULL;
3859 } else {
3860 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3861 return 1;
3862 }
3863 *out = While(test, body, orelse, lineno, col_offset, arena);
3864 if (*out == NULL) goto failed;
3865 return 0;
3866 }
3867 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3868 expr_ty test;
3869 asdl_seq* body;
3870 asdl_seq* orelse;
3871
3872 if (PyObject_HasAttrString(obj, "test")) {
3873 int res;
3874 tmp = PyObject_GetAttrString(obj, "test");
3875 if (tmp == NULL) goto failed;
3876 res = obj2ast_expr(tmp, &test, arena);
3877 if (res != 0) goto failed;
3878 Py_XDECREF(tmp);
3879 tmp = NULL;
3880 } else {
3881 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3882 return 1;
3883 }
3884 if (PyObject_HasAttrString(obj, "body")) {
3885 int res;
3886 Py_ssize_t len;
3887 Py_ssize_t i;
3888 tmp = PyObject_GetAttrString(obj, "body");
3889 if (tmp == NULL) goto failed;
3890 if (!PyList_Check(tmp)) {
3891 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3892 goto failed;
3893 }
3894 len = PyList_GET_SIZE(tmp);
3895 body = asdl_seq_new(len, arena);
3896 if (body == NULL) goto failed;
3897 for (i = 0; i < len; i++) {
3898 stmt_ty value;
3899 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3900 if (res != 0) goto failed;
3901 asdl_seq_SET(body, i, value);
3902 }
3903 Py_XDECREF(tmp);
3904 tmp = NULL;
3905 } else {
3906 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3907 return 1;
3908 }
3909 if (PyObject_HasAttrString(obj, "orelse")) {
3910 int res;
3911 Py_ssize_t len;
3912 Py_ssize_t i;
3913 tmp = PyObject_GetAttrString(obj, "orelse");
3914 if (tmp == NULL) goto failed;
3915 if (!PyList_Check(tmp)) {
3916 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3917 goto failed;
3918 }
3919 len = PyList_GET_SIZE(tmp);
3920 orelse = asdl_seq_new(len, arena);
3921 if (orelse == NULL) goto failed;
3922 for (i = 0; i < len; i++) {
3923 stmt_ty value;
3924 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3925 if (res != 0) goto failed;
3926 asdl_seq_SET(orelse, i, value);
3927 }
3928 Py_XDECREF(tmp);
3929 tmp = NULL;
3930 } else {
3931 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3932 return 1;
3933 }
3934 *out = If(test, body, orelse, lineno, col_offset, arena);
3935 if (*out == NULL) goto failed;
3936 return 0;
3937 }
3938 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3939 expr_ty context_expr;
3940 expr_ty optional_vars;
3941 asdl_seq* body;
3942
3943 if (PyObject_HasAttrString(obj, "context_expr")) {
3944 int res;
3945 tmp = PyObject_GetAttrString(obj, "context_expr");
3946 if (tmp == NULL) goto failed;
3947 res = obj2ast_expr(tmp, &context_expr, arena);
3948 if (res != 0) goto failed;
3949 Py_XDECREF(tmp);
3950 tmp = NULL;
3951 } else {
3952 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3953 return 1;
3954 }
3955 if (PyObject_HasAttrString(obj, "optional_vars")) {
3956 int res;
3957 tmp = PyObject_GetAttrString(obj, "optional_vars");
3958 if (tmp == NULL) goto failed;
3959 res = obj2ast_expr(tmp, &optional_vars, arena);
3960 if (res != 0) goto failed;
3961 Py_XDECREF(tmp);
3962 tmp = NULL;
3963 } else {
3964 optional_vars = NULL;
3965 }
3966 if (PyObject_HasAttrString(obj, "body")) {
3967 int res;
3968 Py_ssize_t len;
3969 Py_ssize_t i;
3970 tmp = PyObject_GetAttrString(obj, "body");
3971 if (tmp == NULL) goto failed;
3972 if (!PyList_Check(tmp)) {
3973 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3974 goto failed;
3975 }
3976 len = PyList_GET_SIZE(tmp);
3977 body = asdl_seq_new(len, arena);
3978 if (body == NULL) goto failed;
3979 for (i = 0; i < len; i++) {
3980 stmt_ty value;
3981 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3982 if (res != 0) goto failed;
3983 asdl_seq_SET(body, i, value);
3984 }
3985 Py_XDECREF(tmp);
3986 tmp = NULL;
3987 } else {
3988 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3989 return 1;
3990 }
3991 *out = With(context_expr, optional_vars, body, lineno,
3992 col_offset, arena);
3993 if (*out == NULL) goto failed;
3994 return 0;
3995 }
3996 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3997 expr_ty type;
3998 expr_ty inst;
3999 expr_ty tback;
4000
4001 if (PyObject_HasAttrString(obj, "type")) {
4002 int res;
4003 tmp = PyObject_GetAttrString(obj, "type");
4004 if (tmp == NULL) goto failed;
4005 res = obj2ast_expr(tmp, &type, arena);
4006 if (res != 0) goto failed;
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 type = NULL;
4011 }
4012 if (PyObject_HasAttrString(obj, "inst")) {
4013 int res;
4014 tmp = PyObject_GetAttrString(obj, "inst");
4015 if (tmp == NULL) goto failed;
4016 res = obj2ast_expr(tmp, &inst, arena);
4017 if (res != 0) goto failed;
4018 Py_XDECREF(tmp);
4019 tmp = NULL;
4020 } else {
4021 inst = NULL;
4022 }
4023 if (PyObject_HasAttrString(obj, "tback")) {
4024 int res;
4025 tmp = PyObject_GetAttrString(obj, "tback");
4026 if (tmp == NULL) goto failed;
4027 res = obj2ast_expr(tmp, &tback, arena);
4028 if (res != 0) goto failed;
4029 Py_XDECREF(tmp);
4030 tmp = NULL;
4031 } else {
4032 tback = NULL;
4033 }
4034 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4035 if (*out == NULL) goto failed;
4036 return 0;
4037 }
4038 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4039 asdl_seq* body;
4040 asdl_seq* handlers;
4041 asdl_seq* orelse;
4042
4043 if (PyObject_HasAttrString(obj, "body")) {
4044 int res;
4045 Py_ssize_t len;
4046 Py_ssize_t i;
4047 tmp = PyObject_GetAttrString(obj, "body");
4048 if (tmp == NULL) goto failed;
4049 if (!PyList_Check(tmp)) {
4050 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4051 goto failed;
4052 }
4053 len = PyList_GET_SIZE(tmp);
4054 body = asdl_seq_new(len, arena);
4055 if (body == NULL) goto failed;
4056 for (i = 0; i < len; i++) {
4057 stmt_ty value;
4058 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4059 if (res != 0) goto failed;
4060 asdl_seq_SET(body, i, value);
4061 }
4062 Py_XDECREF(tmp);
4063 tmp = NULL;
4064 } else {
4065 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4066 return 1;
4067 }
4068 if (PyObject_HasAttrString(obj, "handlers")) {
4069 int res;
4070 Py_ssize_t len;
4071 Py_ssize_t i;
4072 tmp = PyObject_GetAttrString(obj, "handlers");
4073 if (tmp == NULL) goto failed;
4074 if (!PyList_Check(tmp)) {
4075 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4076 goto failed;
4077 }
4078 len = PyList_GET_SIZE(tmp);
4079 handlers = asdl_seq_new(len, arena);
4080 if (handlers == NULL) goto failed;
4081 for (i = 0; i < len; i++) {
4082 excepthandler_ty value;
4083 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4084 if (res != 0) goto failed;
4085 asdl_seq_SET(handlers, i, value);
4086 }
4087 Py_XDECREF(tmp);
4088 tmp = NULL;
4089 } else {
4090 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4091 return 1;
4092 }
4093 if (PyObject_HasAttrString(obj, "orelse")) {
4094 int res;
4095 Py_ssize_t len;
4096 Py_ssize_t i;
4097 tmp = PyObject_GetAttrString(obj, "orelse");
4098 if (tmp == NULL) goto failed;
4099 if (!PyList_Check(tmp)) {
4100 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4101 goto failed;
4102 }
4103 len = PyList_GET_SIZE(tmp);
4104 orelse = asdl_seq_new(len, arena);
4105 if (orelse == NULL) goto failed;
4106 for (i = 0; i < len; i++) {
4107 stmt_ty value;
4108 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4109 if (res != 0) goto failed;
4110 asdl_seq_SET(orelse, i, value);
4111 }
4112 Py_XDECREF(tmp);
4113 tmp = NULL;
4114 } else {
4115 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4116 return 1;
4117 }
4118 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4119 arena);
4120 if (*out == NULL) goto failed;
4121 return 0;
4122 }
4123 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4124 asdl_seq* body;
4125 asdl_seq* finalbody;
4126
4127 if (PyObject_HasAttrString(obj, "body")) {
4128 int res;
4129 Py_ssize_t len;
4130 Py_ssize_t i;
4131 tmp = PyObject_GetAttrString(obj, "body");
4132 if (tmp == NULL) goto failed;
4133 if (!PyList_Check(tmp)) {
4134 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4135 goto failed;
4136 }
4137 len = PyList_GET_SIZE(tmp);
4138 body = asdl_seq_new(len, arena);
4139 if (body == NULL) goto failed;
4140 for (i = 0; i < len; i++) {
4141 stmt_ty value;
4142 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4143 if (res != 0) goto failed;
4144 asdl_seq_SET(body, i, value);
4145 }
4146 Py_XDECREF(tmp);
4147 tmp = NULL;
4148 } else {
4149 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4150 return 1;
4151 }
4152 if (PyObject_HasAttrString(obj, "finalbody")) {
4153 int res;
4154 Py_ssize_t len;
4155 Py_ssize_t i;
4156 tmp = PyObject_GetAttrString(obj, "finalbody");
4157 if (tmp == NULL) goto failed;
4158 if (!PyList_Check(tmp)) {
4159 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4160 goto failed;
4161 }
4162 len = PyList_GET_SIZE(tmp);
4163 finalbody = asdl_seq_new(len, arena);
4164 if (finalbody == NULL) goto failed;
4165 for (i = 0; i < len; i++) {
4166 stmt_ty value;
4167 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4168 if (res != 0) goto failed;
4169 asdl_seq_SET(finalbody, i, value);
4170 }
4171 Py_XDECREF(tmp);
4172 tmp = NULL;
4173 } else {
4174 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4175 return 1;
4176 }
4177 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4178 if (*out == NULL) goto failed;
4179 return 0;
4180 }
4181 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4182 expr_ty test;
4183 expr_ty msg;
4184
4185 if (PyObject_HasAttrString(obj, "test")) {
4186 int res;
4187 tmp = PyObject_GetAttrString(obj, "test");
4188 if (tmp == NULL) goto failed;
4189 res = obj2ast_expr(tmp, &test, arena);
4190 if (res != 0) goto failed;
4191 Py_XDECREF(tmp);
4192 tmp = NULL;
4193 } else {
4194 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4195 return 1;
4196 }
4197 if (PyObject_HasAttrString(obj, "msg")) {
4198 int res;
4199 tmp = PyObject_GetAttrString(obj, "msg");
4200 if (tmp == NULL) goto failed;
4201 res = obj2ast_expr(tmp, &msg, arena);
4202 if (res != 0) goto failed;
4203 Py_XDECREF(tmp);
4204 tmp = NULL;
4205 } else {
4206 msg = NULL;
4207 }
4208 *out = Assert(test, msg, lineno, col_offset, arena);
4209 if (*out == NULL) goto failed;
4210 return 0;
4211 }
4212 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4213 asdl_seq* names;
4214
4215 if (PyObject_HasAttrString(obj, "names")) {
4216 int res;
4217 Py_ssize_t len;
4218 Py_ssize_t i;
4219 tmp = PyObject_GetAttrString(obj, "names");
4220 if (tmp == NULL) goto failed;
4221 if (!PyList_Check(tmp)) {
4222 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4223 goto failed;
4224 }
4225 len = PyList_GET_SIZE(tmp);
4226 names = asdl_seq_new(len, arena);
4227 if (names == NULL) goto failed;
4228 for (i = 0; i < len; i++) {
4229 alias_ty value;
4230 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4231 if (res != 0) goto failed;
4232 asdl_seq_SET(names, i, value);
4233 }
4234 Py_XDECREF(tmp);
4235 tmp = NULL;
4236 } else {
4237 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4238 return 1;
4239 }
4240 *out = Import(names, lineno, col_offset, arena);
4241 if (*out == NULL) goto failed;
4242 return 0;
4243 }
4244 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4245 identifier module;
4246 asdl_seq* names;
4247 int level;
4248
4249 if (PyObject_HasAttrString(obj, "module")) {
4250 int res;
4251 tmp = PyObject_GetAttrString(obj, "module");
4252 if (tmp == NULL) goto failed;
4253 res = obj2ast_identifier(tmp, &module, arena);
4254 if (res != 0) goto failed;
4255 Py_XDECREF(tmp);
4256 tmp = NULL;
4257 } else {
4258 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4259 return 1;
4260 }
4261 if (PyObject_HasAttrString(obj, "names")) {
4262 int res;
4263 Py_ssize_t len;
4264 Py_ssize_t i;
4265 tmp = PyObject_GetAttrString(obj, "names");
4266 if (tmp == NULL) goto failed;
4267 if (!PyList_Check(tmp)) {
4268 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4269 goto failed;
4270 }
4271 len = PyList_GET_SIZE(tmp);
4272 names = asdl_seq_new(len, arena);
4273 if (names == NULL) goto failed;
4274 for (i = 0; i < len; i++) {
4275 alias_ty value;
4276 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4277 if (res != 0) goto failed;
4278 asdl_seq_SET(names, i, value);
4279 }
4280 Py_XDECREF(tmp);
4281 tmp = NULL;
4282 } else {
4283 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4284 return 1;
4285 }
4286 if (PyObject_HasAttrString(obj, "level")) {
4287 int res;
4288 tmp = PyObject_GetAttrString(obj, "level");
4289 if (tmp == NULL) goto failed;
4290 res = obj2ast_int(tmp, &level, arena);
4291 if (res != 0) goto failed;
4292 Py_XDECREF(tmp);
4293 tmp = NULL;
4294 } else {
4295 level = 0;
4296 }
4297 *out = ImportFrom(module, names, level, lineno, col_offset,
4298 arena);
4299 if (*out == NULL) goto failed;
4300 return 0;
4301 }
4302 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4303 expr_ty body;
4304 expr_ty globals;
4305 expr_ty locals;
4306
4307 if (PyObject_HasAttrString(obj, "body")) {
4308 int res;
4309 tmp = PyObject_GetAttrString(obj, "body");
4310 if (tmp == NULL) goto failed;
4311 res = obj2ast_expr(tmp, &body, arena);
4312 if (res != 0) goto failed;
4313 Py_XDECREF(tmp);
4314 tmp = NULL;
4315 } else {
4316 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4317 return 1;
4318 }
4319 if (PyObject_HasAttrString(obj, "globals")) {
4320 int res;
4321 tmp = PyObject_GetAttrString(obj, "globals");
4322 if (tmp == NULL) goto failed;
4323 res = obj2ast_expr(tmp, &globals, arena);
4324 if (res != 0) goto failed;
4325 Py_XDECREF(tmp);
4326 tmp = NULL;
4327 } else {
4328 globals = NULL;
4329 }
4330 if (PyObject_HasAttrString(obj, "locals")) {
4331 int res;
4332 tmp = PyObject_GetAttrString(obj, "locals");
4333 if (tmp == NULL) goto failed;
4334 res = obj2ast_expr(tmp, &locals, arena);
4335 if (res != 0) goto failed;
4336 Py_XDECREF(tmp);
4337 tmp = NULL;
4338 } else {
4339 locals = NULL;
4340 }
4341 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4342 if (*out == NULL) goto failed;
4343 return 0;
4344 }
4345 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4346 asdl_seq* names;
4347
4348 if (PyObject_HasAttrString(obj, "names")) {
4349 int res;
4350 Py_ssize_t len;
4351 Py_ssize_t i;
4352 tmp = PyObject_GetAttrString(obj, "names");
4353 if (tmp == NULL) goto failed;
4354 if (!PyList_Check(tmp)) {
4355 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4356 goto failed;
4357 }
4358 len = PyList_GET_SIZE(tmp);
4359 names = asdl_seq_new(len, arena);
4360 if (names == NULL) goto failed;
4361 for (i = 0; i < len; i++) {
4362 identifier value;
4363 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4364 if (res != 0) goto failed;
4365 asdl_seq_SET(names, i, value);
4366 }
4367 Py_XDECREF(tmp);
4368 tmp = NULL;
4369 } else {
4370 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4371 return 1;
4372 }
4373 *out = Global(names, lineno, col_offset, arena);
4374 if (*out == NULL) goto failed;
4375 return 0;
4376 }
4377 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4378 expr_ty value;
4379
4380 if (PyObject_HasAttrString(obj, "value")) {
4381 int res;
4382 tmp = PyObject_GetAttrString(obj, "value");
4383 if (tmp == NULL) goto failed;
4384 res = obj2ast_expr(tmp, &value, arena);
4385 if (res != 0) goto failed;
4386 Py_XDECREF(tmp);
4387 tmp = NULL;
4388 } else {
4389 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4390 return 1;
4391 }
4392 *out = Expr(value, lineno, col_offset, arena);
4393 if (*out == NULL) goto failed;
4394 return 0;
4395 }
4396 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4397
4398 *out = Pass(lineno, col_offset, arena);
4399 if (*out == NULL) goto failed;
4400 return 0;
4401 }
4402 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4403
4404 *out = Break(lineno, col_offset, arena);
4405 if (*out == NULL) goto failed;
4406 return 0;
4407 }
4408 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4409
4410 *out = Continue(lineno, col_offset, arena);
4411 if (*out == NULL) goto failed;
4412 return 0;
4413 }
4414
4415 tmp = PyObject_Repr(obj);
4416 if (tmp == NULL) goto failed;
4417 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4418failed:
4419 Py_XDECREF(tmp);
4420 return 1;
4421}
4422
4423int
4424obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4425{
4426 PyObject* tmp = NULL;
4427
4428 int lineno;
4429 int col_offset;
4430
4431 if (obj == Py_None) {
4432 *out = NULL;
4433 return 0;
4434 }
4435 if (PyObject_HasAttrString(obj, "lineno")) {
4436 int res;
4437 tmp = PyObject_GetAttrString(obj, "lineno");
4438 if (tmp == NULL) goto failed;
4439 res = obj2ast_int(tmp, &lineno, arena);
4440 if (res != 0) goto failed;
4441 Py_XDECREF(tmp);
4442 tmp = NULL;
4443 } else {
4444 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4445 return 1;
4446 }
4447 if (PyObject_HasAttrString(obj, "col_offset")) {
4448 int res;
4449 tmp = PyObject_GetAttrString(obj, "col_offset");
4450 if (tmp == NULL) goto failed;
4451 res = obj2ast_int(tmp, &col_offset, arena);
4452 if (res != 0) goto failed;
4453 Py_XDECREF(tmp);
4454 tmp = NULL;
4455 } else {
4456 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4457 return 1;
4458 }
4459 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4460 boolop_ty op;
4461 asdl_seq* values;
4462
4463 if (PyObject_HasAttrString(obj, "op")) {
4464 int res;
4465 tmp = PyObject_GetAttrString(obj, "op");
4466 if (tmp == NULL) goto failed;
4467 res = obj2ast_boolop(tmp, &op, arena);
4468 if (res != 0) goto failed;
4469 Py_XDECREF(tmp);
4470 tmp = NULL;
4471 } else {
4472 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4473 return 1;
4474 }
4475 if (PyObject_HasAttrString(obj, "values")) {
4476 int res;
4477 Py_ssize_t len;
4478 Py_ssize_t i;
4479 tmp = PyObject_GetAttrString(obj, "values");
4480 if (tmp == NULL) goto failed;
4481 if (!PyList_Check(tmp)) {
4482 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4483 goto failed;
4484 }
4485 len = PyList_GET_SIZE(tmp);
4486 values = asdl_seq_new(len, arena);
4487 if (values == NULL) goto failed;
4488 for (i = 0; i < len; i++) {
4489 expr_ty value;
4490 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4491 if (res != 0) goto failed;
4492 asdl_seq_SET(values, i, value);
4493 }
4494 Py_XDECREF(tmp);
4495 tmp = NULL;
4496 } else {
4497 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4498 return 1;
4499 }
4500 *out = BoolOp(op, values, lineno, col_offset, arena);
4501 if (*out == NULL) goto failed;
4502 return 0;
4503 }
4504 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4505 expr_ty left;
4506 operator_ty op;
4507 expr_ty right;
4508
4509 if (PyObject_HasAttrString(obj, "left")) {
4510 int res;
4511 tmp = PyObject_GetAttrString(obj, "left");
4512 if (tmp == NULL) goto failed;
4513 res = obj2ast_expr(tmp, &left, arena);
4514 if (res != 0) goto failed;
4515 Py_XDECREF(tmp);
4516 tmp = NULL;
4517 } else {
4518 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4519 return 1;
4520 }
4521 if (PyObject_HasAttrString(obj, "op")) {
4522 int res;
4523 tmp = PyObject_GetAttrString(obj, "op");
4524 if (tmp == NULL) goto failed;
4525 res = obj2ast_operator(tmp, &op, arena);
4526 if (res != 0) goto failed;
4527 Py_XDECREF(tmp);
4528 tmp = NULL;
4529 } else {
4530 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4531 return 1;
4532 }
4533 if (PyObject_HasAttrString(obj, "right")) {
4534 int res;
4535 tmp = PyObject_GetAttrString(obj, "right");
4536 if (tmp == NULL) goto failed;
4537 res = obj2ast_expr(tmp, &right, arena);
4538 if (res != 0) goto failed;
4539 Py_XDECREF(tmp);
4540 tmp = NULL;
4541 } else {
4542 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4543 return 1;
4544 }
4545 *out = BinOp(left, op, right, lineno, col_offset, arena);
4546 if (*out == NULL) goto failed;
4547 return 0;
4548 }
4549 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4550 unaryop_ty op;
4551 expr_ty operand;
4552
4553 if (PyObject_HasAttrString(obj, "op")) {
4554 int res;
4555 tmp = PyObject_GetAttrString(obj, "op");
4556 if (tmp == NULL) goto failed;
4557 res = obj2ast_unaryop(tmp, &op, arena);
4558 if (res != 0) goto failed;
4559 Py_XDECREF(tmp);
4560 tmp = NULL;
4561 } else {
4562 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4563 return 1;
4564 }
4565 if (PyObject_HasAttrString(obj, "operand")) {
4566 int res;
4567 tmp = PyObject_GetAttrString(obj, "operand");
4568 if (tmp == NULL) goto failed;
4569 res = obj2ast_expr(tmp, &operand, arena);
4570 if (res != 0) goto failed;
4571 Py_XDECREF(tmp);
4572 tmp = NULL;
4573 } else {
4574 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4575 return 1;
4576 }
4577 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4578 if (*out == NULL) goto failed;
4579 return 0;
4580 }
4581 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4582 arguments_ty args;
4583 expr_ty body;
4584
4585 if (PyObject_HasAttrString(obj, "args")) {
4586 int res;
4587 tmp = PyObject_GetAttrString(obj, "args");
4588 if (tmp == NULL) goto failed;
4589 res = obj2ast_arguments(tmp, &args, arena);
4590 if (res != 0) goto failed;
4591 Py_XDECREF(tmp);
4592 tmp = NULL;
4593 } else {
4594 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4595 return 1;
4596 }
4597 if (PyObject_HasAttrString(obj, "body")) {
4598 int res;
4599 tmp = PyObject_GetAttrString(obj, "body");
4600 if (tmp == NULL) goto failed;
4601 res = obj2ast_expr(tmp, &body, arena);
4602 if (res != 0) goto failed;
4603 Py_XDECREF(tmp);
4604 tmp = NULL;
4605 } else {
4606 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4607 return 1;
4608 }
4609 *out = Lambda(args, body, lineno, col_offset, arena);
4610 if (*out == NULL) goto failed;
4611 return 0;
4612 }
4613 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4614 expr_ty test;
4615 expr_ty body;
4616 expr_ty orelse;
4617
4618 if (PyObject_HasAttrString(obj, "test")) {
4619 int res;
4620 tmp = PyObject_GetAttrString(obj, "test");
4621 if (tmp == NULL) goto failed;
4622 res = obj2ast_expr(tmp, &test, arena);
4623 if (res != 0) goto failed;
4624 Py_XDECREF(tmp);
4625 tmp = NULL;
4626 } else {
4627 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4628 return 1;
4629 }
4630 if (PyObject_HasAttrString(obj, "body")) {
4631 int res;
4632 tmp = PyObject_GetAttrString(obj, "body");
4633 if (tmp == NULL) goto failed;
4634 res = obj2ast_expr(tmp, &body, arena);
4635 if (res != 0) goto failed;
4636 Py_XDECREF(tmp);
4637 tmp = NULL;
4638 } else {
4639 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4640 return 1;
4641 }
4642 if (PyObject_HasAttrString(obj, "orelse")) {
4643 int res;
4644 tmp = PyObject_GetAttrString(obj, "orelse");
4645 if (tmp == NULL) goto failed;
4646 res = obj2ast_expr(tmp, &orelse, arena);
4647 if (res != 0) goto failed;
4648 Py_XDECREF(tmp);
4649 tmp = NULL;
4650 } else {
4651 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4652 return 1;
4653 }
4654 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4655 if (*out == NULL) goto failed;
4656 return 0;
4657 }
4658 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4659 asdl_seq* keys;
4660 asdl_seq* values;
4661
4662 if (PyObject_HasAttrString(obj, "keys")) {
4663 int res;
4664 Py_ssize_t len;
4665 Py_ssize_t i;
4666 tmp = PyObject_GetAttrString(obj, "keys");
4667 if (tmp == NULL) goto failed;
4668 if (!PyList_Check(tmp)) {
4669 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4670 goto failed;
4671 }
4672 len = PyList_GET_SIZE(tmp);
4673 keys = asdl_seq_new(len, arena);
4674 if (keys == NULL) goto failed;
4675 for (i = 0; i < len; i++) {
4676 expr_ty value;
4677 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4678 if (res != 0) goto failed;
4679 asdl_seq_SET(keys, i, value);
4680 }
4681 Py_XDECREF(tmp);
4682 tmp = NULL;
4683 } else {
4684 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4685 return 1;
4686 }
4687 if (PyObject_HasAttrString(obj, "values")) {
4688 int res;
4689 Py_ssize_t len;
4690 Py_ssize_t i;
4691 tmp = PyObject_GetAttrString(obj, "values");
4692 if (tmp == NULL) goto failed;
4693 if (!PyList_Check(tmp)) {
4694 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4695 goto failed;
4696 }
4697 len = PyList_GET_SIZE(tmp);
4698 values = asdl_seq_new(len, arena);
4699 if (values == NULL) goto failed;
4700 for (i = 0; i < len; i++) {
4701 expr_ty value;
4702 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4703 if (res != 0) goto failed;
4704 asdl_seq_SET(values, i, value);
4705 }
4706 Py_XDECREF(tmp);
4707 tmp = NULL;
4708 } else {
4709 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4710 return 1;
4711 }
4712 *out = Dict(keys, values, lineno, col_offset, arena);
4713 if (*out == NULL) goto failed;
4714 return 0;
4715 }
4716 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4717 expr_ty elt;
4718 asdl_seq* generators;
4719
4720 if (PyObject_HasAttrString(obj, "elt")) {
4721 int res;
4722 tmp = PyObject_GetAttrString(obj, "elt");
4723 if (tmp == NULL) goto failed;
4724 res = obj2ast_expr(tmp, &elt, arena);
4725 if (res != 0) goto failed;
4726 Py_XDECREF(tmp);
4727 tmp = NULL;
4728 } else {
4729 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4730 return 1;
4731 }
4732 if (PyObject_HasAttrString(obj, "generators")) {
4733 int res;
4734 Py_ssize_t len;
4735 Py_ssize_t i;
4736 tmp = PyObject_GetAttrString(obj, "generators");
4737 if (tmp == NULL) goto failed;
4738 if (!PyList_Check(tmp)) {
4739 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4740 goto failed;
4741 }
4742 len = PyList_GET_SIZE(tmp);
4743 generators = asdl_seq_new(len, arena);
4744 if (generators == NULL) goto failed;
4745 for (i = 0; i < len; i++) {
4746 comprehension_ty value;
4747 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4748 if (res != 0) goto failed;
4749 asdl_seq_SET(generators, i, value);
4750 }
4751 Py_XDECREF(tmp);
4752 tmp = NULL;
4753 } else {
4754 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4755 return 1;
4756 }
4757 *out = ListComp(elt, generators, lineno, col_offset, arena);
4758 if (*out == NULL) goto failed;
4759 return 0;
4760 }
4761 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4762 expr_ty elt;
4763 asdl_seq* generators;
4764
4765 if (PyObject_HasAttrString(obj, "elt")) {
4766 int res;
4767 tmp = PyObject_GetAttrString(obj, "elt");
4768 if (tmp == NULL) goto failed;
4769 res = obj2ast_expr(tmp, &elt, arena);
4770 if (res != 0) goto failed;
4771 Py_XDECREF(tmp);
4772 tmp = NULL;
4773 } else {
4774 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4775 return 1;
4776 }
4777 if (PyObject_HasAttrString(obj, "generators")) {
4778 int res;
4779 Py_ssize_t len;
4780 Py_ssize_t i;
4781 tmp = PyObject_GetAttrString(obj, "generators");
4782 if (tmp == NULL) goto failed;
4783 if (!PyList_Check(tmp)) {
4784 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4785 goto failed;
4786 }
4787 len = PyList_GET_SIZE(tmp);
4788 generators = asdl_seq_new(len, arena);
4789 if (generators == NULL) goto failed;
4790 for (i = 0; i < len; i++) {
4791 comprehension_ty value;
4792 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4793 if (res != 0) goto failed;
4794 asdl_seq_SET(generators, i, value);
4795 }
4796 Py_XDECREF(tmp);
4797 tmp = NULL;
4798 } else {
4799 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4800 return 1;
4801 }
4802 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4803 if (*out == NULL) goto failed;
4804 return 0;
4805 }
4806 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4807 expr_ty value;
4808
4809 if (PyObject_HasAttrString(obj, "value")) {
4810 int res;
4811 tmp = PyObject_GetAttrString(obj, "value");
4812 if (tmp == NULL) goto failed;
4813 res = obj2ast_expr(tmp, &value, arena);
4814 if (res != 0) goto failed;
4815 Py_XDECREF(tmp);
4816 tmp = NULL;
4817 } else {
4818 value = NULL;
4819 }
4820 *out = Yield(value, lineno, col_offset, arena);
4821 if (*out == NULL) goto failed;
4822 return 0;
4823 }
4824 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4825 expr_ty left;
4826 asdl_int_seq* ops;
4827 asdl_seq* comparators;
4828
4829 if (PyObject_HasAttrString(obj, "left")) {
4830 int res;
4831 tmp = PyObject_GetAttrString(obj, "left");
4832 if (tmp == NULL) goto failed;
4833 res = obj2ast_expr(tmp, &left, arena);
4834 if (res != 0) goto failed;
4835 Py_XDECREF(tmp);
4836 tmp = NULL;
4837 } else {
4838 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4839 return 1;
4840 }
4841 if (PyObject_HasAttrString(obj, "ops")) {
4842 int res;
4843 Py_ssize_t len;
4844 Py_ssize_t i;
4845 tmp = PyObject_GetAttrString(obj, "ops");
4846 if (tmp == NULL) goto failed;
4847 if (!PyList_Check(tmp)) {
4848 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4849 goto failed;
4850 }
4851 len = PyList_GET_SIZE(tmp);
4852 ops = asdl_int_seq_new(len, arena);
4853 if (ops == NULL) goto failed;
4854 for (i = 0; i < len; i++) {
4855 cmpop_ty value;
4856 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4857 if (res != 0) goto failed;
4858 asdl_seq_SET(ops, i, value);
4859 }
4860 Py_XDECREF(tmp);
4861 tmp = NULL;
4862 } else {
4863 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4864 return 1;
4865 }
4866 if (PyObject_HasAttrString(obj, "comparators")) {
4867 int res;
4868 Py_ssize_t len;
4869 Py_ssize_t i;
4870 tmp = PyObject_GetAttrString(obj, "comparators");
4871 if (tmp == NULL) goto failed;
4872 if (!PyList_Check(tmp)) {
4873 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4874 goto failed;
4875 }
4876 len = PyList_GET_SIZE(tmp);
4877 comparators = asdl_seq_new(len, arena);
4878 if (comparators == NULL) goto failed;
4879 for (i = 0; i < len; i++) {
4880 expr_ty value;
4881 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4882 if (res != 0) goto failed;
4883 asdl_seq_SET(comparators, i, value);
4884 }
4885 Py_XDECREF(tmp);
4886 tmp = NULL;
4887 } else {
4888 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4889 return 1;
4890 }
4891 *out = Compare(left, ops, comparators, lineno, col_offset,
4892 arena);
4893 if (*out == NULL) goto failed;
4894 return 0;
4895 }
4896 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4897 expr_ty func;
4898 asdl_seq* args;
4899 asdl_seq* keywords;
4900 expr_ty starargs;
4901 expr_ty kwargs;
4902
4903 if (PyObject_HasAttrString(obj, "func")) {
4904 int res;
4905 tmp = PyObject_GetAttrString(obj, "func");
4906 if (tmp == NULL) goto failed;
4907 res = obj2ast_expr(tmp, &func, arena);
4908 if (res != 0) goto failed;
4909 Py_XDECREF(tmp);
4910 tmp = NULL;
4911 } else {
4912 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4913 return 1;
4914 }
4915 if (PyObject_HasAttrString(obj, "args")) {
4916 int res;
4917 Py_ssize_t len;
4918 Py_ssize_t i;
4919 tmp = PyObject_GetAttrString(obj, "args");
4920 if (tmp == NULL) goto failed;
4921 if (!PyList_Check(tmp)) {
4922 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4923 goto failed;
4924 }
4925 len = PyList_GET_SIZE(tmp);
4926 args = asdl_seq_new(len, arena);
4927 if (args == NULL) goto failed;
4928 for (i = 0; i < len; i++) {
4929 expr_ty value;
4930 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4931 if (res != 0) goto failed;
4932 asdl_seq_SET(args, i, value);
4933 }
4934 Py_XDECREF(tmp);
4935 tmp = NULL;
4936 } else {
4937 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4938 return 1;
4939 }
4940 if (PyObject_HasAttrString(obj, "keywords")) {
4941 int res;
4942 Py_ssize_t len;
4943 Py_ssize_t i;
4944 tmp = PyObject_GetAttrString(obj, "keywords");
4945 if (tmp == NULL) goto failed;
4946 if (!PyList_Check(tmp)) {
4947 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4948 goto failed;
4949 }
4950 len = PyList_GET_SIZE(tmp);
4951 keywords = asdl_seq_new(len, arena);
4952 if (keywords == NULL) goto failed;
4953 for (i = 0; i < len; i++) {
4954 keyword_ty value;
4955 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4956 if (res != 0) goto failed;
4957 asdl_seq_SET(keywords, i, value);
4958 }
4959 Py_XDECREF(tmp);
4960 tmp = NULL;
4961 } else {
4962 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4963 return 1;
4964 }
4965 if (PyObject_HasAttrString(obj, "starargs")) {
4966 int res;
4967 tmp = PyObject_GetAttrString(obj, "starargs");
4968 if (tmp == NULL) goto failed;
4969 res = obj2ast_expr(tmp, &starargs, arena);
4970 if (res != 0) goto failed;
4971 Py_XDECREF(tmp);
4972 tmp = NULL;
4973 } else {
4974 starargs = NULL;
4975 }
4976 if (PyObject_HasAttrString(obj, "kwargs")) {
4977 int res;
4978 tmp = PyObject_GetAttrString(obj, "kwargs");
4979 if (tmp == NULL) goto failed;
4980 res = obj2ast_expr(tmp, &kwargs, arena);
4981 if (res != 0) goto failed;
4982 Py_XDECREF(tmp);
4983 tmp = NULL;
4984 } else {
4985 kwargs = NULL;
4986 }
4987 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4988 col_offset, arena);
4989 if (*out == NULL) goto failed;
4990 return 0;
4991 }
4992 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4993 expr_ty value;
4994
4995 if (PyObject_HasAttrString(obj, "value")) {
4996 int res;
4997 tmp = PyObject_GetAttrString(obj, "value");
4998 if (tmp == NULL) goto failed;
4999 res = obj2ast_expr(tmp, &value, arena);
5000 if (res != 0) goto failed;
5001 Py_XDECREF(tmp);
5002 tmp = NULL;
5003 } else {
5004 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
5005 return 1;
5006 }
5007 *out = Repr(value, lineno, col_offset, arena);
5008 if (*out == NULL) goto failed;
5009 return 0;
5010 }
5011 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
5012 object n;
5013
5014 if (PyObject_HasAttrString(obj, "n")) {
5015 int res;
5016 tmp = PyObject_GetAttrString(obj, "n");
5017 if (tmp == NULL) goto failed;
5018 res = obj2ast_object(tmp, &n, arena);
5019 if (res != 0) goto failed;
5020 Py_XDECREF(tmp);
5021 tmp = NULL;
5022 } else {
5023 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5024 return 1;
5025 }
5026 *out = Num(n, lineno, col_offset, arena);
5027 if (*out == NULL) goto failed;
5028 return 0;
5029 }
5030 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5031 string s;
5032
5033 if (PyObject_HasAttrString(obj, "s")) {
5034 int res;
5035 tmp = PyObject_GetAttrString(obj, "s");
5036 if (tmp == NULL) goto failed;
5037 res = obj2ast_string(tmp, &s, arena);
5038 if (res != 0) goto failed;
5039 Py_XDECREF(tmp);
5040 tmp = NULL;
5041 } else {
5042 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5043 return 1;
5044 }
5045 *out = Str(s, lineno, col_offset, arena);
5046 if (*out == NULL) goto failed;
5047 return 0;
5048 }
5049 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5050 expr_ty value;
5051 identifier attr;
5052 expr_context_ty ctx;
5053
5054 if (PyObject_HasAttrString(obj, "value")) {
5055 int res;
5056 tmp = PyObject_GetAttrString(obj, "value");
5057 if (tmp == NULL) goto failed;
5058 res = obj2ast_expr(tmp, &value, arena);
5059 if (res != 0) goto failed;
5060 Py_XDECREF(tmp);
5061 tmp = NULL;
5062 } else {
5063 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5064 return 1;
5065 }
5066 if (PyObject_HasAttrString(obj, "attr")) {
5067 int res;
5068 tmp = PyObject_GetAttrString(obj, "attr");
5069 if (tmp == NULL) goto failed;
5070 res = obj2ast_identifier(tmp, &attr, arena);
5071 if (res != 0) goto failed;
5072 Py_XDECREF(tmp);
5073 tmp = NULL;
5074 } else {
5075 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5076 return 1;
5077 }
5078 if (PyObject_HasAttrString(obj, "ctx")) {
5079 int res;
5080 tmp = PyObject_GetAttrString(obj, "ctx");
5081 if (tmp == NULL) goto failed;
5082 res = obj2ast_expr_context(tmp, &ctx, arena);
5083 if (res != 0) goto failed;
5084 Py_XDECREF(tmp);
5085 tmp = NULL;
5086 } else {
5087 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5088 return 1;
5089 }
5090 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5091 if (*out == NULL) goto failed;
5092 return 0;
5093 }
5094 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5095 expr_ty value;
5096 slice_ty slice;
5097 expr_context_ty ctx;
5098
5099 if (PyObject_HasAttrString(obj, "value")) {
5100 int res;
5101 tmp = PyObject_GetAttrString(obj, "value");
5102 if (tmp == NULL) goto failed;
5103 res = obj2ast_expr(tmp, &value, arena);
5104 if (res != 0) goto failed;
5105 Py_XDECREF(tmp);
5106 tmp = NULL;
5107 } else {
5108 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5109 return 1;
5110 }
5111 if (PyObject_HasAttrString(obj, "slice")) {
5112 int res;
5113 tmp = PyObject_GetAttrString(obj, "slice");
5114 if (tmp == NULL) goto failed;
5115 res = obj2ast_slice(tmp, &slice, arena);
5116 if (res != 0) goto failed;
5117 Py_XDECREF(tmp);
5118 tmp = NULL;
5119 } else {
5120 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5121 return 1;
5122 }
5123 if (PyObject_HasAttrString(obj, "ctx")) {
5124 int res;
5125 tmp = PyObject_GetAttrString(obj, "ctx");
5126 if (tmp == NULL) goto failed;
5127 res = obj2ast_expr_context(tmp, &ctx, arena);
5128 if (res != 0) goto failed;
5129 Py_XDECREF(tmp);
5130 tmp = NULL;
5131 } else {
5132 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5133 return 1;
5134 }
5135 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5136 if (*out == NULL) goto failed;
5137 return 0;
5138 }
5139 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5140 identifier id;
5141 expr_context_ty ctx;
5142
5143 if (PyObject_HasAttrString(obj, "id")) {
5144 int res;
5145 tmp = PyObject_GetAttrString(obj, "id");
5146 if (tmp == NULL) goto failed;
5147 res = obj2ast_identifier(tmp, &id, arena);
5148 if (res != 0) goto failed;
5149 Py_XDECREF(tmp);
5150 tmp = NULL;
5151 } else {
5152 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5153 return 1;
5154 }
5155 if (PyObject_HasAttrString(obj, "ctx")) {
5156 int res;
5157 tmp = PyObject_GetAttrString(obj, "ctx");
5158 if (tmp == NULL) goto failed;
5159 res = obj2ast_expr_context(tmp, &ctx, arena);
5160 if (res != 0) goto failed;
5161 Py_XDECREF(tmp);
5162 tmp = NULL;
5163 } else {
5164 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5165 return 1;
5166 }
5167 *out = Name(id, ctx, lineno, col_offset, arena);
5168 if (*out == NULL) goto failed;
5169 return 0;
5170 }
5171 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5172 asdl_seq* elts;
5173 expr_context_ty ctx;
5174
5175 if (PyObject_HasAttrString(obj, "elts")) {
5176 int res;
5177 Py_ssize_t len;
5178 Py_ssize_t i;
5179 tmp = PyObject_GetAttrString(obj, "elts");
5180 if (tmp == NULL) goto failed;
5181 if (!PyList_Check(tmp)) {
5182 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5183 goto failed;
5184 }
5185 len = PyList_GET_SIZE(tmp);
5186 elts = asdl_seq_new(len, arena);
5187 if (elts == NULL) goto failed;
5188 for (i = 0; i < len; i++) {
5189 expr_ty value;
5190 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5191 if (res != 0) goto failed;
5192 asdl_seq_SET(elts, i, value);
5193 }
5194 Py_XDECREF(tmp);
5195 tmp = NULL;
5196 } else {
5197 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5198 return 1;
5199 }
5200 if (PyObject_HasAttrString(obj, "ctx")) {
5201 int res;
5202 tmp = PyObject_GetAttrString(obj, "ctx");
5203 if (tmp == NULL) goto failed;
5204 res = obj2ast_expr_context(tmp, &ctx, arena);
5205 if (res != 0) goto failed;
5206 Py_XDECREF(tmp);
5207 tmp = NULL;
5208 } else {
5209 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5210 return 1;
5211 }
5212 *out = List(elts, ctx, lineno, col_offset, arena);
5213 if (*out == NULL) goto failed;
5214 return 0;
5215 }
5216 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5217 asdl_seq* elts;
5218 expr_context_ty ctx;
5219
5220 if (PyObject_HasAttrString(obj, "elts")) {
5221 int res;
5222 Py_ssize_t len;
5223 Py_ssize_t i;
5224 tmp = PyObject_GetAttrString(obj, "elts");
5225 if (tmp == NULL) goto failed;
5226 if (!PyList_Check(tmp)) {
5227 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5228 goto failed;
5229 }
5230 len = PyList_GET_SIZE(tmp);
5231 elts = asdl_seq_new(len, arena);
5232 if (elts == NULL) goto failed;
5233 for (i = 0; i < len; i++) {
5234 expr_ty value;
5235 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5236 if (res != 0) goto failed;
5237 asdl_seq_SET(elts, i, value);
5238 }
5239 Py_XDECREF(tmp);
5240 tmp = NULL;
5241 } else {
5242 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5243 return 1;
5244 }
5245 if (PyObject_HasAttrString(obj, "ctx")) {
5246 int res;
5247 tmp = PyObject_GetAttrString(obj, "ctx");
5248 if (tmp == NULL) goto failed;
5249 res = obj2ast_expr_context(tmp, &ctx, arena);
5250 if (res != 0) goto failed;
5251 Py_XDECREF(tmp);
5252 tmp = NULL;
5253 } else {
5254 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5255 return 1;
5256 }
5257 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5258 if (*out == NULL) goto failed;
5259 return 0;
5260 }
5261
5262 tmp = PyObject_Repr(obj);
5263 if (tmp == NULL) goto failed;
5264 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5265failed:
5266 Py_XDECREF(tmp);
5267 return 1;
5268}
5269
5270int
5271obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5272{
5273 PyObject* tmp = NULL;
5274
5275 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5276 *out = Load;
5277 return 0;
5278 }
5279 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5280 *out = Store;
5281 return 0;
5282 }
5283 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5284 *out = Del;
5285 return 0;
5286 }
5287 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5288 *out = AugLoad;
5289 return 0;
5290 }
5291 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5292 *out = AugStore;
5293 return 0;
5294 }
5295 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5296 *out = Param;
5297 return 0;
5298 }
5299
5300 tmp = PyObject_Repr(obj);
5301 if (tmp == NULL) goto failed;
5302 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5303failed:
5304 Py_XDECREF(tmp);
5305 return 1;
5306}
5307
5308int
5309obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5310{
5311 PyObject* tmp = NULL;
5312
5313
5314 if (obj == Py_None) {
5315 *out = NULL;
5316 return 0;
5317 }
5318 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5319
5320 *out = Ellipsis(arena);
5321 if (*out == NULL) goto failed;
5322 return 0;
5323 }
5324 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5325 expr_ty lower;
5326 expr_ty upper;
5327 expr_ty step;
5328
5329 if (PyObject_HasAttrString(obj, "lower")) {
5330 int res;
5331 tmp = PyObject_GetAttrString(obj, "lower");
5332 if (tmp == NULL) goto failed;
5333 res = obj2ast_expr(tmp, &lower, arena);
5334 if (res != 0) goto failed;
5335 Py_XDECREF(tmp);
5336 tmp = NULL;
5337 } else {
5338 lower = NULL;
5339 }
5340 if (PyObject_HasAttrString(obj, "upper")) {
5341 int res;
5342 tmp = PyObject_GetAttrString(obj, "upper");
5343 if (tmp == NULL) goto failed;
5344 res = obj2ast_expr(tmp, &upper, arena);
5345 if (res != 0) goto failed;
5346 Py_XDECREF(tmp);
5347 tmp = NULL;
5348 } else {
5349 upper = NULL;
5350 }
5351 if (PyObject_HasAttrString(obj, "step")) {
5352 int res;
5353 tmp = PyObject_GetAttrString(obj, "step");
5354 if (tmp == NULL) goto failed;
5355 res = obj2ast_expr(tmp, &step, arena);
5356 if (res != 0) goto failed;
5357 Py_XDECREF(tmp);
5358 tmp = NULL;
5359 } else {
5360 step = NULL;
5361 }
5362 *out = Slice(lower, upper, step, arena);
5363 if (*out == NULL) goto failed;
5364 return 0;
5365 }
5366 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5367 asdl_seq* dims;
5368
5369 if (PyObject_HasAttrString(obj, "dims")) {
5370 int res;
5371 Py_ssize_t len;
5372 Py_ssize_t i;
5373 tmp = PyObject_GetAttrString(obj, "dims");
5374 if (tmp == NULL) goto failed;
5375 if (!PyList_Check(tmp)) {
5376 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5377 goto failed;
5378 }
5379 len = PyList_GET_SIZE(tmp);
5380 dims = asdl_seq_new(len, arena);
5381 if (dims == NULL) goto failed;
5382 for (i = 0; i < len; i++) {
5383 slice_ty value;
5384 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5385 if (res != 0) goto failed;
5386 asdl_seq_SET(dims, i, value);
5387 }
5388 Py_XDECREF(tmp);
5389 tmp = NULL;
5390 } else {
5391 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5392 return 1;
5393 }
5394 *out = ExtSlice(dims, arena);
5395 if (*out == NULL) goto failed;
5396 return 0;
5397 }
5398 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5399 expr_ty value;
5400
5401 if (PyObject_HasAttrString(obj, "value")) {
5402 int res;
5403 tmp = PyObject_GetAttrString(obj, "value");
5404 if (tmp == NULL) goto failed;
5405 res = obj2ast_expr(tmp, &value, arena);
5406 if (res != 0) goto failed;
5407 Py_XDECREF(tmp);
5408 tmp = NULL;
5409 } else {
5410 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5411 return 1;
5412 }
5413 *out = Index(value, arena);
5414 if (*out == NULL) goto failed;
5415 return 0;
5416 }
5417
5418 tmp = PyObject_Repr(obj);
5419 if (tmp == NULL) goto failed;
5420 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5421failed:
5422 Py_XDECREF(tmp);
5423 return 1;
5424}
5425
5426int
5427obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5428{
5429 PyObject* tmp = NULL;
5430
5431 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5432 *out = And;
5433 return 0;
5434 }
5435 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5436 *out = Or;
5437 return 0;
5438 }
5439
5440 tmp = PyObject_Repr(obj);
5441 if (tmp == NULL) goto failed;
5442 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5443failed:
5444 Py_XDECREF(tmp);
5445 return 1;
5446}
5447
5448int
5449obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5450{
5451 PyObject* tmp = NULL;
5452
5453 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5454 *out = Add;
5455 return 0;
5456 }
5457 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5458 *out = Sub;
5459 return 0;
5460 }
5461 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5462 *out = Mult;
5463 return 0;
5464 }
5465 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5466 *out = Div;
5467 return 0;
5468 }
5469 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5470 *out = Mod;
5471 return 0;
5472 }
5473 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5474 *out = Pow;
5475 return 0;
5476 }
5477 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5478 *out = LShift;
5479 return 0;
5480 }
5481 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5482 *out = RShift;
5483 return 0;
5484 }
5485 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5486 *out = BitOr;
5487 return 0;
5488 }
5489 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5490 *out = BitXor;
5491 return 0;
5492 }
5493 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5494 *out = BitAnd;
5495 return 0;
5496 }
5497 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5498 *out = FloorDiv;
5499 return 0;
5500 }
5501
5502 tmp = PyObject_Repr(obj);
5503 if (tmp == NULL) goto failed;
5504 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5505failed:
5506 Py_XDECREF(tmp);
5507 return 1;
5508}
5509
5510int
5511obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5512{
5513 PyObject* tmp = NULL;
5514
5515 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5516 *out = Invert;
5517 return 0;
5518 }
5519 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5520 *out = Not;
5521 return 0;
5522 }
5523 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5524 *out = UAdd;
5525 return 0;
5526 }
5527 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5528 *out = USub;
5529 return 0;
5530 }
5531
5532 tmp = PyObject_Repr(obj);
5533 if (tmp == NULL) goto failed;
5534 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5535failed:
5536 Py_XDECREF(tmp);
5537 return 1;
5538}
5539
5540int
5541obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5542{
5543 PyObject* tmp = NULL;
5544
5545 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5546 *out = Eq;
5547 return 0;
5548 }
5549 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5550 *out = NotEq;
5551 return 0;
5552 }
5553 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5554 *out = Lt;
5555 return 0;
5556 }
5557 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5558 *out = LtE;
5559 return 0;
5560 }
5561 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5562 *out = Gt;
5563 return 0;
5564 }
5565 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5566 *out = GtE;
5567 return 0;
5568 }
5569 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5570 *out = Is;
5571 return 0;
5572 }
5573 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5574 *out = IsNot;
5575 return 0;
5576 }
5577 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5578 *out = In;
5579 return 0;
5580 }
5581 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5582 *out = NotIn;
5583 return 0;
5584 }
5585
5586 tmp = PyObject_Repr(obj);
5587 if (tmp == NULL) goto failed;
5588 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5589failed:
5590 Py_XDECREF(tmp);
5591 return 1;
5592}
5593
5594int
5595obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5596{
5597 PyObject* tmp = NULL;
5598 expr_ty target;
5599 expr_ty iter;
5600 asdl_seq* ifs;
5601
5602 if (PyObject_HasAttrString(obj, "target")) {
5603 int res;
5604 tmp = PyObject_GetAttrString(obj, "target");
5605 if (tmp == NULL) goto failed;
5606 res = obj2ast_expr(tmp, &target, arena);
5607 if (res != 0) goto failed;
5608 Py_XDECREF(tmp);
5609 tmp = NULL;
5610 } else {
5611 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5612 return 1;
5613 }
5614 if (PyObject_HasAttrString(obj, "iter")) {
5615 int res;
5616 tmp = PyObject_GetAttrString(obj, "iter");
5617 if (tmp == NULL) goto failed;
5618 res = obj2ast_expr(tmp, &iter, arena);
5619 if (res != 0) goto failed;
5620 Py_XDECREF(tmp);
5621 tmp = NULL;
5622 } else {
5623 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5624 return 1;
5625 }
5626 if (PyObject_HasAttrString(obj, "ifs")) {
5627 int res;
5628 Py_ssize_t len;
5629 Py_ssize_t i;
5630 tmp = PyObject_GetAttrString(obj, "ifs");
5631 if (tmp == NULL) goto failed;
5632 if (!PyList_Check(tmp)) {
5633 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5634 goto failed;
5635 }
5636 len = PyList_GET_SIZE(tmp);
5637 ifs = asdl_seq_new(len, arena);
5638 if (ifs == NULL) goto failed;
5639 for (i = 0; i < len; i++) {
5640 expr_ty value;
5641 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5642 if (res != 0) goto failed;
5643 asdl_seq_SET(ifs, i, value);
5644 }
5645 Py_XDECREF(tmp);
5646 tmp = NULL;
5647 } else {
5648 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5649 return 1;
5650 }
5651 *out = comprehension(target, iter, ifs, arena);
5652 return 0;
5653failed:
5654 Py_XDECREF(tmp);
5655 return 1;
5656}
5657
5658int
5659obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5660{
5661 PyObject* tmp = NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00005662
Georg Brandlfc8eef32008-03-28 12:11:56 +00005663 int lineno;
5664 int col_offset;
5665
Georg Brandla48f3ab2008-03-30 06:40:17 +00005666 if (obj == Py_None) {
5667 *out = NULL;
5668 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005669 }
5670 if (PyObject_HasAttrString(obj, "lineno")) {
5671 int res;
5672 tmp = PyObject_GetAttrString(obj, "lineno");
5673 if (tmp == NULL) goto failed;
5674 res = obj2ast_int(tmp, &lineno, arena);
5675 if (res != 0) goto failed;
5676 Py_XDECREF(tmp);
5677 tmp = NULL;
5678 } else {
5679 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5680 return 1;
5681 }
5682 if (PyObject_HasAttrString(obj, "col_offset")) {
5683 int res;
5684 tmp = PyObject_GetAttrString(obj, "col_offset");
5685 if (tmp == NULL) goto failed;
5686 res = obj2ast_int(tmp, &col_offset, arena);
5687 if (res != 0) goto failed;
5688 Py_XDECREF(tmp);
5689 tmp = NULL;
5690 } else {
5691 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5692 return 1;
5693 }
Georg Brandla48f3ab2008-03-30 06:40:17 +00005694 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
5695 expr_ty type;
5696 expr_ty name;
5697 asdl_seq* body;
5698
5699 if (PyObject_HasAttrString(obj, "type")) {
5700 int res;
5701 tmp = PyObject_GetAttrString(obj, "type");
5702 if (tmp == NULL) goto failed;
5703 res = obj2ast_expr(tmp, &type, arena);
5704 if (res != 0) goto failed;
5705 Py_XDECREF(tmp);
5706 tmp = NULL;
5707 } else {
5708 type = NULL;
5709 }
5710 if (PyObject_HasAttrString(obj, "name")) {
5711 int res;
5712 tmp = PyObject_GetAttrString(obj, "name");
5713 if (tmp == NULL) goto failed;
5714 res = obj2ast_expr(tmp, &name, arena);
5715 if (res != 0) goto failed;
5716 Py_XDECREF(tmp);
5717 tmp = NULL;
5718 } else {
5719 name = NULL;
5720 }
5721 if (PyObject_HasAttrString(obj, "body")) {
5722 int res;
5723 Py_ssize_t len;
5724 Py_ssize_t i;
5725 tmp = PyObject_GetAttrString(obj, "body");
5726 if (tmp == NULL) goto failed;
5727 if (!PyList_Check(tmp)) {
5728 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5729 goto failed;
5730 }
5731 len = PyList_GET_SIZE(tmp);
5732 body = asdl_seq_new(len, arena);
5733 if (body == NULL) goto failed;
5734 for (i = 0; i < len; i++) {
5735 stmt_ty value;
5736 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5737 if (res != 0) goto failed;
5738 asdl_seq_SET(body, i, value);
5739 }
5740 Py_XDECREF(tmp);
5741 tmp = NULL;
5742 } else {
5743 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
5744 return 1;
5745 }
5746 *out = ExceptHandler(type, name, body, lineno, col_offset,
5747 arena);
5748 if (*out == NULL) goto failed;
5749 return 0;
5750 }
5751
5752 tmp = PyObject_Repr(obj);
5753 if (tmp == NULL) goto failed;
5754 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005755failed:
5756 Py_XDECREF(tmp);
5757 return 1;
5758}
5759
5760int
5761obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5762{
5763 PyObject* tmp = NULL;
5764 asdl_seq* args;
5765 identifier vararg;
5766 identifier kwarg;
5767 asdl_seq* defaults;
5768
5769 if (PyObject_HasAttrString(obj, "args")) {
5770 int res;
5771 Py_ssize_t len;
5772 Py_ssize_t i;
5773 tmp = PyObject_GetAttrString(obj, "args");
5774 if (tmp == NULL) goto failed;
5775 if (!PyList_Check(tmp)) {
5776 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5777 goto failed;
5778 }
5779 len = PyList_GET_SIZE(tmp);
5780 args = asdl_seq_new(len, arena);
5781 if (args == NULL) goto failed;
5782 for (i = 0; i < len; i++) {
5783 expr_ty value;
5784 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5785 if (res != 0) goto failed;
5786 asdl_seq_SET(args, i, value);
5787 }
5788 Py_XDECREF(tmp);
5789 tmp = NULL;
5790 } else {
5791 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5792 return 1;
5793 }
5794 if (PyObject_HasAttrString(obj, "vararg")) {
5795 int res;
5796 tmp = PyObject_GetAttrString(obj, "vararg");
5797 if (tmp == NULL) goto failed;
5798 res = obj2ast_identifier(tmp, &vararg, arena);
5799 if (res != 0) goto failed;
5800 Py_XDECREF(tmp);
5801 tmp = NULL;
5802 } else {
5803 vararg = NULL;
5804 }
5805 if (PyObject_HasAttrString(obj, "kwarg")) {
5806 int res;
5807 tmp = PyObject_GetAttrString(obj, "kwarg");
5808 if (tmp == NULL) goto failed;
5809 res = obj2ast_identifier(tmp, &kwarg, arena);
5810 if (res != 0) goto failed;
5811 Py_XDECREF(tmp);
5812 tmp = NULL;
5813 } else {
5814 kwarg = NULL;
5815 }
5816 if (PyObject_HasAttrString(obj, "defaults")) {
5817 int res;
5818 Py_ssize_t len;
5819 Py_ssize_t i;
5820 tmp = PyObject_GetAttrString(obj, "defaults");
5821 if (tmp == NULL) goto failed;
5822 if (!PyList_Check(tmp)) {
5823 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5824 goto failed;
5825 }
5826 len = PyList_GET_SIZE(tmp);
5827 defaults = asdl_seq_new(len, arena);
5828 if (defaults == NULL) goto failed;
5829 for (i = 0; i < len; i++) {
5830 expr_ty value;
5831 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5832 if (res != 0) goto failed;
5833 asdl_seq_SET(defaults, i, value);
5834 }
5835 Py_XDECREF(tmp);
5836 tmp = NULL;
5837 } else {
5838 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5839 return 1;
5840 }
5841 *out = arguments(args, vararg, kwarg, defaults, arena);
5842 return 0;
5843failed:
5844 Py_XDECREF(tmp);
5845 return 1;
5846}
5847
5848int
5849obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5850{
5851 PyObject* tmp = NULL;
5852 identifier arg;
5853 expr_ty value;
5854
5855 if (PyObject_HasAttrString(obj, "arg")) {
5856 int res;
5857 tmp = PyObject_GetAttrString(obj, "arg");
5858 if (tmp == NULL) goto failed;
5859 res = obj2ast_identifier(tmp, &arg, arena);
5860 if (res != 0) goto failed;
5861 Py_XDECREF(tmp);
5862 tmp = NULL;
5863 } else {
5864 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5865 return 1;
5866 }
5867 if (PyObject_HasAttrString(obj, "value")) {
5868 int res;
5869 tmp = PyObject_GetAttrString(obj, "value");
5870 if (tmp == NULL) goto failed;
5871 res = obj2ast_expr(tmp, &value, arena);
5872 if (res != 0) goto failed;
5873 Py_XDECREF(tmp);
5874 tmp = NULL;
5875 } else {
5876 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5877 return 1;
5878 }
5879 *out = keyword(arg, value, arena);
5880 return 0;
5881failed:
5882 Py_XDECREF(tmp);
5883 return 1;
5884}
5885
5886int
5887obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5888{
5889 PyObject* tmp = NULL;
5890 identifier name;
5891 identifier asname;
5892
5893 if (PyObject_HasAttrString(obj, "name")) {
5894 int res;
5895 tmp = PyObject_GetAttrString(obj, "name");
5896 if (tmp == NULL) goto failed;
5897 res = obj2ast_identifier(tmp, &name, arena);
5898 if (res != 0) goto failed;
5899 Py_XDECREF(tmp);
5900 tmp = NULL;
5901 } else {
5902 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5903 return 1;
5904 }
5905 if (PyObject_HasAttrString(obj, "asname")) {
5906 int res;
5907 tmp = PyObject_GetAttrString(obj, "asname");
5908 if (tmp == NULL) goto failed;
5909 res = obj2ast_identifier(tmp, &asname, arena);
5910 if (res != 0) goto failed;
5911 Py_XDECREF(tmp);
5912 tmp = NULL;
5913 } else {
5914 asname = NULL;
5915 }
5916 *out = alias(name, asname, arena);
5917 return 0;
5918failed:
5919 Py_XDECREF(tmp);
5920 return 1;
5921}
5922
5923
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005924PyMODINIT_FUNC
5925init_ast(void)
5926{
5927 PyObject *m, *d;
5928 if (!init_types()) return;
5929 m = Py_InitModule3("_ast", NULL, NULL);
5930 if (!m) return;
5931 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00005932 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005933 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5934 return;
Georg Brandlc52ed592008-03-30 07:01:47 +00005935 if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00005936 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00005937 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5938 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5939 return;
5940 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5941 < 0) return;
5942 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5943 0) return;
5944 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5945 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5946 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5947 < 0) return;
5948 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5949 return;
5950 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5951 return;
5952 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5953 return;
5954 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5955 return;
5956 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5957 0) return;
5958 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5959 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5960 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5961 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5962 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5963 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5964 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5965 0) return;
5966 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5967 0) return;
5968 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5969 return;
5970 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5971 return;
5972 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5973 0) return;
5974 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5975 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5976 return;
5977 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5978 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5979 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5980 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5981 return;
5982 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5983 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5984 return;
5985 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5986 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5987 return;
5988 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
5989 return;
5990 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
5991 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
5992 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
5993 return;
5994 if (PyDict_SetItemString(d, "GeneratorExp",
5995 (PyObject*)GeneratorExp_type) < 0) return;
5996 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
5997 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
5998 return;
5999 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
6000 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
6001 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
6002 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
6003 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
6004 0) return;
6005 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
6006 0) return;
6007 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
6008 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
6009 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
6010 if (PyDict_SetItemString(d, "expr_context",
6011 (PyObject*)expr_context_type) < 0) return;
6012 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
6013 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
6014 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
6015 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
6016 return;
6017 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
6018 return;
6019 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
6020 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
6021 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6022 return;
6023 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6024 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6025 return;
6026 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6027 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6028 return;
6029 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6030 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6031 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6032 return;
6033 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6034 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6035 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6036 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6037 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6038 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6039 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6040 return;
6041 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6042 return;
6043 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6044 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6045 return;
6046 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6047 return;
6048 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6049 return;
6050 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6051 return;
6052 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6053 return;
6054 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6055 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6056 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6057 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6058 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6059 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6060 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6061 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6062 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6063 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6064 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6065 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6066 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6067 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6068 if (PyDict_SetItemString(d, "comprehension",
6069 (PyObject*)comprehension_type) < 0) return;
6070 if (PyDict_SetItemString(d, "excepthandler",
6071 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006072 if (PyDict_SetItemString(d, "ExceptHandler",
6073 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006074 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6075 0) return;
6076 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6077 return;
6078 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006079}
6080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006081
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006082PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006083{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006084 init_types();
6085 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006086}
6087
Georg Brandlf2bfd542008-03-29 13:24:23 +00006088/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6089mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006090{
6091 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006092 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6093 (PyObject*)Interactive_type};
6094 char *req_name[] = {"Module", "Expression", "Interactive"};
6095 assert(0 <= mode && mode <= 2);
6096
Georg Brandlfc8eef32008-03-28 12:11:56 +00006097 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006098
6099 if (!PyObject_IsInstance(ast, req_type[mode])) {
6100 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6101 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006102 return NULL;
6103 }
6104 if (obj2ast_mod(ast, &res, arena) != 0)
6105 return NULL;
6106 else
6107 return res;
6108}
6109
6110int PyAST_Check(PyObject* obj)
6111{
6112 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006113 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006114}
6115
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006116