blob: c44ce383c6859b6e58d4813da443ac5dfbbf6e10 [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)) {
389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes either 0 or "
390 "%d positional argument%s", Py_TYPE(self)->tp_name,
391 numfields, numfields == 1 ? "" : "s");
392 res = -1;
393 goto cleanup;
394 }
395 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
396 /* cannot be reached when fields is NULL */
397 PyObject *name = PySequence_GetItem(fields, i);
398 if (!name) {
399 res = -1;
400 goto cleanup;
401 }
402 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
403 Py_DECREF(name);
404 if (res < 0)
405 goto cleanup;
406 }
407 }
408 if (kw) {
409 i = 0; /* needed by PyDict_Next */
410 while (PyDict_Next(kw, &i, &key, &value)) {
411 res = PyObject_SetAttr(self, key, value);
412 if (res < 0)
413 goto cleanup;
414 }
415 }
416 cleanup:
417 Py_XDECREF(fields);
418 return res;
419}
420
421static PyTypeObject AST_type = {
422 PyVarObject_HEAD_INIT(&PyType_Type, 0)
423 "AST",
424 sizeof(PyObject),
425 0,
426 0, /* tp_dealloc */
427 0, /* tp_print */
428 0, /* tp_getattr */
429 0, /* tp_setattr */
430 0, /* tp_compare */
431 0, /* tp_repr */
432 0, /* tp_as_number */
433 0, /* tp_as_sequence */
434 0, /* tp_as_mapping */
435 0, /* tp_hash */
436 0, /* tp_call */
437 0, /* tp_str */
438 PyObject_GenericGetAttr, /* tp_getattro */
439 PyObject_GenericSetAttr, /* tp_setattro */
440 0, /* tp_as_buffer */
441 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
442 0, /* tp_doc */
443 0, /* tp_traverse */
444 0, /* tp_clear */
445 0, /* tp_richcompare */
446 0, /* tp_weaklistoffset */
447 0, /* tp_iter */
448 0, /* tp_iternext */
449 0, /* tp_methods */
450 0, /* tp_members */
451 0, /* tp_getset */
452 0, /* tp_base */
453 0, /* tp_dict */
454 0, /* tp_descr_get */
455 0, /* tp_descr_set */
456 0, /* tp_dictoffset */
457 (initproc)ast_type_init, /* tp_init */
458 PyType_GenericAlloc, /* tp_alloc */
459 PyType_GenericNew, /* tp_new */
460 PyObject_Del, /* tp_free */
461};
462
463
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000464static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
465{
466 PyObject *fnames, *result;
467 int i;
468 if (num_fields) {
469 fnames = PyTuple_New(num_fields);
470 if (!fnames) return NULL;
471 } else {
472 fnames = Py_None;
473 Py_INCREF(Py_None);
474 }
475 for(i=0; i < num_fields; i++) {
476 PyObject *field = PyString_FromString(fields[i]);
477 if (!field) {
478 Py_DECREF(fnames);
479 return NULL;
480 }
481 PyTuple_SET_ITEM(fnames, i, field);
482 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000483 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000484 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000485 Py_DECREF(fnames);
486 return (PyTypeObject*)result;
487}
488
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000489static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
490{
Neal Norwitz19379f12006-04-03 04:50:58 +0000491 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000492 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000493 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000494 for(i = 0; i < num_fields; i++) {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000495 s = PyString_FromString(attrs[i]);
496 if (!s) {
497 Py_DECREF(l);
498 return 0;
499 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000500 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000501 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000502 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
503 Py_DECREF(l);
504 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000505}
506
Georg Brandlfc8eef32008-03-28 12:11:56 +0000507/* Conversion AST -> Python */
508
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000509static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
510{
511 int i, n = asdl_seq_LEN(seq);
512 PyObject *result = PyList_New(n);
513 PyObject *value;
514 if (!result)
515 return NULL;
516 for (i = 0; i < n; i++) {
517 value = func(asdl_seq_GET(seq, i));
518 if (!value) {
519 Py_DECREF(result);
520 return NULL;
521 }
522 PyList_SET_ITEM(result, i, value);
523 }
524 return result;
525}
526
527static PyObject* ast2obj_object(void *o)
528{
529 if (!o)
530 o = Py_None;
531 Py_INCREF((PyObject*)o);
532 return (PyObject*)o;
533}
534#define ast2obj_identifier ast2obj_object
535#define ast2obj_string ast2obj_object
536static PyObject* ast2obj_bool(bool b)
537{
538 return PyBool_FromLong(b);
539}
540
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000541static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000542{
543 return PyInt_FromLong(b);
544}
545
Georg Brandlfc8eef32008-03-28 12:11:56 +0000546/* Conversion Python -> AST */
547
548static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
549{
550 if (obj == Py_None)
551 obj = NULL;
552 if (obj)
553 PyArena_AddPyObject(arena, obj);
554 Py_XINCREF(obj);
555 *out = obj;
556 return 0;
557}
558
559#define obj2ast_identifier obj2ast_object
560#define obj2ast_string obj2ast_object
561
562static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
563{
564 int i;
565 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
566 PyObject *s = PyObject_Repr(obj);
567 if (s == NULL) return 1;
568 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
569 PyString_AS_STRING(s));
570 Py_DECREF(s);
571 return 1;
572 }
573
574 i = (int)PyLong_AsLong(obj);
575 if (i == -1 && PyErr_Occurred())
576 return 1;
577 *out = i;
578 return 0;
579}
580
581static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
582{
583 if (!PyBool_Check(obj)) {
584 PyObject *s = PyObject_Repr(obj);
585 if (s == NULL) return 1;
586 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
587 PyString_AS_STRING(s));
588 Py_DECREF(s);
589 return 1;
590 }
591
592 *out = (obj == Py_True);
593 return 0;
594}
595
596
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000597static int init_types(void)
598{
Neal Norwitz19379f12006-04-03 04:50:58 +0000599 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000600 if (initialized) return 1;
Georg Brandlc52ed592008-03-30 07:01:47 +0000601 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000602 if (!mod_type) return 0;
603 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000604 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000605 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000606 Interactive_type = make_type("Interactive", mod_type,
607 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000608 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000609 Expression_type = make_type("Expression", mod_type, Expression_fields,
610 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000611 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000612 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000613 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000614 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000615 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000616 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000617 FunctionDef_type = make_type("FunctionDef", stmt_type,
618 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000619 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000620 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000621 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000622 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000623 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000624 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000625 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000626 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000627 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000628 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000629 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000630 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000631 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000632 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000633 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000634 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000635 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000636 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000637 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000638 With_type = make_type("With", stmt_type, With_fields, 3);
639 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000640 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000641 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000642 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
645 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000646 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000647 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000648 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000649 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000652 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000653 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000654 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000655 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000656 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000657 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000658 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000659 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000660 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000661 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000662 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000663 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000664 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000665 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000666 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000668 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000669 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000670 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000671 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000672 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000673 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000674 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000675 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000676 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000677 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000678 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000679 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000680 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000681 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000683 GeneratorExp_type = make_type("GeneratorExp", expr_type,
684 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000686 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000690 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000694 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000698 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000702 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000706 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000708 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!expr_context_type) return 0;
710 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000711 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000712 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000713 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000714 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000715 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000716 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000717 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000719 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000721 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000723 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000725 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000727 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000729 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000731 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000733 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000734 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000735 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!slice_type) return 0;
737 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000738 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000740 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000744 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000746 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!boolop_type) return 0;
748 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000749 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000750 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000751 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000752 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000753 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000754 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000755 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000756 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000757 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!operator_type) return 0;
759 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000760 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000764 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000768 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000770 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000772 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000776 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000777 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000778 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000779 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000780 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000782 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000783 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000784 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000785 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000786 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000787 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000788 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000789 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000790 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000791 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000792 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000793 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000794 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000795 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000796 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000797 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000798 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000799 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000800 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000801 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000802 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000803 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000804 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000805 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000806 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000807 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000808 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!unaryop_type) return 0;
810 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000811 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000812 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000813 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000814 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000815 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000816 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000817 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000818 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000819 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000820 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000821 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000822 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000823 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000824 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000825 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000826 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000827 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!cmpop_type) return 0;
829 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000830 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000831 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000832 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000833 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000834 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000835 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000836 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000837 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000838 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000839 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000840 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000841 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000842 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000843 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000844 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000845 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000846 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000847 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000848 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000849 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000850 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000851 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000852 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000853 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000854 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000855 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000856 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000857 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000858 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000859 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000860 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000861 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000862 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000863 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000864 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000865 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000866 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000867 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000868 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000870 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000871 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000872 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000873 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000874 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000875 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
876 return 0;
877 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
878 ExceptHandler_fields, 3);
879 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000880 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000881 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000882 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000883 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000884 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000885 if (!alias_type) return 0;
886 initialized = 1;
887 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000888}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000889
Georg Brandlfc8eef32008-03-28 12:11:56 +0000890static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
891static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
892static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
893static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
894 arena);
895static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
896static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
897static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
898static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
899static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
900static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
901 arena);
902static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
903 arena);
904static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
905static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
906static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000909Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910{
911 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000912 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000913 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 p->kind = Module_kind;
916 p->v.Module.body = body;
917 return p;
918}
919
920mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000921Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922{
923 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000924 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000925 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927 p->kind = Interactive_kind;
928 p->v.Interactive.body = body;
929 return p;
930}
931
932mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000933Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934{
935 mod_ty p;
936 if (!body) {
937 PyErr_SetString(PyExc_ValueError,
938 "field body is required for Expression");
939 return NULL;
940 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000941 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000942 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944 p->kind = Expression_kind;
945 p->v.Expression.body = body;
946 return p;
947}
948
949mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000950Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
952 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000953 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000954 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 p->kind = Suite_kind;
957 p->v.Suite.body = body;
958 return p;
959}
960
961stmt_ty
962FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +0000963 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964{
965 stmt_ty p;
966 if (!name) {
967 PyErr_SetString(PyExc_ValueError,
968 "field name is required for FunctionDef");
969 return NULL;
970 }
971 if (!args) {
972 PyErr_SetString(PyExc_ValueError,
973 "field args is required for FunctionDef");
974 return NULL;
975 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000976 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000977 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 p->kind = FunctionDef_kind;
980 p->v.FunctionDef.name = name;
981 p->v.FunctionDef.args = args;
982 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +0000983 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000985 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 return p;
987}
988
989stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000990ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
991 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992{
993 stmt_ty p;
994 if (!name) {
995 PyErr_SetString(PyExc_ValueError,
996 "field name is required for ClassDef");
997 return NULL;
998 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000999 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001000 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 p->kind = ClassDef_kind;
1003 p->v.ClassDef.name = name;
1004 p->v.ClassDef.bases = bases;
1005 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001006 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001008 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 return p;
1010}
1011
1012stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001013Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014{
1015 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001016 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001017 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 p->kind = Return_kind;
1020 p->v.Return.value = value;
1021 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001022 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023 return p;
1024}
1025
1026stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001027Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028{
1029 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001030 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001031 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 p->kind = Delete_kind;
1034 p->v.Delete.targets = targets;
1035 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001036 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 return p;
1038}
1039
1040stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001041Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1042 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 stmt_ty p;
1045 if (!value) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field value is required for Assign");
1048 return NULL;
1049 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001050 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001051 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 p->kind = Assign_kind;
1054 p->v.Assign.targets = targets;
1055 p->v.Assign.value = value;
1056 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001057 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 return p;
1059}
1060
1061stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001062AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1063 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064{
1065 stmt_ty p;
1066 if (!target) {
1067 PyErr_SetString(PyExc_ValueError,
1068 "field target is required for AugAssign");
1069 return NULL;
1070 }
1071 if (!op) {
1072 PyErr_SetString(PyExc_ValueError,
1073 "field op is required for AugAssign");
1074 return NULL;
1075 }
1076 if (!value) {
1077 PyErr_SetString(PyExc_ValueError,
1078 "field value is required for AugAssign");
1079 return NULL;
1080 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001081 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001082 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 p->kind = AugAssign_kind;
1085 p->v.AugAssign.target = target;
1086 p->v.AugAssign.op = op;
1087 p->v.AugAssign.value = value;
1088 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001089 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 return p;
1091}
1092
1093stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001094Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1095 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096{
1097 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001098 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001099 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101 p->kind = Print_kind;
1102 p->v.Print.dest = dest;
1103 p->v.Print.values = values;
1104 p->v.Print.nl = nl;
1105 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001106 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107 return p;
1108}
1109
1110stmt_ty
1111For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001112 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113{
1114 stmt_ty p;
1115 if (!target) {
1116 PyErr_SetString(PyExc_ValueError,
1117 "field target is required for For");
1118 return NULL;
1119 }
1120 if (!iter) {
1121 PyErr_SetString(PyExc_ValueError,
1122 "field iter is required for For");
1123 return NULL;
1124 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001125 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001126 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 p->kind = For_kind;
1129 p->v.For.target = target;
1130 p->v.For.iter = iter;
1131 p->v.For.body = body;
1132 p->v.For.orelse = orelse;
1133 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001134 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135 return p;
1136}
1137
1138stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001139While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1140 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141{
1142 stmt_ty p;
1143 if (!test) {
1144 PyErr_SetString(PyExc_ValueError,
1145 "field test is required for While");
1146 return NULL;
1147 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001148 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001149 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 p->kind = While_kind;
1152 p->v.While.test = test;
1153 p->v.While.body = body;
1154 p->v.While.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 +00001161If(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 If");
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 = If_kind;
1174 p->v.If.test = test;
1175 p->v.If.body = body;
1176 p->v.If.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
Guido van Rossumc2e20742006-02-27 22:32:47 +00001183With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001184 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001185{
1186 stmt_ty p;
1187 if (!context_expr) {
1188 PyErr_SetString(PyExc_ValueError,
1189 "field context_expr is required for With");
1190 return NULL;
1191 }
1192 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001193 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001194 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001195 p->kind = With_kind;
1196 p->v.With.context_expr = context_expr;
1197 p->v.With.optional_vars = optional_vars;
1198 p->v.With.body = body;
1199 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001200 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001201 return p;
1202}
1203
1204stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001205Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1206 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207{
1208 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001209 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001210 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 p->kind = Raise_kind;
1213 p->v.Raise.type = type;
1214 p->v.Raise.inst = inst;
1215 p->v.Raise.tback = tback;
1216 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001217 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 return p;
1219}
1220
1221stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001222TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001223 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224{
1225 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001226 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001227 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 p->kind = TryExcept_kind;
1230 p->v.TryExcept.body = body;
1231 p->v.TryExcept.handlers = handlers;
1232 p->v.TryExcept.orelse = orelse;
1233 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001234 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 return p;
1236}
1237
1238stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001239TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1240 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241{
1242 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001243 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001244 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 p->kind = TryFinally_kind;
1247 p->v.TryFinally.body = body;
1248 p->v.TryFinally.finalbody = finalbody;
1249 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001250 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return p;
1252}
1253
1254stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001255Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256{
1257 stmt_ty p;
1258 if (!test) {
1259 PyErr_SetString(PyExc_ValueError,
1260 "field test is required for Assert");
1261 return NULL;
1262 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001263 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001264 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 p->kind = Assert_kind;
1267 p->v.Assert.test = test;
1268 p->v.Assert.msg = msg;
1269 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001270 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 return p;
1272}
1273
1274stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001275Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276{
1277 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001278 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001279 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 p->kind = Import_kind;
1282 p->v.Import.names = names;
1283 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001284 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 return p;
1286}
1287
1288stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001289ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1290 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291{
1292 stmt_ty p;
1293 if (!module) {
1294 PyErr_SetString(PyExc_ValueError,
1295 "field module is required for ImportFrom");
1296 return NULL;
1297 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001298 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001299 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 p->kind = ImportFrom_kind;
1302 p->v.ImportFrom.module = module;
1303 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001304 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 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 +00001311Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1312 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313{
1314 stmt_ty p;
1315 if (!body) {
1316 PyErr_SetString(PyExc_ValueError,
1317 "field body is required for Exec");
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 = Exec_kind;
1324 p->v.Exec.body = body;
1325 p->v.Exec.globals = globals;
1326 p->v.Exec.locals = locals;
1327 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 +00001333Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334{
1335 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001336 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001337 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001338 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001339 p->kind = Global_kind;
1340 p->v.Global.names = names;
1341 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001342 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 return p;
1344}
1345
1346stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001347Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348{
1349 stmt_ty p;
1350 if (!value) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "field value is required for Expr");
1353 return NULL;
1354 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001355 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001356 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 p->kind = Expr_kind;
1359 p->v.Expr.value = value;
1360 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001361 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 return p;
1363}
1364
1365stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001366Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367{
1368 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001369 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001370 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 p->kind = Pass_kind;
1373 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001374 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 return p;
1376}
1377
1378stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001379Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
1381 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001382 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001383 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001384 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 p->kind = Break_kind;
1386 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001387 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 return p;
1389}
1390
1391stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001392Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393{
1394 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001395 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001396 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 p->kind = Continue_kind;
1399 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001400 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 return p;
1402}
1403
1404expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001405BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1406 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407{
1408 expr_ty p;
1409 if (!op) {
1410 PyErr_SetString(PyExc_ValueError,
1411 "field op is required for BoolOp");
1412 return NULL;
1413 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001414 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001415 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 p->kind = BoolOp_kind;
1418 p->v.BoolOp.op = op;
1419 p->v.BoolOp.values = values;
1420 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001421 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 return p;
1423}
1424
1425expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001426BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1427 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428{
1429 expr_ty p;
1430 if (!left) {
1431 PyErr_SetString(PyExc_ValueError,
1432 "field left is required for BinOp");
1433 return NULL;
1434 }
1435 if (!op) {
1436 PyErr_SetString(PyExc_ValueError,
1437 "field op is required for BinOp");
1438 return NULL;
1439 }
1440 if (!right) {
1441 PyErr_SetString(PyExc_ValueError,
1442 "field right is required for BinOp");
1443 return NULL;
1444 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001445 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001446 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 p->kind = BinOp_kind;
1449 p->v.BinOp.left = left;
1450 p->v.BinOp.op = op;
1451 p->v.BinOp.right = right;
1452 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001453 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return p;
1455}
1456
1457expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001458UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1459 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460{
1461 expr_ty p;
1462 if (!op) {
1463 PyErr_SetString(PyExc_ValueError,
1464 "field op is required for UnaryOp");
1465 return NULL;
1466 }
1467 if (!operand) {
1468 PyErr_SetString(PyExc_ValueError,
1469 "field operand is required for UnaryOp");
1470 return NULL;
1471 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001472 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001473 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 p->kind = UnaryOp_kind;
1476 p->v.UnaryOp.op = op;
1477 p->v.UnaryOp.operand = operand;
1478 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001479 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480 return p;
1481}
1482
1483expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001484Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1485 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486{
1487 expr_ty p;
1488 if (!args) {
1489 PyErr_SetString(PyExc_ValueError,
1490 "field args is required for Lambda");
1491 return NULL;
1492 }
1493 if (!body) {
1494 PyErr_SetString(PyExc_ValueError,
1495 "field body is required for Lambda");
1496 return NULL;
1497 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001498 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001499 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 p->kind = Lambda_kind;
1502 p->v.Lambda.args = args;
1503 p->v.Lambda.body = body;
1504 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001505 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 return p;
1507}
1508
1509expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001510IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1511 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001512{
1513 expr_ty p;
1514 if (!test) {
1515 PyErr_SetString(PyExc_ValueError,
1516 "field test is required for IfExp");
1517 return NULL;
1518 }
1519 if (!body) {
1520 PyErr_SetString(PyExc_ValueError,
1521 "field body is required for IfExp");
1522 return NULL;
1523 }
1524 if (!orelse) {
1525 PyErr_SetString(PyExc_ValueError,
1526 "field orelse is required for IfExp");
1527 return NULL;
1528 }
1529 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001530 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001531 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001532 p->kind = IfExp_kind;
1533 p->v.IfExp.test = test;
1534 p->v.IfExp.body = body;
1535 p->v.IfExp.orelse = orelse;
1536 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001537 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001538 return p;
1539}
1540
1541expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001542Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1543 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544{
1545 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001546 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001547 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 p->kind = Dict_kind;
1550 p->v.Dict.keys = keys;
1551 p->v.Dict.values = values;
1552 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001553 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001554 return p;
1555}
1556
1557expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001558ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1559 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001560{
1561 expr_ty p;
1562 if (!elt) {
1563 PyErr_SetString(PyExc_ValueError,
1564 "field elt is required for ListComp");
1565 return NULL;
1566 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001567 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001568 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570 p->kind = ListComp_kind;
1571 p->v.ListComp.elt = elt;
1572 p->v.ListComp.generators = generators;
1573 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001574 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 return p;
1576}
1577
1578expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001579GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1580 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581{
1582 expr_ty p;
1583 if (!elt) {
1584 PyErr_SetString(PyExc_ValueError,
1585 "field elt is required for GeneratorExp");
1586 return NULL;
1587 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001588 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001589 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 p->kind = GeneratorExp_kind;
1592 p->v.GeneratorExp.elt = elt;
1593 p->v.GeneratorExp.generators = generators;
1594 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001595 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 return p;
1597}
1598
1599expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001600Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601{
1602 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001603 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001604 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 p->kind = Yield_kind;
1607 p->v.Yield.value = value;
1608 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001609 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 return p;
1611}
1612
1613expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001614Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1615 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616{
1617 expr_ty p;
1618 if (!left) {
1619 PyErr_SetString(PyExc_ValueError,
1620 "field left is required for Compare");
1621 return NULL;
1622 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001623 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001624 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001625 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 p->kind = Compare_kind;
1627 p->v.Compare.left = left;
1628 p->v.Compare.ops = ops;
1629 p->v.Compare.comparators = comparators;
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
1636Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001637 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001638{
1639 expr_ty p;
1640 if (!func) {
1641 PyErr_SetString(PyExc_ValueError,
1642 "field func is required for Call");
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 = Call_kind;
1649 p->v.Call.func = func;
1650 p->v.Call.args = args;
1651 p->v.Call.keywords = keywords;
1652 p->v.Call.starargs = starargs;
1653 p->v.Call.kwargs = kwargs;
1654 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001655 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 return p;
1657}
1658
1659expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001660Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661{
1662 expr_ty p;
1663 if (!value) {
1664 PyErr_SetString(PyExc_ValueError,
1665 "field value is required for Repr");
1666 return NULL;
1667 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001668 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001669 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 p->kind = Repr_kind;
1672 p->v.Repr.value = value;
1673 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001674 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 return p;
1676}
1677
1678expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001679Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680{
1681 expr_ty p;
1682 if (!n) {
1683 PyErr_SetString(PyExc_ValueError,
1684 "field n is required for Num");
1685 return NULL;
1686 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001687 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001688 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 p->kind = Num_kind;
1691 p->v.Num.n = n;
1692 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001693 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694 return p;
1695}
1696
1697expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001698Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699{
1700 expr_ty p;
1701 if (!s) {
1702 PyErr_SetString(PyExc_ValueError,
1703 "field s is required for Str");
1704 return NULL;
1705 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001706 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001707 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001708 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 p->kind = Str_kind;
1710 p->v.Str.s = s;
1711 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001712 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713 return p;
1714}
1715
1716expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001717Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1718 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001719{
1720 expr_ty p;
1721 if (!value) {
1722 PyErr_SetString(PyExc_ValueError,
1723 "field value is required for Attribute");
1724 return NULL;
1725 }
1726 if (!attr) {
1727 PyErr_SetString(PyExc_ValueError,
1728 "field attr is required for Attribute");
1729 return NULL;
1730 }
1731 if (!ctx) {
1732 PyErr_SetString(PyExc_ValueError,
1733 "field ctx is required for Attribute");
1734 return NULL;
1735 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001736 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001737 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001739 p->kind = Attribute_kind;
1740 p->v.Attribute.value = value;
1741 p->v.Attribute.attr = attr;
1742 p->v.Attribute.ctx = ctx;
1743 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001744 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 return p;
1746}
1747
1748expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001749Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1750 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751{
1752 expr_ty p;
1753 if (!value) {
1754 PyErr_SetString(PyExc_ValueError,
1755 "field value is required for Subscript");
1756 return NULL;
1757 }
1758 if (!slice) {
1759 PyErr_SetString(PyExc_ValueError,
1760 "field slice is required for Subscript");
1761 return NULL;
1762 }
1763 if (!ctx) {
1764 PyErr_SetString(PyExc_ValueError,
1765 "field ctx is required for Subscript");
1766 return NULL;
1767 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001768 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001769 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771 p->kind = Subscript_kind;
1772 p->v.Subscript.value = value;
1773 p->v.Subscript.slice = slice;
1774 p->v.Subscript.ctx = ctx;
1775 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001776 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 return p;
1778}
1779
1780expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001781Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1782 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783{
1784 expr_ty p;
1785 if (!id) {
1786 PyErr_SetString(PyExc_ValueError,
1787 "field id is required for Name");
1788 return NULL;
1789 }
1790 if (!ctx) {
1791 PyErr_SetString(PyExc_ValueError,
1792 "field ctx is required for Name");
1793 return NULL;
1794 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001795 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001796 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 p->kind = Name_kind;
1799 p->v.Name.id = id;
1800 p->v.Name.ctx = ctx;
1801 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001802 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 return p;
1804}
1805
1806expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001807List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1808 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809{
1810 expr_ty p;
1811 if (!ctx) {
1812 PyErr_SetString(PyExc_ValueError,
1813 "field ctx is required for List");
1814 return NULL;
1815 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001816 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001817 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 p->kind = List_kind;
1820 p->v.List.elts = elts;
1821 p->v.List.ctx = ctx;
1822 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001823 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 return p;
1825}
1826
1827expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001828Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1829 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830{
1831 expr_ty p;
1832 if (!ctx) {
1833 PyErr_SetString(PyExc_ValueError,
1834 "field ctx is required for Tuple");
1835 return NULL;
1836 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001837 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001838 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 p->kind = Tuple_kind;
1841 p->v.Tuple.elts = elts;
1842 p->v.Tuple.ctx = ctx;
1843 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001844 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 return p;
1846}
1847
1848slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850{
1851 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001852 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001853 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 p->kind = Ellipsis_kind;
1856 return p;
1857}
1858
1859slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861{
1862 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001863 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001864 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 p->kind = Slice_kind;
1867 p->v.Slice.lower = lower;
1868 p->v.Slice.upper = upper;
1869 p->v.Slice.step = step;
1870 return p;
1871}
1872
1873slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875{
1876 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001877 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001878 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 p->kind = ExtSlice_kind;
1881 p->v.ExtSlice.dims = dims;
1882 return p;
1883}
1884
1885slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001886Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887{
1888 slice_ty p;
1889 if (!value) {
1890 PyErr_SetString(PyExc_ValueError,
1891 "field value is required for Index");
1892 return NULL;
1893 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001894 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001895 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 p->kind = Index_kind;
1898 p->v.Index.value = value;
1899 return p;
1900}
1901
1902comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001903comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904{
1905 comprehension_ty p;
1906 if (!target) {
1907 PyErr_SetString(PyExc_ValueError,
1908 "field target is required for comprehension");
1909 return NULL;
1910 }
1911 if (!iter) {
1912 PyErr_SetString(PyExc_ValueError,
1913 "field iter is required for comprehension");
1914 return NULL;
1915 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001916 p = (comprehension_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->target = target;
1920 p->iter = iter;
1921 p->ifs = ifs;
1922 return p;
1923}
1924
1925excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001926ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001927 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928{
1929 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001930 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001931 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001933 p->kind = ExceptHandler_kind;
1934 p->v.ExceptHandler.type = type;
1935 p->v.ExceptHandler.name = name;
1936 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001937 p->lineno = lineno;
1938 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 return p;
1940}
1941
1942arguments_ty
1943arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001944 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945{
1946 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001947 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001948 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 p->args = args;
1951 p->vararg = vararg;
1952 p->kwarg = kwarg;
1953 p->defaults = defaults;
1954 return p;
1955}
1956
1957keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001958keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959{
1960 keyword_ty p;
1961 if (!arg) {
1962 PyErr_SetString(PyExc_ValueError,
1963 "field arg is required for keyword");
1964 return NULL;
1965 }
1966 if (!value) {
1967 PyErr_SetString(PyExc_ValueError,
1968 "field value is required for keyword");
1969 return NULL;
1970 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001971 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001972 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 p->arg = arg;
1975 p->value = value;
1976 return p;
1977}
1978
1979alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001980alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981{
1982 alias_ty p;
1983 if (!name) {
1984 PyErr_SetString(PyExc_ValueError,
1985 "field name is required for alias");
1986 return NULL;
1987 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001988 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001989 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 p->name = name;
1992 p->asname = asname;
1993 return p;
1994}
1995
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001996
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001997PyObject*
1998ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001999{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002000 mod_ty o = (mod_ty)_o;
2001 PyObject *result = NULL, *value = NULL;
2002 if (!o) {
2003 Py_INCREF(Py_None);
2004 return Py_None;
2005 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002006
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002007 switch (o->kind) {
2008 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002009 result = PyType_GenericNew(Module_type, NULL, NULL);
2010 if (!result) goto failed;
2011 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2012 if (!value) goto failed;
2013 if (PyObject_SetAttrString(result, "body", value) == -1)
2014 goto failed;
2015 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 break;
2017 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002018 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2019 if (!result) goto failed;
2020 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2021 if (!value) goto failed;
2022 if (PyObject_SetAttrString(result, "body", value) == -1)
2023 goto failed;
2024 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 break;
2026 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002027 result = PyType_GenericNew(Expression_type, NULL, NULL);
2028 if (!result) goto failed;
2029 value = ast2obj_expr(o->v.Expression.body);
2030 if (!value) goto failed;
2031 if (PyObject_SetAttrString(result, "body", value) == -1)
2032 goto failed;
2033 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 break;
2035 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002036 result = PyType_GenericNew(Suite_type, NULL, NULL);
2037 if (!result) goto failed;
2038 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2039 if (!value) goto failed;
2040 if (PyObject_SetAttrString(result, "body", value) == -1)
2041 goto failed;
2042 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 break;
2044 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002045 return result;
2046failed:
2047 Py_XDECREF(value);
2048 Py_XDECREF(result);
2049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050}
2051
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002052PyObject*
2053ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002055 stmt_ty o = (stmt_ty)_o;
2056 PyObject *result = NULL, *value = NULL;
2057 if (!o) {
2058 Py_INCREF(Py_None);
2059 return Py_None;
2060 }
2061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002062 switch (o->kind) {
2063 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002064 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2065 if (!result) goto failed;
2066 value = ast2obj_identifier(o->v.FunctionDef.name);
2067 if (!value) goto failed;
2068 if (PyObject_SetAttrString(result, "name", value) == -1)
2069 goto failed;
2070 Py_DECREF(value);
2071 value = ast2obj_arguments(o->v.FunctionDef.args);
2072 if (!value) goto failed;
2073 if (PyObject_SetAttrString(result, "args", value) == -1)
2074 goto failed;
2075 Py_DECREF(value);
2076 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2077 if (!value) goto failed;
2078 if (PyObject_SetAttrString(result, "body", value) == -1)
2079 goto failed;
2080 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002081 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2082 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002083 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002084 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2085 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002086 goto failed;
2087 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 break;
2089 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002090 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2091 if (!result) goto failed;
2092 value = ast2obj_identifier(o->v.ClassDef.name);
2093 if (!value) goto failed;
2094 if (PyObject_SetAttrString(result, "name", value) == -1)
2095 goto failed;
2096 Py_DECREF(value);
2097 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2098 if (!value) goto failed;
2099 if (PyObject_SetAttrString(result, "bases", value) == -1)
2100 goto failed;
2101 Py_DECREF(value);
2102 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2103 if (!value) goto failed;
2104 if (PyObject_SetAttrString(result, "body", value) == -1)
2105 goto failed;
2106 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002107 value = ast2obj_list(o->v.ClassDef.decorator_list,
2108 ast2obj_expr);
2109 if (!value) goto failed;
2110 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2111 -1)
2112 goto failed;
2113 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 break;
2115 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002116 result = PyType_GenericNew(Return_type, NULL, NULL);
2117 if (!result) goto failed;
2118 value = ast2obj_expr(o->v.Return.value);
2119 if (!value) goto failed;
2120 if (PyObject_SetAttrString(result, "value", value) == -1)
2121 goto failed;
2122 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 break;
2124 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002125 result = PyType_GenericNew(Delete_type, NULL, NULL);
2126 if (!result) goto failed;
2127 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2128 if (!value) goto failed;
2129 if (PyObject_SetAttrString(result, "targets", value) == -1)
2130 goto failed;
2131 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 break;
2133 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002134 result = PyType_GenericNew(Assign_type, NULL, NULL);
2135 if (!result) goto failed;
2136 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2137 if (!value) goto failed;
2138 if (PyObject_SetAttrString(result, "targets", value) == -1)
2139 goto failed;
2140 Py_DECREF(value);
2141 value = ast2obj_expr(o->v.Assign.value);
2142 if (!value) goto failed;
2143 if (PyObject_SetAttrString(result, "value", value) == -1)
2144 goto failed;
2145 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 break;
2147 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002148 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2149 if (!result) goto failed;
2150 value = ast2obj_expr(o->v.AugAssign.target);
2151 if (!value) goto failed;
2152 if (PyObject_SetAttrString(result, "target", value) == -1)
2153 goto failed;
2154 Py_DECREF(value);
2155 value = ast2obj_operator(o->v.AugAssign.op);
2156 if (!value) goto failed;
2157 if (PyObject_SetAttrString(result, "op", value) == -1)
2158 goto failed;
2159 Py_DECREF(value);
2160 value = ast2obj_expr(o->v.AugAssign.value);
2161 if (!value) goto failed;
2162 if (PyObject_SetAttrString(result, "value", value) == -1)
2163 goto failed;
2164 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 break;
2166 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002167 result = PyType_GenericNew(Print_type, NULL, NULL);
2168 if (!result) goto failed;
2169 value = ast2obj_expr(o->v.Print.dest);
2170 if (!value) goto failed;
2171 if (PyObject_SetAttrString(result, "dest", value) == -1)
2172 goto failed;
2173 Py_DECREF(value);
2174 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2175 if (!value) goto failed;
2176 if (PyObject_SetAttrString(result, "values", value) == -1)
2177 goto failed;
2178 Py_DECREF(value);
2179 value = ast2obj_bool(o->v.Print.nl);
2180 if (!value) goto failed;
2181 if (PyObject_SetAttrString(result, "nl", value) == -1)
2182 goto failed;
2183 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 break;
2185 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002186 result = PyType_GenericNew(For_type, NULL, NULL);
2187 if (!result) goto failed;
2188 value = ast2obj_expr(o->v.For.target);
2189 if (!value) goto failed;
2190 if (PyObject_SetAttrString(result, "target", value) == -1)
2191 goto failed;
2192 Py_DECREF(value);
2193 value = ast2obj_expr(o->v.For.iter);
2194 if (!value) goto failed;
2195 if (PyObject_SetAttrString(result, "iter", value) == -1)
2196 goto failed;
2197 Py_DECREF(value);
2198 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2199 if (!value) goto failed;
2200 if (PyObject_SetAttrString(result, "body", value) == -1)
2201 goto failed;
2202 Py_DECREF(value);
2203 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2204 if (!value) goto failed;
2205 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2206 goto failed;
2207 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 break;
2209 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002210 result = PyType_GenericNew(While_type, NULL, NULL);
2211 if (!result) goto failed;
2212 value = ast2obj_expr(o->v.While.test);
2213 if (!value) goto failed;
2214 if (PyObject_SetAttrString(result, "test", value) == -1)
2215 goto failed;
2216 Py_DECREF(value);
2217 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2218 if (!value) goto failed;
2219 if (PyObject_SetAttrString(result, "body", value) == -1)
2220 goto failed;
2221 Py_DECREF(value);
2222 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2223 if (!value) goto failed;
2224 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2225 goto failed;
2226 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 break;
2228 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002229 result = PyType_GenericNew(If_type, NULL, NULL);
2230 if (!result) goto failed;
2231 value = ast2obj_expr(o->v.If.test);
2232 if (!value) goto failed;
2233 if (PyObject_SetAttrString(result, "test", value) == -1)
2234 goto failed;
2235 Py_DECREF(value);
2236 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2237 if (!value) goto failed;
2238 if (PyObject_SetAttrString(result, "body", value) == -1)
2239 goto failed;
2240 Py_DECREF(value);
2241 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2242 if (!value) goto failed;
2243 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2244 goto failed;
2245 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002247 case With_kind:
2248 result = PyType_GenericNew(With_type, NULL, NULL);
2249 if (!result) goto failed;
2250 value = ast2obj_expr(o->v.With.context_expr);
2251 if (!value) goto failed;
2252 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2253 goto failed;
2254 Py_DECREF(value);
2255 value = ast2obj_expr(o->v.With.optional_vars);
2256 if (!value) goto failed;
2257 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2258 -1)
2259 goto failed;
2260 Py_DECREF(value);
2261 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2262 if (!value) goto failed;
2263 if (PyObject_SetAttrString(result, "body", value) == -1)
2264 goto failed;
2265 Py_DECREF(value);
2266 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002268 result = PyType_GenericNew(Raise_type, NULL, NULL);
2269 if (!result) goto failed;
2270 value = ast2obj_expr(o->v.Raise.type);
2271 if (!value) goto failed;
2272 if (PyObject_SetAttrString(result, "type", value) == -1)
2273 goto failed;
2274 Py_DECREF(value);
2275 value = ast2obj_expr(o->v.Raise.inst);
2276 if (!value) goto failed;
2277 if (PyObject_SetAttrString(result, "inst", value) == -1)
2278 goto failed;
2279 Py_DECREF(value);
2280 value = ast2obj_expr(o->v.Raise.tback);
2281 if (!value) goto failed;
2282 if (PyObject_SetAttrString(result, "tback", value) == -1)
2283 goto failed;
2284 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 break;
2286 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002287 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2288 if (!result) goto failed;
2289 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2290 if (!value) goto failed;
2291 if (PyObject_SetAttrString(result, "body", value) == -1)
2292 goto failed;
2293 Py_DECREF(value);
2294 value = ast2obj_list(o->v.TryExcept.handlers,
2295 ast2obj_excepthandler);
2296 if (!value) goto failed;
2297 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2298 goto failed;
2299 Py_DECREF(value);
2300 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2301 if (!value) goto failed;
2302 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2303 goto failed;
2304 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 break;
2306 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002307 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2308 if (!result) goto failed;
2309 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2310 if (!value) goto failed;
2311 if (PyObject_SetAttrString(result, "body", value) == -1)
2312 goto failed;
2313 Py_DECREF(value);
2314 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2315 if (!value) goto failed;
2316 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2317 goto failed;
2318 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 break;
2320 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002321 result = PyType_GenericNew(Assert_type, NULL, NULL);
2322 if (!result) goto failed;
2323 value = ast2obj_expr(o->v.Assert.test);
2324 if (!value) goto failed;
2325 if (PyObject_SetAttrString(result, "test", value) == -1)
2326 goto failed;
2327 Py_DECREF(value);
2328 value = ast2obj_expr(o->v.Assert.msg);
2329 if (!value) goto failed;
2330 if (PyObject_SetAttrString(result, "msg", value) == -1)
2331 goto failed;
2332 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 break;
2334 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002335 result = PyType_GenericNew(Import_type, NULL, NULL);
2336 if (!result) goto failed;
2337 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2338 if (!value) goto failed;
2339 if (PyObject_SetAttrString(result, "names", value) == -1)
2340 goto failed;
2341 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 break;
2343 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002344 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2345 if (!result) goto failed;
2346 value = ast2obj_identifier(o->v.ImportFrom.module);
2347 if (!value) goto failed;
2348 if (PyObject_SetAttrString(result, "module", value) == -1)
2349 goto failed;
2350 Py_DECREF(value);
2351 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2352 if (!value) goto failed;
2353 if (PyObject_SetAttrString(result, "names", value) == -1)
2354 goto failed;
2355 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002356 value = ast2obj_int(o->v.ImportFrom.level);
2357 if (!value) goto failed;
2358 if (PyObject_SetAttrString(result, "level", value) == -1)
2359 goto failed;
2360 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 break;
2362 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002363 result = PyType_GenericNew(Exec_type, NULL, NULL);
2364 if (!result) goto failed;
2365 value = ast2obj_expr(o->v.Exec.body);
2366 if (!value) goto failed;
2367 if (PyObject_SetAttrString(result, "body", value) == -1)
2368 goto failed;
2369 Py_DECREF(value);
2370 value = ast2obj_expr(o->v.Exec.globals);
2371 if (!value) goto failed;
2372 if (PyObject_SetAttrString(result, "globals", value) == -1)
2373 goto failed;
2374 Py_DECREF(value);
2375 value = ast2obj_expr(o->v.Exec.locals);
2376 if (!value) goto failed;
2377 if (PyObject_SetAttrString(result, "locals", value) == -1)
2378 goto failed;
2379 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380 break;
2381 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002382 result = PyType_GenericNew(Global_type, NULL, NULL);
2383 if (!result) goto failed;
2384 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2385 if (!value) goto failed;
2386 if (PyObject_SetAttrString(result, "names", value) == -1)
2387 goto failed;
2388 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 break;
2390 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002391 result = PyType_GenericNew(Expr_type, NULL, NULL);
2392 if (!result) goto failed;
2393 value = ast2obj_expr(o->v.Expr.value);
2394 if (!value) goto failed;
2395 if (PyObject_SetAttrString(result, "value", value) == -1)
2396 goto failed;
2397 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 break;
2399 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002400 result = PyType_GenericNew(Pass_type, NULL, NULL);
2401 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 break;
2403 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002404 result = PyType_GenericNew(Break_type, NULL, NULL);
2405 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 break;
2407 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002408 result = PyType_GenericNew(Continue_type, NULL, NULL);
2409 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 break;
2411 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002412 value = ast2obj_int(o->lineno);
2413 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002414 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2415 goto failed;
2416 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002417 value = ast2obj_int(o->col_offset);
2418 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002419 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2420 goto failed;
2421 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002422 return result;
2423failed:
2424 Py_XDECREF(value);
2425 Py_XDECREF(result);
2426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427}
2428
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429PyObject*
2430ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002432 expr_ty o = (expr_ty)_o;
2433 PyObject *result = NULL, *value = NULL;
2434 if (!o) {
2435 Py_INCREF(Py_None);
2436 return Py_None;
2437 }
2438
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 switch (o->kind) {
2440 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002441 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2442 if (!result) goto failed;
2443 value = ast2obj_boolop(o->v.BoolOp.op);
2444 if (!value) goto failed;
2445 if (PyObject_SetAttrString(result, "op", value) == -1)
2446 goto failed;
2447 Py_DECREF(value);
2448 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2449 if (!value) goto failed;
2450 if (PyObject_SetAttrString(result, "values", value) == -1)
2451 goto failed;
2452 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 break;
2454 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002455 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2456 if (!result) goto failed;
2457 value = ast2obj_expr(o->v.BinOp.left);
2458 if (!value) goto failed;
2459 if (PyObject_SetAttrString(result, "left", value) == -1)
2460 goto failed;
2461 Py_DECREF(value);
2462 value = ast2obj_operator(o->v.BinOp.op);
2463 if (!value) goto failed;
2464 if (PyObject_SetAttrString(result, "op", value) == -1)
2465 goto failed;
2466 Py_DECREF(value);
2467 value = ast2obj_expr(o->v.BinOp.right);
2468 if (!value) goto failed;
2469 if (PyObject_SetAttrString(result, "right", value) == -1)
2470 goto failed;
2471 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 break;
2473 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002474 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2475 if (!result) goto failed;
2476 value = ast2obj_unaryop(o->v.UnaryOp.op);
2477 if (!value) goto failed;
2478 if (PyObject_SetAttrString(result, "op", value) == -1)
2479 goto failed;
2480 Py_DECREF(value);
2481 value = ast2obj_expr(o->v.UnaryOp.operand);
2482 if (!value) goto failed;
2483 if (PyObject_SetAttrString(result, "operand", value) == -1)
2484 goto failed;
2485 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 break;
2487 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002488 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2489 if (!result) goto failed;
2490 value = ast2obj_arguments(o->v.Lambda.args);
2491 if (!value) goto failed;
2492 if (PyObject_SetAttrString(result, "args", value) == -1)
2493 goto failed;
2494 Py_DECREF(value);
2495 value = ast2obj_expr(o->v.Lambda.body);
2496 if (!value) goto failed;
2497 if (PyObject_SetAttrString(result, "body", value) == -1)
2498 goto failed;
2499 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002501 case IfExp_kind:
2502 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2503 if (!result) goto failed;
2504 value = ast2obj_expr(o->v.IfExp.test);
2505 if (!value) goto failed;
2506 if (PyObject_SetAttrString(result, "test", value) == -1)
2507 goto failed;
2508 Py_DECREF(value);
2509 value = ast2obj_expr(o->v.IfExp.body);
2510 if (!value) goto failed;
2511 if (PyObject_SetAttrString(result, "body", value) == -1)
2512 goto failed;
2513 Py_DECREF(value);
2514 value = ast2obj_expr(o->v.IfExp.orelse);
2515 if (!value) goto failed;
2516 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2517 goto failed;
2518 Py_DECREF(value);
2519 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002521 result = PyType_GenericNew(Dict_type, NULL, NULL);
2522 if (!result) goto failed;
2523 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2524 if (!value) goto failed;
2525 if (PyObject_SetAttrString(result, "keys", value) == -1)
2526 goto failed;
2527 Py_DECREF(value);
2528 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2529 if (!value) goto failed;
2530 if (PyObject_SetAttrString(result, "values", value) == -1)
2531 goto failed;
2532 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 break;
2534 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002535 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2536 if (!result) goto failed;
2537 value = ast2obj_expr(o->v.ListComp.elt);
2538 if (!value) goto failed;
2539 if (PyObject_SetAttrString(result, "elt", value) == -1)
2540 goto failed;
2541 Py_DECREF(value);
2542 value = ast2obj_list(o->v.ListComp.generators,
2543 ast2obj_comprehension);
2544 if (!value) goto failed;
2545 if (PyObject_SetAttrString(result, "generators", value) == -1)
2546 goto failed;
2547 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002548 break;
2549 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002550 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2551 if (!result) goto failed;
2552 value = ast2obj_expr(o->v.GeneratorExp.elt);
2553 if (!value) goto failed;
2554 if (PyObject_SetAttrString(result, "elt", value) == -1)
2555 goto failed;
2556 Py_DECREF(value);
2557 value = ast2obj_list(o->v.GeneratorExp.generators,
2558 ast2obj_comprehension);
2559 if (!value) goto failed;
2560 if (PyObject_SetAttrString(result, "generators", value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 break;
2564 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002565 result = PyType_GenericNew(Yield_type, NULL, NULL);
2566 if (!result) goto failed;
2567 value = ast2obj_expr(o->v.Yield.value);
2568 if (!value) goto failed;
2569 if (PyObject_SetAttrString(result, "value", value) == -1)
2570 goto failed;
2571 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 break;
2573 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002574 result = PyType_GenericNew(Compare_type, NULL, NULL);
2575 if (!result) goto failed;
2576 value = ast2obj_expr(o->v.Compare.left);
2577 if (!value) goto failed;
2578 if (PyObject_SetAttrString(result, "left", value) == -1)
2579 goto failed;
2580 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002581 {
2582 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2583 value = PyList_New(n);
2584 if (!value) goto failed;
2585 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002586 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 +00002587 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "ops", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
2592 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2593 if (!value) goto failed;
2594 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2595 goto failed;
2596 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 break;
2598 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002599 result = PyType_GenericNew(Call_type, NULL, NULL);
2600 if (!result) goto failed;
2601 value = ast2obj_expr(o->v.Call.func);
2602 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "func", value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
2606 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2607 if (!value) goto failed;
2608 if (PyObject_SetAttrString(result, "args", value) == -1)
2609 goto failed;
2610 Py_DECREF(value);
2611 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2612 if (!value) goto failed;
2613 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2614 goto failed;
2615 Py_DECREF(value);
2616 value = ast2obj_expr(o->v.Call.starargs);
2617 if (!value) goto failed;
2618 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2619 goto failed;
2620 Py_DECREF(value);
2621 value = ast2obj_expr(o->v.Call.kwargs);
2622 if (!value) goto failed;
2623 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2624 goto failed;
2625 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 break;
2627 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002628 result = PyType_GenericNew(Repr_type, NULL, NULL);
2629 if (!result) goto failed;
2630 value = ast2obj_expr(o->v.Repr.value);
2631 if (!value) goto failed;
2632 if (PyObject_SetAttrString(result, "value", value) == -1)
2633 goto failed;
2634 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 break;
2636 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002637 result = PyType_GenericNew(Num_type, NULL, NULL);
2638 if (!result) goto failed;
2639 value = ast2obj_object(o->v.Num.n);
2640 if (!value) goto failed;
2641 if (PyObject_SetAttrString(result, "n", value) == -1)
2642 goto failed;
2643 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 break;
2645 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002646 result = PyType_GenericNew(Str_type, NULL, NULL);
2647 if (!result) goto failed;
2648 value = ast2obj_string(o->v.Str.s);
2649 if (!value) goto failed;
2650 if (PyObject_SetAttrString(result, "s", value) == -1)
2651 goto failed;
2652 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 break;
2654 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002655 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2656 if (!result) goto failed;
2657 value = ast2obj_expr(o->v.Attribute.value);
2658 if (!value) goto failed;
2659 if (PyObject_SetAttrString(result, "value", value) == -1)
2660 goto failed;
2661 Py_DECREF(value);
2662 value = ast2obj_identifier(o->v.Attribute.attr);
2663 if (!value) goto failed;
2664 if (PyObject_SetAttrString(result, "attr", value) == -1)
2665 goto failed;
2666 Py_DECREF(value);
2667 value = ast2obj_expr_context(o->v.Attribute.ctx);
2668 if (!value) goto failed;
2669 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2670 goto failed;
2671 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 break;
2673 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002674 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2675 if (!result) goto failed;
2676 value = ast2obj_expr(o->v.Subscript.value);
2677 if (!value) goto failed;
2678 if (PyObject_SetAttrString(result, "value", value) == -1)
2679 goto failed;
2680 Py_DECREF(value);
2681 value = ast2obj_slice(o->v.Subscript.slice);
2682 if (!value) goto failed;
2683 if (PyObject_SetAttrString(result, "slice", value) == -1)
2684 goto failed;
2685 Py_DECREF(value);
2686 value = ast2obj_expr_context(o->v.Subscript.ctx);
2687 if (!value) goto failed;
2688 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2689 goto failed;
2690 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 break;
2692 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002693 result = PyType_GenericNew(Name_type, NULL, NULL);
2694 if (!result) goto failed;
2695 value = ast2obj_identifier(o->v.Name.id);
2696 if (!value) goto failed;
2697 if (PyObject_SetAttrString(result, "id", value) == -1)
2698 goto failed;
2699 Py_DECREF(value);
2700 value = ast2obj_expr_context(o->v.Name.ctx);
2701 if (!value) goto failed;
2702 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2703 goto failed;
2704 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 break;
2706 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002707 result = PyType_GenericNew(List_type, NULL, NULL);
2708 if (!result) goto failed;
2709 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2710 if (!value) goto failed;
2711 if (PyObject_SetAttrString(result, "elts", value) == -1)
2712 goto failed;
2713 Py_DECREF(value);
2714 value = ast2obj_expr_context(o->v.List.ctx);
2715 if (!value) goto failed;
2716 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2717 goto failed;
2718 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 break;
2720 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002721 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2722 if (!result) goto failed;
2723 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2724 if (!value) goto failed;
2725 if (PyObject_SetAttrString(result, "elts", value) == -1)
2726 goto failed;
2727 Py_DECREF(value);
2728 value = ast2obj_expr_context(o->v.Tuple.ctx);
2729 if (!value) goto failed;
2730 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2731 goto failed;
2732 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 break;
2734 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002735 value = ast2obj_int(o->lineno);
2736 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002737 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2738 goto failed;
2739 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002740 value = ast2obj_int(o->col_offset);
2741 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002742 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2743 goto failed;
2744 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002745 return result;
2746failed:
2747 Py_XDECREF(value);
2748 Py_XDECREF(result);
2749 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750}
2751
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002752PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002754 switch(o) {
2755 case Load:
2756 Py_INCREF(Load_singleton);
2757 return Load_singleton;
2758 case Store:
2759 Py_INCREF(Store_singleton);
2760 return Store_singleton;
2761 case Del:
2762 Py_INCREF(Del_singleton);
2763 return Del_singleton;
2764 case AugLoad:
2765 Py_INCREF(AugLoad_singleton);
2766 return AugLoad_singleton;
2767 case AugStore:
2768 Py_INCREF(AugStore_singleton);
2769 return AugStore_singleton;
2770 case Param:
2771 Py_INCREF(Param_singleton);
2772 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002773 default:
2774 /* should never happen, but just in case ... */
2775 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2776 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002779PyObject*
2780ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002782 slice_ty o = (slice_ty)_o;
2783 PyObject *result = NULL, *value = NULL;
2784 if (!o) {
2785 Py_INCREF(Py_None);
2786 return Py_None;
2787 }
2788
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 switch (o->kind) {
2790 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002791 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2792 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 break;
2794 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002795 result = PyType_GenericNew(Slice_type, NULL, NULL);
2796 if (!result) goto failed;
2797 value = ast2obj_expr(o->v.Slice.lower);
2798 if (!value) goto failed;
2799 if (PyObject_SetAttrString(result, "lower", value) == -1)
2800 goto failed;
2801 Py_DECREF(value);
2802 value = ast2obj_expr(o->v.Slice.upper);
2803 if (!value) goto failed;
2804 if (PyObject_SetAttrString(result, "upper", value) == -1)
2805 goto failed;
2806 Py_DECREF(value);
2807 value = ast2obj_expr(o->v.Slice.step);
2808 if (!value) goto failed;
2809 if (PyObject_SetAttrString(result, "step", value) == -1)
2810 goto failed;
2811 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 break;
2813 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002814 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2815 if (!result) goto failed;
2816 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2817 if (!value) goto failed;
2818 if (PyObject_SetAttrString(result, "dims", value) == -1)
2819 goto failed;
2820 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002821 break;
2822 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002823 result = PyType_GenericNew(Index_type, NULL, NULL);
2824 if (!result) goto failed;
2825 value = ast2obj_expr(o->v.Index.value);
2826 if (!value) goto failed;
2827 if (PyObject_SetAttrString(result, "value", value) == -1)
2828 goto failed;
2829 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 break;
2831 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002832 return result;
2833failed:
2834 Py_XDECREF(value);
2835 Py_XDECREF(result);
2836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837}
2838
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002839PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002841 switch(o) {
2842 case And:
2843 Py_INCREF(And_singleton);
2844 return And_singleton;
2845 case Or:
2846 Py_INCREF(Or_singleton);
2847 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002848 default:
2849 /* should never happen, but just in case ... */
2850 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002853}
2854PyObject* ast2obj_operator(operator_ty o)
2855{
2856 switch(o) {
2857 case Add:
2858 Py_INCREF(Add_singleton);
2859 return Add_singleton;
2860 case Sub:
2861 Py_INCREF(Sub_singleton);
2862 return Sub_singleton;
2863 case Mult:
2864 Py_INCREF(Mult_singleton);
2865 return Mult_singleton;
2866 case Div:
2867 Py_INCREF(Div_singleton);
2868 return Div_singleton;
2869 case Mod:
2870 Py_INCREF(Mod_singleton);
2871 return Mod_singleton;
2872 case Pow:
2873 Py_INCREF(Pow_singleton);
2874 return Pow_singleton;
2875 case LShift:
2876 Py_INCREF(LShift_singleton);
2877 return LShift_singleton;
2878 case RShift:
2879 Py_INCREF(RShift_singleton);
2880 return RShift_singleton;
2881 case BitOr:
2882 Py_INCREF(BitOr_singleton);
2883 return BitOr_singleton;
2884 case BitXor:
2885 Py_INCREF(BitXor_singleton);
2886 return BitXor_singleton;
2887 case BitAnd:
2888 Py_INCREF(BitAnd_singleton);
2889 return BitAnd_singleton;
2890 case FloorDiv:
2891 Py_INCREF(FloorDiv_singleton);
2892 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002893 default:
2894 /* should never happen, but just in case ... */
2895 PyErr_Format(PyExc_SystemError, "unknown operator found");
2896 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002897 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002898}
2899PyObject* ast2obj_unaryop(unaryop_ty o)
2900{
2901 switch(o) {
2902 case Invert:
2903 Py_INCREF(Invert_singleton);
2904 return Invert_singleton;
2905 case Not:
2906 Py_INCREF(Not_singleton);
2907 return Not_singleton;
2908 case UAdd:
2909 Py_INCREF(UAdd_singleton);
2910 return UAdd_singleton;
2911 case USub:
2912 Py_INCREF(USub_singleton);
2913 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002914 default:
2915 /* should never happen, but just in case ... */
2916 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2917 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002918 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002919}
2920PyObject* ast2obj_cmpop(cmpop_ty o)
2921{
2922 switch(o) {
2923 case Eq:
2924 Py_INCREF(Eq_singleton);
2925 return Eq_singleton;
2926 case NotEq:
2927 Py_INCREF(NotEq_singleton);
2928 return NotEq_singleton;
2929 case Lt:
2930 Py_INCREF(Lt_singleton);
2931 return Lt_singleton;
2932 case LtE:
2933 Py_INCREF(LtE_singleton);
2934 return LtE_singleton;
2935 case Gt:
2936 Py_INCREF(Gt_singleton);
2937 return Gt_singleton;
2938 case GtE:
2939 Py_INCREF(GtE_singleton);
2940 return GtE_singleton;
2941 case Is:
2942 Py_INCREF(Is_singleton);
2943 return Is_singleton;
2944 case IsNot:
2945 Py_INCREF(IsNot_singleton);
2946 return IsNot_singleton;
2947 case In:
2948 Py_INCREF(In_singleton);
2949 return In_singleton;
2950 case NotIn:
2951 Py_INCREF(NotIn_singleton);
2952 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002953 default:
2954 /* should never happen, but just in case ... */
2955 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2956 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002957 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002958}
2959PyObject*
2960ast2obj_comprehension(void* _o)
2961{
2962 comprehension_ty o = (comprehension_ty)_o;
2963 PyObject *result = NULL, *value = NULL;
2964 if (!o) {
2965 Py_INCREF(Py_None);
2966 return Py_None;
2967 }
2968
2969 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2970 if (!result) return NULL;
2971 value = ast2obj_expr(o->target);
2972 if (!value) goto failed;
2973 if (PyObject_SetAttrString(result, "target", value) == -1)
2974 goto failed;
2975 Py_DECREF(value);
2976 value = ast2obj_expr(o->iter);
2977 if (!value) goto failed;
2978 if (PyObject_SetAttrString(result, "iter", value) == -1)
2979 goto failed;
2980 Py_DECREF(value);
2981 value = ast2obj_list(o->ifs, ast2obj_expr);
2982 if (!value) goto failed;
2983 if (PyObject_SetAttrString(result, "ifs", value) == -1)
2984 goto failed;
2985 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002986 return result;
2987failed:
2988 Py_XDECREF(value);
2989 Py_XDECREF(result);
2990 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991}
2992
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002993PyObject*
2994ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002996 excepthandler_ty o = (excepthandler_ty)_o;
2997 PyObject *result = NULL, *value = NULL;
2998 if (!o) {
2999 Py_INCREF(Py_None);
3000 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003002
Georg Brandla48f3ab2008-03-30 06:40:17 +00003003 switch (o->kind) {
3004 case ExceptHandler_kind:
3005 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3006 if (!result) goto failed;
3007 value = ast2obj_expr(o->v.ExceptHandler.type);
3008 if (!value) goto failed;
3009 if (PyObject_SetAttrString(result, "type", value) == -1)
3010 goto failed;
3011 Py_DECREF(value);
3012 value = ast2obj_expr(o->v.ExceptHandler.name);
3013 if (!value) goto failed;
3014 if (PyObject_SetAttrString(result, "name", value) == -1)
3015 goto failed;
3016 Py_DECREF(value);
3017 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3018 if (!value) goto failed;
3019 if (PyObject_SetAttrString(result, "body", value) == -1)
3020 goto failed;
3021 Py_DECREF(value);
3022 break;
3023 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003024 value = ast2obj_int(o->lineno);
3025 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003026 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003027 goto failed;
3028 Py_DECREF(value);
3029 value = ast2obj_int(o->col_offset);
3030 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003031 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003032 goto failed;
3033 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003034 return result;
3035failed:
3036 Py_XDECREF(value);
3037 Py_XDECREF(result);
3038 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039}
3040
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003041PyObject*
3042ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003044 arguments_ty o = (arguments_ty)_o;
3045 PyObject *result = NULL, *value = NULL;
3046 if (!o) {
3047 Py_INCREF(Py_None);
3048 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003050
3051 result = PyType_GenericNew(arguments_type, NULL, NULL);
3052 if (!result) return NULL;
3053 value = ast2obj_list(o->args, ast2obj_expr);
3054 if (!value) goto failed;
3055 if (PyObject_SetAttrString(result, "args", value) == -1)
3056 goto failed;
3057 Py_DECREF(value);
3058 value = ast2obj_identifier(o->vararg);
3059 if (!value) goto failed;
3060 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3061 goto failed;
3062 Py_DECREF(value);
3063 value = ast2obj_identifier(o->kwarg);
3064 if (!value) goto failed;
3065 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3066 goto failed;
3067 Py_DECREF(value);
3068 value = ast2obj_list(o->defaults, ast2obj_expr);
3069 if (!value) goto failed;
3070 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3071 goto failed;
3072 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003073 return result;
3074failed:
3075 Py_XDECREF(value);
3076 Py_XDECREF(result);
3077 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078}
3079
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003080PyObject*
3081ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003083 keyword_ty o = (keyword_ty)_o;
3084 PyObject *result = NULL, *value = NULL;
3085 if (!o) {
3086 Py_INCREF(Py_None);
3087 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003089
3090 result = PyType_GenericNew(keyword_type, NULL, NULL);
3091 if (!result) return NULL;
3092 value = ast2obj_identifier(o->arg);
3093 if (!value) goto failed;
3094 if (PyObject_SetAttrString(result, "arg", value) == -1)
3095 goto failed;
3096 Py_DECREF(value);
3097 value = ast2obj_expr(o->value);
3098 if (!value) goto failed;
3099 if (PyObject_SetAttrString(result, "value", value) == -1)
3100 goto failed;
3101 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003102 return result;
3103failed:
3104 Py_XDECREF(value);
3105 Py_XDECREF(result);
3106 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107}
3108
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003109PyObject*
3110ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003112 alias_ty o = (alias_ty)_o;
3113 PyObject *result = NULL, *value = NULL;
3114 if (!o) {
3115 Py_INCREF(Py_None);
3116 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003118
3119 result = PyType_GenericNew(alias_type, NULL, NULL);
3120 if (!result) return NULL;
3121 value = ast2obj_identifier(o->name);
3122 if (!value) goto failed;
3123 if (PyObject_SetAttrString(result, "name", value) == -1)
3124 goto failed;
3125 Py_DECREF(value);
3126 value = ast2obj_identifier(o->asname);
3127 if (!value) goto failed;
3128 if (PyObject_SetAttrString(result, "asname", value) == -1)
3129 goto failed;
3130 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003131 return result;
3132failed:
3133 Py_XDECREF(value);
3134 Py_XDECREF(result);
3135 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136}
3137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138
Georg Brandlfc8eef32008-03-28 12:11:56 +00003139int
3140obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3141{
3142 PyObject* tmp = NULL;
3143
3144
3145 if (obj == Py_None) {
3146 *out = NULL;
3147 return 0;
3148 }
3149 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3150 asdl_seq* body;
3151
3152 if (PyObject_HasAttrString(obj, "body")) {
3153 int res;
3154 Py_ssize_t len;
3155 Py_ssize_t i;
3156 tmp = PyObject_GetAttrString(obj, "body");
3157 if (tmp == NULL) goto failed;
3158 if (!PyList_Check(tmp)) {
3159 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3160 goto failed;
3161 }
3162 len = PyList_GET_SIZE(tmp);
3163 body = asdl_seq_new(len, arena);
3164 if (body == NULL) goto failed;
3165 for (i = 0; i < len; i++) {
3166 stmt_ty value;
3167 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3168 if (res != 0) goto failed;
3169 asdl_seq_SET(body, i, value);
3170 }
3171 Py_XDECREF(tmp);
3172 tmp = NULL;
3173 } else {
3174 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3175 return 1;
3176 }
3177 *out = Module(body, arena);
3178 if (*out == NULL) goto failed;
3179 return 0;
3180 }
3181 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3182 asdl_seq* body;
3183
3184 if (PyObject_HasAttrString(obj, "body")) {
3185 int res;
3186 Py_ssize_t len;
3187 Py_ssize_t i;
3188 tmp = PyObject_GetAttrString(obj, "body");
3189 if (tmp == NULL) goto failed;
3190 if (!PyList_Check(tmp)) {
3191 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3192 goto failed;
3193 }
3194 len = PyList_GET_SIZE(tmp);
3195 body = asdl_seq_new(len, arena);
3196 if (body == NULL) goto failed;
3197 for (i = 0; i < len; i++) {
3198 stmt_ty value;
3199 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3200 if (res != 0) goto failed;
3201 asdl_seq_SET(body, i, value);
3202 }
3203 Py_XDECREF(tmp);
3204 tmp = NULL;
3205 } else {
3206 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3207 return 1;
3208 }
3209 *out = Interactive(body, arena);
3210 if (*out == NULL) goto failed;
3211 return 0;
3212 }
3213 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3214 expr_ty body;
3215
3216 if (PyObject_HasAttrString(obj, "body")) {
3217 int res;
3218 tmp = PyObject_GetAttrString(obj, "body");
3219 if (tmp == NULL) goto failed;
3220 res = obj2ast_expr(tmp, &body, arena);
3221 if (res != 0) goto failed;
3222 Py_XDECREF(tmp);
3223 tmp = NULL;
3224 } else {
3225 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3226 return 1;
3227 }
3228 *out = Expression(body, arena);
3229 if (*out == NULL) goto failed;
3230 return 0;
3231 }
3232 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3233 asdl_seq* body;
3234
3235 if (PyObject_HasAttrString(obj, "body")) {
3236 int res;
3237 Py_ssize_t len;
3238 Py_ssize_t i;
3239 tmp = PyObject_GetAttrString(obj, "body");
3240 if (tmp == NULL) goto failed;
3241 if (!PyList_Check(tmp)) {
3242 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3243 goto failed;
3244 }
3245 len = PyList_GET_SIZE(tmp);
3246 body = asdl_seq_new(len, arena);
3247 if (body == NULL) goto failed;
3248 for (i = 0; i < len; i++) {
3249 stmt_ty value;
3250 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3251 if (res != 0) goto failed;
3252 asdl_seq_SET(body, i, value);
3253 }
3254 Py_XDECREF(tmp);
3255 tmp = NULL;
3256 } else {
3257 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3258 return 1;
3259 }
3260 *out = Suite(body, arena);
3261 if (*out == NULL) goto failed;
3262 return 0;
3263 }
3264
3265 tmp = PyObject_Repr(obj);
3266 if (tmp == NULL) goto failed;
3267 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3268failed:
3269 Py_XDECREF(tmp);
3270 return 1;
3271}
3272
3273int
3274obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3275{
3276 PyObject* tmp = NULL;
3277
3278 int lineno;
3279 int col_offset;
3280
3281 if (obj == Py_None) {
3282 *out = NULL;
3283 return 0;
3284 }
3285 if (PyObject_HasAttrString(obj, "lineno")) {
3286 int res;
3287 tmp = PyObject_GetAttrString(obj, "lineno");
3288 if (tmp == NULL) goto failed;
3289 res = obj2ast_int(tmp, &lineno, arena);
3290 if (res != 0) goto failed;
3291 Py_XDECREF(tmp);
3292 tmp = NULL;
3293 } else {
3294 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3295 return 1;
3296 }
3297 if (PyObject_HasAttrString(obj, "col_offset")) {
3298 int res;
3299 tmp = PyObject_GetAttrString(obj, "col_offset");
3300 if (tmp == NULL) goto failed;
3301 res = obj2ast_int(tmp, &col_offset, arena);
3302 if (res != 0) goto failed;
3303 Py_XDECREF(tmp);
3304 tmp = NULL;
3305 } else {
3306 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3307 return 1;
3308 }
3309 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3310 identifier name;
3311 arguments_ty args;
3312 asdl_seq* body;
3313 asdl_seq* decorator_list;
3314
3315 if (PyObject_HasAttrString(obj, "name")) {
3316 int res;
3317 tmp = PyObject_GetAttrString(obj, "name");
3318 if (tmp == NULL) goto failed;
3319 res = obj2ast_identifier(tmp, &name, arena);
3320 if (res != 0) goto failed;
3321 Py_XDECREF(tmp);
3322 tmp = NULL;
3323 } else {
3324 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3325 return 1;
3326 }
3327 if (PyObject_HasAttrString(obj, "args")) {
3328 int res;
3329 tmp = PyObject_GetAttrString(obj, "args");
3330 if (tmp == NULL) goto failed;
3331 res = obj2ast_arguments(tmp, &args, arena);
3332 if (res != 0) goto failed;
3333 Py_XDECREF(tmp);
3334 tmp = NULL;
3335 } else {
3336 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3337 return 1;
3338 }
3339 if (PyObject_HasAttrString(obj, "body")) {
3340 int res;
3341 Py_ssize_t len;
3342 Py_ssize_t i;
3343 tmp = PyObject_GetAttrString(obj, "body");
3344 if (tmp == NULL) goto failed;
3345 if (!PyList_Check(tmp)) {
3346 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3347 goto failed;
3348 }
3349 len = PyList_GET_SIZE(tmp);
3350 body = asdl_seq_new(len, arena);
3351 if (body == NULL) goto failed;
3352 for (i = 0; i < len; i++) {
3353 stmt_ty value;
3354 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3355 if (res != 0) goto failed;
3356 asdl_seq_SET(body, i, value);
3357 }
3358 Py_XDECREF(tmp);
3359 tmp = NULL;
3360 } else {
3361 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3362 return 1;
3363 }
3364 if (PyObject_HasAttrString(obj, "decorator_list")) {
3365 int res;
3366 Py_ssize_t len;
3367 Py_ssize_t i;
3368 tmp = PyObject_GetAttrString(obj, "decorator_list");
3369 if (tmp == NULL) goto failed;
3370 if (!PyList_Check(tmp)) {
3371 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3372 goto failed;
3373 }
3374 len = PyList_GET_SIZE(tmp);
3375 decorator_list = asdl_seq_new(len, arena);
3376 if (decorator_list == NULL) goto failed;
3377 for (i = 0; i < len; i++) {
3378 expr_ty value;
3379 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3380 if (res != 0) goto failed;
3381 asdl_seq_SET(decorator_list, i, value);
3382 }
3383 Py_XDECREF(tmp);
3384 tmp = NULL;
3385 } else {
3386 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3387 return 1;
3388 }
3389 *out = FunctionDef(name, args, body, decorator_list, lineno,
3390 col_offset, arena);
3391 if (*out == NULL) goto failed;
3392 return 0;
3393 }
3394 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3395 identifier name;
3396 asdl_seq* bases;
3397 asdl_seq* body;
3398 asdl_seq* decorator_list;
3399
3400 if (PyObject_HasAttrString(obj, "name")) {
3401 int res;
3402 tmp = PyObject_GetAttrString(obj, "name");
3403 if (tmp == NULL) goto failed;
3404 res = obj2ast_identifier(tmp, &name, arena);
3405 if (res != 0) goto failed;
3406 Py_XDECREF(tmp);
3407 tmp = NULL;
3408 } else {
3409 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3410 return 1;
3411 }
3412 if (PyObject_HasAttrString(obj, "bases")) {
3413 int res;
3414 Py_ssize_t len;
3415 Py_ssize_t i;
3416 tmp = PyObject_GetAttrString(obj, "bases");
3417 if (tmp == NULL) goto failed;
3418 if (!PyList_Check(tmp)) {
3419 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3420 goto failed;
3421 }
3422 len = PyList_GET_SIZE(tmp);
3423 bases = asdl_seq_new(len, arena);
3424 if (bases == NULL) goto failed;
3425 for (i = 0; i < len; i++) {
3426 expr_ty value;
3427 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3428 if (res != 0) goto failed;
3429 asdl_seq_SET(bases, i, value);
3430 }
3431 Py_XDECREF(tmp);
3432 tmp = NULL;
3433 } else {
3434 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3435 return 1;
3436 }
3437 if (PyObject_HasAttrString(obj, "body")) {
3438 int res;
3439 Py_ssize_t len;
3440 Py_ssize_t i;
3441 tmp = PyObject_GetAttrString(obj, "body");
3442 if (tmp == NULL) goto failed;
3443 if (!PyList_Check(tmp)) {
3444 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3445 goto failed;
3446 }
3447 len = PyList_GET_SIZE(tmp);
3448 body = asdl_seq_new(len, arena);
3449 if (body == NULL) goto failed;
3450 for (i = 0; i < len; i++) {
3451 stmt_ty value;
3452 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3453 if (res != 0) goto failed;
3454 asdl_seq_SET(body, i, value);
3455 }
3456 Py_XDECREF(tmp);
3457 tmp = NULL;
3458 } else {
3459 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3460 return 1;
3461 }
3462 if (PyObject_HasAttrString(obj, "decorator_list")) {
3463 int res;
3464 Py_ssize_t len;
3465 Py_ssize_t i;
3466 tmp = PyObject_GetAttrString(obj, "decorator_list");
3467 if (tmp == NULL) goto failed;
3468 if (!PyList_Check(tmp)) {
3469 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3470 goto failed;
3471 }
3472 len = PyList_GET_SIZE(tmp);
3473 decorator_list = asdl_seq_new(len, arena);
3474 if (decorator_list == NULL) goto failed;
3475 for (i = 0; i < len; i++) {
3476 expr_ty value;
3477 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3478 if (res != 0) goto failed;
3479 asdl_seq_SET(decorator_list, i, value);
3480 }
3481 Py_XDECREF(tmp);
3482 tmp = NULL;
3483 } else {
3484 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3485 return 1;
3486 }
3487 *out = ClassDef(name, bases, body, decorator_list, lineno,
3488 col_offset, arena);
3489 if (*out == NULL) goto failed;
3490 return 0;
3491 }
3492 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3493 expr_ty value;
3494
3495 if (PyObject_HasAttrString(obj, "value")) {
3496 int res;
3497 tmp = PyObject_GetAttrString(obj, "value");
3498 if (tmp == NULL) goto failed;
3499 res = obj2ast_expr(tmp, &value, arena);
3500 if (res != 0) goto failed;
3501 Py_XDECREF(tmp);
3502 tmp = NULL;
3503 } else {
3504 value = NULL;
3505 }
3506 *out = Return(value, lineno, col_offset, arena);
3507 if (*out == NULL) goto failed;
3508 return 0;
3509 }
3510 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3511 asdl_seq* targets;
3512
3513 if (PyObject_HasAttrString(obj, "targets")) {
3514 int res;
3515 Py_ssize_t len;
3516 Py_ssize_t i;
3517 tmp = PyObject_GetAttrString(obj, "targets");
3518 if (tmp == NULL) goto failed;
3519 if (!PyList_Check(tmp)) {
3520 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3521 goto failed;
3522 }
3523 len = PyList_GET_SIZE(tmp);
3524 targets = asdl_seq_new(len, arena);
3525 if (targets == NULL) goto failed;
3526 for (i = 0; i < len; i++) {
3527 expr_ty value;
3528 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3529 if (res != 0) goto failed;
3530 asdl_seq_SET(targets, i, value);
3531 }
3532 Py_XDECREF(tmp);
3533 tmp = NULL;
3534 } else {
3535 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3536 return 1;
3537 }
3538 *out = Delete(targets, lineno, col_offset, arena);
3539 if (*out == NULL) goto failed;
3540 return 0;
3541 }
3542 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3543 asdl_seq* targets;
3544 expr_ty value;
3545
3546 if (PyObject_HasAttrString(obj, "targets")) {
3547 int res;
3548 Py_ssize_t len;
3549 Py_ssize_t i;
3550 tmp = PyObject_GetAttrString(obj, "targets");
3551 if (tmp == NULL) goto failed;
3552 if (!PyList_Check(tmp)) {
3553 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3554 goto failed;
3555 }
3556 len = PyList_GET_SIZE(tmp);
3557 targets = asdl_seq_new(len, arena);
3558 if (targets == NULL) goto failed;
3559 for (i = 0; i < len; i++) {
3560 expr_ty value;
3561 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3562 if (res != 0) goto failed;
3563 asdl_seq_SET(targets, i, value);
3564 }
3565 Py_XDECREF(tmp);
3566 tmp = NULL;
3567 } else {
3568 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3569 return 1;
3570 }
3571 if (PyObject_HasAttrString(obj, "value")) {
3572 int res;
3573 tmp = PyObject_GetAttrString(obj, "value");
3574 if (tmp == NULL) goto failed;
3575 res = obj2ast_expr(tmp, &value, arena);
3576 if (res != 0) goto failed;
3577 Py_XDECREF(tmp);
3578 tmp = NULL;
3579 } else {
3580 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3581 return 1;
3582 }
3583 *out = Assign(targets, value, lineno, col_offset, arena);
3584 if (*out == NULL) goto failed;
3585 return 0;
3586 }
3587 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3588 expr_ty target;
3589 operator_ty op;
3590 expr_ty value;
3591
3592 if (PyObject_HasAttrString(obj, "target")) {
3593 int res;
3594 tmp = PyObject_GetAttrString(obj, "target");
3595 if (tmp == NULL) goto failed;
3596 res = obj2ast_expr(tmp, &target, arena);
3597 if (res != 0) goto failed;
3598 Py_XDECREF(tmp);
3599 tmp = NULL;
3600 } else {
3601 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3602 return 1;
3603 }
3604 if (PyObject_HasAttrString(obj, "op")) {
3605 int res;
3606 tmp = PyObject_GetAttrString(obj, "op");
3607 if (tmp == NULL) goto failed;
3608 res = obj2ast_operator(tmp, &op, arena);
3609 if (res != 0) goto failed;
3610 Py_XDECREF(tmp);
3611 tmp = NULL;
3612 } else {
3613 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3614 return 1;
3615 }
3616 if (PyObject_HasAttrString(obj, "value")) {
3617 int res;
3618 tmp = PyObject_GetAttrString(obj, "value");
3619 if (tmp == NULL) goto failed;
3620 res = obj2ast_expr(tmp, &value, arena);
3621 if (res != 0) goto failed;
3622 Py_XDECREF(tmp);
3623 tmp = NULL;
3624 } else {
3625 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3626 return 1;
3627 }
3628 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3629 if (*out == NULL) goto failed;
3630 return 0;
3631 }
3632 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3633 expr_ty dest;
3634 asdl_seq* values;
3635 bool nl;
3636
3637 if (PyObject_HasAttrString(obj, "dest")) {
3638 int res;
3639 tmp = PyObject_GetAttrString(obj, "dest");
3640 if (tmp == NULL) goto failed;
3641 res = obj2ast_expr(tmp, &dest, arena);
3642 if (res != 0) goto failed;
3643 Py_XDECREF(tmp);
3644 tmp = NULL;
3645 } else {
3646 dest = NULL;
3647 }
3648 if (PyObject_HasAttrString(obj, "values")) {
3649 int res;
3650 Py_ssize_t len;
3651 Py_ssize_t i;
3652 tmp = PyObject_GetAttrString(obj, "values");
3653 if (tmp == NULL) goto failed;
3654 if (!PyList_Check(tmp)) {
3655 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3656 goto failed;
3657 }
3658 len = PyList_GET_SIZE(tmp);
3659 values = asdl_seq_new(len, arena);
3660 if (values == NULL) goto failed;
3661 for (i = 0; i < len; i++) {
3662 expr_ty value;
3663 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3664 if (res != 0) goto failed;
3665 asdl_seq_SET(values, i, value);
3666 }
3667 Py_XDECREF(tmp);
3668 tmp = NULL;
3669 } else {
3670 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3671 return 1;
3672 }
3673 if (PyObject_HasAttrString(obj, "nl")) {
3674 int res;
3675 tmp = PyObject_GetAttrString(obj, "nl");
3676 if (tmp == NULL) goto failed;
3677 res = obj2ast_bool(tmp, &nl, arena);
3678 if (res != 0) goto failed;
3679 Py_XDECREF(tmp);
3680 tmp = NULL;
3681 } else {
3682 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3683 return 1;
3684 }
3685 *out = Print(dest, values, nl, lineno, col_offset, arena);
3686 if (*out == NULL) goto failed;
3687 return 0;
3688 }
3689 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3690 expr_ty target;
3691 expr_ty iter;
3692 asdl_seq* body;
3693 asdl_seq* orelse;
3694
3695 if (PyObject_HasAttrString(obj, "target")) {
3696 int res;
3697 tmp = PyObject_GetAttrString(obj, "target");
3698 if (tmp == NULL) goto failed;
3699 res = obj2ast_expr(tmp, &target, arena);
3700 if (res != 0) goto failed;
3701 Py_XDECREF(tmp);
3702 tmp = NULL;
3703 } else {
3704 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3705 return 1;
3706 }
3707 if (PyObject_HasAttrString(obj, "iter")) {
3708 int res;
3709 tmp = PyObject_GetAttrString(obj, "iter");
3710 if (tmp == NULL) goto failed;
3711 res = obj2ast_expr(tmp, &iter, arena);
3712 if (res != 0) goto failed;
3713 Py_XDECREF(tmp);
3714 tmp = NULL;
3715 } else {
3716 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3717 return 1;
3718 }
3719 if (PyObject_HasAttrString(obj, "body")) {
3720 int res;
3721 Py_ssize_t len;
3722 Py_ssize_t i;
3723 tmp = PyObject_GetAttrString(obj, "body");
3724 if (tmp == NULL) goto failed;
3725 if (!PyList_Check(tmp)) {
3726 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3727 goto failed;
3728 }
3729 len = PyList_GET_SIZE(tmp);
3730 body = asdl_seq_new(len, arena);
3731 if (body == NULL) goto failed;
3732 for (i = 0; i < len; i++) {
3733 stmt_ty value;
3734 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3735 if (res != 0) goto failed;
3736 asdl_seq_SET(body, i, value);
3737 }
3738 Py_XDECREF(tmp);
3739 tmp = NULL;
3740 } else {
3741 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3742 return 1;
3743 }
3744 if (PyObject_HasAttrString(obj, "orelse")) {
3745 int res;
3746 Py_ssize_t len;
3747 Py_ssize_t i;
3748 tmp = PyObject_GetAttrString(obj, "orelse");
3749 if (tmp == NULL) goto failed;
3750 if (!PyList_Check(tmp)) {
3751 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3752 goto failed;
3753 }
3754 len = PyList_GET_SIZE(tmp);
3755 orelse = asdl_seq_new(len, arena);
3756 if (orelse == NULL) goto failed;
3757 for (i = 0; i < len; i++) {
3758 stmt_ty value;
3759 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3760 if (res != 0) goto failed;
3761 asdl_seq_SET(orelse, i, value);
3762 }
3763 Py_XDECREF(tmp);
3764 tmp = NULL;
3765 } else {
3766 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3767 return 1;
3768 }
3769 *out = For(target, iter, body, orelse, lineno, col_offset,
3770 arena);
3771 if (*out == NULL) goto failed;
3772 return 0;
3773 }
3774 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3775 expr_ty test;
3776 asdl_seq* body;
3777 asdl_seq* orelse;
3778
3779 if (PyObject_HasAttrString(obj, "test")) {
3780 int res;
3781 tmp = PyObject_GetAttrString(obj, "test");
3782 if (tmp == NULL) goto failed;
3783 res = obj2ast_expr(tmp, &test, arena);
3784 if (res != 0) goto failed;
3785 Py_XDECREF(tmp);
3786 tmp = NULL;
3787 } else {
3788 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3789 return 1;
3790 }
3791 if (PyObject_HasAttrString(obj, "body")) {
3792 int res;
3793 Py_ssize_t len;
3794 Py_ssize_t i;
3795 tmp = PyObject_GetAttrString(obj, "body");
3796 if (tmp == NULL) goto failed;
3797 if (!PyList_Check(tmp)) {
3798 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3799 goto failed;
3800 }
3801 len = PyList_GET_SIZE(tmp);
3802 body = asdl_seq_new(len, arena);
3803 if (body == NULL) goto failed;
3804 for (i = 0; i < len; i++) {
3805 stmt_ty value;
3806 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3807 if (res != 0) goto failed;
3808 asdl_seq_SET(body, i, value);
3809 }
3810 Py_XDECREF(tmp);
3811 tmp = NULL;
3812 } else {
3813 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3814 return 1;
3815 }
3816 if (PyObject_HasAttrString(obj, "orelse")) {
3817 int res;
3818 Py_ssize_t len;
3819 Py_ssize_t i;
3820 tmp = PyObject_GetAttrString(obj, "orelse");
3821 if (tmp == NULL) goto failed;
3822 if (!PyList_Check(tmp)) {
3823 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3824 goto failed;
3825 }
3826 len = PyList_GET_SIZE(tmp);
3827 orelse = asdl_seq_new(len, arena);
3828 if (orelse == NULL) goto failed;
3829 for (i = 0; i < len; i++) {
3830 stmt_ty value;
3831 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3832 if (res != 0) goto failed;
3833 asdl_seq_SET(orelse, i, value);
3834 }
3835 Py_XDECREF(tmp);
3836 tmp = NULL;
3837 } else {
3838 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3839 return 1;
3840 }
3841 *out = While(test, body, orelse, lineno, col_offset, arena);
3842 if (*out == NULL) goto failed;
3843 return 0;
3844 }
3845 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3846 expr_ty test;
3847 asdl_seq* body;
3848 asdl_seq* orelse;
3849
3850 if (PyObject_HasAttrString(obj, "test")) {
3851 int res;
3852 tmp = PyObject_GetAttrString(obj, "test");
3853 if (tmp == NULL) goto failed;
3854 res = obj2ast_expr(tmp, &test, arena);
3855 if (res != 0) goto failed;
3856 Py_XDECREF(tmp);
3857 tmp = NULL;
3858 } else {
3859 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3860 return 1;
3861 }
3862 if (PyObject_HasAttrString(obj, "body")) {
3863 int res;
3864 Py_ssize_t len;
3865 Py_ssize_t i;
3866 tmp = PyObject_GetAttrString(obj, "body");
3867 if (tmp == NULL) goto failed;
3868 if (!PyList_Check(tmp)) {
3869 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3870 goto failed;
3871 }
3872 len = PyList_GET_SIZE(tmp);
3873 body = asdl_seq_new(len, arena);
3874 if (body == NULL) goto failed;
3875 for (i = 0; i < len; i++) {
3876 stmt_ty value;
3877 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3878 if (res != 0) goto failed;
3879 asdl_seq_SET(body, i, value);
3880 }
3881 Py_XDECREF(tmp);
3882 tmp = NULL;
3883 } else {
3884 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3885 return 1;
3886 }
3887 if (PyObject_HasAttrString(obj, "orelse")) {
3888 int res;
3889 Py_ssize_t len;
3890 Py_ssize_t i;
3891 tmp = PyObject_GetAttrString(obj, "orelse");
3892 if (tmp == NULL) goto failed;
3893 if (!PyList_Check(tmp)) {
3894 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3895 goto failed;
3896 }
3897 len = PyList_GET_SIZE(tmp);
3898 orelse = asdl_seq_new(len, arena);
3899 if (orelse == NULL) goto failed;
3900 for (i = 0; i < len; i++) {
3901 stmt_ty value;
3902 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3903 if (res != 0) goto failed;
3904 asdl_seq_SET(orelse, i, value);
3905 }
3906 Py_XDECREF(tmp);
3907 tmp = NULL;
3908 } else {
3909 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3910 return 1;
3911 }
3912 *out = If(test, body, orelse, lineno, col_offset, arena);
3913 if (*out == NULL) goto failed;
3914 return 0;
3915 }
3916 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3917 expr_ty context_expr;
3918 expr_ty optional_vars;
3919 asdl_seq* body;
3920
3921 if (PyObject_HasAttrString(obj, "context_expr")) {
3922 int res;
3923 tmp = PyObject_GetAttrString(obj, "context_expr");
3924 if (tmp == NULL) goto failed;
3925 res = obj2ast_expr(tmp, &context_expr, arena);
3926 if (res != 0) goto failed;
3927 Py_XDECREF(tmp);
3928 tmp = NULL;
3929 } else {
3930 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3931 return 1;
3932 }
3933 if (PyObject_HasAttrString(obj, "optional_vars")) {
3934 int res;
3935 tmp = PyObject_GetAttrString(obj, "optional_vars");
3936 if (tmp == NULL) goto failed;
3937 res = obj2ast_expr(tmp, &optional_vars, arena);
3938 if (res != 0) goto failed;
3939 Py_XDECREF(tmp);
3940 tmp = NULL;
3941 } else {
3942 optional_vars = NULL;
3943 }
3944 if (PyObject_HasAttrString(obj, "body")) {
3945 int res;
3946 Py_ssize_t len;
3947 Py_ssize_t i;
3948 tmp = PyObject_GetAttrString(obj, "body");
3949 if (tmp == NULL) goto failed;
3950 if (!PyList_Check(tmp)) {
3951 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3952 goto failed;
3953 }
3954 len = PyList_GET_SIZE(tmp);
3955 body = asdl_seq_new(len, arena);
3956 if (body == NULL) goto failed;
3957 for (i = 0; i < len; i++) {
3958 stmt_ty value;
3959 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3960 if (res != 0) goto failed;
3961 asdl_seq_SET(body, i, value);
3962 }
3963 Py_XDECREF(tmp);
3964 tmp = NULL;
3965 } else {
3966 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3967 return 1;
3968 }
3969 *out = With(context_expr, optional_vars, body, lineno,
3970 col_offset, arena);
3971 if (*out == NULL) goto failed;
3972 return 0;
3973 }
3974 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3975 expr_ty type;
3976 expr_ty inst;
3977 expr_ty tback;
3978
3979 if (PyObject_HasAttrString(obj, "type")) {
3980 int res;
3981 tmp = PyObject_GetAttrString(obj, "type");
3982 if (tmp == NULL) goto failed;
3983 res = obj2ast_expr(tmp, &type, arena);
3984 if (res != 0) goto failed;
3985 Py_XDECREF(tmp);
3986 tmp = NULL;
3987 } else {
3988 type = NULL;
3989 }
3990 if (PyObject_HasAttrString(obj, "inst")) {
3991 int res;
3992 tmp = PyObject_GetAttrString(obj, "inst");
3993 if (tmp == NULL) goto failed;
3994 res = obj2ast_expr(tmp, &inst, arena);
3995 if (res != 0) goto failed;
3996 Py_XDECREF(tmp);
3997 tmp = NULL;
3998 } else {
3999 inst = NULL;
4000 }
4001 if (PyObject_HasAttrString(obj, "tback")) {
4002 int res;
4003 tmp = PyObject_GetAttrString(obj, "tback");
4004 if (tmp == NULL) goto failed;
4005 res = obj2ast_expr(tmp, &tback, arena);
4006 if (res != 0) goto failed;
4007 Py_XDECREF(tmp);
4008 tmp = NULL;
4009 } else {
4010 tback = NULL;
4011 }
4012 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4013 if (*out == NULL) goto failed;
4014 return 0;
4015 }
4016 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4017 asdl_seq* body;
4018 asdl_seq* handlers;
4019 asdl_seq* orelse;
4020
4021 if (PyObject_HasAttrString(obj, "body")) {
4022 int res;
4023 Py_ssize_t len;
4024 Py_ssize_t i;
4025 tmp = PyObject_GetAttrString(obj, "body");
4026 if (tmp == NULL) goto failed;
4027 if (!PyList_Check(tmp)) {
4028 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4029 goto failed;
4030 }
4031 len = PyList_GET_SIZE(tmp);
4032 body = asdl_seq_new(len, arena);
4033 if (body == NULL) goto failed;
4034 for (i = 0; i < len; i++) {
4035 stmt_ty value;
4036 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4037 if (res != 0) goto failed;
4038 asdl_seq_SET(body, i, value);
4039 }
4040 Py_XDECREF(tmp);
4041 tmp = NULL;
4042 } else {
4043 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4044 return 1;
4045 }
4046 if (PyObject_HasAttrString(obj, "handlers")) {
4047 int res;
4048 Py_ssize_t len;
4049 Py_ssize_t i;
4050 tmp = PyObject_GetAttrString(obj, "handlers");
4051 if (tmp == NULL) goto failed;
4052 if (!PyList_Check(tmp)) {
4053 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4054 goto failed;
4055 }
4056 len = PyList_GET_SIZE(tmp);
4057 handlers = asdl_seq_new(len, arena);
4058 if (handlers == NULL) goto failed;
4059 for (i = 0; i < len; i++) {
4060 excepthandler_ty value;
4061 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4062 if (res != 0) goto failed;
4063 asdl_seq_SET(handlers, i, value);
4064 }
4065 Py_XDECREF(tmp);
4066 tmp = NULL;
4067 } else {
4068 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4069 return 1;
4070 }
4071 if (PyObject_HasAttrString(obj, "orelse")) {
4072 int res;
4073 Py_ssize_t len;
4074 Py_ssize_t i;
4075 tmp = PyObject_GetAttrString(obj, "orelse");
4076 if (tmp == NULL) goto failed;
4077 if (!PyList_Check(tmp)) {
4078 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4079 goto failed;
4080 }
4081 len = PyList_GET_SIZE(tmp);
4082 orelse = asdl_seq_new(len, arena);
4083 if (orelse == NULL) goto failed;
4084 for (i = 0; i < len; i++) {
4085 stmt_ty value;
4086 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4087 if (res != 0) goto failed;
4088 asdl_seq_SET(orelse, i, value);
4089 }
4090 Py_XDECREF(tmp);
4091 tmp = NULL;
4092 } else {
4093 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4094 return 1;
4095 }
4096 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4097 arena);
4098 if (*out == NULL) goto failed;
4099 return 0;
4100 }
4101 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4102 asdl_seq* body;
4103 asdl_seq* finalbody;
4104
4105 if (PyObject_HasAttrString(obj, "body")) {
4106 int res;
4107 Py_ssize_t len;
4108 Py_ssize_t i;
4109 tmp = PyObject_GetAttrString(obj, "body");
4110 if (tmp == NULL) goto failed;
4111 if (!PyList_Check(tmp)) {
4112 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4113 goto failed;
4114 }
4115 len = PyList_GET_SIZE(tmp);
4116 body = asdl_seq_new(len, arena);
4117 if (body == NULL) goto failed;
4118 for (i = 0; i < len; i++) {
4119 stmt_ty value;
4120 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4121 if (res != 0) goto failed;
4122 asdl_seq_SET(body, i, value);
4123 }
4124 Py_XDECREF(tmp);
4125 tmp = NULL;
4126 } else {
4127 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4128 return 1;
4129 }
4130 if (PyObject_HasAttrString(obj, "finalbody")) {
4131 int res;
4132 Py_ssize_t len;
4133 Py_ssize_t i;
4134 tmp = PyObject_GetAttrString(obj, "finalbody");
4135 if (tmp == NULL) goto failed;
4136 if (!PyList_Check(tmp)) {
4137 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4138 goto failed;
4139 }
4140 len = PyList_GET_SIZE(tmp);
4141 finalbody = asdl_seq_new(len, arena);
4142 if (finalbody == NULL) goto failed;
4143 for (i = 0; i < len; i++) {
4144 stmt_ty value;
4145 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4146 if (res != 0) goto failed;
4147 asdl_seq_SET(finalbody, i, value);
4148 }
4149 Py_XDECREF(tmp);
4150 tmp = NULL;
4151 } else {
4152 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4153 return 1;
4154 }
4155 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4156 if (*out == NULL) goto failed;
4157 return 0;
4158 }
4159 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4160 expr_ty test;
4161 expr_ty msg;
4162
4163 if (PyObject_HasAttrString(obj, "test")) {
4164 int res;
4165 tmp = PyObject_GetAttrString(obj, "test");
4166 if (tmp == NULL) goto failed;
4167 res = obj2ast_expr(tmp, &test, arena);
4168 if (res != 0) goto failed;
4169 Py_XDECREF(tmp);
4170 tmp = NULL;
4171 } else {
4172 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4173 return 1;
4174 }
4175 if (PyObject_HasAttrString(obj, "msg")) {
4176 int res;
4177 tmp = PyObject_GetAttrString(obj, "msg");
4178 if (tmp == NULL) goto failed;
4179 res = obj2ast_expr(tmp, &msg, arena);
4180 if (res != 0) goto failed;
4181 Py_XDECREF(tmp);
4182 tmp = NULL;
4183 } else {
4184 msg = NULL;
4185 }
4186 *out = Assert(test, msg, lineno, col_offset, arena);
4187 if (*out == NULL) goto failed;
4188 return 0;
4189 }
4190 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4191 asdl_seq* names;
4192
4193 if (PyObject_HasAttrString(obj, "names")) {
4194 int res;
4195 Py_ssize_t len;
4196 Py_ssize_t i;
4197 tmp = PyObject_GetAttrString(obj, "names");
4198 if (tmp == NULL) goto failed;
4199 if (!PyList_Check(tmp)) {
4200 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4201 goto failed;
4202 }
4203 len = PyList_GET_SIZE(tmp);
4204 names = asdl_seq_new(len, arena);
4205 if (names == NULL) goto failed;
4206 for (i = 0; i < len; i++) {
4207 alias_ty value;
4208 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4209 if (res != 0) goto failed;
4210 asdl_seq_SET(names, i, value);
4211 }
4212 Py_XDECREF(tmp);
4213 tmp = NULL;
4214 } else {
4215 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4216 return 1;
4217 }
4218 *out = Import(names, lineno, col_offset, arena);
4219 if (*out == NULL) goto failed;
4220 return 0;
4221 }
4222 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4223 identifier module;
4224 asdl_seq* names;
4225 int level;
4226
4227 if (PyObject_HasAttrString(obj, "module")) {
4228 int res;
4229 tmp = PyObject_GetAttrString(obj, "module");
4230 if (tmp == NULL) goto failed;
4231 res = obj2ast_identifier(tmp, &module, arena);
4232 if (res != 0) goto failed;
4233 Py_XDECREF(tmp);
4234 tmp = NULL;
4235 } else {
4236 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4237 return 1;
4238 }
4239 if (PyObject_HasAttrString(obj, "names")) {
4240 int res;
4241 Py_ssize_t len;
4242 Py_ssize_t i;
4243 tmp = PyObject_GetAttrString(obj, "names");
4244 if (tmp == NULL) goto failed;
4245 if (!PyList_Check(tmp)) {
4246 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4247 goto failed;
4248 }
4249 len = PyList_GET_SIZE(tmp);
4250 names = asdl_seq_new(len, arena);
4251 if (names == NULL) goto failed;
4252 for (i = 0; i < len; i++) {
4253 alias_ty value;
4254 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4255 if (res != 0) goto failed;
4256 asdl_seq_SET(names, i, value);
4257 }
4258 Py_XDECREF(tmp);
4259 tmp = NULL;
4260 } else {
4261 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4262 return 1;
4263 }
4264 if (PyObject_HasAttrString(obj, "level")) {
4265 int res;
4266 tmp = PyObject_GetAttrString(obj, "level");
4267 if (tmp == NULL) goto failed;
4268 res = obj2ast_int(tmp, &level, arena);
4269 if (res != 0) goto failed;
4270 Py_XDECREF(tmp);
4271 tmp = NULL;
4272 } else {
4273 level = 0;
4274 }
4275 *out = ImportFrom(module, names, level, lineno, col_offset,
4276 arena);
4277 if (*out == NULL) goto failed;
4278 return 0;
4279 }
4280 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4281 expr_ty body;
4282 expr_ty globals;
4283 expr_ty locals;
4284
4285 if (PyObject_HasAttrString(obj, "body")) {
4286 int res;
4287 tmp = PyObject_GetAttrString(obj, "body");
4288 if (tmp == NULL) goto failed;
4289 res = obj2ast_expr(tmp, &body, arena);
4290 if (res != 0) goto failed;
4291 Py_XDECREF(tmp);
4292 tmp = NULL;
4293 } else {
4294 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4295 return 1;
4296 }
4297 if (PyObject_HasAttrString(obj, "globals")) {
4298 int res;
4299 tmp = PyObject_GetAttrString(obj, "globals");
4300 if (tmp == NULL) goto failed;
4301 res = obj2ast_expr(tmp, &globals, arena);
4302 if (res != 0) goto failed;
4303 Py_XDECREF(tmp);
4304 tmp = NULL;
4305 } else {
4306 globals = NULL;
4307 }
4308 if (PyObject_HasAttrString(obj, "locals")) {
4309 int res;
4310 tmp = PyObject_GetAttrString(obj, "locals");
4311 if (tmp == NULL) goto failed;
4312 res = obj2ast_expr(tmp, &locals, arena);
4313 if (res != 0) goto failed;
4314 Py_XDECREF(tmp);
4315 tmp = NULL;
4316 } else {
4317 locals = NULL;
4318 }
4319 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4320 if (*out == NULL) goto failed;
4321 return 0;
4322 }
4323 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4324 asdl_seq* names;
4325
4326 if (PyObject_HasAttrString(obj, "names")) {
4327 int res;
4328 Py_ssize_t len;
4329 Py_ssize_t i;
4330 tmp = PyObject_GetAttrString(obj, "names");
4331 if (tmp == NULL) goto failed;
4332 if (!PyList_Check(tmp)) {
4333 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4334 goto failed;
4335 }
4336 len = PyList_GET_SIZE(tmp);
4337 names = asdl_seq_new(len, arena);
4338 if (names == NULL) goto failed;
4339 for (i = 0; i < len; i++) {
4340 identifier value;
4341 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4342 if (res != 0) goto failed;
4343 asdl_seq_SET(names, i, value);
4344 }
4345 Py_XDECREF(tmp);
4346 tmp = NULL;
4347 } else {
4348 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4349 return 1;
4350 }
4351 *out = Global(names, lineno, col_offset, arena);
4352 if (*out == NULL) goto failed;
4353 return 0;
4354 }
4355 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4356 expr_ty value;
4357
4358 if (PyObject_HasAttrString(obj, "value")) {
4359 int res;
4360 tmp = PyObject_GetAttrString(obj, "value");
4361 if (tmp == NULL) goto failed;
4362 res = obj2ast_expr(tmp, &value, arena);
4363 if (res != 0) goto failed;
4364 Py_XDECREF(tmp);
4365 tmp = NULL;
4366 } else {
4367 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4368 return 1;
4369 }
4370 *out = Expr(value, lineno, col_offset, arena);
4371 if (*out == NULL) goto failed;
4372 return 0;
4373 }
4374 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4375
4376 *out = Pass(lineno, col_offset, arena);
4377 if (*out == NULL) goto failed;
4378 return 0;
4379 }
4380 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4381
4382 *out = Break(lineno, col_offset, arena);
4383 if (*out == NULL) goto failed;
4384 return 0;
4385 }
4386 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4387
4388 *out = Continue(lineno, col_offset, arena);
4389 if (*out == NULL) goto failed;
4390 return 0;
4391 }
4392
4393 tmp = PyObject_Repr(obj);
4394 if (tmp == NULL) goto failed;
4395 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4396failed:
4397 Py_XDECREF(tmp);
4398 return 1;
4399}
4400
4401int
4402obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4403{
4404 PyObject* tmp = NULL;
4405
4406 int lineno;
4407 int col_offset;
4408
4409 if (obj == Py_None) {
4410 *out = NULL;
4411 return 0;
4412 }
4413 if (PyObject_HasAttrString(obj, "lineno")) {
4414 int res;
4415 tmp = PyObject_GetAttrString(obj, "lineno");
4416 if (tmp == NULL) goto failed;
4417 res = obj2ast_int(tmp, &lineno, arena);
4418 if (res != 0) goto failed;
4419 Py_XDECREF(tmp);
4420 tmp = NULL;
4421 } else {
4422 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4423 return 1;
4424 }
4425 if (PyObject_HasAttrString(obj, "col_offset")) {
4426 int res;
4427 tmp = PyObject_GetAttrString(obj, "col_offset");
4428 if (tmp == NULL) goto failed;
4429 res = obj2ast_int(tmp, &col_offset, arena);
4430 if (res != 0) goto failed;
4431 Py_XDECREF(tmp);
4432 tmp = NULL;
4433 } else {
4434 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4435 return 1;
4436 }
4437 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4438 boolop_ty op;
4439 asdl_seq* values;
4440
4441 if (PyObject_HasAttrString(obj, "op")) {
4442 int res;
4443 tmp = PyObject_GetAttrString(obj, "op");
4444 if (tmp == NULL) goto failed;
4445 res = obj2ast_boolop(tmp, &op, arena);
4446 if (res != 0) goto failed;
4447 Py_XDECREF(tmp);
4448 tmp = NULL;
4449 } else {
4450 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4451 return 1;
4452 }
4453 if (PyObject_HasAttrString(obj, "values")) {
4454 int res;
4455 Py_ssize_t len;
4456 Py_ssize_t i;
4457 tmp = PyObject_GetAttrString(obj, "values");
4458 if (tmp == NULL) goto failed;
4459 if (!PyList_Check(tmp)) {
4460 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4461 goto failed;
4462 }
4463 len = PyList_GET_SIZE(tmp);
4464 values = asdl_seq_new(len, arena);
4465 if (values == NULL) goto failed;
4466 for (i = 0; i < len; i++) {
4467 expr_ty value;
4468 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4469 if (res != 0) goto failed;
4470 asdl_seq_SET(values, i, value);
4471 }
4472 Py_XDECREF(tmp);
4473 tmp = NULL;
4474 } else {
4475 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4476 return 1;
4477 }
4478 *out = BoolOp(op, values, lineno, col_offset, arena);
4479 if (*out == NULL) goto failed;
4480 return 0;
4481 }
4482 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4483 expr_ty left;
4484 operator_ty op;
4485 expr_ty right;
4486
4487 if (PyObject_HasAttrString(obj, "left")) {
4488 int res;
4489 tmp = PyObject_GetAttrString(obj, "left");
4490 if (tmp == NULL) goto failed;
4491 res = obj2ast_expr(tmp, &left, arena);
4492 if (res != 0) goto failed;
4493 Py_XDECREF(tmp);
4494 tmp = NULL;
4495 } else {
4496 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4497 return 1;
4498 }
4499 if (PyObject_HasAttrString(obj, "op")) {
4500 int res;
4501 tmp = PyObject_GetAttrString(obj, "op");
4502 if (tmp == NULL) goto failed;
4503 res = obj2ast_operator(tmp, &op, arena);
4504 if (res != 0) goto failed;
4505 Py_XDECREF(tmp);
4506 tmp = NULL;
4507 } else {
4508 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4509 return 1;
4510 }
4511 if (PyObject_HasAttrString(obj, "right")) {
4512 int res;
4513 tmp = PyObject_GetAttrString(obj, "right");
4514 if (tmp == NULL) goto failed;
4515 res = obj2ast_expr(tmp, &right, arena);
4516 if (res != 0) goto failed;
4517 Py_XDECREF(tmp);
4518 tmp = NULL;
4519 } else {
4520 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4521 return 1;
4522 }
4523 *out = BinOp(left, op, right, lineno, col_offset, arena);
4524 if (*out == NULL) goto failed;
4525 return 0;
4526 }
4527 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4528 unaryop_ty op;
4529 expr_ty operand;
4530
4531 if (PyObject_HasAttrString(obj, "op")) {
4532 int res;
4533 tmp = PyObject_GetAttrString(obj, "op");
4534 if (tmp == NULL) goto failed;
4535 res = obj2ast_unaryop(tmp, &op, arena);
4536 if (res != 0) goto failed;
4537 Py_XDECREF(tmp);
4538 tmp = NULL;
4539 } else {
4540 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4541 return 1;
4542 }
4543 if (PyObject_HasAttrString(obj, "operand")) {
4544 int res;
4545 tmp = PyObject_GetAttrString(obj, "operand");
4546 if (tmp == NULL) goto failed;
4547 res = obj2ast_expr(tmp, &operand, arena);
4548 if (res != 0) goto failed;
4549 Py_XDECREF(tmp);
4550 tmp = NULL;
4551 } else {
4552 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4553 return 1;
4554 }
4555 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4556 if (*out == NULL) goto failed;
4557 return 0;
4558 }
4559 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4560 arguments_ty args;
4561 expr_ty body;
4562
4563 if (PyObject_HasAttrString(obj, "args")) {
4564 int res;
4565 tmp = PyObject_GetAttrString(obj, "args");
4566 if (tmp == NULL) goto failed;
4567 res = obj2ast_arguments(tmp, &args, arena);
4568 if (res != 0) goto failed;
4569 Py_XDECREF(tmp);
4570 tmp = NULL;
4571 } else {
4572 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4573 return 1;
4574 }
4575 if (PyObject_HasAttrString(obj, "body")) {
4576 int res;
4577 tmp = PyObject_GetAttrString(obj, "body");
4578 if (tmp == NULL) goto failed;
4579 res = obj2ast_expr(tmp, &body, arena);
4580 if (res != 0) goto failed;
4581 Py_XDECREF(tmp);
4582 tmp = NULL;
4583 } else {
4584 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4585 return 1;
4586 }
4587 *out = Lambda(args, body, lineno, col_offset, arena);
4588 if (*out == NULL) goto failed;
4589 return 0;
4590 }
4591 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4592 expr_ty test;
4593 expr_ty body;
4594 expr_ty orelse;
4595
4596 if (PyObject_HasAttrString(obj, "test")) {
4597 int res;
4598 tmp = PyObject_GetAttrString(obj, "test");
4599 if (tmp == NULL) goto failed;
4600 res = obj2ast_expr(tmp, &test, arena);
4601 if (res != 0) goto failed;
4602 Py_XDECREF(tmp);
4603 tmp = NULL;
4604 } else {
4605 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4606 return 1;
4607 }
4608 if (PyObject_HasAttrString(obj, "body")) {
4609 int res;
4610 tmp = PyObject_GetAttrString(obj, "body");
4611 if (tmp == NULL) goto failed;
4612 res = obj2ast_expr(tmp, &body, arena);
4613 if (res != 0) goto failed;
4614 Py_XDECREF(tmp);
4615 tmp = NULL;
4616 } else {
4617 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4618 return 1;
4619 }
4620 if (PyObject_HasAttrString(obj, "orelse")) {
4621 int res;
4622 tmp = PyObject_GetAttrString(obj, "orelse");
4623 if (tmp == NULL) goto failed;
4624 res = obj2ast_expr(tmp, &orelse, arena);
4625 if (res != 0) goto failed;
4626 Py_XDECREF(tmp);
4627 tmp = NULL;
4628 } else {
4629 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4630 return 1;
4631 }
4632 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4633 if (*out == NULL) goto failed;
4634 return 0;
4635 }
4636 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4637 asdl_seq* keys;
4638 asdl_seq* values;
4639
4640 if (PyObject_HasAttrString(obj, "keys")) {
4641 int res;
4642 Py_ssize_t len;
4643 Py_ssize_t i;
4644 tmp = PyObject_GetAttrString(obj, "keys");
4645 if (tmp == NULL) goto failed;
4646 if (!PyList_Check(tmp)) {
4647 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4648 goto failed;
4649 }
4650 len = PyList_GET_SIZE(tmp);
4651 keys = asdl_seq_new(len, arena);
4652 if (keys == NULL) goto failed;
4653 for (i = 0; i < len; i++) {
4654 expr_ty value;
4655 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4656 if (res != 0) goto failed;
4657 asdl_seq_SET(keys, i, value);
4658 }
4659 Py_XDECREF(tmp);
4660 tmp = NULL;
4661 } else {
4662 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4663 return 1;
4664 }
4665 if (PyObject_HasAttrString(obj, "values")) {
4666 int res;
4667 Py_ssize_t len;
4668 Py_ssize_t i;
4669 tmp = PyObject_GetAttrString(obj, "values");
4670 if (tmp == NULL) goto failed;
4671 if (!PyList_Check(tmp)) {
4672 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4673 goto failed;
4674 }
4675 len = PyList_GET_SIZE(tmp);
4676 values = asdl_seq_new(len, arena);
4677 if (values == NULL) goto failed;
4678 for (i = 0; i < len; i++) {
4679 expr_ty value;
4680 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4681 if (res != 0) goto failed;
4682 asdl_seq_SET(values, i, value);
4683 }
4684 Py_XDECREF(tmp);
4685 tmp = NULL;
4686 } else {
4687 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4688 return 1;
4689 }
4690 *out = Dict(keys, values, lineno, col_offset, arena);
4691 if (*out == NULL) goto failed;
4692 return 0;
4693 }
4694 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4695 expr_ty elt;
4696 asdl_seq* generators;
4697
4698 if (PyObject_HasAttrString(obj, "elt")) {
4699 int res;
4700 tmp = PyObject_GetAttrString(obj, "elt");
4701 if (tmp == NULL) goto failed;
4702 res = obj2ast_expr(tmp, &elt, arena);
4703 if (res != 0) goto failed;
4704 Py_XDECREF(tmp);
4705 tmp = NULL;
4706 } else {
4707 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4708 return 1;
4709 }
4710 if (PyObject_HasAttrString(obj, "generators")) {
4711 int res;
4712 Py_ssize_t len;
4713 Py_ssize_t i;
4714 tmp = PyObject_GetAttrString(obj, "generators");
4715 if (tmp == NULL) goto failed;
4716 if (!PyList_Check(tmp)) {
4717 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4718 goto failed;
4719 }
4720 len = PyList_GET_SIZE(tmp);
4721 generators = asdl_seq_new(len, arena);
4722 if (generators == NULL) goto failed;
4723 for (i = 0; i < len; i++) {
4724 comprehension_ty value;
4725 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4726 if (res != 0) goto failed;
4727 asdl_seq_SET(generators, i, value);
4728 }
4729 Py_XDECREF(tmp);
4730 tmp = NULL;
4731 } else {
4732 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4733 return 1;
4734 }
4735 *out = ListComp(elt, generators, lineno, col_offset, arena);
4736 if (*out == NULL) goto failed;
4737 return 0;
4738 }
4739 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4740 expr_ty elt;
4741 asdl_seq* generators;
4742
4743 if (PyObject_HasAttrString(obj, "elt")) {
4744 int res;
4745 tmp = PyObject_GetAttrString(obj, "elt");
4746 if (tmp == NULL) goto failed;
4747 res = obj2ast_expr(tmp, &elt, arena);
4748 if (res != 0) goto failed;
4749 Py_XDECREF(tmp);
4750 tmp = NULL;
4751 } else {
4752 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4753 return 1;
4754 }
4755 if (PyObject_HasAttrString(obj, "generators")) {
4756 int res;
4757 Py_ssize_t len;
4758 Py_ssize_t i;
4759 tmp = PyObject_GetAttrString(obj, "generators");
4760 if (tmp == NULL) goto failed;
4761 if (!PyList_Check(tmp)) {
4762 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4763 goto failed;
4764 }
4765 len = PyList_GET_SIZE(tmp);
4766 generators = asdl_seq_new(len, arena);
4767 if (generators == NULL) goto failed;
4768 for (i = 0; i < len; i++) {
4769 comprehension_ty value;
4770 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4771 if (res != 0) goto failed;
4772 asdl_seq_SET(generators, i, value);
4773 }
4774 Py_XDECREF(tmp);
4775 tmp = NULL;
4776 } else {
4777 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4778 return 1;
4779 }
4780 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4781 if (*out == NULL) goto failed;
4782 return 0;
4783 }
4784 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4785 expr_ty value;
4786
4787 if (PyObject_HasAttrString(obj, "value")) {
4788 int res;
4789 tmp = PyObject_GetAttrString(obj, "value");
4790 if (tmp == NULL) goto failed;
4791 res = obj2ast_expr(tmp, &value, arena);
4792 if (res != 0) goto failed;
4793 Py_XDECREF(tmp);
4794 tmp = NULL;
4795 } else {
4796 value = NULL;
4797 }
4798 *out = Yield(value, lineno, col_offset, arena);
4799 if (*out == NULL) goto failed;
4800 return 0;
4801 }
4802 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4803 expr_ty left;
4804 asdl_int_seq* ops;
4805 asdl_seq* comparators;
4806
4807 if (PyObject_HasAttrString(obj, "left")) {
4808 int res;
4809 tmp = PyObject_GetAttrString(obj, "left");
4810 if (tmp == NULL) goto failed;
4811 res = obj2ast_expr(tmp, &left, arena);
4812 if (res != 0) goto failed;
4813 Py_XDECREF(tmp);
4814 tmp = NULL;
4815 } else {
4816 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4817 return 1;
4818 }
4819 if (PyObject_HasAttrString(obj, "ops")) {
4820 int res;
4821 Py_ssize_t len;
4822 Py_ssize_t i;
4823 tmp = PyObject_GetAttrString(obj, "ops");
4824 if (tmp == NULL) goto failed;
4825 if (!PyList_Check(tmp)) {
4826 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4827 goto failed;
4828 }
4829 len = PyList_GET_SIZE(tmp);
4830 ops = asdl_int_seq_new(len, arena);
4831 if (ops == NULL) goto failed;
4832 for (i = 0; i < len; i++) {
4833 cmpop_ty value;
4834 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4835 if (res != 0) goto failed;
4836 asdl_seq_SET(ops, i, value);
4837 }
4838 Py_XDECREF(tmp);
4839 tmp = NULL;
4840 } else {
4841 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4842 return 1;
4843 }
4844 if (PyObject_HasAttrString(obj, "comparators")) {
4845 int res;
4846 Py_ssize_t len;
4847 Py_ssize_t i;
4848 tmp = PyObject_GetAttrString(obj, "comparators");
4849 if (tmp == NULL) goto failed;
4850 if (!PyList_Check(tmp)) {
4851 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4852 goto failed;
4853 }
4854 len = PyList_GET_SIZE(tmp);
4855 comparators = asdl_seq_new(len, arena);
4856 if (comparators == NULL) goto failed;
4857 for (i = 0; i < len; i++) {
4858 expr_ty value;
4859 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4860 if (res != 0) goto failed;
4861 asdl_seq_SET(comparators, i, value);
4862 }
4863 Py_XDECREF(tmp);
4864 tmp = NULL;
4865 } else {
4866 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4867 return 1;
4868 }
4869 *out = Compare(left, ops, comparators, lineno, col_offset,
4870 arena);
4871 if (*out == NULL) goto failed;
4872 return 0;
4873 }
4874 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4875 expr_ty func;
4876 asdl_seq* args;
4877 asdl_seq* keywords;
4878 expr_ty starargs;
4879 expr_ty kwargs;
4880
4881 if (PyObject_HasAttrString(obj, "func")) {
4882 int res;
4883 tmp = PyObject_GetAttrString(obj, "func");
4884 if (tmp == NULL) goto failed;
4885 res = obj2ast_expr(tmp, &func, arena);
4886 if (res != 0) goto failed;
4887 Py_XDECREF(tmp);
4888 tmp = NULL;
4889 } else {
4890 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4891 return 1;
4892 }
4893 if (PyObject_HasAttrString(obj, "args")) {
4894 int res;
4895 Py_ssize_t len;
4896 Py_ssize_t i;
4897 tmp = PyObject_GetAttrString(obj, "args");
4898 if (tmp == NULL) goto failed;
4899 if (!PyList_Check(tmp)) {
4900 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4901 goto failed;
4902 }
4903 len = PyList_GET_SIZE(tmp);
4904 args = asdl_seq_new(len, arena);
4905 if (args == NULL) goto failed;
4906 for (i = 0; i < len; i++) {
4907 expr_ty value;
4908 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4909 if (res != 0) goto failed;
4910 asdl_seq_SET(args, i, value);
4911 }
4912 Py_XDECREF(tmp);
4913 tmp = NULL;
4914 } else {
4915 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4916 return 1;
4917 }
4918 if (PyObject_HasAttrString(obj, "keywords")) {
4919 int res;
4920 Py_ssize_t len;
4921 Py_ssize_t i;
4922 tmp = PyObject_GetAttrString(obj, "keywords");
4923 if (tmp == NULL) goto failed;
4924 if (!PyList_Check(tmp)) {
4925 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4926 goto failed;
4927 }
4928 len = PyList_GET_SIZE(tmp);
4929 keywords = asdl_seq_new(len, arena);
4930 if (keywords == NULL) goto failed;
4931 for (i = 0; i < len; i++) {
4932 keyword_ty value;
4933 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4934 if (res != 0) goto failed;
4935 asdl_seq_SET(keywords, i, value);
4936 }
4937 Py_XDECREF(tmp);
4938 tmp = NULL;
4939 } else {
4940 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4941 return 1;
4942 }
4943 if (PyObject_HasAttrString(obj, "starargs")) {
4944 int res;
4945 tmp = PyObject_GetAttrString(obj, "starargs");
4946 if (tmp == NULL) goto failed;
4947 res = obj2ast_expr(tmp, &starargs, arena);
4948 if (res != 0) goto failed;
4949 Py_XDECREF(tmp);
4950 tmp = NULL;
4951 } else {
4952 starargs = NULL;
4953 }
4954 if (PyObject_HasAttrString(obj, "kwargs")) {
4955 int res;
4956 tmp = PyObject_GetAttrString(obj, "kwargs");
4957 if (tmp == NULL) goto failed;
4958 res = obj2ast_expr(tmp, &kwargs, arena);
4959 if (res != 0) goto failed;
4960 Py_XDECREF(tmp);
4961 tmp = NULL;
4962 } else {
4963 kwargs = NULL;
4964 }
4965 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4966 col_offset, arena);
4967 if (*out == NULL) goto failed;
4968 return 0;
4969 }
4970 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4971 expr_ty value;
4972
4973 if (PyObject_HasAttrString(obj, "value")) {
4974 int res;
4975 tmp = PyObject_GetAttrString(obj, "value");
4976 if (tmp == NULL) goto failed;
4977 res = obj2ast_expr(tmp, &value, arena);
4978 if (res != 0) goto failed;
4979 Py_XDECREF(tmp);
4980 tmp = NULL;
4981 } else {
4982 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
4983 return 1;
4984 }
4985 *out = Repr(value, lineno, col_offset, arena);
4986 if (*out == NULL) goto failed;
4987 return 0;
4988 }
4989 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
4990 object n;
4991
4992 if (PyObject_HasAttrString(obj, "n")) {
4993 int res;
4994 tmp = PyObject_GetAttrString(obj, "n");
4995 if (tmp == NULL) goto failed;
4996 res = obj2ast_object(tmp, &n, arena);
4997 if (res != 0) goto failed;
4998 Py_XDECREF(tmp);
4999 tmp = NULL;
5000 } else {
5001 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
5002 return 1;
5003 }
5004 *out = Num(n, lineno, col_offset, arena);
5005 if (*out == NULL) goto failed;
5006 return 0;
5007 }
5008 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5009 string s;
5010
5011 if (PyObject_HasAttrString(obj, "s")) {
5012 int res;
5013 tmp = PyObject_GetAttrString(obj, "s");
5014 if (tmp == NULL) goto failed;
5015 res = obj2ast_string(tmp, &s, arena);
5016 if (res != 0) goto failed;
5017 Py_XDECREF(tmp);
5018 tmp = NULL;
5019 } else {
5020 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5021 return 1;
5022 }
5023 *out = Str(s, lineno, col_offset, arena);
5024 if (*out == NULL) goto failed;
5025 return 0;
5026 }
5027 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5028 expr_ty value;
5029 identifier attr;
5030 expr_context_ty ctx;
5031
5032 if (PyObject_HasAttrString(obj, "value")) {
5033 int res;
5034 tmp = PyObject_GetAttrString(obj, "value");
5035 if (tmp == NULL) goto failed;
5036 res = obj2ast_expr(tmp, &value, arena);
5037 if (res != 0) goto failed;
5038 Py_XDECREF(tmp);
5039 tmp = NULL;
5040 } else {
5041 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5042 return 1;
5043 }
5044 if (PyObject_HasAttrString(obj, "attr")) {
5045 int res;
5046 tmp = PyObject_GetAttrString(obj, "attr");
5047 if (tmp == NULL) goto failed;
5048 res = obj2ast_identifier(tmp, &attr, arena);
5049 if (res != 0) goto failed;
5050 Py_XDECREF(tmp);
5051 tmp = NULL;
5052 } else {
5053 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5054 return 1;
5055 }
5056 if (PyObject_HasAttrString(obj, "ctx")) {
5057 int res;
5058 tmp = PyObject_GetAttrString(obj, "ctx");
5059 if (tmp == NULL) goto failed;
5060 res = obj2ast_expr_context(tmp, &ctx, arena);
5061 if (res != 0) goto failed;
5062 Py_XDECREF(tmp);
5063 tmp = NULL;
5064 } else {
5065 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5066 return 1;
5067 }
5068 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5069 if (*out == NULL) goto failed;
5070 return 0;
5071 }
5072 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5073 expr_ty value;
5074 slice_ty slice;
5075 expr_context_ty ctx;
5076
5077 if (PyObject_HasAttrString(obj, "value")) {
5078 int res;
5079 tmp = PyObject_GetAttrString(obj, "value");
5080 if (tmp == NULL) goto failed;
5081 res = obj2ast_expr(tmp, &value, arena);
5082 if (res != 0) goto failed;
5083 Py_XDECREF(tmp);
5084 tmp = NULL;
5085 } else {
5086 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5087 return 1;
5088 }
5089 if (PyObject_HasAttrString(obj, "slice")) {
5090 int res;
5091 tmp = PyObject_GetAttrString(obj, "slice");
5092 if (tmp == NULL) goto failed;
5093 res = obj2ast_slice(tmp, &slice, arena);
5094 if (res != 0) goto failed;
5095 Py_XDECREF(tmp);
5096 tmp = NULL;
5097 } else {
5098 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5099 return 1;
5100 }
5101 if (PyObject_HasAttrString(obj, "ctx")) {
5102 int res;
5103 tmp = PyObject_GetAttrString(obj, "ctx");
5104 if (tmp == NULL) goto failed;
5105 res = obj2ast_expr_context(tmp, &ctx, arena);
5106 if (res != 0) goto failed;
5107 Py_XDECREF(tmp);
5108 tmp = NULL;
5109 } else {
5110 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5111 return 1;
5112 }
5113 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5114 if (*out == NULL) goto failed;
5115 return 0;
5116 }
5117 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5118 identifier id;
5119 expr_context_ty ctx;
5120
5121 if (PyObject_HasAttrString(obj, "id")) {
5122 int res;
5123 tmp = PyObject_GetAttrString(obj, "id");
5124 if (tmp == NULL) goto failed;
5125 res = obj2ast_identifier(tmp, &id, arena);
5126 if (res != 0) goto failed;
5127 Py_XDECREF(tmp);
5128 tmp = NULL;
5129 } else {
5130 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5131 return 1;
5132 }
5133 if (PyObject_HasAttrString(obj, "ctx")) {
5134 int res;
5135 tmp = PyObject_GetAttrString(obj, "ctx");
5136 if (tmp == NULL) goto failed;
5137 res = obj2ast_expr_context(tmp, &ctx, arena);
5138 if (res != 0) goto failed;
5139 Py_XDECREF(tmp);
5140 tmp = NULL;
5141 } else {
5142 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5143 return 1;
5144 }
5145 *out = Name(id, ctx, lineno, col_offset, arena);
5146 if (*out == NULL) goto failed;
5147 return 0;
5148 }
5149 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5150 asdl_seq* elts;
5151 expr_context_ty ctx;
5152
5153 if (PyObject_HasAttrString(obj, "elts")) {
5154 int res;
5155 Py_ssize_t len;
5156 Py_ssize_t i;
5157 tmp = PyObject_GetAttrString(obj, "elts");
5158 if (tmp == NULL) goto failed;
5159 if (!PyList_Check(tmp)) {
5160 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5161 goto failed;
5162 }
5163 len = PyList_GET_SIZE(tmp);
5164 elts = asdl_seq_new(len, arena);
5165 if (elts == NULL) goto failed;
5166 for (i = 0; i < len; i++) {
5167 expr_ty value;
5168 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5169 if (res != 0) goto failed;
5170 asdl_seq_SET(elts, i, value);
5171 }
5172 Py_XDECREF(tmp);
5173 tmp = NULL;
5174 } else {
5175 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5176 return 1;
5177 }
5178 if (PyObject_HasAttrString(obj, "ctx")) {
5179 int res;
5180 tmp = PyObject_GetAttrString(obj, "ctx");
5181 if (tmp == NULL) goto failed;
5182 res = obj2ast_expr_context(tmp, &ctx, arena);
5183 if (res != 0) goto failed;
5184 Py_XDECREF(tmp);
5185 tmp = NULL;
5186 } else {
5187 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5188 return 1;
5189 }
5190 *out = List(elts, ctx, lineno, col_offset, arena);
5191 if (*out == NULL) goto failed;
5192 return 0;
5193 }
5194 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5195 asdl_seq* elts;
5196 expr_context_ty ctx;
5197
5198 if (PyObject_HasAttrString(obj, "elts")) {
5199 int res;
5200 Py_ssize_t len;
5201 Py_ssize_t i;
5202 tmp = PyObject_GetAttrString(obj, "elts");
5203 if (tmp == NULL) goto failed;
5204 if (!PyList_Check(tmp)) {
5205 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5206 goto failed;
5207 }
5208 len = PyList_GET_SIZE(tmp);
5209 elts = asdl_seq_new(len, arena);
5210 if (elts == NULL) goto failed;
5211 for (i = 0; i < len; i++) {
5212 expr_ty value;
5213 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5214 if (res != 0) goto failed;
5215 asdl_seq_SET(elts, i, value);
5216 }
5217 Py_XDECREF(tmp);
5218 tmp = NULL;
5219 } else {
5220 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5221 return 1;
5222 }
5223 if (PyObject_HasAttrString(obj, "ctx")) {
5224 int res;
5225 tmp = PyObject_GetAttrString(obj, "ctx");
5226 if (tmp == NULL) goto failed;
5227 res = obj2ast_expr_context(tmp, &ctx, arena);
5228 if (res != 0) goto failed;
5229 Py_XDECREF(tmp);
5230 tmp = NULL;
5231 } else {
5232 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5233 return 1;
5234 }
5235 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5236 if (*out == NULL) goto failed;
5237 return 0;
5238 }
5239
5240 tmp = PyObject_Repr(obj);
5241 if (tmp == NULL) goto failed;
5242 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5243failed:
5244 Py_XDECREF(tmp);
5245 return 1;
5246}
5247
5248int
5249obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5250{
5251 PyObject* tmp = NULL;
5252
5253 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5254 *out = Load;
5255 return 0;
5256 }
5257 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5258 *out = Store;
5259 return 0;
5260 }
5261 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5262 *out = Del;
5263 return 0;
5264 }
5265 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5266 *out = AugLoad;
5267 return 0;
5268 }
5269 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5270 *out = AugStore;
5271 return 0;
5272 }
5273 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5274 *out = Param;
5275 return 0;
5276 }
5277
5278 tmp = PyObject_Repr(obj);
5279 if (tmp == NULL) goto failed;
5280 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5281failed:
5282 Py_XDECREF(tmp);
5283 return 1;
5284}
5285
5286int
5287obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5288{
5289 PyObject* tmp = NULL;
5290
5291
5292 if (obj == Py_None) {
5293 *out = NULL;
5294 return 0;
5295 }
5296 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5297
5298 *out = Ellipsis(arena);
5299 if (*out == NULL) goto failed;
5300 return 0;
5301 }
5302 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5303 expr_ty lower;
5304 expr_ty upper;
5305 expr_ty step;
5306
5307 if (PyObject_HasAttrString(obj, "lower")) {
5308 int res;
5309 tmp = PyObject_GetAttrString(obj, "lower");
5310 if (tmp == NULL) goto failed;
5311 res = obj2ast_expr(tmp, &lower, arena);
5312 if (res != 0) goto failed;
5313 Py_XDECREF(tmp);
5314 tmp = NULL;
5315 } else {
5316 lower = NULL;
5317 }
5318 if (PyObject_HasAttrString(obj, "upper")) {
5319 int res;
5320 tmp = PyObject_GetAttrString(obj, "upper");
5321 if (tmp == NULL) goto failed;
5322 res = obj2ast_expr(tmp, &upper, arena);
5323 if (res != 0) goto failed;
5324 Py_XDECREF(tmp);
5325 tmp = NULL;
5326 } else {
5327 upper = NULL;
5328 }
5329 if (PyObject_HasAttrString(obj, "step")) {
5330 int res;
5331 tmp = PyObject_GetAttrString(obj, "step");
5332 if (tmp == NULL) goto failed;
5333 res = obj2ast_expr(tmp, &step, arena);
5334 if (res != 0) goto failed;
5335 Py_XDECREF(tmp);
5336 tmp = NULL;
5337 } else {
5338 step = NULL;
5339 }
5340 *out = Slice(lower, upper, step, arena);
5341 if (*out == NULL) goto failed;
5342 return 0;
5343 }
5344 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5345 asdl_seq* dims;
5346
5347 if (PyObject_HasAttrString(obj, "dims")) {
5348 int res;
5349 Py_ssize_t len;
5350 Py_ssize_t i;
5351 tmp = PyObject_GetAttrString(obj, "dims");
5352 if (tmp == NULL) goto failed;
5353 if (!PyList_Check(tmp)) {
5354 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5355 goto failed;
5356 }
5357 len = PyList_GET_SIZE(tmp);
5358 dims = asdl_seq_new(len, arena);
5359 if (dims == NULL) goto failed;
5360 for (i = 0; i < len; i++) {
5361 slice_ty value;
5362 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5363 if (res != 0) goto failed;
5364 asdl_seq_SET(dims, i, value);
5365 }
5366 Py_XDECREF(tmp);
5367 tmp = NULL;
5368 } else {
5369 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5370 return 1;
5371 }
5372 *out = ExtSlice(dims, arena);
5373 if (*out == NULL) goto failed;
5374 return 0;
5375 }
5376 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5377 expr_ty value;
5378
5379 if (PyObject_HasAttrString(obj, "value")) {
5380 int res;
5381 tmp = PyObject_GetAttrString(obj, "value");
5382 if (tmp == NULL) goto failed;
5383 res = obj2ast_expr(tmp, &value, arena);
5384 if (res != 0) goto failed;
5385 Py_XDECREF(tmp);
5386 tmp = NULL;
5387 } else {
5388 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5389 return 1;
5390 }
5391 *out = Index(value, arena);
5392 if (*out == NULL) goto failed;
5393 return 0;
5394 }
5395
5396 tmp = PyObject_Repr(obj);
5397 if (tmp == NULL) goto failed;
5398 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5399failed:
5400 Py_XDECREF(tmp);
5401 return 1;
5402}
5403
5404int
5405obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5406{
5407 PyObject* tmp = NULL;
5408
5409 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5410 *out = And;
5411 return 0;
5412 }
5413 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5414 *out = Or;
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 boolop, but got %.400s", PyString_AS_STRING(tmp));
5421failed:
5422 Py_XDECREF(tmp);
5423 return 1;
5424}
5425
5426int
5427obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5428{
5429 PyObject* tmp = NULL;
5430
5431 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5432 *out = Add;
5433 return 0;
5434 }
5435 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5436 *out = Sub;
5437 return 0;
5438 }
5439 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5440 *out = Mult;
5441 return 0;
5442 }
5443 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5444 *out = Div;
5445 return 0;
5446 }
5447 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5448 *out = Mod;
5449 return 0;
5450 }
5451 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5452 *out = Pow;
5453 return 0;
5454 }
5455 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5456 *out = LShift;
5457 return 0;
5458 }
5459 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5460 *out = RShift;
5461 return 0;
5462 }
5463 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5464 *out = BitOr;
5465 return 0;
5466 }
5467 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5468 *out = BitXor;
5469 return 0;
5470 }
5471 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5472 *out = BitAnd;
5473 return 0;
5474 }
5475 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5476 *out = FloorDiv;
5477 return 0;
5478 }
5479
5480 tmp = PyObject_Repr(obj);
5481 if (tmp == NULL) goto failed;
5482 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5483failed:
5484 Py_XDECREF(tmp);
5485 return 1;
5486}
5487
5488int
5489obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5490{
5491 PyObject* tmp = NULL;
5492
5493 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5494 *out = Invert;
5495 return 0;
5496 }
5497 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5498 *out = Not;
5499 return 0;
5500 }
5501 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5502 *out = UAdd;
5503 return 0;
5504 }
5505 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5506 *out = USub;
5507 return 0;
5508 }
5509
5510 tmp = PyObject_Repr(obj);
5511 if (tmp == NULL) goto failed;
5512 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5513failed:
5514 Py_XDECREF(tmp);
5515 return 1;
5516}
5517
5518int
5519obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5520{
5521 PyObject* tmp = NULL;
5522
5523 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5524 *out = Eq;
5525 return 0;
5526 }
5527 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5528 *out = NotEq;
5529 return 0;
5530 }
5531 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5532 *out = Lt;
5533 return 0;
5534 }
5535 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5536 *out = LtE;
5537 return 0;
5538 }
5539 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5540 *out = Gt;
5541 return 0;
5542 }
5543 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5544 *out = GtE;
5545 return 0;
5546 }
5547 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5548 *out = Is;
5549 return 0;
5550 }
5551 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5552 *out = IsNot;
5553 return 0;
5554 }
5555 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5556 *out = In;
5557 return 0;
5558 }
5559 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5560 *out = NotIn;
5561 return 0;
5562 }
5563
5564 tmp = PyObject_Repr(obj);
5565 if (tmp == NULL) goto failed;
5566 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5567failed:
5568 Py_XDECREF(tmp);
5569 return 1;
5570}
5571
5572int
5573obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5574{
5575 PyObject* tmp = NULL;
5576 expr_ty target;
5577 expr_ty iter;
5578 asdl_seq* ifs;
5579
5580 if (PyObject_HasAttrString(obj, "target")) {
5581 int res;
5582 tmp = PyObject_GetAttrString(obj, "target");
5583 if (tmp == NULL) goto failed;
5584 res = obj2ast_expr(tmp, &target, arena);
5585 if (res != 0) goto failed;
5586 Py_XDECREF(tmp);
5587 tmp = NULL;
5588 } else {
5589 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5590 return 1;
5591 }
5592 if (PyObject_HasAttrString(obj, "iter")) {
5593 int res;
5594 tmp = PyObject_GetAttrString(obj, "iter");
5595 if (tmp == NULL) goto failed;
5596 res = obj2ast_expr(tmp, &iter, arena);
5597 if (res != 0) goto failed;
5598 Py_XDECREF(tmp);
5599 tmp = NULL;
5600 } else {
5601 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5602 return 1;
5603 }
5604 if (PyObject_HasAttrString(obj, "ifs")) {
5605 int res;
5606 Py_ssize_t len;
5607 Py_ssize_t i;
5608 tmp = PyObject_GetAttrString(obj, "ifs");
5609 if (tmp == NULL) goto failed;
5610 if (!PyList_Check(tmp)) {
5611 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5612 goto failed;
5613 }
5614 len = PyList_GET_SIZE(tmp);
5615 ifs = asdl_seq_new(len, arena);
5616 if (ifs == NULL) goto failed;
5617 for (i = 0; i < len; i++) {
5618 expr_ty value;
5619 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5620 if (res != 0) goto failed;
5621 asdl_seq_SET(ifs, i, value);
5622 }
5623 Py_XDECREF(tmp);
5624 tmp = NULL;
5625 } else {
5626 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5627 return 1;
5628 }
5629 *out = comprehension(target, iter, ifs, arena);
5630 return 0;
5631failed:
5632 Py_XDECREF(tmp);
5633 return 1;
5634}
5635
5636int
5637obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5638{
5639 PyObject* tmp = NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00005640
Georg Brandlfc8eef32008-03-28 12:11:56 +00005641 int lineno;
5642 int col_offset;
5643
Georg Brandla48f3ab2008-03-30 06:40:17 +00005644 if (obj == Py_None) {
5645 *out = NULL;
5646 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005647 }
5648 if (PyObject_HasAttrString(obj, "lineno")) {
5649 int res;
5650 tmp = PyObject_GetAttrString(obj, "lineno");
5651 if (tmp == NULL) goto failed;
5652 res = obj2ast_int(tmp, &lineno, arena);
5653 if (res != 0) goto failed;
5654 Py_XDECREF(tmp);
5655 tmp = NULL;
5656 } else {
5657 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5658 return 1;
5659 }
5660 if (PyObject_HasAttrString(obj, "col_offset")) {
5661 int res;
5662 tmp = PyObject_GetAttrString(obj, "col_offset");
5663 if (tmp == NULL) goto failed;
5664 res = obj2ast_int(tmp, &col_offset, arena);
5665 if (res != 0) goto failed;
5666 Py_XDECREF(tmp);
5667 tmp = NULL;
5668 } else {
5669 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5670 return 1;
5671 }
Georg Brandla48f3ab2008-03-30 06:40:17 +00005672 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
5673 expr_ty type;
5674 expr_ty name;
5675 asdl_seq* body;
5676
5677 if (PyObject_HasAttrString(obj, "type")) {
5678 int res;
5679 tmp = PyObject_GetAttrString(obj, "type");
5680 if (tmp == NULL) goto failed;
5681 res = obj2ast_expr(tmp, &type, arena);
5682 if (res != 0) goto failed;
5683 Py_XDECREF(tmp);
5684 tmp = NULL;
5685 } else {
5686 type = NULL;
5687 }
5688 if (PyObject_HasAttrString(obj, "name")) {
5689 int res;
5690 tmp = PyObject_GetAttrString(obj, "name");
5691 if (tmp == NULL) goto failed;
5692 res = obj2ast_expr(tmp, &name, arena);
5693 if (res != 0) goto failed;
5694 Py_XDECREF(tmp);
5695 tmp = NULL;
5696 } else {
5697 name = NULL;
5698 }
5699 if (PyObject_HasAttrString(obj, "body")) {
5700 int res;
5701 Py_ssize_t len;
5702 Py_ssize_t i;
5703 tmp = PyObject_GetAttrString(obj, "body");
5704 if (tmp == NULL) goto failed;
5705 if (!PyList_Check(tmp)) {
5706 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5707 goto failed;
5708 }
5709 len = PyList_GET_SIZE(tmp);
5710 body = asdl_seq_new(len, arena);
5711 if (body == NULL) goto failed;
5712 for (i = 0; i < len; i++) {
5713 stmt_ty value;
5714 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5715 if (res != 0) goto failed;
5716 asdl_seq_SET(body, i, value);
5717 }
5718 Py_XDECREF(tmp);
5719 tmp = NULL;
5720 } else {
5721 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
5722 return 1;
5723 }
5724 *out = ExceptHandler(type, name, body, lineno, col_offset,
5725 arena);
5726 if (*out == NULL) goto failed;
5727 return 0;
5728 }
5729
5730 tmp = PyObject_Repr(obj);
5731 if (tmp == NULL) goto failed;
5732 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005733failed:
5734 Py_XDECREF(tmp);
5735 return 1;
5736}
5737
5738int
5739obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5740{
5741 PyObject* tmp = NULL;
5742 asdl_seq* args;
5743 identifier vararg;
5744 identifier kwarg;
5745 asdl_seq* defaults;
5746
5747 if (PyObject_HasAttrString(obj, "args")) {
5748 int res;
5749 Py_ssize_t len;
5750 Py_ssize_t i;
5751 tmp = PyObject_GetAttrString(obj, "args");
5752 if (tmp == NULL) goto failed;
5753 if (!PyList_Check(tmp)) {
5754 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5755 goto failed;
5756 }
5757 len = PyList_GET_SIZE(tmp);
5758 args = asdl_seq_new(len, arena);
5759 if (args == NULL) goto failed;
5760 for (i = 0; i < len; i++) {
5761 expr_ty value;
5762 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5763 if (res != 0) goto failed;
5764 asdl_seq_SET(args, i, value);
5765 }
5766 Py_XDECREF(tmp);
5767 tmp = NULL;
5768 } else {
5769 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5770 return 1;
5771 }
5772 if (PyObject_HasAttrString(obj, "vararg")) {
5773 int res;
5774 tmp = PyObject_GetAttrString(obj, "vararg");
5775 if (tmp == NULL) goto failed;
5776 res = obj2ast_identifier(tmp, &vararg, arena);
5777 if (res != 0) goto failed;
5778 Py_XDECREF(tmp);
5779 tmp = NULL;
5780 } else {
5781 vararg = NULL;
5782 }
5783 if (PyObject_HasAttrString(obj, "kwarg")) {
5784 int res;
5785 tmp = PyObject_GetAttrString(obj, "kwarg");
5786 if (tmp == NULL) goto failed;
5787 res = obj2ast_identifier(tmp, &kwarg, arena);
5788 if (res != 0) goto failed;
5789 Py_XDECREF(tmp);
5790 tmp = NULL;
5791 } else {
5792 kwarg = NULL;
5793 }
5794 if (PyObject_HasAttrString(obj, "defaults")) {
5795 int res;
5796 Py_ssize_t len;
5797 Py_ssize_t i;
5798 tmp = PyObject_GetAttrString(obj, "defaults");
5799 if (tmp == NULL) goto failed;
5800 if (!PyList_Check(tmp)) {
5801 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5802 goto failed;
5803 }
5804 len = PyList_GET_SIZE(tmp);
5805 defaults = asdl_seq_new(len, arena);
5806 if (defaults == NULL) goto failed;
5807 for (i = 0; i < len; i++) {
5808 expr_ty value;
5809 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5810 if (res != 0) goto failed;
5811 asdl_seq_SET(defaults, i, value);
5812 }
5813 Py_XDECREF(tmp);
5814 tmp = NULL;
5815 } else {
5816 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5817 return 1;
5818 }
5819 *out = arguments(args, vararg, kwarg, defaults, arena);
5820 return 0;
5821failed:
5822 Py_XDECREF(tmp);
5823 return 1;
5824}
5825
5826int
5827obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5828{
5829 PyObject* tmp = NULL;
5830 identifier arg;
5831 expr_ty value;
5832
5833 if (PyObject_HasAttrString(obj, "arg")) {
5834 int res;
5835 tmp = PyObject_GetAttrString(obj, "arg");
5836 if (tmp == NULL) goto failed;
5837 res = obj2ast_identifier(tmp, &arg, arena);
5838 if (res != 0) goto failed;
5839 Py_XDECREF(tmp);
5840 tmp = NULL;
5841 } else {
5842 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5843 return 1;
5844 }
5845 if (PyObject_HasAttrString(obj, "value")) {
5846 int res;
5847 tmp = PyObject_GetAttrString(obj, "value");
5848 if (tmp == NULL) goto failed;
5849 res = obj2ast_expr(tmp, &value, arena);
5850 if (res != 0) goto failed;
5851 Py_XDECREF(tmp);
5852 tmp = NULL;
5853 } else {
5854 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5855 return 1;
5856 }
5857 *out = keyword(arg, value, arena);
5858 return 0;
5859failed:
5860 Py_XDECREF(tmp);
5861 return 1;
5862}
5863
5864int
5865obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5866{
5867 PyObject* tmp = NULL;
5868 identifier name;
5869 identifier asname;
5870
5871 if (PyObject_HasAttrString(obj, "name")) {
5872 int res;
5873 tmp = PyObject_GetAttrString(obj, "name");
5874 if (tmp == NULL) goto failed;
5875 res = obj2ast_identifier(tmp, &name, arena);
5876 if (res != 0) goto failed;
5877 Py_XDECREF(tmp);
5878 tmp = NULL;
5879 } else {
5880 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5881 return 1;
5882 }
5883 if (PyObject_HasAttrString(obj, "asname")) {
5884 int res;
5885 tmp = PyObject_GetAttrString(obj, "asname");
5886 if (tmp == NULL) goto failed;
5887 res = obj2ast_identifier(tmp, &asname, arena);
5888 if (res != 0) goto failed;
5889 Py_XDECREF(tmp);
5890 tmp = NULL;
5891 } else {
5892 asname = NULL;
5893 }
5894 *out = alias(name, asname, arena);
5895 return 0;
5896failed:
5897 Py_XDECREF(tmp);
5898 return 1;
5899}
5900
5901
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005902PyMODINIT_FUNC
5903init_ast(void)
5904{
5905 PyObject *m, *d;
5906 if (!init_types()) return;
5907 m = Py_InitModule3("_ast", NULL, NULL);
5908 if (!m) return;
5909 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00005910 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005911 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5912 return;
Georg Brandlc52ed592008-03-30 07:01:47 +00005913 if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00005914 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00005915 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5916 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5917 return;
5918 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5919 < 0) return;
5920 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5921 0) return;
5922 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5923 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5924 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5925 < 0) return;
5926 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5927 return;
5928 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5929 return;
5930 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5931 return;
5932 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5933 return;
5934 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5935 0) return;
5936 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5937 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5938 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5939 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5940 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5941 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5942 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5943 0) return;
5944 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5945 0) return;
5946 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5947 return;
5948 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5949 return;
5950 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5951 0) return;
5952 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5953 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5954 return;
5955 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5956 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5957 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5958 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5959 return;
5960 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5961 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5962 return;
5963 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5964 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5965 return;
5966 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
5967 return;
5968 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
5969 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
5970 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
5971 return;
5972 if (PyDict_SetItemString(d, "GeneratorExp",
5973 (PyObject*)GeneratorExp_type) < 0) return;
5974 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
5975 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
5976 return;
5977 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
5978 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
5979 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
5980 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
5981 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
5982 0) return;
5983 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
5984 0) return;
5985 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
5986 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
5987 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
5988 if (PyDict_SetItemString(d, "expr_context",
5989 (PyObject*)expr_context_type) < 0) return;
5990 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
5991 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
5992 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
5993 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
5994 return;
5995 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
5996 return;
5997 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
5998 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
5999 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
6000 return;
6001 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
6002 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6003 return;
6004 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6005 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6006 return;
6007 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6008 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6009 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6010 return;
6011 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6012 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6013 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6014 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6015 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6016 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6017 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6018 return;
6019 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6020 return;
6021 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6022 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6023 return;
6024 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6025 return;
6026 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6027 return;
6028 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6029 return;
6030 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6031 return;
6032 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6033 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6034 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6035 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6036 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6037 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6038 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6039 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6040 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6041 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6042 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6043 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6044 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6045 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6046 if (PyDict_SetItemString(d, "comprehension",
6047 (PyObject*)comprehension_type) < 0) return;
6048 if (PyDict_SetItemString(d, "excepthandler",
6049 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006050 if (PyDict_SetItemString(d, "ExceptHandler",
6051 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006052 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6053 0) return;
6054 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6055 return;
6056 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006057}
6058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006059
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006060PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006061{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006062 init_types();
6063 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006064}
6065
Georg Brandlf2bfd542008-03-29 13:24:23 +00006066/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6067mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006068{
6069 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006070 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6071 (PyObject*)Interactive_type};
6072 char *req_name[] = {"Module", "Expression", "Interactive"};
6073 assert(0 <= mode && mode <= 2);
6074
Georg Brandlfc8eef32008-03-28 12:11:56 +00006075 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006076
6077 if (!PyObject_IsInstance(ast, req_type[mode])) {
6078 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6079 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006080 return NULL;
6081 }
6082 if (obj2ast_mod(ast, &res, arena) != 0)
6083 return NULL;
6084 else
6085 return res;
6086}
6087
6088int PyAST_Check(PyObject* obj)
6089{
6090 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006091 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006092}
6093
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006094