blob: ba310e60890232e15678e1a31d9eabdf3a4f95dc [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Georg Brandlc52ed592008-03-30 07:01:47 +00005 __version__ 62047.
Brett Cannon0db62aa2007-02-12 03:51:02 +00006
7 This module must be committed separately after each AST grammar change;
8 The __version__ number is set to the revision number of the commit
9 containing the grammar change.
10*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000011
12#include "Python.h"
13#include "Python-ast.h"
14
Georg Brandlc52ed592008-03-30 07:01:47 +000015static PyTypeObject AST_type;
Neal Norwitz53d960c2006-02-28 22:47:29 +000016static PyTypeObject *mod_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +000017static PyObject* ast2obj_mod(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000018static PyTypeObject *Module_type;
19static char *Module_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000020 "body",
21};
Neal Norwitz53d960c2006-02-28 22:47:29 +000022static PyTypeObject *Interactive_type;
23static char *Interactive_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000024 "body",
25};
Neal Norwitz53d960c2006-02-28 22:47:29 +000026static PyTypeObject *Expression_type;
27static char *Expression_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000028 "body",
29};
Neal Norwitz53d960c2006-02-28 22:47:29 +000030static PyTypeObject *Suite_type;
31static char *Suite_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000032 "body",
33};
Neal Norwitz53d960c2006-02-28 22:47:29 +000034static PyTypeObject *stmt_type;
35static char *stmt_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +000036 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +000037 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +000038};
Martin v. Löwisbd260da2006-02-26 19:42:26 +000039static PyObject* ast2obj_stmt(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +000040static PyTypeObject *FunctionDef_type;
41static char *FunctionDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000042 "name",
43 "args",
44 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000045 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000046};
Neal Norwitz53d960c2006-02-28 22:47:29 +000047static PyTypeObject *ClassDef_type;
48static char *ClassDef_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000049 "name",
50 "bases",
51 "body",
Christian Heimes5224d282008-02-23 15:01:05 +000052 "decorator_list",
Martin v. Löwisbd260da2006-02-26 19:42:26 +000053};
Neal Norwitz53d960c2006-02-28 22:47:29 +000054static PyTypeObject *Return_type;
55static char *Return_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000056 "value",
57};
Neal Norwitz53d960c2006-02-28 22:47:29 +000058static PyTypeObject *Delete_type;
59static char *Delete_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000060 "targets",
61};
Neal Norwitz53d960c2006-02-28 22:47:29 +000062static PyTypeObject *Assign_type;
63static char *Assign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000064 "targets",
65 "value",
66};
Neal Norwitz53d960c2006-02-28 22:47:29 +000067static PyTypeObject *AugAssign_type;
68static char *AugAssign_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000069 "target",
70 "op",
71 "value",
72};
Neal Norwitz53d960c2006-02-28 22:47:29 +000073static PyTypeObject *Print_type;
74static char *Print_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000075 "dest",
76 "values",
77 "nl",
78};
Neal Norwitz53d960c2006-02-28 22:47:29 +000079static PyTypeObject *For_type;
80static char *For_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000081 "target",
82 "iter",
83 "body",
84 "orelse",
85};
Neal Norwitz53d960c2006-02-28 22:47:29 +000086static PyTypeObject *While_type;
87static char *While_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000088 "test",
89 "body",
90 "orelse",
91};
Neal Norwitz53d960c2006-02-28 22:47:29 +000092static PyTypeObject *If_type;
93static char *If_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +000094 "test",
95 "body",
96 "orelse",
97};
Neal Norwitz53d960c2006-02-28 22:47:29 +000098static PyTypeObject *With_type;
99static char *With_fields[]={
Guido van Rossumc2e20742006-02-27 22:32:47 +0000100 "context_expr",
101 "optional_vars",
102 "body",
103};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000104static PyTypeObject *Raise_type;
105static char *Raise_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000106 "type",
107 "inst",
108 "tback",
109};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000110static PyTypeObject *TryExcept_type;
111static char *TryExcept_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000112 "body",
113 "handlers",
114 "orelse",
115};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000116static PyTypeObject *TryFinally_type;
117static char *TryFinally_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000118 "body",
119 "finalbody",
120};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000121static PyTypeObject *Assert_type;
122static char *Assert_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000123 "test",
124 "msg",
125};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000126static PyTypeObject *Import_type;
127static char *Import_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000128 "names",
129};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000130static PyTypeObject *ImportFrom_type;
131static char *ImportFrom_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000132 "module",
133 "names",
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000134 "level",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000135};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000136static PyTypeObject *Exec_type;
137static char *Exec_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000138 "body",
139 "globals",
140 "locals",
141};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000142static PyTypeObject *Global_type;
143static char *Global_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000144 "names",
145};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000146static PyTypeObject *Expr_type;
147static char *Expr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000148 "value",
149};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000150static PyTypeObject *Pass_type;
151static PyTypeObject *Break_type;
152static PyTypeObject *Continue_type;
153static PyTypeObject *expr_type;
154static char *expr_attributes[] = {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000155 "lineno",
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000156 "col_offset",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000157};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000158static PyObject* ast2obj_expr(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000159static PyTypeObject *BoolOp_type;
160static char *BoolOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000161 "op",
162 "values",
163};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000164static PyTypeObject *BinOp_type;
165static char *BinOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000166 "left",
167 "op",
168 "right",
169};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000170static PyTypeObject *UnaryOp_type;
171static char *UnaryOp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000172 "op",
173 "operand",
174};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000175static PyTypeObject *Lambda_type;
176static char *Lambda_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000177 "args",
178 "body",
179};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000180static PyTypeObject *IfExp_type;
181static char *IfExp_fields[]={
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000182 "test",
183 "body",
184 "orelse",
185};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000186static PyTypeObject *Dict_type;
187static char *Dict_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000188 "keys",
189 "values",
190};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000191static PyTypeObject *ListComp_type;
192static char *ListComp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000193 "elt",
194 "generators",
195};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000196static PyTypeObject *GeneratorExp_type;
197static char *GeneratorExp_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000198 "elt",
199 "generators",
200};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000201static PyTypeObject *Yield_type;
202static char *Yield_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000203 "value",
204};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000205static PyTypeObject *Compare_type;
206static char *Compare_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000207 "left",
208 "ops",
209 "comparators",
210};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000211static PyTypeObject *Call_type;
212static char *Call_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000213 "func",
214 "args",
215 "keywords",
216 "starargs",
217 "kwargs",
218};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000219static PyTypeObject *Repr_type;
220static char *Repr_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000221 "value",
222};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000223static PyTypeObject *Num_type;
224static char *Num_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000225 "n",
226};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000227static PyTypeObject *Str_type;
228static char *Str_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000229 "s",
230};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000231static PyTypeObject *Attribute_type;
232static char *Attribute_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000233 "value",
234 "attr",
235 "ctx",
236};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000237static PyTypeObject *Subscript_type;
238static char *Subscript_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000239 "value",
240 "slice",
241 "ctx",
242};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000243static PyTypeObject *Name_type;
244static char *Name_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000245 "id",
246 "ctx",
247};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000248static PyTypeObject *List_type;
249static char *List_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000250 "elts",
251 "ctx",
252};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000253static PyTypeObject *Tuple_type;
254static char *Tuple_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000255 "elts",
256 "ctx",
257};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000258static PyTypeObject *expr_context_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000259static PyObject *Load_singleton, *Store_singleton, *Del_singleton,
260*AugLoad_singleton, *AugStore_singleton, *Param_singleton;
261static PyObject* ast2obj_expr_context(expr_context_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000262static PyTypeObject *Load_type;
263static PyTypeObject *Store_type;
264static PyTypeObject *Del_type;
265static PyTypeObject *AugLoad_type;
266static PyTypeObject *AugStore_type;
267static PyTypeObject *Param_type;
268static PyTypeObject *slice_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000269static PyObject* ast2obj_slice(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000270static PyTypeObject *Ellipsis_type;
271static PyTypeObject *Slice_type;
272static char *Slice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000273 "lower",
274 "upper",
275 "step",
276};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000277static PyTypeObject *ExtSlice_type;
278static char *ExtSlice_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000279 "dims",
280};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000281static PyTypeObject *Index_type;
282static char *Index_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000283 "value",
284};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000285static PyTypeObject *boolop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000286static PyObject *And_singleton, *Or_singleton;
287static PyObject* ast2obj_boolop(boolop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000288static PyTypeObject *And_type;
289static PyTypeObject *Or_type;
290static PyTypeObject *operator_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000291static PyObject *Add_singleton, *Sub_singleton, *Mult_singleton,
292*Div_singleton, *Mod_singleton, *Pow_singleton, *LShift_singleton,
293*RShift_singleton, *BitOr_singleton, *BitXor_singleton, *BitAnd_singleton,
294*FloorDiv_singleton;
295static PyObject* ast2obj_operator(operator_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000296static PyTypeObject *Add_type;
297static PyTypeObject *Sub_type;
298static PyTypeObject *Mult_type;
299static PyTypeObject *Div_type;
300static PyTypeObject *Mod_type;
301static PyTypeObject *Pow_type;
302static PyTypeObject *LShift_type;
303static PyTypeObject *RShift_type;
304static PyTypeObject *BitOr_type;
305static PyTypeObject *BitXor_type;
306static PyTypeObject *BitAnd_type;
307static PyTypeObject *FloorDiv_type;
308static PyTypeObject *unaryop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000309static PyObject *Invert_singleton, *Not_singleton, *UAdd_singleton,
310*USub_singleton;
311static PyObject* ast2obj_unaryop(unaryop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000312static PyTypeObject *Invert_type;
313static PyTypeObject *Not_type;
314static PyTypeObject *UAdd_type;
315static PyTypeObject *USub_type;
316static PyTypeObject *cmpop_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000317static PyObject *Eq_singleton, *NotEq_singleton, *Lt_singleton, *LtE_singleton,
318*Gt_singleton, *GtE_singleton, *Is_singleton, *IsNot_singleton, *In_singleton,
319*NotIn_singleton;
320static PyObject* ast2obj_cmpop(cmpop_ty);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000321static PyTypeObject *Eq_type;
322static PyTypeObject *NotEq_type;
323static PyTypeObject *Lt_type;
324static PyTypeObject *LtE_type;
325static PyTypeObject *Gt_type;
326static PyTypeObject *GtE_type;
327static PyTypeObject *Is_type;
328static PyTypeObject *IsNot_type;
329static PyTypeObject *In_type;
330static PyTypeObject *NotIn_type;
331static PyTypeObject *comprehension_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000332static PyObject* ast2obj_comprehension(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000333static char *comprehension_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000334 "target",
335 "iter",
336 "ifs",
337};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000338static PyTypeObject *excepthandler_type;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000339static char *excepthandler_attributes[] = {
340 "lineno",
341 "col_offset",
342};
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000343static PyObject* ast2obj_excepthandler(void*);
Georg Brandla48f3ab2008-03-30 06:40:17 +0000344static PyTypeObject *ExceptHandler_type;
345static char *ExceptHandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346 "type",
347 "name",
348 "body",
349};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000350static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000351static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000352static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000353 "args",
354 "vararg",
355 "kwarg",
356 "defaults",
357};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000358static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000359static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000360static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000361 "arg",
362 "value",
363};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000364static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000365static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000366static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000367 "name",
368 "asname",
369};
370
371
Georg Brandlc52ed592008-03-30 07:01:47 +0000372static int
373ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
374{
375 Py_ssize_t i, numfields = 0;
376 int res = -1;
377 PyObject *key, *value, *fields;
378 fields = PyObject_GetAttrString((PyObject*)Py_TYPE(self), "_fields");
379 if (!fields)
380 PyErr_Clear();
381 if (fields) {
382 numfields = PySequence_Size(fields);
383 if (numfields == -1)
384 goto cleanup;
385 }
386 res = 0; /* if no error occurs, this stays 0 to the end */
387 if (PyTuple_GET_SIZE(args) > 0) {
388 if (numfields != PyTuple_GET_SIZE(args)) {
Georg Brandl1721e752008-03-30 19:43:27 +0000389 PyErr_Format(PyExc_TypeError, "%.400s constructor takes %s"
390 "%" PY_FORMAT_SIZE_T "d positional argument%s",
391 Py_TYPE(self)->tp_name,
392 numfields == 0 ? "" : "either 0 or ",
Georg Brandlc52ed592008-03-30 07:01:47 +0000393 numfields, numfields == 1 ? "" : "s");
394 res = -1;
395 goto cleanup;
396 }
397 for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
398 /* cannot be reached when fields is NULL */
399 PyObject *name = PySequence_GetItem(fields, i);
400 if (!name) {
401 res = -1;
402 goto cleanup;
403 }
404 res = PyObject_SetAttr(self, name, PyTuple_GET_ITEM(args, i));
405 Py_DECREF(name);
406 if (res < 0)
407 goto cleanup;
408 }
409 }
410 if (kw) {
411 i = 0; /* needed by PyDict_Next */
412 while (PyDict_Next(kw, &i, &key, &value)) {
413 res = PyObject_SetAttr(self, key, value);
414 if (res < 0)
415 goto cleanup;
416 }
417 }
418 cleanup:
419 Py_XDECREF(fields);
420 return res;
421}
422
423static PyTypeObject AST_type = {
424 PyVarObject_HEAD_INIT(&PyType_Type, 0)
425 "AST",
426 sizeof(PyObject),
427 0,
428 0, /* tp_dealloc */
429 0, /* tp_print */
430 0, /* tp_getattr */
431 0, /* tp_setattr */
432 0, /* tp_compare */
433 0, /* tp_repr */
434 0, /* tp_as_number */
435 0, /* tp_as_sequence */
436 0, /* tp_as_mapping */
437 0, /* tp_hash */
438 0, /* tp_call */
439 0, /* tp_str */
440 PyObject_GenericGetAttr, /* tp_getattro */
441 PyObject_GenericSetAttr, /* tp_setattro */
442 0, /* tp_as_buffer */
443 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
444 0, /* tp_doc */
445 0, /* tp_traverse */
446 0, /* tp_clear */
447 0, /* tp_richcompare */
448 0, /* tp_weaklistoffset */
449 0, /* tp_iter */
450 0, /* tp_iternext */
451 0, /* tp_methods */
452 0, /* tp_members */
453 0, /* tp_getset */
454 0, /* tp_base */
455 0, /* tp_dict */
456 0, /* tp_descr_get */
457 0, /* tp_descr_set */
458 0, /* tp_dictoffset */
459 (initproc)ast_type_init, /* tp_init */
460 PyType_GenericAlloc, /* tp_alloc */
461 PyType_GenericNew, /* tp_new */
462 PyObject_Del, /* tp_free */
463};
464
465
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000466static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
467{
468 PyObject *fnames, *result;
469 int i;
Georg Brandl2c55c592008-03-30 19:00:49 +0000470 fnames = PyTuple_New(num_fields);
471 if (!fnames) return NULL;
472 for (i = 0; i < num_fields; i++) {
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000473 PyObject *field = PyString_FromString(fields[i]);
474 if (!field) {
475 Py_DECREF(fnames);
476 return NULL;
477 }
478 PyTuple_SET_ITEM(fnames, i, field);
479 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000480 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000481 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000482 Py_DECREF(fnames);
483 return (PyTypeObject*)result;
484}
485
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000486static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
487{
Neal Norwitz19379f12006-04-03 04:50:58 +0000488 int i, result;
Georg Brandlc52ed592008-03-30 07:01:47 +0000489 PyObject *s, *l = PyTuple_New(num_fields);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000490 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000491 for(i = 0; i < num_fields; i++) {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000492 s = PyString_FromString(attrs[i]);
493 if (!s) {
494 Py_DECREF(l);
495 return 0;
496 }
Georg Brandlc52ed592008-03-30 07:01:47 +0000497 PyTuple_SET_ITEM(l, i, s);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000498 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000499 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
500 Py_DECREF(l);
501 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000502}
503
Georg Brandlfc8eef32008-03-28 12:11:56 +0000504/* Conversion AST -> Python */
505
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000506static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
507{
508 int i, n = asdl_seq_LEN(seq);
509 PyObject *result = PyList_New(n);
510 PyObject *value;
511 if (!result)
512 return NULL;
513 for (i = 0; i < n; i++) {
514 value = func(asdl_seq_GET(seq, i));
515 if (!value) {
516 Py_DECREF(result);
517 return NULL;
518 }
519 PyList_SET_ITEM(result, i, value);
520 }
521 return result;
522}
523
524static PyObject* ast2obj_object(void *o)
525{
526 if (!o)
527 o = Py_None;
528 Py_INCREF((PyObject*)o);
529 return (PyObject*)o;
530}
531#define ast2obj_identifier ast2obj_object
532#define ast2obj_string ast2obj_object
533static PyObject* ast2obj_bool(bool b)
534{
535 return PyBool_FromLong(b);
536}
537
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000538static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000539{
540 return PyInt_FromLong(b);
541}
542
Georg Brandlfc8eef32008-03-28 12:11:56 +0000543/* Conversion Python -> AST */
544
545static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
546{
547 if (obj == Py_None)
548 obj = NULL;
549 if (obj)
550 PyArena_AddPyObject(arena, obj);
551 Py_XINCREF(obj);
552 *out = obj;
553 return 0;
554}
555
556#define obj2ast_identifier obj2ast_object
557#define obj2ast_string obj2ast_object
558
559static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
560{
561 int i;
562 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
563 PyObject *s = PyObject_Repr(obj);
564 if (s == NULL) return 1;
565 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
566 PyString_AS_STRING(s));
567 Py_DECREF(s);
568 return 1;
569 }
570
571 i = (int)PyLong_AsLong(obj);
572 if (i == -1 && PyErr_Occurred())
573 return 1;
574 *out = i;
575 return 0;
576}
577
578static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
579{
580 if (!PyBool_Check(obj)) {
581 PyObject *s = PyObject_Repr(obj);
582 if (s == NULL) return 1;
583 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
584 PyString_AS_STRING(s));
585 Py_DECREF(s);
586 return 1;
587 }
588
589 *out = (obj == Py_True);
590 return 0;
591}
592
593
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000594static int init_types(void)
595{
Neal Norwitz19379f12006-04-03 04:50:58 +0000596 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000597 if (initialized) return 1;
Georg Brandlc52ed592008-03-30 07:01:47 +0000598 mod_type = make_type("mod", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000599 if (!mod_type) return 0;
600 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000601 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000602 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000603 Interactive_type = make_type("Interactive", mod_type,
604 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000605 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000606 Expression_type = make_type("Expression", mod_type, Expression_fields,
607 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000608 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000609 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000610 if (!Suite_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000611 stmt_type = make_type("stmt", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000612 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000613 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000614 FunctionDef_type = make_type("FunctionDef", stmt_type,
615 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000616 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000617 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000618 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000619 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000620 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000621 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000622 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000623 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000625 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000626 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000627 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000629 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000630 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000631 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000632 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000633 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000634 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000635 With_type = make_type("With", stmt_type, With_fields, 3);
636 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000637 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000640 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000641 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
642 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000643 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000644 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000645 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000646 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000649 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000650 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000651 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000652 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000653 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000654 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000655 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000657 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000659 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000661 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!Continue_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000663 expr_type = make_type("expr", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000664 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000665 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000666 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000670 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000674 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000678 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 GeneratorExp_type = make_type("GeneratorExp", expr_type,
681 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000682 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000683 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000684 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000685 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000686 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000687 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000688 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000689 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000690 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000691 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000692 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000693 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000694 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000695 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000696 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000697 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000698 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000699 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000700 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000701 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000702 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000703 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000704 if (!Tuple_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000705 expr_context_type = make_type("expr_context", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000706 if (!expr_context_type) return 0;
707 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000708 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000710 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000712 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000714 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000715 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000716 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000717 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000718 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000719 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000720 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000721 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000722 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000723 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000724 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000725 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000726 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000727 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000728 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000729 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000730 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000731 if (!Param_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000732 slice_type = make_type("slice", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000733 if (!slice_type) return 0;
734 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000735 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000736 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000737 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000738 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000739 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000740 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000741 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000742 if (!Index_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000743 boolop_type = make_type("boolop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000744 if (!boolop_type) return 0;
745 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000746 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000748 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000750 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000752 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Or_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000754 operator_type = make_type("operator", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!operator_type) return 0;
756 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000757 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000758 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000759 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000760 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000761 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000762 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000763 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000764 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000765 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000766 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000767 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000768 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000769 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000770 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000771 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000772 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000773 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000774 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000775 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000776 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000777 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000779 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000780 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000781 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000782 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000783 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000784 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000785 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000786 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000787 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000788 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000789 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000790 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000791 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000792 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000793 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000794 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000795 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000796 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000797 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000798 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000799 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000800 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000801 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000802 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000803 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000804 if (!FloorDiv_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000805 unaryop_type = make_type("unaryop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000806 if (!unaryop_type) return 0;
807 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000808 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000809 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000810 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000811 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000812 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000813 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000814 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000815 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000816 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000817 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000818 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000819 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000820 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000821 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000822 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000823 if (!USub_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000824 cmpop_type = make_type("cmpop", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000825 if (!cmpop_type) return 0;
826 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000827 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000828 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000829 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000830 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000831 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000832 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000833 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000834 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000835 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000836 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000837 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000838 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000839 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000840 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000841 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000842 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000843 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000844 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000845 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000846 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000847 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000848 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000849 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000850 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000851 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000852 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000853 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000854 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000855 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000856 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000857 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000858 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000859 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000860 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000861 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000862 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000863 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000864 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000865 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000866 if (!NotIn_singleton) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000867 comprehension_type = make_type("comprehension", &AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000868 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000869 if (!comprehension_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000870 excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000871 if (!excepthandler_type) return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +0000872 if (!add_attributes(excepthandler_type, excepthandler_attributes, 2))
873 return 0;
874 ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,
875 ExceptHandler_fields, 3);
876 if (!ExceptHandler_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000877 arguments_type = make_type("arguments", &AST_type, arguments_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000878 if (!arguments_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000879 keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000880 if (!keyword_type) return 0;
Georg Brandlc52ed592008-03-30 07:01:47 +0000881 alias_type = make_type("alias", &AST_type, alias_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000882 if (!alias_type) return 0;
883 initialized = 1;
884 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000885}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000886
Georg Brandlfc8eef32008-03-28 12:11:56 +0000887static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
888static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
889static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
890static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
891 arena);
892static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
893static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
894static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
895static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
896static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
897static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
898 arena);
899static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
900 arena);
901static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
902static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
903static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
904
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000906Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907{
908 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000909 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000910 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 p->kind = Module_kind;
913 p->v.Module.body = body;
914 return p;
915}
916
917mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000918Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919{
920 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000921 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000922 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 p->kind = Interactive_kind;
925 p->v.Interactive.body = body;
926 return p;
927}
928
929mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000930Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931{
932 mod_ty p;
933 if (!body) {
934 PyErr_SetString(PyExc_ValueError,
935 "field body is required for Expression");
936 return NULL;
937 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000938 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000939 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941 p->kind = Expression_kind;
942 p->v.Expression.body = body;
943 return p;
944}
945
946mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000947Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948{
949 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000950 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000951 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953 p->kind = Suite_kind;
954 p->v.Suite.body = body;
955 return p;
956}
957
958stmt_ty
959FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +0000960 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961{
962 stmt_ty p;
963 if (!name) {
964 PyErr_SetString(PyExc_ValueError,
965 "field name is required for FunctionDef");
966 return NULL;
967 }
968 if (!args) {
969 PyErr_SetString(PyExc_ValueError,
970 "field args is required for FunctionDef");
971 return NULL;
972 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000973 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000974 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976 p->kind = FunctionDef_kind;
977 p->v.FunctionDef.name = name;
978 p->v.FunctionDef.args = args;
979 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +0000980 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000982 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983 return p;
984}
985
986stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000987ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
988 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989{
990 stmt_ty p;
991 if (!name) {
992 PyErr_SetString(PyExc_ValueError,
993 "field name is required for ClassDef");
994 return NULL;
995 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000996 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000997 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 p->kind = ClassDef_kind;
1000 p->v.ClassDef.name = name;
1001 p->v.ClassDef.bases = bases;
1002 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +00001003 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001005 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 return p;
1007}
1008
1009stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001010Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011{
1012 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001013 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001014 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016 p->kind = Return_kind;
1017 p->v.Return.value = value;
1018 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001019 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020 return p;
1021}
1022
1023stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001024Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025{
1026 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001027 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001028 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 p->kind = Delete_kind;
1031 p->v.Delete.targets = targets;
1032 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001033 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034 return p;
1035}
1036
1037stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001038Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
1039 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001040{
1041 stmt_ty p;
1042 if (!value) {
1043 PyErr_SetString(PyExc_ValueError,
1044 "field value is required for Assign");
1045 return NULL;
1046 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001047 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001048 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 p->kind = Assign_kind;
1051 p->v.Assign.targets = targets;
1052 p->v.Assign.value = value;
1053 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001054 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return p;
1056}
1057
1058stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001059AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
1060 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061{
1062 stmt_ty p;
1063 if (!target) {
1064 PyErr_SetString(PyExc_ValueError,
1065 "field target is required for AugAssign");
1066 return NULL;
1067 }
1068 if (!op) {
1069 PyErr_SetString(PyExc_ValueError,
1070 "field op is required for AugAssign");
1071 return NULL;
1072 }
1073 if (!value) {
1074 PyErr_SetString(PyExc_ValueError,
1075 "field value is required for AugAssign");
1076 return NULL;
1077 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001078 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001079 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 p->kind = AugAssign_kind;
1082 p->v.AugAssign.target = target;
1083 p->v.AugAssign.op = op;
1084 p->v.AugAssign.value = value;
1085 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001086 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 return p;
1088}
1089
1090stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001091Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
1092 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093{
1094 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001095 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001096 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 p->kind = Print_kind;
1099 p->v.Print.dest = dest;
1100 p->v.Print.values = values;
1101 p->v.Print.nl = nl;
1102 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001103 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104 return p;
1105}
1106
1107stmt_ty
1108For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001109 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110{
1111 stmt_ty p;
1112 if (!target) {
1113 PyErr_SetString(PyExc_ValueError,
1114 "field target is required for For");
1115 return NULL;
1116 }
1117 if (!iter) {
1118 PyErr_SetString(PyExc_ValueError,
1119 "field iter is required for For");
1120 return NULL;
1121 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001122 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001123 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001125 p->kind = For_kind;
1126 p->v.For.target = target;
1127 p->v.For.iter = iter;
1128 p->v.For.body = body;
1129 p->v.For.orelse = orelse;
1130 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001131 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 return p;
1133}
1134
1135stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1137 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138{
1139 stmt_ty p;
1140 if (!test) {
1141 PyErr_SetString(PyExc_ValueError,
1142 "field test is required for While");
1143 return NULL;
1144 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001145 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001146 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 p->kind = While_kind;
1149 p->v.While.test = test;
1150 p->v.While.body = body;
1151 p->v.While.orelse = orelse;
1152 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001153 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return p;
1155}
1156
1157stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001158If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1159 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160{
1161 stmt_ty p;
1162 if (!test) {
1163 PyErr_SetString(PyExc_ValueError,
1164 "field test is required for If");
1165 return NULL;
1166 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001167 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001168 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 p->kind = If_kind;
1171 p->v.If.test = test;
1172 p->v.If.body = body;
1173 p->v.If.orelse = orelse;
1174 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001175 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 return p;
1177}
1178
1179stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001180With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001181 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001182{
1183 stmt_ty p;
1184 if (!context_expr) {
1185 PyErr_SetString(PyExc_ValueError,
1186 "field context_expr is required for With");
1187 return NULL;
1188 }
1189 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001190 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001191 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001192 p->kind = With_kind;
1193 p->v.With.context_expr = context_expr;
1194 p->v.With.optional_vars = optional_vars;
1195 p->v.With.body = body;
1196 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001197 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001198 return p;
1199}
1200
1201stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001202Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1203 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204{
1205 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001206 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001207 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 p->kind = Raise_kind;
1210 p->v.Raise.type = type;
1211 p->v.Raise.inst = inst;
1212 p->v.Raise.tback = tback;
1213 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001214 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 return p;
1216}
1217
1218stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001219TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001220 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221{
1222 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001223 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001224 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 p->kind = TryExcept_kind;
1227 p->v.TryExcept.body = body;
1228 p->v.TryExcept.handlers = handlers;
1229 p->v.TryExcept.orelse = orelse;
1230 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001231 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232 return p;
1233}
1234
1235stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001236TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1237 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238{
1239 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001240 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001241 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 p->kind = TryFinally_kind;
1244 p->v.TryFinally.body = body;
1245 p->v.TryFinally.finalbody = finalbody;
1246 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001247 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248 return p;
1249}
1250
1251stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001252Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253{
1254 stmt_ty p;
1255 if (!test) {
1256 PyErr_SetString(PyExc_ValueError,
1257 "field test is required for Assert");
1258 return NULL;
1259 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001260 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001261 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001263 p->kind = Assert_kind;
1264 p->v.Assert.test = test;
1265 p->v.Assert.msg = msg;
1266 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001267 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 return p;
1269}
1270
1271stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001272Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273{
1274 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001275 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001276 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001278 p->kind = Import_kind;
1279 p->v.Import.names = names;
1280 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 return p;
1283}
1284
1285stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001286ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1287 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001288{
1289 stmt_ty p;
1290 if (!module) {
1291 PyErr_SetString(PyExc_ValueError,
1292 "field module is required for ImportFrom");
1293 return NULL;
1294 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001295 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001296 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 p->kind = ImportFrom_kind;
1299 p->v.ImportFrom.module = module;
1300 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001301 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001303 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304 return p;
1305}
1306
1307stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001308Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1309 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310{
1311 stmt_ty p;
1312 if (!body) {
1313 PyErr_SetString(PyExc_ValueError,
1314 "field body is required for Exec");
1315 return NULL;
1316 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001317 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001318 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 p->kind = Exec_kind;
1321 p->v.Exec.body = body;
1322 p->v.Exec.globals = globals;
1323 p->v.Exec.locals = locals;
1324 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001325 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 return p;
1327}
1328
1329stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001330Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331{
1332 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001333 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001334 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001335 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 p->kind = Global_kind;
1337 p->v.Global.names = names;
1338 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001339 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 return p;
1341}
1342
1343stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001344Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345{
1346 stmt_ty p;
1347 if (!value) {
1348 PyErr_SetString(PyExc_ValueError,
1349 "field value is required for Expr");
1350 return NULL;
1351 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001352 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001353 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 p->kind = Expr_kind;
1356 p->v.Expr.value = value;
1357 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001358 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 return p;
1360}
1361
1362stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001363Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364{
1365 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001366 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001367 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 p->kind = Pass_kind;
1370 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001371 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372 return p;
1373}
1374
1375stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001376Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377{
1378 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001379 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001380 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 p->kind = Break_kind;
1383 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001384 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 return p;
1386}
1387
1388stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001389Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390{
1391 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001392 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001393 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 p->kind = Continue_kind;
1396 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001397 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 return p;
1399}
1400
1401expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001402BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1403 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404{
1405 expr_ty p;
1406 if (!op) {
1407 PyErr_SetString(PyExc_ValueError,
1408 "field op is required for BoolOp");
1409 return NULL;
1410 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001411 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001412 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001414 p->kind = BoolOp_kind;
1415 p->v.BoolOp.op = op;
1416 p->v.BoolOp.values = values;
1417 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001418 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 return p;
1420}
1421
1422expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001423BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1424 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425{
1426 expr_ty p;
1427 if (!left) {
1428 PyErr_SetString(PyExc_ValueError,
1429 "field left is required for BinOp");
1430 return NULL;
1431 }
1432 if (!op) {
1433 PyErr_SetString(PyExc_ValueError,
1434 "field op is required for BinOp");
1435 return NULL;
1436 }
1437 if (!right) {
1438 PyErr_SetString(PyExc_ValueError,
1439 "field right is required for BinOp");
1440 return NULL;
1441 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001442 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001443 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 p->kind = BinOp_kind;
1446 p->v.BinOp.left = left;
1447 p->v.BinOp.op = op;
1448 p->v.BinOp.right = right;
1449 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001450 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 return p;
1452}
1453
1454expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001455UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1456 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457{
1458 expr_ty p;
1459 if (!op) {
1460 PyErr_SetString(PyExc_ValueError,
1461 "field op is required for UnaryOp");
1462 return NULL;
1463 }
1464 if (!operand) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "field operand is required for UnaryOp");
1467 return NULL;
1468 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001469 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001470 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 p->kind = UnaryOp_kind;
1473 p->v.UnaryOp.op = op;
1474 p->v.UnaryOp.operand = operand;
1475 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001476 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 return p;
1478}
1479
1480expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001481Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1482 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483{
1484 expr_ty p;
1485 if (!args) {
1486 PyErr_SetString(PyExc_ValueError,
1487 "field args is required for Lambda");
1488 return NULL;
1489 }
1490 if (!body) {
1491 PyErr_SetString(PyExc_ValueError,
1492 "field body is required for Lambda");
1493 return NULL;
1494 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001495 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001496 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 p->kind = Lambda_kind;
1499 p->v.Lambda.args = args;
1500 p->v.Lambda.body = body;
1501 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001502 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 return p;
1504}
1505
1506expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001507IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1508 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001509{
1510 expr_ty p;
1511 if (!test) {
1512 PyErr_SetString(PyExc_ValueError,
1513 "field test is required for IfExp");
1514 return NULL;
1515 }
1516 if (!body) {
1517 PyErr_SetString(PyExc_ValueError,
1518 "field body is required for IfExp");
1519 return NULL;
1520 }
1521 if (!orelse) {
1522 PyErr_SetString(PyExc_ValueError,
1523 "field orelse is required for IfExp");
1524 return NULL;
1525 }
1526 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001527 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001528 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001529 p->kind = IfExp_kind;
1530 p->v.IfExp.test = test;
1531 p->v.IfExp.body = body;
1532 p->v.IfExp.orelse = orelse;
1533 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001534 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001535 return p;
1536}
1537
1538expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001539Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1540 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541{
1542 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001543 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001544 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546 p->kind = Dict_kind;
1547 p->v.Dict.keys = keys;
1548 p->v.Dict.values = values;
1549 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001550 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 return p;
1552}
1553
1554expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001555ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1556 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001557{
1558 expr_ty p;
1559 if (!elt) {
1560 PyErr_SetString(PyExc_ValueError,
1561 "field elt is required for ListComp");
1562 return NULL;
1563 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001564 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001565 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567 p->kind = ListComp_kind;
1568 p->v.ListComp.elt = elt;
1569 p->v.ListComp.generators = generators;
1570 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001571 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 return p;
1573}
1574
1575expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001576GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1577 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001578{
1579 expr_ty p;
1580 if (!elt) {
1581 PyErr_SetString(PyExc_ValueError,
1582 "field elt is required for GeneratorExp");
1583 return NULL;
1584 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001585 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001586 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001587 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 p->kind = GeneratorExp_kind;
1589 p->v.GeneratorExp.elt = elt;
1590 p->v.GeneratorExp.generators = generators;
1591 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001592 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 return p;
1594}
1595
1596expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001597Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598{
1599 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001600 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001601 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001602 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 p->kind = Yield_kind;
1604 p->v.Yield.value = value;
1605 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001606 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607 return p;
1608}
1609
1610expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001611Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1612 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613{
1614 expr_ty p;
1615 if (!left) {
1616 PyErr_SetString(PyExc_ValueError,
1617 "field left is required for Compare");
1618 return NULL;
1619 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001620 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001621 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 p->kind = Compare_kind;
1624 p->v.Compare.left = left;
1625 p->v.Compare.ops = ops;
1626 p->v.Compare.comparators = comparators;
1627 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001628 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 return p;
1630}
1631
1632expr_ty
1633Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001634 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001635{
1636 expr_ty p;
1637 if (!func) {
1638 PyErr_SetString(PyExc_ValueError,
1639 "field func is required for Call");
1640 return NULL;
1641 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001642 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001643 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 p->kind = Call_kind;
1646 p->v.Call.func = func;
1647 p->v.Call.args = args;
1648 p->v.Call.keywords = keywords;
1649 p->v.Call.starargs = starargs;
1650 p->v.Call.kwargs = kwargs;
1651 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001652 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 return p;
1654}
1655
1656expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001657Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658{
1659 expr_ty p;
1660 if (!value) {
1661 PyErr_SetString(PyExc_ValueError,
1662 "field value is required for Repr");
1663 return NULL;
1664 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001665 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001666 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 p->kind = Repr_kind;
1669 p->v.Repr.value = value;
1670 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001671 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 return p;
1673}
1674
1675expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001676Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677{
1678 expr_ty p;
1679 if (!n) {
1680 PyErr_SetString(PyExc_ValueError,
1681 "field n is required for Num");
1682 return NULL;
1683 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001684 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001685 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 p->kind = Num_kind;
1688 p->v.Num.n = n;
1689 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001690 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691 return p;
1692}
1693
1694expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001695Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696{
1697 expr_ty p;
1698 if (!s) {
1699 PyErr_SetString(PyExc_ValueError,
1700 "field s is required for Str");
1701 return NULL;
1702 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001703 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001704 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 p->kind = Str_kind;
1707 p->v.Str.s = s;
1708 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 return p;
1711}
1712
1713expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001714Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1715 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716{
1717 expr_ty p;
1718 if (!value) {
1719 PyErr_SetString(PyExc_ValueError,
1720 "field value is required for Attribute");
1721 return NULL;
1722 }
1723 if (!attr) {
1724 PyErr_SetString(PyExc_ValueError,
1725 "field attr is required for Attribute");
1726 return NULL;
1727 }
1728 if (!ctx) {
1729 PyErr_SetString(PyExc_ValueError,
1730 "field ctx is required for Attribute");
1731 return NULL;
1732 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001733 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001734 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736 p->kind = Attribute_kind;
1737 p->v.Attribute.value = value;
1738 p->v.Attribute.attr = attr;
1739 p->v.Attribute.ctx = ctx;
1740 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001741 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 return p;
1743}
1744
1745expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1747 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748{
1749 expr_ty p;
1750 if (!value) {
1751 PyErr_SetString(PyExc_ValueError,
1752 "field value is required for Subscript");
1753 return NULL;
1754 }
1755 if (!slice) {
1756 PyErr_SetString(PyExc_ValueError,
1757 "field slice is required for Subscript");
1758 return NULL;
1759 }
1760 if (!ctx) {
1761 PyErr_SetString(PyExc_ValueError,
1762 "field ctx is required for Subscript");
1763 return NULL;
1764 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001765 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001766 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 p->kind = Subscript_kind;
1769 p->v.Subscript.value = value;
1770 p->v.Subscript.slice = slice;
1771 p->v.Subscript.ctx = ctx;
1772 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001773 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 return p;
1775}
1776
1777expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001778Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1779 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780{
1781 expr_ty p;
1782 if (!id) {
1783 PyErr_SetString(PyExc_ValueError,
1784 "field id is required for Name");
1785 return NULL;
1786 }
1787 if (!ctx) {
1788 PyErr_SetString(PyExc_ValueError,
1789 "field ctx is required for Name");
1790 return NULL;
1791 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001792 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001793 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 p->kind = Name_kind;
1796 p->v.Name.id = id;
1797 p->v.Name.ctx = ctx;
1798 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001799 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001800 return p;
1801}
1802
1803expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001804List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1805 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
1807 expr_ty p;
1808 if (!ctx) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field ctx is required for List");
1811 return NULL;
1812 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001813 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001814 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816 p->kind = List_kind;
1817 p->v.List.elts = elts;
1818 p->v.List.ctx = ctx;
1819 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001820 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 return p;
1822}
1823
1824expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001825Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1826 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827{
1828 expr_ty p;
1829 if (!ctx) {
1830 PyErr_SetString(PyExc_ValueError,
1831 "field ctx is required for Tuple");
1832 return NULL;
1833 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001834 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001835 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001837 p->kind = Tuple_kind;
1838 p->v.Tuple.elts = elts;
1839 p->v.Tuple.ctx = ctx;
1840 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001841 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 return p;
1843}
1844
1845slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001846Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847{
1848 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001849 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001850 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 p->kind = Ellipsis_kind;
1853 return p;
1854}
1855
1856slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001857Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858{
1859 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001860 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001861 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 p->kind = Slice_kind;
1864 p->v.Slice.lower = lower;
1865 p->v.Slice.upper = upper;
1866 p->v.Slice.step = step;
1867 return p;
1868}
1869
1870slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001871ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872{
1873 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001874 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001875 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 p->kind = ExtSlice_kind;
1878 p->v.ExtSlice.dims = dims;
1879 return p;
1880}
1881
1882slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001883Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884{
1885 slice_ty p;
1886 if (!value) {
1887 PyErr_SetString(PyExc_ValueError,
1888 "field value is required for Index");
1889 return NULL;
1890 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001891 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001892 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 p->kind = Index_kind;
1895 p->v.Index.value = value;
1896 return p;
1897}
1898
1899comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001900comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901{
1902 comprehension_ty p;
1903 if (!target) {
1904 PyErr_SetString(PyExc_ValueError,
1905 "field target is required for comprehension");
1906 return NULL;
1907 }
1908 if (!iter) {
1909 PyErr_SetString(PyExc_ValueError,
1910 "field iter is required for comprehension");
1911 return NULL;
1912 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001913 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001914 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 p->target = target;
1917 p->iter = iter;
1918 p->ifs = ifs;
1919 return p;
1920}
1921
1922excepthandler_ty
Georg Brandla48f3ab2008-03-30 06:40:17 +00001923ExceptHandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001924 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925{
1926 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001927 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001928 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 return NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001930 p->kind = ExceptHandler_kind;
1931 p->v.ExceptHandler.type = type;
1932 p->v.ExceptHandler.name = name;
1933 p->v.ExceptHandler.body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001934 p->lineno = lineno;
1935 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 return p;
1937}
1938
1939arguments_ty
1940arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001941 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942{
1943 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001944 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001945 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 p->args = args;
1948 p->vararg = vararg;
1949 p->kwarg = kwarg;
1950 p->defaults = defaults;
1951 return p;
1952}
1953
1954keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001955keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956{
1957 keyword_ty p;
1958 if (!arg) {
1959 PyErr_SetString(PyExc_ValueError,
1960 "field arg is required for keyword");
1961 return NULL;
1962 }
1963 if (!value) {
1964 PyErr_SetString(PyExc_ValueError,
1965 "field value is required for keyword");
1966 return NULL;
1967 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001968 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001969 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 p->arg = arg;
1972 p->value = value;
1973 return p;
1974}
1975
1976alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001977alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978{
1979 alias_ty p;
1980 if (!name) {
1981 PyErr_SetString(PyExc_ValueError,
1982 "field name is required for alias");
1983 return NULL;
1984 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001985 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001986 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 p->name = name;
1989 p->asname = asname;
1990 return p;
1991}
1992
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001993
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001994PyObject*
1995ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001996{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001997 mod_ty o = (mod_ty)_o;
1998 PyObject *result = NULL, *value = NULL;
1999 if (!o) {
2000 Py_INCREF(Py_None);
2001 return Py_None;
2002 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00002003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 switch (o->kind) {
2005 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002006 result = PyType_GenericNew(Module_type, NULL, NULL);
2007 if (!result) goto failed;
2008 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
2009 if (!value) goto failed;
2010 if (PyObject_SetAttrString(result, "body", value) == -1)
2011 goto failed;
2012 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 break;
2014 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002015 result = PyType_GenericNew(Interactive_type, NULL, NULL);
2016 if (!result) goto failed;
2017 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
2018 if (!value) goto failed;
2019 if (PyObject_SetAttrString(result, "body", value) == -1)
2020 goto failed;
2021 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 break;
2023 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002024 result = PyType_GenericNew(Expression_type, NULL, NULL);
2025 if (!result) goto failed;
2026 value = ast2obj_expr(o->v.Expression.body);
2027 if (!value) goto failed;
2028 if (PyObject_SetAttrString(result, "body", value) == -1)
2029 goto failed;
2030 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 break;
2032 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002033 result = PyType_GenericNew(Suite_type, NULL, NULL);
2034 if (!result) goto failed;
2035 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
2036 if (!value) goto failed;
2037 if (PyObject_SetAttrString(result, "body", value) == -1)
2038 goto failed;
2039 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 break;
2041 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002042 return result;
2043failed:
2044 Py_XDECREF(value);
2045 Py_XDECREF(result);
2046 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047}
2048
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002049PyObject*
2050ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002052 stmt_ty o = (stmt_ty)_o;
2053 PyObject *result = NULL, *value = NULL;
2054 if (!o) {
2055 Py_INCREF(Py_None);
2056 return Py_None;
2057 }
2058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 switch (o->kind) {
2060 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002061 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
2062 if (!result) goto failed;
2063 value = ast2obj_identifier(o->v.FunctionDef.name);
2064 if (!value) goto failed;
2065 if (PyObject_SetAttrString(result, "name", value) == -1)
2066 goto failed;
2067 Py_DECREF(value);
2068 value = ast2obj_arguments(o->v.FunctionDef.args);
2069 if (!value) goto failed;
2070 if (PyObject_SetAttrString(result, "args", value) == -1)
2071 goto failed;
2072 Py_DECREF(value);
2073 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
2074 if (!value) goto failed;
2075 if (PyObject_SetAttrString(result, "body", value) == -1)
2076 goto failed;
2077 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002078 value = ast2obj_list(o->v.FunctionDef.decorator_list,
2079 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002080 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00002081 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2082 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002083 goto failed;
2084 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 break;
2086 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002087 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
2088 if (!result) goto failed;
2089 value = ast2obj_identifier(o->v.ClassDef.name);
2090 if (!value) goto failed;
2091 if (PyObject_SetAttrString(result, "name", value) == -1)
2092 goto failed;
2093 Py_DECREF(value);
2094 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
2095 if (!value) goto failed;
2096 if (PyObject_SetAttrString(result, "bases", value) == -1)
2097 goto failed;
2098 Py_DECREF(value);
2099 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2100 if (!value) goto failed;
2101 if (PyObject_SetAttrString(result, "body", value) == -1)
2102 goto failed;
2103 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002104 value = ast2obj_list(o->v.ClassDef.decorator_list,
2105 ast2obj_expr);
2106 if (!value) goto failed;
2107 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2108 -1)
2109 goto failed;
2110 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 break;
2112 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002113 result = PyType_GenericNew(Return_type, NULL, NULL);
2114 if (!result) goto failed;
2115 value = ast2obj_expr(o->v.Return.value);
2116 if (!value) goto failed;
2117 if (PyObject_SetAttrString(result, "value", value) == -1)
2118 goto failed;
2119 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 break;
2121 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002122 result = PyType_GenericNew(Delete_type, NULL, NULL);
2123 if (!result) goto failed;
2124 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2125 if (!value) goto failed;
2126 if (PyObject_SetAttrString(result, "targets", value) == -1)
2127 goto failed;
2128 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 break;
2130 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002131 result = PyType_GenericNew(Assign_type, NULL, NULL);
2132 if (!result) goto failed;
2133 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2134 if (!value) goto failed;
2135 if (PyObject_SetAttrString(result, "targets", value) == -1)
2136 goto failed;
2137 Py_DECREF(value);
2138 value = ast2obj_expr(o->v.Assign.value);
2139 if (!value) goto failed;
2140 if (PyObject_SetAttrString(result, "value", value) == -1)
2141 goto failed;
2142 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 break;
2144 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002145 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2146 if (!result) goto failed;
2147 value = ast2obj_expr(o->v.AugAssign.target);
2148 if (!value) goto failed;
2149 if (PyObject_SetAttrString(result, "target", value) == -1)
2150 goto failed;
2151 Py_DECREF(value);
2152 value = ast2obj_operator(o->v.AugAssign.op);
2153 if (!value) goto failed;
2154 if (PyObject_SetAttrString(result, "op", value) == -1)
2155 goto failed;
2156 Py_DECREF(value);
2157 value = ast2obj_expr(o->v.AugAssign.value);
2158 if (!value) goto failed;
2159 if (PyObject_SetAttrString(result, "value", value) == -1)
2160 goto failed;
2161 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162 break;
2163 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002164 result = PyType_GenericNew(Print_type, NULL, NULL);
2165 if (!result) goto failed;
2166 value = ast2obj_expr(o->v.Print.dest);
2167 if (!value) goto failed;
2168 if (PyObject_SetAttrString(result, "dest", value) == -1)
2169 goto failed;
2170 Py_DECREF(value);
2171 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2172 if (!value) goto failed;
2173 if (PyObject_SetAttrString(result, "values", value) == -1)
2174 goto failed;
2175 Py_DECREF(value);
2176 value = ast2obj_bool(o->v.Print.nl);
2177 if (!value) goto failed;
2178 if (PyObject_SetAttrString(result, "nl", value) == -1)
2179 goto failed;
2180 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 break;
2182 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002183 result = PyType_GenericNew(For_type, NULL, NULL);
2184 if (!result) goto failed;
2185 value = ast2obj_expr(o->v.For.target);
2186 if (!value) goto failed;
2187 if (PyObject_SetAttrString(result, "target", value) == -1)
2188 goto failed;
2189 Py_DECREF(value);
2190 value = ast2obj_expr(o->v.For.iter);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "iter", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2196 if (!value) goto failed;
2197 if (PyObject_SetAttrString(result, "body", value) == -1)
2198 goto failed;
2199 Py_DECREF(value);
2200 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2201 if (!value) goto failed;
2202 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2203 goto failed;
2204 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 break;
2206 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002207 result = PyType_GenericNew(While_type, NULL, NULL);
2208 if (!result) goto failed;
2209 value = ast2obj_expr(o->v.While.test);
2210 if (!value) goto failed;
2211 if (PyObject_SetAttrString(result, "test", value) == -1)
2212 goto failed;
2213 Py_DECREF(value);
2214 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2215 if (!value) goto failed;
2216 if (PyObject_SetAttrString(result, "body", value) == -1)
2217 goto failed;
2218 Py_DECREF(value);
2219 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2220 if (!value) goto failed;
2221 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2222 goto failed;
2223 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 break;
2225 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002226 result = PyType_GenericNew(If_type, NULL, NULL);
2227 if (!result) goto failed;
2228 value = ast2obj_expr(o->v.If.test);
2229 if (!value) goto failed;
2230 if (PyObject_SetAttrString(result, "test", value) == -1)
2231 goto failed;
2232 Py_DECREF(value);
2233 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2234 if (!value) goto failed;
2235 if (PyObject_SetAttrString(result, "body", value) == -1)
2236 goto failed;
2237 Py_DECREF(value);
2238 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002244 case With_kind:
2245 result = PyType_GenericNew(With_type, NULL, NULL);
2246 if (!result) goto failed;
2247 value = ast2obj_expr(o->v.With.context_expr);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
2252 value = ast2obj_expr(o->v.With.optional_vars);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2255 -1)
2256 goto failed;
2257 Py_DECREF(value);
2258 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2259 if (!value) goto failed;
2260 if (PyObject_SetAttrString(result, "body", value) == -1)
2261 goto failed;
2262 Py_DECREF(value);
2263 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002265 result = PyType_GenericNew(Raise_type, NULL, NULL);
2266 if (!result) goto failed;
2267 value = ast2obj_expr(o->v.Raise.type);
2268 if (!value) goto failed;
2269 if (PyObject_SetAttrString(result, "type", value) == -1)
2270 goto failed;
2271 Py_DECREF(value);
2272 value = ast2obj_expr(o->v.Raise.inst);
2273 if (!value) goto failed;
2274 if (PyObject_SetAttrString(result, "inst", value) == -1)
2275 goto failed;
2276 Py_DECREF(value);
2277 value = ast2obj_expr(o->v.Raise.tback);
2278 if (!value) goto failed;
2279 if (PyObject_SetAttrString(result, "tback", value) == -1)
2280 goto failed;
2281 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 break;
2283 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002284 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2285 if (!result) goto failed;
2286 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2287 if (!value) goto failed;
2288 if (PyObject_SetAttrString(result, "body", value) == -1)
2289 goto failed;
2290 Py_DECREF(value);
2291 value = ast2obj_list(o->v.TryExcept.handlers,
2292 ast2obj_excepthandler);
2293 if (!value) goto failed;
2294 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2295 goto failed;
2296 Py_DECREF(value);
2297 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2298 if (!value) goto failed;
2299 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2300 goto failed;
2301 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 break;
2303 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002304 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2305 if (!result) goto failed;
2306 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2307 if (!value) goto failed;
2308 if (PyObject_SetAttrString(result, "body", value) == -1)
2309 goto failed;
2310 Py_DECREF(value);
2311 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2312 if (!value) goto failed;
2313 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2314 goto failed;
2315 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 break;
2317 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002318 result = PyType_GenericNew(Assert_type, NULL, NULL);
2319 if (!result) goto failed;
2320 value = ast2obj_expr(o->v.Assert.test);
2321 if (!value) goto failed;
2322 if (PyObject_SetAttrString(result, "test", value) == -1)
2323 goto failed;
2324 Py_DECREF(value);
2325 value = ast2obj_expr(o->v.Assert.msg);
2326 if (!value) goto failed;
2327 if (PyObject_SetAttrString(result, "msg", value) == -1)
2328 goto failed;
2329 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 break;
2331 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002332 result = PyType_GenericNew(Import_type, NULL, NULL);
2333 if (!result) goto failed;
2334 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2335 if (!value) goto failed;
2336 if (PyObject_SetAttrString(result, "names", value) == -1)
2337 goto failed;
2338 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 break;
2340 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002341 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2342 if (!result) goto failed;
2343 value = ast2obj_identifier(o->v.ImportFrom.module);
2344 if (!value) goto failed;
2345 if (PyObject_SetAttrString(result, "module", value) == -1)
2346 goto failed;
2347 Py_DECREF(value);
2348 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2349 if (!value) goto failed;
2350 if (PyObject_SetAttrString(result, "names", value) == -1)
2351 goto failed;
2352 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002353 value = ast2obj_int(o->v.ImportFrom.level);
2354 if (!value) goto failed;
2355 if (PyObject_SetAttrString(result, "level", value) == -1)
2356 goto failed;
2357 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002358 break;
2359 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002360 result = PyType_GenericNew(Exec_type, NULL, NULL);
2361 if (!result) goto failed;
2362 value = ast2obj_expr(o->v.Exec.body);
2363 if (!value) goto failed;
2364 if (PyObject_SetAttrString(result, "body", value) == -1)
2365 goto failed;
2366 Py_DECREF(value);
2367 value = ast2obj_expr(o->v.Exec.globals);
2368 if (!value) goto failed;
2369 if (PyObject_SetAttrString(result, "globals", value) == -1)
2370 goto failed;
2371 Py_DECREF(value);
2372 value = ast2obj_expr(o->v.Exec.locals);
2373 if (!value) goto failed;
2374 if (PyObject_SetAttrString(result, "locals", value) == -1)
2375 goto failed;
2376 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 break;
2378 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002379 result = PyType_GenericNew(Global_type, NULL, NULL);
2380 if (!result) goto failed;
2381 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2382 if (!value) goto failed;
2383 if (PyObject_SetAttrString(result, "names", value) == -1)
2384 goto failed;
2385 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 break;
2387 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002388 result = PyType_GenericNew(Expr_type, NULL, NULL);
2389 if (!result) goto failed;
2390 value = ast2obj_expr(o->v.Expr.value);
2391 if (!value) goto failed;
2392 if (PyObject_SetAttrString(result, "value", value) == -1)
2393 goto failed;
2394 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 break;
2396 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002397 result = PyType_GenericNew(Pass_type, NULL, NULL);
2398 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399 break;
2400 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002401 result = PyType_GenericNew(Break_type, NULL, NULL);
2402 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 break;
2404 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002405 result = PyType_GenericNew(Continue_type, NULL, NULL);
2406 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 break;
2408 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002409 value = ast2obj_int(o->lineno);
2410 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002411 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2412 goto failed;
2413 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002414 value = ast2obj_int(o->col_offset);
2415 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002416 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2417 goto failed;
2418 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002419 return result;
2420failed:
2421 Py_XDECREF(value);
2422 Py_XDECREF(result);
2423 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424}
2425
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002426PyObject*
2427ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002429 expr_ty o = (expr_ty)_o;
2430 PyObject *result = NULL, *value = NULL;
2431 if (!o) {
2432 Py_INCREF(Py_None);
2433 return Py_None;
2434 }
2435
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 switch (o->kind) {
2437 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002438 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2439 if (!result) goto failed;
2440 value = ast2obj_boolop(o->v.BoolOp.op);
2441 if (!value) goto failed;
2442 if (PyObject_SetAttrString(result, "op", value) == -1)
2443 goto failed;
2444 Py_DECREF(value);
2445 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2446 if (!value) goto failed;
2447 if (PyObject_SetAttrString(result, "values", value) == -1)
2448 goto failed;
2449 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 break;
2451 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002452 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2453 if (!result) goto failed;
2454 value = ast2obj_expr(o->v.BinOp.left);
2455 if (!value) goto failed;
2456 if (PyObject_SetAttrString(result, "left", value) == -1)
2457 goto failed;
2458 Py_DECREF(value);
2459 value = ast2obj_operator(o->v.BinOp.op);
2460 if (!value) goto failed;
2461 if (PyObject_SetAttrString(result, "op", value) == -1)
2462 goto failed;
2463 Py_DECREF(value);
2464 value = ast2obj_expr(o->v.BinOp.right);
2465 if (!value) goto failed;
2466 if (PyObject_SetAttrString(result, "right", value) == -1)
2467 goto failed;
2468 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 break;
2470 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002471 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2472 if (!result) goto failed;
2473 value = ast2obj_unaryop(o->v.UnaryOp.op);
2474 if (!value) goto failed;
2475 if (PyObject_SetAttrString(result, "op", value) == -1)
2476 goto failed;
2477 Py_DECREF(value);
2478 value = ast2obj_expr(o->v.UnaryOp.operand);
2479 if (!value) goto failed;
2480 if (PyObject_SetAttrString(result, "operand", value) == -1)
2481 goto failed;
2482 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 break;
2484 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002485 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2486 if (!result) goto failed;
2487 value = ast2obj_arguments(o->v.Lambda.args);
2488 if (!value) goto failed;
2489 if (PyObject_SetAttrString(result, "args", value) == -1)
2490 goto failed;
2491 Py_DECREF(value);
2492 value = ast2obj_expr(o->v.Lambda.body);
2493 if (!value) goto failed;
2494 if (PyObject_SetAttrString(result, "body", value) == -1)
2495 goto failed;
2496 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002498 case IfExp_kind:
2499 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2500 if (!result) goto failed;
2501 value = ast2obj_expr(o->v.IfExp.test);
2502 if (!value) goto failed;
2503 if (PyObject_SetAttrString(result, "test", value) == -1)
2504 goto failed;
2505 Py_DECREF(value);
2506 value = ast2obj_expr(o->v.IfExp.body);
2507 if (!value) goto failed;
2508 if (PyObject_SetAttrString(result, "body", value) == -1)
2509 goto failed;
2510 Py_DECREF(value);
2511 value = ast2obj_expr(o->v.IfExp.orelse);
2512 if (!value) goto failed;
2513 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2514 goto failed;
2515 Py_DECREF(value);
2516 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002518 result = PyType_GenericNew(Dict_type, NULL, NULL);
2519 if (!result) goto failed;
2520 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2521 if (!value) goto failed;
2522 if (PyObject_SetAttrString(result, "keys", value) == -1)
2523 goto failed;
2524 Py_DECREF(value);
2525 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2526 if (!value) goto failed;
2527 if (PyObject_SetAttrString(result, "values", value) == -1)
2528 goto failed;
2529 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 break;
2531 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002532 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2533 if (!result) goto failed;
2534 value = ast2obj_expr(o->v.ListComp.elt);
2535 if (!value) goto failed;
2536 if (PyObject_SetAttrString(result, "elt", value) == -1)
2537 goto failed;
2538 Py_DECREF(value);
2539 value = ast2obj_list(o->v.ListComp.generators,
2540 ast2obj_comprehension);
2541 if (!value) goto failed;
2542 if (PyObject_SetAttrString(result, "generators", value) == -1)
2543 goto failed;
2544 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 break;
2546 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2548 if (!result) goto failed;
2549 value = ast2obj_expr(o->v.GeneratorExp.elt);
2550 if (!value) goto failed;
2551 if (PyObject_SetAttrString(result, "elt", value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
2554 value = ast2obj_list(o->v.GeneratorExp.generators,
2555 ast2obj_comprehension);
2556 if (!value) goto failed;
2557 if (PyObject_SetAttrString(result, "generators", value) == -1)
2558 goto failed;
2559 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 break;
2561 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002562 result = PyType_GenericNew(Yield_type, NULL, NULL);
2563 if (!result) goto failed;
2564 value = ast2obj_expr(o->v.Yield.value);
2565 if (!value) goto failed;
2566 if (PyObject_SetAttrString(result, "value", value) == -1)
2567 goto failed;
2568 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 break;
2570 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002571 result = PyType_GenericNew(Compare_type, NULL, NULL);
2572 if (!result) goto failed;
2573 value = ast2obj_expr(o->v.Compare.left);
2574 if (!value) goto failed;
2575 if (PyObject_SetAttrString(result, "left", value) == -1)
2576 goto failed;
2577 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002578 {
2579 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2580 value = PyList_New(n);
2581 if (!value) goto failed;
2582 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002583 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 +00002584 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002585 if (!value) goto failed;
2586 if (PyObject_SetAttrString(result, "ops", value) == -1)
2587 goto failed;
2588 Py_DECREF(value);
2589 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2590 if (!value) goto failed;
2591 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2592 goto failed;
2593 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594 break;
2595 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002596 result = PyType_GenericNew(Call_type, NULL, NULL);
2597 if (!result) goto failed;
2598 value = ast2obj_expr(o->v.Call.func);
2599 if (!value) goto failed;
2600 if (PyObject_SetAttrString(result, "func", value) == -1)
2601 goto failed;
2602 Py_DECREF(value);
2603 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2604 if (!value) goto failed;
2605 if (PyObject_SetAttrString(result, "args", value) == -1)
2606 goto failed;
2607 Py_DECREF(value);
2608 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2609 if (!value) goto failed;
2610 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2611 goto failed;
2612 Py_DECREF(value);
2613 value = ast2obj_expr(o->v.Call.starargs);
2614 if (!value) goto failed;
2615 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2616 goto failed;
2617 Py_DECREF(value);
2618 value = ast2obj_expr(o->v.Call.kwargs);
2619 if (!value) goto failed;
2620 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2621 goto failed;
2622 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 break;
2624 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002625 result = PyType_GenericNew(Repr_type, NULL, NULL);
2626 if (!result) goto failed;
2627 value = ast2obj_expr(o->v.Repr.value);
2628 if (!value) goto failed;
2629 if (PyObject_SetAttrString(result, "value", value) == -1)
2630 goto failed;
2631 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 break;
2633 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002634 result = PyType_GenericNew(Num_type, NULL, NULL);
2635 if (!result) goto failed;
2636 value = ast2obj_object(o->v.Num.n);
2637 if (!value) goto failed;
2638 if (PyObject_SetAttrString(result, "n", value) == -1)
2639 goto failed;
2640 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 break;
2642 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002643 result = PyType_GenericNew(Str_type, NULL, NULL);
2644 if (!result) goto failed;
2645 value = ast2obj_string(o->v.Str.s);
2646 if (!value) goto failed;
2647 if (PyObject_SetAttrString(result, "s", value) == -1)
2648 goto failed;
2649 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 break;
2651 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002652 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2653 if (!result) goto failed;
2654 value = ast2obj_expr(o->v.Attribute.value);
2655 if (!value) goto failed;
2656 if (PyObject_SetAttrString(result, "value", value) == -1)
2657 goto failed;
2658 Py_DECREF(value);
2659 value = ast2obj_identifier(o->v.Attribute.attr);
2660 if (!value) goto failed;
2661 if (PyObject_SetAttrString(result, "attr", value) == -1)
2662 goto failed;
2663 Py_DECREF(value);
2664 value = ast2obj_expr_context(o->v.Attribute.ctx);
2665 if (!value) goto failed;
2666 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2667 goto failed;
2668 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 break;
2670 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002671 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2672 if (!result) goto failed;
2673 value = ast2obj_expr(o->v.Subscript.value);
2674 if (!value) goto failed;
2675 if (PyObject_SetAttrString(result, "value", value) == -1)
2676 goto failed;
2677 Py_DECREF(value);
2678 value = ast2obj_slice(o->v.Subscript.slice);
2679 if (!value) goto failed;
2680 if (PyObject_SetAttrString(result, "slice", value) == -1)
2681 goto failed;
2682 Py_DECREF(value);
2683 value = ast2obj_expr_context(o->v.Subscript.ctx);
2684 if (!value) goto failed;
2685 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2686 goto failed;
2687 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 break;
2689 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002690 result = PyType_GenericNew(Name_type, NULL, NULL);
2691 if (!result) goto failed;
2692 value = ast2obj_identifier(o->v.Name.id);
2693 if (!value) goto failed;
2694 if (PyObject_SetAttrString(result, "id", value) == -1)
2695 goto failed;
2696 Py_DECREF(value);
2697 value = ast2obj_expr_context(o->v.Name.ctx);
2698 if (!value) goto failed;
2699 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2700 goto failed;
2701 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 break;
2703 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002704 result = PyType_GenericNew(List_type, NULL, NULL);
2705 if (!result) goto failed;
2706 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2707 if (!value) goto failed;
2708 if (PyObject_SetAttrString(result, "elts", value) == -1)
2709 goto failed;
2710 Py_DECREF(value);
2711 value = ast2obj_expr_context(o->v.List.ctx);
2712 if (!value) goto failed;
2713 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2714 goto failed;
2715 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 break;
2717 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002718 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2719 if (!result) goto failed;
2720 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2721 if (!value) goto failed;
2722 if (PyObject_SetAttrString(result, "elts", value) == -1)
2723 goto failed;
2724 Py_DECREF(value);
2725 value = ast2obj_expr_context(o->v.Tuple.ctx);
2726 if (!value) goto failed;
2727 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2728 goto failed;
2729 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 break;
2731 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002732 value = ast2obj_int(o->lineno);
2733 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002734 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2735 goto failed;
2736 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002737 value = ast2obj_int(o->col_offset);
2738 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002739 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2740 goto failed;
2741 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002742 return result;
2743failed:
2744 Py_XDECREF(value);
2745 Py_XDECREF(result);
2746 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747}
2748
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002749PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002751 switch(o) {
2752 case Load:
2753 Py_INCREF(Load_singleton);
2754 return Load_singleton;
2755 case Store:
2756 Py_INCREF(Store_singleton);
2757 return Store_singleton;
2758 case Del:
2759 Py_INCREF(Del_singleton);
2760 return Del_singleton;
2761 case AugLoad:
2762 Py_INCREF(AugLoad_singleton);
2763 return AugLoad_singleton;
2764 case AugStore:
2765 Py_INCREF(AugStore_singleton);
2766 return AugStore_singleton;
2767 case Param:
2768 Py_INCREF(Param_singleton);
2769 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002770 default:
2771 /* should never happen, but just in case ... */
2772 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2773 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002776PyObject*
2777ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002779 slice_ty o = (slice_ty)_o;
2780 PyObject *result = NULL, *value = NULL;
2781 if (!o) {
2782 Py_INCREF(Py_None);
2783 return Py_None;
2784 }
2785
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 switch (o->kind) {
2787 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002788 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2789 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 break;
2791 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002792 result = PyType_GenericNew(Slice_type, NULL, NULL);
2793 if (!result) goto failed;
2794 value = ast2obj_expr(o->v.Slice.lower);
2795 if (!value) goto failed;
2796 if (PyObject_SetAttrString(result, "lower", value) == -1)
2797 goto failed;
2798 Py_DECREF(value);
2799 value = ast2obj_expr(o->v.Slice.upper);
2800 if (!value) goto failed;
2801 if (PyObject_SetAttrString(result, "upper", value) == -1)
2802 goto failed;
2803 Py_DECREF(value);
2804 value = ast2obj_expr(o->v.Slice.step);
2805 if (!value) goto failed;
2806 if (PyObject_SetAttrString(result, "step", value) == -1)
2807 goto failed;
2808 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 break;
2810 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002811 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2812 if (!result) goto failed;
2813 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2814 if (!value) goto failed;
2815 if (PyObject_SetAttrString(result, "dims", value) == -1)
2816 goto failed;
2817 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818 break;
2819 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002820 result = PyType_GenericNew(Index_type, NULL, NULL);
2821 if (!result) goto failed;
2822 value = ast2obj_expr(o->v.Index.value);
2823 if (!value) goto failed;
2824 if (PyObject_SetAttrString(result, "value", value) == -1)
2825 goto failed;
2826 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 break;
2828 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002829 return result;
2830failed:
2831 Py_XDECREF(value);
2832 Py_XDECREF(result);
2833 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834}
2835
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002836PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002838 switch(o) {
2839 case And:
2840 Py_INCREF(And_singleton);
2841 return And_singleton;
2842 case Or:
2843 Py_INCREF(Or_singleton);
2844 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002845 default:
2846 /* should never happen, but just in case ... */
2847 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2848 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002850}
2851PyObject* ast2obj_operator(operator_ty o)
2852{
2853 switch(o) {
2854 case Add:
2855 Py_INCREF(Add_singleton);
2856 return Add_singleton;
2857 case Sub:
2858 Py_INCREF(Sub_singleton);
2859 return Sub_singleton;
2860 case Mult:
2861 Py_INCREF(Mult_singleton);
2862 return Mult_singleton;
2863 case Div:
2864 Py_INCREF(Div_singleton);
2865 return Div_singleton;
2866 case Mod:
2867 Py_INCREF(Mod_singleton);
2868 return Mod_singleton;
2869 case Pow:
2870 Py_INCREF(Pow_singleton);
2871 return Pow_singleton;
2872 case LShift:
2873 Py_INCREF(LShift_singleton);
2874 return LShift_singleton;
2875 case RShift:
2876 Py_INCREF(RShift_singleton);
2877 return RShift_singleton;
2878 case BitOr:
2879 Py_INCREF(BitOr_singleton);
2880 return BitOr_singleton;
2881 case BitXor:
2882 Py_INCREF(BitXor_singleton);
2883 return BitXor_singleton;
2884 case BitAnd:
2885 Py_INCREF(BitAnd_singleton);
2886 return BitAnd_singleton;
2887 case FloorDiv:
2888 Py_INCREF(FloorDiv_singleton);
2889 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002890 default:
2891 /* should never happen, but just in case ... */
2892 PyErr_Format(PyExc_SystemError, "unknown operator found");
2893 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002894 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002895}
2896PyObject* ast2obj_unaryop(unaryop_ty o)
2897{
2898 switch(o) {
2899 case Invert:
2900 Py_INCREF(Invert_singleton);
2901 return Invert_singleton;
2902 case Not:
2903 Py_INCREF(Not_singleton);
2904 return Not_singleton;
2905 case UAdd:
2906 Py_INCREF(UAdd_singleton);
2907 return UAdd_singleton;
2908 case USub:
2909 Py_INCREF(USub_singleton);
2910 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002911 default:
2912 /* should never happen, but just in case ... */
2913 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2914 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002915 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002916}
2917PyObject* ast2obj_cmpop(cmpop_ty o)
2918{
2919 switch(o) {
2920 case Eq:
2921 Py_INCREF(Eq_singleton);
2922 return Eq_singleton;
2923 case NotEq:
2924 Py_INCREF(NotEq_singleton);
2925 return NotEq_singleton;
2926 case Lt:
2927 Py_INCREF(Lt_singleton);
2928 return Lt_singleton;
2929 case LtE:
2930 Py_INCREF(LtE_singleton);
2931 return LtE_singleton;
2932 case Gt:
2933 Py_INCREF(Gt_singleton);
2934 return Gt_singleton;
2935 case GtE:
2936 Py_INCREF(GtE_singleton);
2937 return GtE_singleton;
2938 case Is:
2939 Py_INCREF(Is_singleton);
2940 return Is_singleton;
2941 case IsNot:
2942 Py_INCREF(IsNot_singleton);
2943 return IsNot_singleton;
2944 case In:
2945 Py_INCREF(In_singleton);
2946 return In_singleton;
2947 case NotIn:
2948 Py_INCREF(NotIn_singleton);
2949 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002950 default:
2951 /* should never happen, but just in case ... */
2952 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2953 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002954 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002955}
2956PyObject*
2957ast2obj_comprehension(void* _o)
2958{
2959 comprehension_ty o = (comprehension_ty)_o;
2960 PyObject *result = NULL, *value = NULL;
2961 if (!o) {
2962 Py_INCREF(Py_None);
2963 return Py_None;
2964 }
2965
2966 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2967 if (!result) return NULL;
2968 value = ast2obj_expr(o->target);
2969 if (!value) goto failed;
2970 if (PyObject_SetAttrString(result, "target", value) == -1)
2971 goto failed;
2972 Py_DECREF(value);
2973 value = ast2obj_expr(o->iter);
2974 if (!value) goto failed;
2975 if (PyObject_SetAttrString(result, "iter", value) == -1)
2976 goto failed;
2977 Py_DECREF(value);
2978 value = ast2obj_list(o->ifs, ast2obj_expr);
2979 if (!value) goto failed;
2980 if (PyObject_SetAttrString(result, "ifs", value) == -1)
2981 goto failed;
2982 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002983 return result;
2984failed:
2985 Py_XDECREF(value);
2986 Py_XDECREF(result);
2987 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988}
2989
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002990PyObject*
2991ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002993 excepthandler_ty o = (excepthandler_ty)_o;
2994 PyObject *result = NULL, *value = NULL;
2995 if (!o) {
2996 Py_INCREF(Py_None);
2997 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002999
Georg Brandla48f3ab2008-03-30 06:40:17 +00003000 switch (o->kind) {
3001 case ExceptHandler_kind:
3002 result = PyType_GenericNew(ExceptHandler_type, NULL, NULL);
3003 if (!result) goto failed;
3004 value = ast2obj_expr(o->v.ExceptHandler.type);
3005 if (!value) goto failed;
3006 if (PyObject_SetAttrString(result, "type", value) == -1)
3007 goto failed;
3008 Py_DECREF(value);
3009 value = ast2obj_expr(o->v.ExceptHandler.name);
3010 if (!value) goto failed;
3011 if (PyObject_SetAttrString(result, "name", value) == -1)
3012 goto failed;
3013 Py_DECREF(value);
3014 value = ast2obj_list(o->v.ExceptHandler.body, ast2obj_stmt);
3015 if (!value) goto failed;
3016 if (PyObject_SetAttrString(result, "body", value) == -1)
3017 goto failed;
3018 Py_DECREF(value);
3019 break;
3020 }
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003021 value = ast2obj_int(o->lineno);
3022 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003023 if (PyObject_SetAttrString(result, "lineno", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003024 goto failed;
3025 Py_DECREF(value);
3026 value = ast2obj_int(o->col_offset);
3027 if (!value) goto failed;
Georg Brandla48f3ab2008-03-30 06:40:17 +00003028 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
Jeremy Hylton2f327c12006-04-04 04:00:23 +00003029 goto failed;
3030 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003031 return result;
3032failed:
3033 Py_XDECREF(value);
3034 Py_XDECREF(result);
3035 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036}
3037
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003038PyObject*
3039ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003041 arguments_ty o = (arguments_ty)_o;
3042 PyObject *result = NULL, *value = NULL;
3043 if (!o) {
3044 Py_INCREF(Py_None);
3045 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003047
3048 result = PyType_GenericNew(arguments_type, NULL, NULL);
3049 if (!result) return NULL;
3050 value = ast2obj_list(o->args, ast2obj_expr);
3051 if (!value) goto failed;
3052 if (PyObject_SetAttrString(result, "args", value) == -1)
3053 goto failed;
3054 Py_DECREF(value);
3055 value = ast2obj_identifier(o->vararg);
3056 if (!value) goto failed;
3057 if (PyObject_SetAttrString(result, "vararg", value) == -1)
3058 goto failed;
3059 Py_DECREF(value);
3060 value = ast2obj_identifier(o->kwarg);
3061 if (!value) goto failed;
3062 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
3063 goto failed;
3064 Py_DECREF(value);
3065 value = ast2obj_list(o->defaults, ast2obj_expr);
3066 if (!value) goto failed;
3067 if (PyObject_SetAttrString(result, "defaults", value) == -1)
3068 goto failed;
3069 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003070 return result;
3071failed:
3072 Py_XDECREF(value);
3073 Py_XDECREF(result);
3074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003075}
3076
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003077PyObject*
3078ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003080 keyword_ty o = (keyword_ty)_o;
3081 PyObject *result = NULL, *value = NULL;
3082 if (!o) {
3083 Py_INCREF(Py_None);
3084 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003086
3087 result = PyType_GenericNew(keyword_type, NULL, NULL);
3088 if (!result) return NULL;
3089 value = ast2obj_identifier(o->arg);
3090 if (!value) goto failed;
3091 if (PyObject_SetAttrString(result, "arg", value) == -1)
3092 goto failed;
3093 Py_DECREF(value);
3094 value = ast2obj_expr(o->value);
3095 if (!value) goto failed;
3096 if (PyObject_SetAttrString(result, "value", value) == -1)
3097 goto failed;
3098 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003099 return result;
3100failed:
3101 Py_XDECREF(value);
3102 Py_XDECREF(result);
3103 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104}
3105
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003106PyObject*
3107ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003109 alias_ty o = (alias_ty)_o;
3110 PyObject *result = NULL, *value = NULL;
3111 if (!o) {
3112 Py_INCREF(Py_None);
3113 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003115
3116 result = PyType_GenericNew(alias_type, NULL, NULL);
3117 if (!result) return NULL;
3118 value = ast2obj_identifier(o->name);
3119 if (!value) goto failed;
3120 if (PyObject_SetAttrString(result, "name", value) == -1)
3121 goto failed;
3122 Py_DECREF(value);
3123 value = ast2obj_identifier(o->asname);
3124 if (!value) goto failed;
3125 if (PyObject_SetAttrString(result, "asname", value) == -1)
3126 goto failed;
3127 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003128 return result;
3129failed:
3130 Py_XDECREF(value);
3131 Py_XDECREF(result);
3132 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133}
3134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135
Georg Brandlfc8eef32008-03-28 12:11:56 +00003136int
3137obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3138{
3139 PyObject* tmp = NULL;
3140
3141
3142 if (obj == Py_None) {
3143 *out = NULL;
3144 return 0;
3145 }
3146 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3147 asdl_seq* body;
3148
3149 if (PyObject_HasAttrString(obj, "body")) {
3150 int res;
3151 Py_ssize_t len;
3152 Py_ssize_t i;
3153 tmp = PyObject_GetAttrString(obj, "body");
3154 if (tmp == NULL) goto failed;
3155 if (!PyList_Check(tmp)) {
3156 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3157 goto failed;
3158 }
3159 len = PyList_GET_SIZE(tmp);
3160 body = asdl_seq_new(len, arena);
3161 if (body == NULL) goto failed;
3162 for (i = 0; i < len; i++) {
3163 stmt_ty value;
3164 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3165 if (res != 0) goto failed;
3166 asdl_seq_SET(body, i, value);
3167 }
3168 Py_XDECREF(tmp);
3169 tmp = NULL;
3170 } else {
3171 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3172 return 1;
3173 }
3174 *out = Module(body, arena);
3175 if (*out == NULL) goto failed;
3176 return 0;
3177 }
3178 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3179 asdl_seq* body;
3180
3181 if (PyObject_HasAttrString(obj, "body")) {
3182 int res;
3183 Py_ssize_t len;
3184 Py_ssize_t i;
3185 tmp = PyObject_GetAttrString(obj, "body");
3186 if (tmp == NULL) goto failed;
3187 if (!PyList_Check(tmp)) {
3188 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3189 goto failed;
3190 }
3191 len = PyList_GET_SIZE(tmp);
3192 body = asdl_seq_new(len, arena);
3193 if (body == NULL) goto failed;
3194 for (i = 0; i < len; i++) {
3195 stmt_ty value;
3196 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3197 if (res != 0) goto failed;
3198 asdl_seq_SET(body, i, value);
3199 }
3200 Py_XDECREF(tmp);
3201 tmp = NULL;
3202 } else {
3203 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3204 return 1;
3205 }
3206 *out = Interactive(body, arena);
3207 if (*out == NULL) goto failed;
3208 return 0;
3209 }
3210 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3211 expr_ty body;
3212
3213 if (PyObject_HasAttrString(obj, "body")) {
3214 int res;
3215 tmp = PyObject_GetAttrString(obj, "body");
3216 if (tmp == NULL) goto failed;
3217 res = obj2ast_expr(tmp, &body, arena);
3218 if (res != 0) goto failed;
3219 Py_XDECREF(tmp);
3220 tmp = NULL;
3221 } else {
3222 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3223 return 1;
3224 }
3225 *out = Expression(body, arena);
3226 if (*out == NULL) goto failed;
3227 return 0;
3228 }
3229 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3230 asdl_seq* body;
3231
3232 if (PyObject_HasAttrString(obj, "body")) {
3233 int res;
3234 Py_ssize_t len;
3235 Py_ssize_t i;
3236 tmp = PyObject_GetAttrString(obj, "body");
3237 if (tmp == NULL) goto failed;
3238 if (!PyList_Check(tmp)) {
3239 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3240 goto failed;
3241 }
3242 len = PyList_GET_SIZE(tmp);
3243 body = asdl_seq_new(len, arena);
3244 if (body == NULL) goto failed;
3245 for (i = 0; i < len; i++) {
3246 stmt_ty value;
3247 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3248 if (res != 0) goto failed;
3249 asdl_seq_SET(body, i, value);
3250 }
3251 Py_XDECREF(tmp);
3252 tmp = NULL;
3253 } else {
3254 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3255 return 1;
3256 }
3257 *out = Suite(body, arena);
3258 if (*out == NULL) goto failed;
3259 return 0;
3260 }
3261
3262 tmp = PyObject_Repr(obj);
3263 if (tmp == NULL) goto failed;
3264 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3265failed:
3266 Py_XDECREF(tmp);
3267 return 1;
3268}
3269
3270int
3271obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3272{
3273 PyObject* tmp = NULL;
3274
3275 int lineno;
3276 int col_offset;
3277
3278 if (obj == Py_None) {
3279 *out = NULL;
3280 return 0;
3281 }
3282 if (PyObject_HasAttrString(obj, "lineno")) {
3283 int res;
3284 tmp = PyObject_GetAttrString(obj, "lineno");
3285 if (tmp == NULL) goto failed;
3286 res = obj2ast_int(tmp, &lineno, arena);
3287 if (res != 0) goto failed;
3288 Py_XDECREF(tmp);
3289 tmp = NULL;
3290 } else {
3291 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3292 return 1;
3293 }
3294 if (PyObject_HasAttrString(obj, "col_offset")) {
3295 int res;
3296 tmp = PyObject_GetAttrString(obj, "col_offset");
3297 if (tmp == NULL) goto failed;
3298 res = obj2ast_int(tmp, &col_offset, arena);
3299 if (res != 0) goto failed;
3300 Py_XDECREF(tmp);
3301 tmp = NULL;
3302 } else {
3303 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3304 return 1;
3305 }
3306 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3307 identifier name;
3308 arguments_ty args;
3309 asdl_seq* body;
3310 asdl_seq* decorator_list;
3311
3312 if (PyObject_HasAttrString(obj, "name")) {
3313 int res;
3314 tmp = PyObject_GetAttrString(obj, "name");
3315 if (tmp == NULL) goto failed;
3316 res = obj2ast_identifier(tmp, &name, arena);
3317 if (res != 0) goto failed;
3318 Py_XDECREF(tmp);
3319 tmp = NULL;
3320 } else {
3321 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3322 return 1;
3323 }
3324 if (PyObject_HasAttrString(obj, "args")) {
3325 int res;
3326 tmp = PyObject_GetAttrString(obj, "args");
3327 if (tmp == NULL) goto failed;
3328 res = obj2ast_arguments(tmp, &args, arena);
3329 if (res != 0) goto failed;
3330 Py_XDECREF(tmp);
3331 tmp = NULL;
3332 } else {
3333 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3334 return 1;
3335 }
3336 if (PyObject_HasAttrString(obj, "body")) {
3337 int res;
3338 Py_ssize_t len;
3339 Py_ssize_t i;
3340 tmp = PyObject_GetAttrString(obj, "body");
3341 if (tmp == NULL) goto failed;
3342 if (!PyList_Check(tmp)) {
3343 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3344 goto failed;
3345 }
3346 len = PyList_GET_SIZE(tmp);
3347 body = asdl_seq_new(len, arena);
3348 if (body == NULL) goto failed;
3349 for (i = 0; i < len; i++) {
3350 stmt_ty value;
3351 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3352 if (res != 0) goto failed;
3353 asdl_seq_SET(body, i, value);
3354 }
3355 Py_XDECREF(tmp);
3356 tmp = NULL;
3357 } else {
3358 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3359 return 1;
3360 }
3361 if (PyObject_HasAttrString(obj, "decorator_list")) {
3362 int res;
3363 Py_ssize_t len;
3364 Py_ssize_t i;
3365 tmp = PyObject_GetAttrString(obj, "decorator_list");
3366 if (tmp == NULL) goto failed;
3367 if (!PyList_Check(tmp)) {
3368 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3369 goto failed;
3370 }
3371 len = PyList_GET_SIZE(tmp);
3372 decorator_list = asdl_seq_new(len, arena);
3373 if (decorator_list == NULL) goto failed;
3374 for (i = 0; i < len; i++) {
3375 expr_ty value;
3376 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3377 if (res != 0) goto failed;
3378 asdl_seq_SET(decorator_list, i, value);
3379 }
3380 Py_XDECREF(tmp);
3381 tmp = NULL;
3382 } else {
3383 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3384 return 1;
3385 }
3386 *out = FunctionDef(name, args, body, decorator_list, lineno,
3387 col_offset, arena);
3388 if (*out == NULL) goto failed;
3389 return 0;
3390 }
3391 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3392 identifier name;
3393 asdl_seq* bases;
3394 asdl_seq* body;
3395 asdl_seq* decorator_list;
3396
3397 if (PyObject_HasAttrString(obj, "name")) {
3398 int res;
3399 tmp = PyObject_GetAttrString(obj, "name");
3400 if (tmp == NULL) goto failed;
3401 res = obj2ast_identifier(tmp, &name, arena);
3402 if (res != 0) goto failed;
3403 Py_XDECREF(tmp);
3404 tmp = NULL;
3405 } else {
3406 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3407 return 1;
3408 }
3409 if (PyObject_HasAttrString(obj, "bases")) {
3410 int res;
3411 Py_ssize_t len;
3412 Py_ssize_t i;
3413 tmp = PyObject_GetAttrString(obj, "bases");
3414 if (tmp == NULL) goto failed;
3415 if (!PyList_Check(tmp)) {
3416 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3417 goto failed;
3418 }
3419 len = PyList_GET_SIZE(tmp);
3420 bases = asdl_seq_new(len, arena);
3421 if (bases == NULL) goto failed;
3422 for (i = 0; i < len; i++) {
3423 expr_ty value;
3424 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3425 if (res != 0) goto failed;
3426 asdl_seq_SET(bases, i, value);
3427 }
3428 Py_XDECREF(tmp);
3429 tmp = NULL;
3430 } else {
3431 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3432 return 1;
3433 }
3434 if (PyObject_HasAttrString(obj, "body")) {
3435 int res;
3436 Py_ssize_t len;
3437 Py_ssize_t i;
3438 tmp = PyObject_GetAttrString(obj, "body");
3439 if (tmp == NULL) goto failed;
3440 if (!PyList_Check(tmp)) {
3441 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3442 goto failed;
3443 }
3444 len = PyList_GET_SIZE(tmp);
3445 body = asdl_seq_new(len, arena);
3446 if (body == NULL) goto failed;
3447 for (i = 0; i < len; i++) {
3448 stmt_ty value;
3449 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3450 if (res != 0) goto failed;
3451 asdl_seq_SET(body, i, value);
3452 }
3453 Py_XDECREF(tmp);
3454 tmp = NULL;
3455 } else {
3456 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3457 return 1;
3458 }
3459 if (PyObject_HasAttrString(obj, "decorator_list")) {
3460 int res;
3461 Py_ssize_t len;
3462 Py_ssize_t i;
3463 tmp = PyObject_GetAttrString(obj, "decorator_list");
3464 if (tmp == NULL) goto failed;
3465 if (!PyList_Check(tmp)) {
3466 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3467 goto failed;
3468 }
3469 len = PyList_GET_SIZE(tmp);
3470 decorator_list = asdl_seq_new(len, arena);
3471 if (decorator_list == NULL) goto failed;
3472 for (i = 0; i < len; i++) {
3473 expr_ty value;
3474 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3475 if (res != 0) goto failed;
3476 asdl_seq_SET(decorator_list, i, value);
3477 }
3478 Py_XDECREF(tmp);
3479 tmp = NULL;
3480 } else {
3481 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3482 return 1;
3483 }
3484 *out = ClassDef(name, bases, body, decorator_list, lineno,
3485 col_offset, arena);
3486 if (*out == NULL) goto failed;
3487 return 0;
3488 }
3489 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3490 expr_ty value;
3491
3492 if (PyObject_HasAttrString(obj, "value")) {
3493 int res;
3494 tmp = PyObject_GetAttrString(obj, "value");
3495 if (tmp == NULL) goto failed;
3496 res = obj2ast_expr(tmp, &value, arena);
3497 if (res != 0) goto failed;
3498 Py_XDECREF(tmp);
3499 tmp = NULL;
3500 } else {
3501 value = NULL;
3502 }
3503 *out = Return(value, lineno, col_offset, arena);
3504 if (*out == NULL) goto failed;
3505 return 0;
3506 }
3507 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3508 asdl_seq* targets;
3509
3510 if (PyObject_HasAttrString(obj, "targets")) {
3511 int res;
3512 Py_ssize_t len;
3513 Py_ssize_t i;
3514 tmp = PyObject_GetAttrString(obj, "targets");
3515 if (tmp == NULL) goto failed;
3516 if (!PyList_Check(tmp)) {
3517 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3518 goto failed;
3519 }
3520 len = PyList_GET_SIZE(tmp);
3521 targets = asdl_seq_new(len, arena);
3522 if (targets == NULL) goto failed;
3523 for (i = 0; i < len; i++) {
3524 expr_ty value;
3525 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3526 if (res != 0) goto failed;
3527 asdl_seq_SET(targets, i, value);
3528 }
3529 Py_XDECREF(tmp);
3530 tmp = NULL;
3531 } else {
3532 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3533 return 1;
3534 }
3535 *out = Delete(targets, lineno, col_offset, arena);
3536 if (*out == NULL) goto failed;
3537 return 0;
3538 }
3539 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3540 asdl_seq* targets;
3541 expr_ty value;
3542
3543 if (PyObject_HasAttrString(obj, "targets")) {
3544 int res;
3545 Py_ssize_t len;
3546 Py_ssize_t i;
3547 tmp = PyObject_GetAttrString(obj, "targets");
3548 if (tmp == NULL) goto failed;
3549 if (!PyList_Check(tmp)) {
3550 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3551 goto failed;
3552 }
3553 len = PyList_GET_SIZE(tmp);
3554 targets = asdl_seq_new(len, arena);
3555 if (targets == NULL) goto failed;
3556 for (i = 0; i < len; i++) {
3557 expr_ty value;
3558 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3559 if (res != 0) goto failed;
3560 asdl_seq_SET(targets, i, value);
3561 }
3562 Py_XDECREF(tmp);
3563 tmp = NULL;
3564 } else {
3565 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3566 return 1;
3567 }
3568 if (PyObject_HasAttrString(obj, "value")) {
3569 int res;
3570 tmp = PyObject_GetAttrString(obj, "value");
3571 if (tmp == NULL) goto failed;
3572 res = obj2ast_expr(tmp, &value, arena);
3573 if (res != 0) goto failed;
3574 Py_XDECREF(tmp);
3575 tmp = NULL;
3576 } else {
3577 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3578 return 1;
3579 }
3580 *out = Assign(targets, value, lineno, col_offset, arena);
3581 if (*out == NULL) goto failed;
3582 return 0;
3583 }
3584 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3585 expr_ty target;
3586 operator_ty op;
3587 expr_ty value;
3588
3589 if (PyObject_HasAttrString(obj, "target")) {
3590 int res;
3591 tmp = PyObject_GetAttrString(obj, "target");
3592 if (tmp == NULL) goto failed;
3593 res = obj2ast_expr(tmp, &target, arena);
3594 if (res != 0) goto failed;
3595 Py_XDECREF(tmp);
3596 tmp = NULL;
3597 } else {
3598 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3599 return 1;
3600 }
3601 if (PyObject_HasAttrString(obj, "op")) {
3602 int res;
3603 tmp = PyObject_GetAttrString(obj, "op");
3604 if (tmp == NULL) goto failed;
3605 res = obj2ast_operator(tmp, &op, arena);
3606 if (res != 0) goto failed;
3607 Py_XDECREF(tmp);
3608 tmp = NULL;
3609 } else {
3610 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3611 return 1;
3612 }
3613 if (PyObject_HasAttrString(obj, "value")) {
3614 int res;
3615 tmp = PyObject_GetAttrString(obj, "value");
3616 if (tmp == NULL) goto failed;
3617 res = obj2ast_expr(tmp, &value, arena);
3618 if (res != 0) goto failed;
3619 Py_XDECREF(tmp);
3620 tmp = NULL;
3621 } else {
3622 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3623 return 1;
3624 }
3625 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3626 if (*out == NULL) goto failed;
3627 return 0;
3628 }
3629 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3630 expr_ty dest;
3631 asdl_seq* values;
3632 bool nl;
3633
3634 if (PyObject_HasAttrString(obj, "dest")) {
3635 int res;
3636 tmp = PyObject_GetAttrString(obj, "dest");
3637 if (tmp == NULL) goto failed;
3638 res = obj2ast_expr(tmp, &dest, arena);
3639 if (res != 0) goto failed;
3640 Py_XDECREF(tmp);
3641 tmp = NULL;
3642 } else {
3643 dest = NULL;
3644 }
3645 if (PyObject_HasAttrString(obj, "values")) {
3646 int res;
3647 Py_ssize_t len;
3648 Py_ssize_t i;
3649 tmp = PyObject_GetAttrString(obj, "values");
3650 if (tmp == NULL) goto failed;
3651 if (!PyList_Check(tmp)) {
3652 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3653 goto failed;
3654 }
3655 len = PyList_GET_SIZE(tmp);
3656 values = asdl_seq_new(len, arena);
3657 if (values == NULL) goto failed;
3658 for (i = 0; i < len; i++) {
3659 expr_ty value;
3660 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3661 if (res != 0) goto failed;
3662 asdl_seq_SET(values, i, value);
3663 }
3664 Py_XDECREF(tmp);
3665 tmp = NULL;
3666 } else {
3667 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3668 return 1;
3669 }
3670 if (PyObject_HasAttrString(obj, "nl")) {
3671 int res;
3672 tmp = PyObject_GetAttrString(obj, "nl");
3673 if (tmp == NULL) goto failed;
3674 res = obj2ast_bool(tmp, &nl, arena);
3675 if (res != 0) goto failed;
3676 Py_XDECREF(tmp);
3677 tmp = NULL;
3678 } else {
3679 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3680 return 1;
3681 }
3682 *out = Print(dest, values, nl, lineno, col_offset, arena);
3683 if (*out == NULL) goto failed;
3684 return 0;
3685 }
3686 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3687 expr_ty target;
3688 expr_ty iter;
3689 asdl_seq* body;
3690 asdl_seq* orelse;
3691
3692 if (PyObject_HasAttrString(obj, "target")) {
3693 int res;
3694 tmp = PyObject_GetAttrString(obj, "target");
3695 if (tmp == NULL) goto failed;
3696 res = obj2ast_expr(tmp, &target, arena);
3697 if (res != 0) goto failed;
3698 Py_XDECREF(tmp);
3699 tmp = NULL;
3700 } else {
3701 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3702 return 1;
3703 }
3704 if (PyObject_HasAttrString(obj, "iter")) {
3705 int res;
3706 tmp = PyObject_GetAttrString(obj, "iter");
3707 if (tmp == NULL) goto failed;
3708 res = obj2ast_expr(tmp, &iter, arena);
3709 if (res != 0) goto failed;
3710 Py_XDECREF(tmp);
3711 tmp = NULL;
3712 } else {
3713 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3714 return 1;
3715 }
3716 if (PyObject_HasAttrString(obj, "body")) {
3717 int res;
3718 Py_ssize_t len;
3719 Py_ssize_t i;
3720 tmp = PyObject_GetAttrString(obj, "body");
3721 if (tmp == NULL) goto failed;
3722 if (!PyList_Check(tmp)) {
3723 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3724 goto failed;
3725 }
3726 len = PyList_GET_SIZE(tmp);
3727 body = asdl_seq_new(len, arena);
3728 if (body == NULL) goto failed;
3729 for (i = 0; i < len; i++) {
3730 stmt_ty value;
3731 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3732 if (res != 0) goto failed;
3733 asdl_seq_SET(body, i, value);
3734 }
3735 Py_XDECREF(tmp);
3736 tmp = NULL;
3737 } else {
3738 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3739 return 1;
3740 }
3741 if (PyObject_HasAttrString(obj, "orelse")) {
3742 int res;
3743 Py_ssize_t len;
3744 Py_ssize_t i;
3745 tmp = PyObject_GetAttrString(obj, "orelse");
3746 if (tmp == NULL) goto failed;
3747 if (!PyList_Check(tmp)) {
3748 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3749 goto failed;
3750 }
3751 len = PyList_GET_SIZE(tmp);
3752 orelse = asdl_seq_new(len, arena);
3753 if (orelse == NULL) goto failed;
3754 for (i = 0; i < len; i++) {
3755 stmt_ty value;
3756 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3757 if (res != 0) goto failed;
3758 asdl_seq_SET(orelse, i, value);
3759 }
3760 Py_XDECREF(tmp);
3761 tmp = NULL;
3762 } else {
3763 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3764 return 1;
3765 }
3766 *out = For(target, iter, body, orelse, lineno, col_offset,
3767 arena);
3768 if (*out == NULL) goto failed;
3769 return 0;
3770 }
3771 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3772 expr_ty test;
3773 asdl_seq* body;
3774 asdl_seq* orelse;
3775
3776 if (PyObject_HasAttrString(obj, "test")) {
3777 int res;
3778 tmp = PyObject_GetAttrString(obj, "test");
3779 if (tmp == NULL) goto failed;
3780 res = obj2ast_expr(tmp, &test, arena);
3781 if (res != 0) goto failed;
3782 Py_XDECREF(tmp);
3783 tmp = NULL;
3784 } else {
3785 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3786 return 1;
3787 }
3788 if (PyObject_HasAttrString(obj, "body")) {
3789 int res;
3790 Py_ssize_t len;
3791 Py_ssize_t i;
3792 tmp = PyObject_GetAttrString(obj, "body");
3793 if (tmp == NULL) goto failed;
3794 if (!PyList_Check(tmp)) {
3795 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3796 goto failed;
3797 }
3798 len = PyList_GET_SIZE(tmp);
3799 body = asdl_seq_new(len, arena);
3800 if (body == NULL) goto failed;
3801 for (i = 0; i < len; i++) {
3802 stmt_ty value;
3803 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3804 if (res != 0) goto failed;
3805 asdl_seq_SET(body, i, value);
3806 }
3807 Py_XDECREF(tmp);
3808 tmp = NULL;
3809 } else {
3810 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3811 return 1;
3812 }
3813 if (PyObject_HasAttrString(obj, "orelse")) {
3814 int res;
3815 Py_ssize_t len;
3816 Py_ssize_t i;
3817 tmp = PyObject_GetAttrString(obj, "orelse");
3818 if (tmp == NULL) goto failed;
3819 if (!PyList_Check(tmp)) {
3820 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3821 goto failed;
3822 }
3823 len = PyList_GET_SIZE(tmp);
3824 orelse = asdl_seq_new(len, arena);
3825 if (orelse == NULL) goto failed;
3826 for (i = 0; i < len; i++) {
3827 stmt_ty value;
3828 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3829 if (res != 0) goto failed;
3830 asdl_seq_SET(orelse, i, value);
3831 }
3832 Py_XDECREF(tmp);
3833 tmp = NULL;
3834 } else {
3835 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3836 return 1;
3837 }
3838 *out = While(test, body, orelse, lineno, col_offset, arena);
3839 if (*out == NULL) goto failed;
3840 return 0;
3841 }
3842 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3843 expr_ty test;
3844 asdl_seq* body;
3845 asdl_seq* orelse;
3846
3847 if (PyObject_HasAttrString(obj, "test")) {
3848 int res;
3849 tmp = PyObject_GetAttrString(obj, "test");
3850 if (tmp == NULL) goto failed;
3851 res = obj2ast_expr(tmp, &test, arena);
3852 if (res != 0) goto failed;
3853 Py_XDECREF(tmp);
3854 tmp = NULL;
3855 } else {
3856 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3857 return 1;
3858 }
3859 if (PyObject_HasAttrString(obj, "body")) {
3860 int res;
3861 Py_ssize_t len;
3862 Py_ssize_t i;
3863 tmp = PyObject_GetAttrString(obj, "body");
3864 if (tmp == NULL) goto failed;
3865 if (!PyList_Check(tmp)) {
3866 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3867 goto failed;
3868 }
3869 len = PyList_GET_SIZE(tmp);
3870 body = asdl_seq_new(len, arena);
3871 if (body == NULL) goto failed;
3872 for (i = 0; i < len; i++) {
3873 stmt_ty value;
3874 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3875 if (res != 0) goto failed;
3876 asdl_seq_SET(body, i, value);
3877 }
3878 Py_XDECREF(tmp);
3879 tmp = NULL;
3880 } else {
3881 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3882 return 1;
3883 }
3884 if (PyObject_HasAttrString(obj, "orelse")) {
3885 int res;
3886 Py_ssize_t len;
3887 Py_ssize_t i;
3888 tmp = PyObject_GetAttrString(obj, "orelse");
3889 if (tmp == NULL) goto failed;
3890 if (!PyList_Check(tmp)) {
3891 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3892 goto failed;
3893 }
3894 len = PyList_GET_SIZE(tmp);
3895 orelse = asdl_seq_new(len, arena);
3896 if (orelse == NULL) goto failed;
3897 for (i = 0; i < len; i++) {
3898 stmt_ty value;
3899 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3900 if (res != 0) goto failed;
3901 asdl_seq_SET(orelse, i, value);
3902 }
3903 Py_XDECREF(tmp);
3904 tmp = NULL;
3905 } else {
3906 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3907 return 1;
3908 }
3909 *out = If(test, body, orelse, lineno, col_offset, arena);
3910 if (*out == NULL) goto failed;
3911 return 0;
3912 }
3913 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3914 expr_ty context_expr;
3915 expr_ty optional_vars;
3916 asdl_seq* body;
3917
3918 if (PyObject_HasAttrString(obj, "context_expr")) {
3919 int res;
3920 tmp = PyObject_GetAttrString(obj, "context_expr");
3921 if (tmp == NULL) goto failed;
3922 res = obj2ast_expr(tmp, &context_expr, arena);
3923 if (res != 0) goto failed;
3924 Py_XDECREF(tmp);
3925 tmp = NULL;
3926 } else {
3927 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3928 return 1;
3929 }
3930 if (PyObject_HasAttrString(obj, "optional_vars")) {
3931 int res;
3932 tmp = PyObject_GetAttrString(obj, "optional_vars");
3933 if (tmp == NULL) goto failed;
3934 res = obj2ast_expr(tmp, &optional_vars, arena);
3935 if (res != 0) goto failed;
3936 Py_XDECREF(tmp);
3937 tmp = NULL;
3938 } else {
3939 optional_vars = NULL;
3940 }
3941 if (PyObject_HasAttrString(obj, "body")) {
3942 int res;
3943 Py_ssize_t len;
3944 Py_ssize_t i;
3945 tmp = PyObject_GetAttrString(obj, "body");
3946 if (tmp == NULL) goto failed;
3947 if (!PyList_Check(tmp)) {
3948 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3949 goto failed;
3950 }
3951 len = PyList_GET_SIZE(tmp);
3952 body = asdl_seq_new(len, arena);
3953 if (body == NULL) goto failed;
3954 for (i = 0; i < len; i++) {
3955 stmt_ty value;
3956 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3957 if (res != 0) goto failed;
3958 asdl_seq_SET(body, i, value);
3959 }
3960 Py_XDECREF(tmp);
3961 tmp = NULL;
3962 } else {
3963 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3964 return 1;
3965 }
3966 *out = With(context_expr, optional_vars, body, lineno,
3967 col_offset, arena);
3968 if (*out == NULL) goto failed;
3969 return 0;
3970 }
3971 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3972 expr_ty type;
3973 expr_ty inst;
3974 expr_ty tback;
3975
3976 if (PyObject_HasAttrString(obj, "type")) {
3977 int res;
3978 tmp = PyObject_GetAttrString(obj, "type");
3979 if (tmp == NULL) goto failed;
3980 res = obj2ast_expr(tmp, &type, arena);
3981 if (res != 0) goto failed;
3982 Py_XDECREF(tmp);
3983 tmp = NULL;
3984 } else {
3985 type = NULL;
3986 }
3987 if (PyObject_HasAttrString(obj, "inst")) {
3988 int res;
3989 tmp = PyObject_GetAttrString(obj, "inst");
3990 if (tmp == NULL) goto failed;
3991 res = obj2ast_expr(tmp, &inst, arena);
3992 if (res != 0) goto failed;
3993 Py_XDECREF(tmp);
3994 tmp = NULL;
3995 } else {
3996 inst = NULL;
3997 }
3998 if (PyObject_HasAttrString(obj, "tback")) {
3999 int res;
4000 tmp = PyObject_GetAttrString(obj, "tback");
4001 if (tmp == NULL) goto failed;
4002 res = obj2ast_expr(tmp, &tback, arena);
4003 if (res != 0) goto failed;
4004 Py_XDECREF(tmp);
4005 tmp = NULL;
4006 } else {
4007 tback = NULL;
4008 }
4009 *out = Raise(type, inst, tback, lineno, col_offset, arena);
4010 if (*out == NULL) goto failed;
4011 return 0;
4012 }
4013 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
4014 asdl_seq* body;
4015 asdl_seq* handlers;
4016 asdl_seq* orelse;
4017
4018 if (PyObject_HasAttrString(obj, "body")) {
4019 int res;
4020 Py_ssize_t len;
4021 Py_ssize_t i;
4022 tmp = PyObject_GetAttrString(obj, "body");
4023 if (tmp == NULL) goto failed;
4024 if (!PyList_Check(tmp)) {
4025 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4026 goto failed;
4027 }
4028 len = PyList_GET_SIZE(tmp);
4029 body = asdl_seq_new(len, arena);
4030 if (body == NULL) goto failed;
4031 for (i = 0; i < len; i++) {
4032 stmt_ty value;
4033 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4034 if (res != 0) goto failed;
4035 asdl_seq_SET(body, i, value);
4036 }
4037 Py_XDECREF(tmp);
4038 tmp = NULL;
4039 } else {
4040 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
4041 return 1;
4042 }
4043 if (PyObject_HasAttrString(obj, "handlers")) {
4044 int res;
4045 Py_ssize_t len;
4046 Py_ssize_t i;
4047 tmp = PyObject_GetAttrString(obj, "handlers");
4048 if (tmp == NULL) goto failed;
4049 if (!PyList_Check(tmp)) {
4050 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4051 goto failed;
4052 }
4053 len = PyList_GET_SIZE(tmp);
4054 handlers = asdl_seq_new(len, arena);
4055 if (handlers == NULL) goto failed;
4056 for (i = 0; i < len; i++) {
4057 excepthandler_ty value;
4058 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
4059 if (res != 0) goto failed;
4060 asdl_seq_SET(handlers, i, value);
4061 }
4062 Py_XDECREF(tmp);
4063 tmp = NULL;
4064 } else {
4065 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
4066 return 1;
4067 }
4068 if (PyObject_HasAttrString(obj, "orelse")) {
4069 int res;
4070 Py_ssize_t len;
4071 Py_ssize_t i;
4072 tmp = PyObject_GetAttrString(obj, "orelse");
4073 if (tmp == NULL) goto failed;
4074 if (!PyList_Check(tmp)) {
4075 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4076 goto failed;
4077 }
4078 len = PyList_GET_SIZE(tmp);
4079 orelse = asdl_seq_new(len, arena);
4080 if (orelse == NULL) goto failed;
4081 for (i = 0; i < len; i++) {
4082 stmt_ty value;
4083 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4084 if (res != 0) goto failed;
4085 asdl_seq_SET(orelse, i, value);
4086 }
4087 Py_XDECREF(tmp);
4088 tmp = NULL;
4089 } else {
4090 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
4091 return 1;
4092 }
4093 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
4094 arena);
4095 if (*out == NULL) goto failed;
4096 return 0;
4097 }
4098 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
4099 asdl_seq* body;
4100 asdl_seq* finalbody;
4101
4102 if (PyObject_HasAttrString(obj, "body")) {
4103 int res;
4104 Py_ssize_t len;
4105 Py_ssize_t i;
4106 tmp = PyObject_GetAttrString(obj, "body");
4107 if (tmp == NULL) goto failed;
4108 if (!PyList_Check(tmp)) {
4109 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4110 goto failed;
4111 }
4112 len = PyList_GET_SIZE(tmp);
4113 body = asdl_seq_new(len, arena);
4114 if (body == NULL) goto failed;
4115 for (i = 0; i < len; i++) {
4116 stmt_ty value;
4117 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4118 if (res != 0) goto failed;
4119 asdl_seq_SET(body, i, value);
4120 }
4121 Py_XDECREF(tmp);
4122 tmp = NULL;
4123 } else {
4124 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4125 return 1;
4126 }
4127 if (PyObject_HasAttrString(obj, "finalbody")) {
4128 int res;
4129 Py_ssize_t len;
4130 Py_ssize_t i;
4131 tmp = PyObject_GetAttrString(obj, "finalbody");
4132 if (tmp == NULL) goto failed;
4133 if (!PyList_Check(tmp)) {
4134 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4135 goto failed;
4136 }
4137 len = PyList_GET_SIZE(tmp);
4138 finalbody = asdl_seq_new(len, arena);
4139 if (finalbody == NULL) goto failed;
4140 for (i = 0; i < len; i++) {
4141 stmt_ty value;
4142 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4143 if (res != 0) goto failed;
4144 asdl_seq_SET(finalbody, i, value);
4145 }
4146 Py_XDECREF(tmp);
4147 tmp = NULL;
4148 } else {
4149 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4150 return 1;
4151 }
4152 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4153 if (*out == NULL) goto failed;
4154 return 0;
4155 }
4156 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4157 expr_ty test;
4158 expr_ty msg;
4159
4160 if (PyObject_HasAttrString(obj, "test")) {
4161 int res;
4162 tmp = PyObject_GetAttrString(obj, "test");
4163 if (tmp == NULL) goto failed;
4164 res = obj2ast_expr(tmp, &test, arena);
4165 if (res != 0) goto failed;
4166 Py_XDECREF(tmp);
4167 tmp = NULL;
4168 } else {
4169 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4170 return 1;
4171 }
4172 if (PyObject_HasAttrString(obj, "msg")) {
4173 int res;
4174 tmp = PyObject_GetAttrString(obj, "msg");
4175 if (tmp == NULL) goto failed;
4176 res = obj2ast_expr(tmp, &msg, arena);
4177 if (res != 0) goto failed;
4178 Py_XDECREF(tmp);
4179 tmp = NULL;
4180 } else {
4181 msg = NULL;
4182 }
4183 *out = Assert(test, msg, lineno, col_offset, arena);
4184 if (*out == NULL) goto failed;
4185 return 0;
4186 }
4187 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4188 asdl_seq* names;
4189
4190 if (PyObject_HasAttrString(obj, "names")) {
4191 int res;
4192 Py_ssize_t len;
4193 Py_ssize_t i;
4194 tmp = PyObject_GetAttrString(obj, "names");
4195 if (tmp == NULL) goto failed;
4196 if (!PyList_Check(tmp)) {
4197 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4198 goto failed;
4199 }
4200 len = PyList_GET_SIZE(tmp);
4201 names = asdl_seq_new(len, arena);
4202 if (names == NULL) goto failed;
4203 for (i = 0; i < len; i++) {
4204 alias_ty value;
4205 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4206 if (res != 0) goto failed;
4207 asdl_seq_SET(names, i, value);
4208 }
4209 Py_XDECREF(tmp);
4210 tmp = NULL;
4211 } else {
4212 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4213 return 1;
4214 }
4215 *out = Import(names, lineno, col_offset, arena);
4216 if (*out == NULL) goto failed;
4217 return 0;
4218 }
4219 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4220 identifier module;
4221 asdl_seq* names;
4222 int level;
4223
4224 if (PyObject_HasAttrString(obj, "module")) {
4225 int res;
4226 tmp = PyObject_GetAttrString(obj, "module");
4227 if (tmp == NULL) goto failed;
4228 res = obj2ast_identifier(tmp, &module, arena);
4229 if (res != 0) goto failed;
4230 Py_XDECREF(tmp);
4231 tmp = NULL;
4232 } else {
4233 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4234 return 1;
4235 }
4236 if (PyObject_HasAttrString(obj, "names")) {
4237 int res;
4238 Py_ssize_t len;
4239 Py_ssize_t i;
4240 tmp = PyObject_GetAttrString(obj, "names");
4241 if (tmp == NULL) goto failed;
4242 if (!PyList_Check(tmp)) {
4243 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4244 goto failed;
4245 }
4246 len = PyList_GET_SIZE(tmp);
4247 names = asdl_seq_new(len, arena);
4248 if (names == NULL) goto failed;
4249 for (i = 0; i < len; i++) {
4250 alias_ty value;
4251 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4252 if (res != 0) goto failed;
4253 asdl_seq_SET(names, i, value);
4254 }
4255 Py_XDECREF(tmp);
4256 tmp = NULL;
4257 } else {
4258 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4259 return 1;
4260 }
4261 if (PyObject_HasAttrString(obj, "level")) {
4262 int res;
4263 tmp = PyObject_GetAttrString(obj, "level");
4264 if (tmp == NULL) goto failed;
4265 res = obj2ast_int(tmp, &level, arena);
4266 if (res != 0) goto failed;
4267 Py_XDECREF(tmp);
4268 tmp = NULL;
4269 } else {
4270 level = 0;
4271 }
4272 *out = ImportFrom(module, names, level, lineno, col_offset,
4273 arena);
4274 if (*out == NULL) goto failed;
4275 return 0;
4276 }
4277 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4278 expr_ty body;
4279 expr_ty globals;
4280 expr_ty locals;
4281
4282 if (PyObject_HasAttrString(obj, "body")) {
4283 int res;
4284 tmp = PyObject_GetAttrString(obj, "body");
4285 if (tmp == NULL) goto failed;
4286 res = obj2ast_expr(tmp, &body, arena);
4287 if (res != 0) goto failed;
4288 Py_XDECREF(tmp);
4289 tmp = NULL;
4290 } else {
4291 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4292 return 1;
4293 }
4294 if (PyObject_HasAttrString(obj, "globals")) {
4295 int res;
4296 tmp = PyObject_GetAttrString(obj, "globals");
4297 if (tmp == NULL) goto failed;
4298 res = obj2ast_expr(tmp, &globals, arena);
4299 if (res != 0) goto failed;
4300 Py_XDECREF(tmp);
4301 tmp = NULL;
4302 } else {
4303 globals = NULL;
4304 }
4305 if (PyObject_HasAttrString(obj, "locals")) {
4306 int res;
4307 tmp = PyObject_GetAttrString(obj, "locals");
4308 if (tmp == NULL) goto failed;
4309 res = obj2ast_expr(tmp, &locals, arena);
4310 if (res != 0) goto failed;
4311 Py_XDECREF(tmp);
4312 tmp = NULL;
4313 } else {
4314 locals = NULL;
4315 }
4316 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4317 if (*out == NULL) goto failed;
4318 return 0;
4319 }
4320 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4321 asdl_seq* names;
4322
4323 if (PyObject_HasAttrString(obj, "names")) {
4324 int res;
4325 Py_ssize_t len;
4326 Py_ssize_t i;
4327 tmp = PyObject_GetAttrString(obj, "names");
4328 if (tmp == NULL) goto failed;
4329 if (!PyList_Check(tmp)) {
4330 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4331 goto failed;
4332 }
4333 len = PyList_GET_SIZE(tmp);
4334 names = asdl_seq_new(len, arena);
4335 if (names == NULL) goto failed;
4336 for (i = 0; i < len; i++) {
4337 identifier value;
4338 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4339 if (res != 0) goto failed;
4340 asdl_seq_SET(names, i, value);
4341 }
4342 Py_XDECREF(tmp);
4343 tmp = NULL;
4344 } else {
4345 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4346 return 1;
4347 }
4348 *out = Global(names, lineno, col_offset, arena);
4349 if (*out == NULL) goto failed;
4350 return 0;
4351 }
4352 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4353 expr_ty value;
4354
4355 if (PyObject_HasAttrString(obj, "value")) {
4356 int res;
4357 tmp = PyObject_GetAttrString(obj, "value");
4358 if (tmp == NULL) goto failed;
4359 res = obj2ast_expr(tmp, &value, arena);
4360 if (res != 0) goto failed;
4361 Py_XDECREF(tmp);
4362 tmp = NULL;
4363 } else {
4364 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4365 return 1;
4366 }
4367 *out = Expr(value, lineno, col_offset, arena);
4368 if (*out == NULL) goto failed;
4369 return 0;
4370 }
4371 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4372
4373 *out = Pass(lineno, col_offset, arena);
4374 if (*out == NULL) goto failed;
4375 return 0;
4376 }
4377 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4378
4379 *out = Break(lineno, col_offset, arena);
4380 if (*out == NULL) goto failed;
4381 return 0;
4382 }
4383 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4384
4385 *out = Continue(lineno, col_offset, arena);
4386 if (*out == NULL) goto failed;
4387 return 0;
4388 }
4389
4390 tmp = PyObject_Repr(obj);
4391 if (tmp == NULL) goto failed;
4392 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4393failed:
4394 Py_XDECREF(tmp);
4395 return 1;
4396}
4397
4398int
4399obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4400{
4401 PyObject* tmp = NULL;
4402
4403 int lineno;
4404 int col_offset;
4405
4406 if (obj == Py_None) {
4407 *out = NULL;
4408 return 0;
4409 }
4410 if (PyObject_HasAttrString(obj, "lineno")) {
4411 int res;
4412 tmp = PyObject_GetAttrString(obj, "lineno");
4413 if (tmp == NULL) goto failed;
4414 res = obj2ast_int(tmp, &lineno, arena);
4415 if (res != 0) goto failed;
4416 Py_XDECREF(tmp);
4417 tmp = NULL;
4418 } else {
4419 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4420 return 1;
4421 }
4422 if (PyObject_HasAttrString(obj, "col_offset")) {
4423 int res;
4424 tmp = PyObject_GetAttrString(obj, "col_offset");
4425 if (tmp == NULL) goto failed;
4426 res = obj2ast_int(tmp, &col_offset, arena);
4427 if (res != 0) goto failed;
4428 Py_XDECREF(tmp);
4429 tmp = NULL;
4430 } else {
4431 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4432 return 1;
4433 }
4434 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4435 boolop_ty op;
4436 asdl_seq* values;
4437
4438 if (PyObject_HasAttrString(obj, "op")) {
4439 int res;
4440 tmp = PyObject_GetAttrString(obj, "op");
4441 if (tmp == NULL) goto failed;
4442 res = obj2ast_boolop(tmp, &op, arena);
4443 if (res != 0) goto failed;
4444 Py_XDECREF(tmp);
4445 tmp = NULL;
4446 } else {
4447 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4448 return 1;
4449 }
4450 if (PyObject_HasAttrString(obj, "values")) {
4451 int res;
4452 Py_ssize_t len;
4453 Py_ssize_t i;
4454 tmp = PyObject_GetAttrString(obj, "values");
4455 if (tmp == NULL) goto failed;
4456 if (!PyList_Check(tmp)) {
4457 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4458 goto failed;
4459 }
4460 len = PyList_GET_SIZE(tmp);
4461 values = asdl_seq_new(len, arena);
4462 if (values == NULL) goto failed;
4463 for (i = 0; i < len; i++) {
4464 expr_ty value;
4465 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4466 if (res != 0) goto failed;
4467 asdl_seq_SET(values, i, value);
4468 }
4469 Py_XDECREF(tmp);
4470 tmp = NULL;
4471 } else {
4472 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4473 return 1;
4474 }
4475 *out = BoolOp(op, values, lineno, col_offset, arena);
4476 if (*out == NULL) goto failed;
4477 return 0;
4478 }
4479 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4480 expr_ty left;
4481 operator_ty op;
4482 expr_ty right;
4483
4484 if (PyObject_HasAttrString(obj, "left")) {
4485 int res;
4486 tmp = PyObject_GetAttrString(obj, "left");
4487 if (tmp == NULL) goto failed;
4488 res = obj2ast_expr(tmp, &left, arena);
4489 if (res != 0) goto failed;
4490 Py_XDECREF(tmp);
4491 tmp = NULL;
4492 } else {
4493 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4494 return 1;
4495 }
4496 if (PyObject_HasAttrString(obj, "op")) {
4497 int res;
4498 tmp = PyObject_GetAttrString(obj, "op");
4499 if (tmp == NULL) goto failed;
4500 res = obj2ast_operator(tmp, &op, arena);
4501 if (res != 0) goto failed;
4502 Py_XDECREF(tmp);
4503 tmp = NULL;
4504 } else {
4505 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4506 return 1;
4507 }
4508 if (PyObject_HasAttrString(obj, "right")) {
4509 int res;
4510 tmp = PyObject_GetAttrString(obj, "right");
4511 if (tmp == NULL) goto failed;
4512 res = obj2ast_expr(tmp, &right, arena);
4513 if (res != 0) goto failed;
4514 Py_XDECREF(tmp);
4515 tmp = NULL;
4516 } else {
4517 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4518 return 1;
4519 }
4520 *out = BinOp(left, op, right, lineno, col_offset, arena);
4521 if (*out == NULL) goto failed;
4522 return 0;
4523 }
4524 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4525 unaryop_ty op;
4526 expr_ty operand;
4527
4528 if (PyObject_HasAttrString(obj, "op")) {
4529 int res;
4530 tmp = PyObject_GetAttrString(obj, "op");
4531 if (tmp == NULL) goto failed;
4532 res = obj2ast_unaryop(tmp, &op, arena);
4533 if (res != 0) goto failed;
4534 Py_XDECREF(tmp);
4535 tmp = NULL;
4536 } else {
4537 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4538 return 1;
4539 }
4540 if (PyObject_HasAttrString(obj, "operand")) {
4541 int res;
4542 tmp = PyObject_GetAttrString(obj, "operand");
4543 if (tmp == NULL) goto failed;
4544 res = obj2ast_expr(tmp, &operand, arena);
4545 if (res != 0) goto failed;
4546 Py_XDECREF(tmp);
4547 tmp = NULL;
4548 } else {
4549 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4550 return 1;
4551 }
4552 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4553 if (*out == NULL) goto failed;
4554 return 0;
4555 }
4556 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4557 arguments_ty args;
4558 expr_ty body;
4559
4560 if (PyObject_HasAttrString(obj, "args")) {
4561 int res;
4562 tmp = PyObject_GetAttrString(obj, "args");
4563 if (tmp == NULL) goto failed;
4564 res = obj2ast_arguments(tmp, &args, arena);
4565 if (res != 0) goto failed;
4566 Py_XDECREF(tmp);
4567 tmp = NULL;
4568 } else {
4569 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4570 return 1;
4571 }
4572 if (PyObject_HasAttrString(obj, "body")) {
4573 int res;
4574 tmp = PyObject_GetAttrString(obj, "body");
4575 if (tmp == NULL) goto failed;
4576 res = obj2ast_expr(tmp, &body, arena);
4577 if (res != 0) goto failed;
4578 Py_XDECREF(tmp);
4579 tmp = NULL;
4580 } else {
4581 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4582 return 1;
4583 }
4584 *out = Lambda(args, body, lineno, col_offset, arena);
4585 if (*out == NULL) goto failed;
4586 return 0;
4587 }
4588 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4589 expr_ty test;
4590 expr_ty body;
4591 expr_ty orelse;
4592
4593 if (PyObject_HasAttrString(obj, "test")) {
4594 int res;
4595 tmp = PyObject_GetAttrString(obj, "test");
4596 if (tmp == NULL) goto failed;
4597 res = obj2ast_expr(tmp, &test, arena);
4598 if (res != 0) goto failed;
4599 Py_XDECREF(tmp);
4600 tmp = NULL;
4601 } else {
4602 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4603 return 1;
4604 }
4605 if (PyObject_HasAttrString(obj, "body")) {
4606 int res;
4607 tmp = PyObject_GetAttrString(obj, "body");
4608 if (tmp == NULL) goto failed;
4609 res = obj2ast_expr(tmp, &body, arena);
4610 if (res != 0) goto failed;
4611 Py_XDECREF(tmp);
4612 tmp = NULL;
4613 } else {
4614 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4615 return 1;
4616 }
4617 if (PyObject_HasAttrString(obj, "orelse")) {
4618 int res;
4619 tmp = PyObject_GetAttrString(obj, "orelse");
4620 if (tmp == NULL) goto failed;
4621 res = obj2ast_expr(tmp, &orelse, arena);
4622 if (res != 0) goto failed;
4623 Py_XDECREF(tmp);
4624 tmp = NULL;
4625 } else {
4626 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4627 return 1;
4628 }
4629 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4630 if (*out == NULL) goto failed;
4631 return 0;
4632 }
4633 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4634 asdl_seq* keys;
4635 asdl_seq* values;
4636
4637 if (PyObject_HasAttrString(obj, "keys")) {
4638 int res;
4639 Py_ssize_t len;
4640 Py_ssize_t i;
4641 tmp = PyObject_GetAttrString(obj, "keys");
4642 if (tmp == NULL) goto failed;
4643 if (!PyList_Check(tmp)) {
4644 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4645 goto failed;
4646 }
4647 len = PyList_GET_SIZE(tmp);
4648 keys = asdl_seq_new(len, arena);
4649 if (keys == NULL) goto failed;
4650 for (i = 0; i < len; i++) {
4651 expr_ty value;
4652 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4653 if (res != 0) goto failed;
4654 asdl_seq_SET(keys, i, value);
4655 }
4656 Py_XDECREF(tmp);
4657 tmp = NULL;
4658 } else {
4659 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4660 return 1;
4661 }
4662 if (PyObject_HasAttrString(obj, "values")) {
4663 int res;
4664 Py_ssize_t len;
4665 Py_ssize_t i;
4666 tmp = PyObject_GetAttrString(obj, "values");
4667 if (tmp == NULL) goto failed;
4668 if (!PyList_Check(tmp)) {
4669 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4670 goto failed;
4671 }
4672 len = PyList_GET_SIZE(tmp);
4673 values = asdl_seq_new(len, arena);
4674 if (values == NULL) goto failed;
4675 for (i = 0; i < len; i++) {
4676 expr_ty value;
4677 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4678 if (res != 0) goto failed;
4679 asdl_seq_SET(values, i, value);
4680 }
4681 Py_XDECREF(tmp);
4682 tmp = NULL;
4683 } else {
4684 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4685 return 1;
4686 }
4687 *out = Dict(keys, values, lineno, col_offset, arena);
4688 if (*out == NULL) goto failed;
4689 return 0;
4690 }
4691 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4692 expr_ty elt;
4693 asdl_seq* generators;
4694
4695 if (PyObject_HasAttrString(obj, "elt")) {
4696 int res;
4697 tmp = PyObject_GetAttrString(obj, "elt");
4698 if (tmp == NULL) goto failed;
4699 res = obj2ast_expr(tmp, &elt, arena);
4700 if (res != 0) goto failed;
4701 Py_XDECREF(tmp);
4702 tmp = NULL;
4703 } else {
4704 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4705 return 1;
4706 }
4707 if (PyObject_HasAttrString(obj, "generators")) {
4708 int res;
4709 Py_ssize_t len;
4710 Py_ssize_t i;
4711 tmp = PyObject_GetAttrString(obj, "generators");
4712 if (tmp == NULL) goto failed;
4713 if (!PyList_Check(tmp)) {
4714 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4715 goto failed;
4716 }
4717 len = PyList_GET_SIZE(tmp);
4718 generators = asdl_seq_new(len, arena);
4719 if (generators == NULL) goto failed;
4720 for (i = 0; i < len; i++) {
4721 comprehension_ty value;
4722 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4723 if (res != 0) goto failed;
4724 asdl_seq_SET(generators, i, value);
4725 }
4726 Py_XDECREF(tmp);
4727 tmp = NULL;
4728 } else {
4729 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4730 return 1;
4731 }
4732 *out = ListComp(elt, generators, lineno, col_offset, arena);
4733 if (*out == NULL) goto failed;
4734 return 0;
4735 }
4736 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4737 expr_ty elt;
4738 asdl_seq* generators;
4739
4740 if (PyObject_HasAttrString(obj, "elt")) {
4741 int res;
4742 tmp = PyObject_GetAttrString(obj, "elt");
4743 if (tmp == NULL) goto failed;
4744 res = obj2ast_expr(tmp, &elt, arena);
4745 if (res != 0) goto failed;
4746 Py_XDECREF(tmp);
4747 tmp = NULL;
4748 } else {
4749 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4750 return 1;
4751 }
4752 if (PyObject_HasAttrString(obj, "generators")) {
4753 int res;
4754 Py_ssize_t len;
4755 Py_ssize_t i;
4756 tmp = PyObject_GetAttrString(obj, "generators");
4757 if (tmp == NULL) goto failed;
4758 if (!PyList_Check(tmp)) {
4759 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4760 goto failed;
4761 }
4762 len = PyList_GET_SIZE(tmp);
4763 generators = asdl_seq_new(len, arena);
4764 if (generators == NULL) goto failed;
4765 for (i = 0; i < len; i++) {
4766 comprehension_ty value;
4767 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4768 if (res != 0) goto failed;
4769 asdl_seq_SET(generators, i, value);
4770 }
4771 Py_XDECREF(tmp);
4772 tmp = NULL;
4773 } else {
4774 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4775 return 1;
4776 }
4777 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4778 if (*out == NULL) goto failed;
4779 return 0;
4780 }
4781 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4782 expr_ty value;
4783
4784 if (PyObject_HasAttrString(obj, "value")) {
4785 int res;
4786 tmp = PyObject_GetAttrString(obj, "value");
4787 if (tmp == NULL) goto failed;
4788 res = obj2ast_expr(tmp, &value, arena);
4789 if (res != 0) goto failed;
4790 Py_XDECREF(tmp);
4791 tmp = NULL;
4792 } else {
4793 value = NULL;
4794 }
4795 *out = Yield(value, lineno, col_offset, arena);
4796 if (*out == NULL) goto failed;
4797 return 0;
4798 }
4799 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4800 expr_ty left;
4801 asdl_int_seq* ops;
4802 asdl_seq* comparators;
4803
4804 if (PyObject_HasAttrString(obj, "left")) {
4805 int res;
4806 tmp = PyObject_GetAttrString(obj, "left");
4807 if (tmp == NULL) goto failed;
4808 res = obj2ast_expr(tmp, &left, arena);
4809 if (res != 0) goto failed;
4810 Py_XDECREF(tmp);
4811 tmp = NULL;
4812 } else {
4813 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4814 return 1;
4815 }
4816 if (PyObject_HasAttrString(obj, "ops")) {
4817 int res;
4818 Py_ssize_t len;
4819 Py_ssize_t i;
4820 tmp = PyObject_GetAttrString(obj, "ops");
4821 if (tmp == NULL) goto failed;
4822 if (!PyList_Check(tmp)) {
4823 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4824 goto failed;
4825 }
4826 len = PyList_GET_SIZE(tmp);
4827 ops = asdl_int_seq_new(len, arena);
4828 if (ops == NULL) goto failed;
4829 for (i = 0; i < len; i++) {
4830 cmpop_ty value;
4831 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4832 if (res != 0) goto failed;
4833 asdl_seq_SET(ops, i, value);
4834 }
4835 Py_XDECREF(tmp);
4836 tmp = NULL;
4837 } else {
4838 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4839 return 1;
4840 }
4841 if (PyObject_HasAttrString(obj, "comparators")) {
4842 int res;
4843 Py_ssize_t len;
4844 Py_ssize_t i;
4845 tmp = PyObject_GetAttrString(obj, "comparators");
4846 if (tmp == NULL) goto failed;
4847 if (!PyList_Check(tmp)) {
4848 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4849 goto failed;
4850 }
4851 len = PyList_GET_SIZE(tmp);
4852 comparators = asdl_seq_new(len, arena);
4853 if (comparators == NULL) goto failed;
4854 for (i = 0; i < len; i++) {
4855 expr_ty value;
4856 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4857 if (res != 0) goto failed;
4858 asdl_seq_SET(comparators, i, value);
4859 }
4860 Py_XDECREF(tmp);
4861 tmp = NULL;
4862 } else {
4863 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4864 return 1;
4865 }
4866 *out = Compare(left, ops, comparators, lineno, col_offset,
4867 arena);
4868 if (*out == NULL) goto failed;
4869 return 0;
4870 }
4871 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4872 expr_ty func;
4873 asdl_seq* args;
4874 asdl_seq* keywords;
4875 expr_ty starargs;
4876 expr_ty kwargs;
4877
4878 if (PyObject_HasAttrString(obj, "func")) {
4879 int res;
4880 tmp = PyObject_GetAttrString(obj, "func");
4881 if (tmp == NULL) goto failed;
4882 res = obj2ast_expr(tmp, &func, arena);
4883 if (res != 0) goto failed;
4884 Py_XDECREF(tmp);
4885 tmp = NULL;
4886 } else {
4887 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4888 return 1;
4889 }
4890 if (PyObject_HasAttrString(obj, "args")) {
4891 int res;
4892 Py_ssize_t len;
4893 Py_ssize_t i;
4894 tmp = PyObject_GetAttrString(obj, "args");
4895 if (tmp == NULL) goto failed;
4896 if (!PyList_Check(tmp)) {
4897 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4898 goto failed;
4899 }
4900 len = PyList_GET_SIZE(tmp);
4901 args = asdl_seq_new(len, arena);
4902 if (args == NULL) goto failed;
4903 for (i = 0; i < len; i++) {
4904 expr_ty value;
4905 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4906 if (res != 0) goto failed;
4907 asdl_seq_SET(args, i, value);
4908 }
4909 Py_XDECREF(tmp);
4910 tmp = NULL;
4911 } else {
4912 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4913 return 1;
4914 }
4915 if (PyObject_HasAttrString(obj, "keywords")) {
4916 int res;
4917 Py_ssize_t len;
4918 Py_ssize_t i;
4919 tmp = PyObject_GetAttrString(obj, "keywords");
4920 if (tmp == NULL) goto failed;
4921 if (!PyList_Check(tmp)) {
4922 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4923 goto failed;
4924 }
4925 len = PyList_GET_SIZE(tmp);
4926 keywords = asdl_seq_new(len, arena);
4927 if (keywords == NULL) goto failed;
4928 for (i = 0; i < len; i++) {
4929 keyword_ty value;
4930 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4931 if (res != 0) goto failed;
4932 asdl_seq_SET(keywords, i, value);
4933 }
4934 Py_XDECREF(tmp);
4935 tmp = NULL;
4936 } else {
4937 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4938 return 1;
4939 }
4940 if (PyObject_HasAttrString(obj, "starargs")) {
4941 int res;
4942 tmp = PyObject_GetAttrString(obj, "starargs");
4943 if (tmp == NULL) goto failed;
4944 res = obj2ast_expr(tmp, &starargs, arena);
4945 if (res != 0) goto failed;
4946 Py_XDECREF(tmp);
4947 tmp = NULL;
4948 } else {
4949 starargs = NULL;
4950 }
4951 if (PyObject_HasAttrString(obj, "kwargs")) {
4952 int res;
4953 tmp = PyObject_GetAttrString(obj, "kwargs");
4954 if (tmp == NULL) goto failed;
4955 res = obj2ast_expr(tmp, &kwargs, arena);
4956 if (res != 0) goto failed;
4957 Py_XDECREF(tmp);
4958 tmp = NULL;
4959 } else {
4960 kwargs = NULL;
4961 }
4962 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4963 col_offset, arena);
4964 if (*out == NULL) goto failed;
4965 return 0;
4966 }
4967 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4968 expr_ty value;
4969
4970 if (PyObject_HasAttrString(obj, "value")) {
4971 int res;
4972 tmp = PyObject_GetAttrString(obj, "value");
4973 if (tmp == NULL) goto failed;
4974 res = obj2ast_expr(tmp, &value, arena);
4975 if (res != 0) goto failed;
4976 Py_XDECREF(tmp);
4977 tmp = NULL;
4978 } else {
4979 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
4980 return 1;
4981 }
4982 *out = Repr(value, lineno, col_offset, arena);
4983 if (*out == NULL) goto failed;
4984 return 0;
4985 }
4986 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
4987 object n;
4988
4989 if (PyObject_HasAttrString(obj, "n")) {
4990 int res;
4991 tmp = PyObject_GetAttrString(obj, "n");
4992 if (tmp == NULL) goto failed;
4993 res = obj2ast_object(tmp, &n, arena);
4994 if (res != 0) goto failed;
4995 Py_XDECREF(tmp);
4996 tmp = NULL;
4997 } else {
4998 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
4999 return 1;
5000 }
5001 *out = Num(n, lineno, col_offset, arena);
5002 if (*out == NULL) goto failed;
5003 return 0;
5004 }
5005 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
5006 string s;
5007
5008 if (PyObject_HasAttrString(obj, "s")) {
5009 int res;
5010 tmp = PyObject_GetAttrString(obj, "s");
5011 if (tmp == NULL) goto failed;
5012 res = obj2ast_string(tmp, &s, arena);
5013 if (res != 0) goto failed;
5014 Py_XDECREF(tmp);
5015 tmp = NULL;
5016 } else {
5017 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
5018 return 1;
5019 }
5020 *out = Str(s, lineno, col_offset, arena);
5021 if (*out == NULL) goto failed;
5022 return 0;
5023 }
5024 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
5025 expr_ty value;
5026 identifier attr;
5027 expr_context_ty ctx;
5028
5029 if (PyObject_HasAttrString(obj, "value")) {
5030 int res;
5031 tmp = PyObject_GetAttrString(obj, "value");
5032 if (tmp == NULL) goto failed;
5033 res = obj2ast_expr(tmp, &value, arena);
5034 if (res != 0) goto failed;
5035 Py_XDECREF(tmp);
5036 tmp = NULL;
5037 } else {
5038 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
5039 return 1;
5040 }
5041 if (PyObject_HasAttrString(obj, "attr")) {
5042 int res;
5043 tmp = PyObject_GetAttrString(obj, "attr");
5044 if (tmp == NULL) goto failed;
5045 res = obj2ast_identifier(tmp, &attr, arena);
5046 if (res != 0) goto failed;
5047 Py_XDECREF(tmp);
5048 tmp = NULL;
5049 } else {
5050 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
5051 return 1;
5052 }
5053 if (PyObject_HasAttrString(obj, "ctx")) {
5054 int res;
5055 tmp = PyObject_GetAttrString(obj, "ctx");
5056 if (tmp == NULL) goto failed;
5057 res = obj2ast_expr_context(tmp, &ctx, arena);
5058 if (res != 0) goto failed;
5059 Py_XDECREF(tmp);
5060 tmp = NULL;
5061 } else {
5062 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
5063 return 1;
5064 }
5065 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
5066 if (*out == NULL) goto failed;
5067 return 0;
5068 }
5069 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
5070 expr_ty value;
5071 slice_ty slice;
5072 expr_context_ty ctx;
5073
5074 if (PyObject_HasAttrString(obj, "value")) {
5075 int res;
5076 tmp = PyObject_GetAttrString(obj, "value");
5077 if (tmp == NULL) goto failed;
5078 res = obj2ast_expr(tmp, &value, arena);
5079 if (res != 0) goto failed;
5080 Py_XDECREF(tmp);
5081 tmp = NULL;
5082 } else {
5083 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
5084 return 1;
5085 }
5086 if (PyObject_HasAttrString(obj, "slice")) {
5087 int res;
5088 tmp = PyObject_GetAttrString(obj, "slice");
5089 if (tmp == NULL) goto failed;
5090 res = obj2ast_slice(tmp, &slice, arena);
5091 if (res != 0) goto failed;
5092 Py_XDECREF(tmp);
5093 tmp = NULL;
5094 } else {
5095 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
5096 return 1;
5097 }
5098 if (PyObject_HasAttrString(obj, "ctx")) {
5099 int res;
5100 tmp = PyObject_GetAttrString(obj, "ctx");
5101 if (tmp == NULL) goto failed;
5102 res = obj2ast_expr_context(tmp, &ctx, arena);
5103 if (res != 0) goto failed;
5104 Py_XDECREF(tmp);
5105 tmp = NULL;
5106 } else {
5107 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5108 return 1;
5109 }
5110 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5111 if (*out == NULL) goto failed;
5112 return 0;
5113 }
5114 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5115 identifier id;
5116 expr_context_ty ctx;
5117
5118 if (PyObject_HasAttrString(obj, "id")) {
5119 int res;
5120 tmp = PyObject_GetAttrString(obj, "id");
5121 if (tmp == NULL) goto failed;
5122 res = obj2ast_identifier(tmp, &id, arena);
5123 if (res != 0) goto failed;
5124 Py_XDECREF(tmp);
5125 tmp = NULL;
5126 } else {
5127 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5128 return 1;
5129 }
5130 if (PyObject_HasAttrString(obj, "ctx")) {
5131 int res;
5132 tmp = PyObject_GetAttrString(obj, "ctx");
5133 if (tmp == NULL) goto failed;
5134 res = obj2ast_expr_context(tmp, &ctx, arena);
5135 if (res != 0) goto failed;
5136 Py_XDECREF(tmp);
5137 tmp = NULL;
5138 } else {
5139 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5140 return 1;
5141 }
5142 *out = Name(id, ctx, lineno, col_offset, arena);
5143 if (*out == NULL) goto failed;
5144 return 0;
5145 }
5146 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5147 asdl_seq* elts;
5148 expr_context_ty ctx;
5149
5150 if (PyObject_HasAttrString(obj, "elts")) {
5151 int res;
5152 Py_ssize_t len;
5153 Py_ssize_t i;
5154 tmp = PyObject_GetAttrString(obj, "elts");
5155 if (tmp == NULL) goto failed;
5156 if (!PyList_Check(tmp)) {
5157 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5158 goto failed;
5159 }
5160 len = PyList_GET_SIZE(tmp);
5161 elts = asdl_seq_new(len, arena);
5162 if (elts == NULL) goto failed;
5163 for (i = 0; i < len; i++) {
5164 expr_ty value;
5165 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5166 if (res != 0) goto failed;
5167 asdl_seq_SET(elts, i, value);
5168 }
5169 Py_XDECREF(tmp);
5170 tmp = NULL;
5171 } else {
5172 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5173 return 1;
5174 }
5175 if (PyObject_HasAttrString(obj, "ctx")) {
5176 int res;
5177 tmp = PyObject_GetAttrString(obj, "ctx");
5178 if (tmp == NULL) goto failed;
5179 res = obj2ast_expr_context(tmp, &ctx, arena);
5180 if (res != 0) goto failed;
5181 Py_XDECREF(tmp);
5182 tmp = NULL;
5183 } else {
5184 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5185 return 1;
5186 }
5187 *out = List(elts, ctx, lineno, col_offset, arena);
5188 if (*out == NULL) goto failed;
5189 return 0;
5190 }
5191 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5192 asdl_seq* elts;
5193 expr_context_ty ctx;
5194
5195 if (PyObject_HasAttrString(obj, "elts")) {
5196 int res;
5197 Py_ssize_t len;
5198 Py_ssize_t i;
5199 tmp = PyObject_GetAttrString(obj, "elts");
5200 if (tmp == NULL) goto failed;
5201 if (!PyList_Check(tmp)) {
5202 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5203 goto failed;
5204 }
5205 len = PyList_GET_SIZE(tmp);
5206 elts = asdl_seq_new(len, arena);
5207 if (elts == NULL) goto failed;
5208 for (i = 0; i < len; i++) {
5209 expr_ty value;
5210 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5211 if (res != 0) goto failed;
5212 asdl_seq_SET(elts, i, value);
5213 }
5214 Py_XDECREF(tmp);
5215 tmp = NULL;
5216 } else {
5217 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5218 return 1;
5219 }
5220 if (PyObject_HasAttrString(obj, "ctx")) {
5221 int res;
5222 tmp = PyObject_GetAttrString(obj, "ctx");
5223 if (tmp == NULL) goto failed;
5224 res = obj2ast_expr_context(tmp, &ctx, arena);
5225 if (res != 0) goto failed;
5226 Py_XDECREF(tmp);
5227 tmp = NULL;
5228 } else {
5229 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5230 return 1;
5231 }
5232 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5233 if (*out == NULL) goto failed;
5234 return 0;
5235 }
5236
5237 tmp = PyObject_Repr(obj);
5238 if (tmp == NULL) goto failed;
5239 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5240failed:
5241 Py_XDECREF(tmp);
5242 return 1;
5243}
5244
5245int
5246obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5247{
5248 PyObject* tmp = NULL;
5249
5250 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5251 *out = Load;
5252 return 0;
5253 }
5254 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5255 *out = Store;
5256 return 0;
5257 }
5258 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5259 *out = Del;
5260 return 0;
5261 }
5262 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5263 *out = AugLoad;
5264 return 0;
5265 }
5266 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5267 *out = AugStore;
5268 return 0;
5269 }
5270 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5271 *out = Param;
5272 return 0;
5273 }
5274
5275 tmp = PyObject_Repr(obj);
5276 if (tmp == NULL) goto failed;
5277 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5278failed:
5279 Py_XDECREF(tmp);
5280 return 1;
5281}
5282
5283int
5284obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5285{
5286 PyObject* tmp = NULL;
5287
5288
5289 if (obj == Py_None) {
5290 *out = NULL;
5291 return 0;
5292 }
5293 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5294
5295 *out = Ellipsis(arena);
5296 if (*out == NULL) goto failed;
5297 return 0;
5298 }
5299 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5300 expr_ty lower;
5301 expr_ty upper;
5302 expr_ty step;
5303
5304 if (PyObject_HasAttrString(obj, "lower")) {
5305 int res;
5306 tmp = PyObject_GetAttrString(obj, "lower");
5307 if (tmp == NULL) goto failed;
5308 res = obj2ast_expr(tmp, &lower, arena);
5309 if (res != 0) goto failed;
5310 Py_XDECREF(tmp);
5311 tmp = NULL;
5312 } else {
5313 lower = NULL;
5314 }
5315 if (PyObject_HasAttrString(obj, "upper")) {
5316 int res;
5317 tmp = PyObject_GetAttrString(obj, "upper");
5318 if (tmp == NULL) goto failed;
5319 res = obj2ast_expr(tmp, &upper, arena);
5320 if (res != 0) goto failed;
5321 Py_XDECREF(tmp);
5322 tmp = NULL;
5323 } else {
5324 upper = NULL;
5325 }
5326 if (PyObject_HasAttrString(obj, "step")) {
5327 int res;
5328 tmp = PyObject_GetAttrString(obj, "step");
5329 if (tmp == NULL) goto failed;
5330 res = obj2ast_expr(tmp, &step, arena);
5331 if (res != 0) goto failed;
5332 Py_XDECREF(tmp);
5333 tmp = NULL;
5334 } else {
5335 step = NULL;
5336 }
5337 *out = Slice(lower, upper, step, arena);
5338 if (*out == NULL) goto failed;
5339 return 0;
5340 }
5341 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5342 asdl_seq* dims;
5343
5344 if (PyObject_HasAttrString(obj, "dims")) {
5345 int res;
5346 Py_ssize_t len;
5347 Py_ssize_t i;
5348 tmp = PyObject_GetAttrString(obj, "dims");
5349 if (tmp == NULL) goto failed;
5350 if (!PyList_Check(tmp)) {
5351 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5352 goto failed;
5353 }
5354 len = PyList_GET_SIZE(tmp);
5355 dims = asdl_seq_new(len, arena);
5356 if (dims == NULL) goto failed;
5357 for (i = 0; i < len; i++) {
5358 slice_ty value;
5359 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5360 if (res != 0) goto failed;
5361 asdl_seq_SET(dims, i, value);
5362 }
5363 Py_XDECREF(tmp);
5364 tmp = NULL;
5365 } else {
5366 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5367 return 1;
5368 }
5369 *out = ExtSlice(dims, arena);
5370 if (*out == NULL) goto failed;
5371 return 0;
5372 }
5373 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5374 expr_ty value;
5375
5376 if (PyObject_HasAttrString(obj, "value")) {
5377 int res;
5378 tmp = PyObject_GetAttrString(obj, "value");
5379 if (tmp == NULL) goto failed;
5380 res = obj2ast_expr(tmp, &value, arena);
5381 if (res != 0) goto failed;
5382 Py_XDECREF(tmp);
5383 tmp = NULL;
5384 } else {
5385 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5386 return 1;
5387 }
5388 *out = Index(value, arena);
5389 if (*out == NULL) goto failed;
5390 return 0;
5391 }
5392
5393 tmp = PyObject_Repr(obj);
5394 if (tmp == NULL) goto failed;
5395 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5396failed:
5397 Py_XDECREF(tmp);
5398 return 1;
5399}
5400
5401int
5402obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5403{
5404 PyObject* tmp = NULL;
5405
5406 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5407 *out = And;
5408 return 0;
5409 }
5410 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5411 *out = Or;
5412 return 0;
5413 }
5414
5415 tmp = PyObject_Repr(obj);
5416 if (tmp == NULL) goto failed;
5417 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5418failed:
5419 Py_XDECREF(tmp);
5420 return 1;
5421}
5422
5423int
5424obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5425{
5426 PyObject* tmp = NULL;
5427
5428 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5429 *out = Add;
5430 return 0;
5431 }
5432 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5433 *out = Sub;
5434 return 0;
5435 }
5436 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5437 *out = Mult;
5438 return 0;
5439 }
5440 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5441 *out = Div;
5442 return 0;
5443 }
5444 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5445 *out = Mod;
5446 return 0;
5447 }
5448 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5449 *out = Pow;
5450 return 0;
5451 }
5452 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5453 *out = LShift;
5454 return 0;
5455 }
5456 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5457 *out = RShift;
5458 return 0;
5459 }
5460 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5461 *out = BitOr;
5462 return 0;
5463 }
5464 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5465 *out = BitXor;
5466 return 0;
5467 }
5468 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5469 *out = BitAnd;
5470 return 0;
5471 }
5472 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5473 *out = FloorDiv;
5474 return 0;
5475 }
5476
5477 tmp = PyObject_Repr(obj);
5478 if (tmp == NULL) goto failed;
5479 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5480failed:
5481 Py_XDECREF(tmp);
5482 return 1;
5483}
5484
5485int
5486obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5487{
5488 PyObject* tmp = NULL;
5489
5490 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5491 *out = Invert;
5492 return 0;
5493 }
5494 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5495 *out = Not;
5496 return 0;
5497 }
5498 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5499 *out = UAdd;
5500 return 0;
5501 }
5502 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5503 *out = USub;
5504 return 0;
5505 }
5506
5507 tmp = PyObject_Repr(obj);
5508 if (tmp == NULL) goto failed;
5509 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5510failed:
5511 Py_XDECREF(tmp);
5512 return 1;
5513}
5514
5515int
5516obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5517{
5518 PyObject* tmp = NULL;
5519
5520 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5521 *out = Eq;
5522 return 0;
5523 }
5524 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5525 *out = NotEq;
5526 return 0;
5527 }
5528 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5529 *out = Lt;
5530 return 0;
5531 }
5532 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5533 *out = LtE;
5534 return 0;
5535 }
5536 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5537 *out = Gt;
5538 return 0;
5539 }
5540 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5541 *out = GtE;
5542 return 0;
5543 }
5544 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5545 *out = Is;
5546 return 0;
5547 }
5548 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5549 *out = IsNot;
5550 return 0;
5551 }
5552 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5553 *out = In;
5554 return 0;
5555 }
5556 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5557 *out = NotIn;
5558 return 0;
5559 }
5560
5561 tmp = PyObject_Repr(obj);
5562 if (tmp == NULL) goto failed;
5563 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5564failed:
5565 Py_XDECREF(tmp);
5566 return 1;
5567}
5568
5569int
5570obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5571{
5572 PyObject* tmp = NULL;
5573 expr_ty target;
5574 expr_ty iter;
5575 asdl_seq* ifs;
5576
5577 if (PyObject_HasAttrString(obj, "target")) {
5578 int res;
5579 tmp = PyObject_GetAttrString(obj, "target");
5580 if (tmp == NULL) goto failed;
5581 res = obj2ast_expr(tmp, &target, arena);
5582 if (res != 0) goto failed;
5583 Py_XDECREF(tmp);
5584 tmp = NULL;
5585 } else {
5586 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5587 return 1;
5588 }
5589 if (PyObject_HasAttrString(obj, "iter")) {
5590 int res;
5591 tmp = PyObject_GetAttrString(obj, "iter");
5592 if (tmp == NULL) goto failed;
5593 res = obj2ast_expr(tmp, &iter, arena);
5594 if (res != 0) goto failed;
5595 Py_XDECREF(tmp);
5596 tmp = NULL;
5597 } else {
5598 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5599 return 1;
5600 }
5601 if (PyObject_HasAttrString(obj, "ifs")) {
5602 int res;
5603 Py_ssize_t len;
5604 Py_ssize_t i;
5605 tmp = PyObject_GetAttrString(obj, "ifs");
5606 if (tmp == NULL) goto failed;
5607 if (!PyList_Check(tmp)) {
5608 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5609 goto failed;
5610 }
5611 len = PyList_GET_SIZE(tmp);
5612 ifs = asdl_seq_new(len, arena);
5613 if (ifs == NULL) goto failed;
5614 for (i = 0; i < len; i++) {
5615 expr_ty value;
5616 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5617 if (res != 0) goto failed;
5618 asdl_seq_SET(ifs, i, value);
5619 }
5620 Py_XDECREF(tmp);
5621 tmp = NULL;
5622 } else {
5623 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5624 return 1;
5625 }
5626 *out = comprehension(target, iter, ifs, arena);
5627 return 0;
5628failed:
5629 Py_XDECREF(tmp);
5630 return 1;
5631}
5632
5633int
5634obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5635{
5636 PyObject* tmp = NULL;
Georg Brandla48f3ab2008-03-30 06:40:17 +00005637
Georg Brandlfc8eef32008-03-28 12:11:56 +00005638 int lineno;
5639 int col_offset;
5640
Georg Brandla48f3ab2008-03-30 06:40:17 +00005641 if (obj == Py_None) {
5642 *out = NULL;
5643 return 0;
Georg Brandlfc8eef32008-03-28 12:11:56 +00005644 }
5645 if (PyObject_HasAttrString(obj, "lineno")) {
5646 int res;
5647 tmp = PyObject_GetAttrString(obj, "lineno");
5648 if (tmp == NULL) goto failed;
5649 res = obj2ast_int(tmp, &lineno, arena);
5650 if (res != 0) goto failed;
5651 Py_XDECREF(tmp);
5652 tmp = NULL;
5653 } else {
5654 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5655 return 1;
5656 }
5657 if (PyObject_HasAttrString(obj, "col_offset")) {
5658 int res;
5659 tmp = PyObject_GetAttrString(obj, "col_offset");
5660 if (tmp == NULL) goto failed;
5661 res = obj2ast_int(tmp, &col_offset, arena);
5662 if (res != 0) goto failed;
5663 Py_XDECREF(tmp);
5664 tmp = NULL;
5665 } else {
5666 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5667 return 1;
5668 }
Georg Brandla48f3ab2008-03-30 06:40:17 +00005669 if (PyObject_IsInstance(obj, (PyObject*)ExceptHandler_type)) {
5670 expr_ty type;
5671 expr_ty name;
5672 asdl_seq* body;
5673
5674 if (PyObject_HasAttrString(obj, "type")) {
5675 int res;
5676 tmp = PyObject_GetAttrString(obj, "type");
5677 if (tmp == NULL) goto failed;
5678 res = obj2ast_expr(tmp, &type, arena);
5679 if (res != 0) goto failed;
5680 Py_XDECREF(tmp);
5681 tmp = NULL;
5682 } else {
5683 type = NULL;
5684 }
5685 if (PyObject_HasAttrString(obj, "name")) {
5686 int res;
5687 tmp = PyObject_GetAttrString(obj, "name");
5688 if (tmp == NULL) goto failed;
5689 res = obj2ast_expr(tmp, &name, arena);
5690 if (res != 0) goto failed;
5691 Py_XDECREF(tmp);
5692 tmp = NULL;
5693 } else {
5694 name = NULL;
5695 }
5696 if (PyObject_HasAttrString(obj, "body")) {
5697 int res;
5698 Py_ssize_t len;
5699 Py_ssize_t i;
5700 tmp = PyObject_GetAttrString(obj, "body");
5701 if (tmp == NULL) goto failed;
5702 if (!PyList_Check(tmp)) {
5703 PyErr_Format(PyExc_TypeError, "ExceptHandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5704 goto failed;
5705 }
5706 len = PyList_GET_SIZE(tmp);
5707 body = asdl_seq_new(len, arena);
5708 if (body == NULL) goto failed;
5709 for (i = 0; i < len; i++) {
5710 stmt_ty value;
5711 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5712 if (res != 0) goto failed;
5713 asdl_seq_SET(body, i, value);
5714 }
5715 Py_XDECREF(tmp);
5716 tmp = NULL;
5717 } else {
5718 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ExceptHandler");
5719 return 1;
5720 }
5721 *out = ExceptHandler(type, name, body, lineno, col_offset,
5722 arena);
5723 if (*out == NULL) goto failed;
5724 return 0;
5725 }
5726
5727 tmp = PyObject_Repr(obj);
5728 if (tmp == NULL) goto failed;
5729 PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyString_AS_STRING(tmp));
Georg Brandlfc8eef32008-03-28 12:11:56 +00005730failed:
5731 Py_XDECREF(tmp);
5732 return 1;
5733}
5734
5735int
5736obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5737{
5738 PyObject* tmp = NULL;
5739 asdl_seq* args;
5740 identifier vararg;
5741 identifier kwarg;
5742 asdl_seq* defaults;
5743
5744 if (PyObject_HasAttrString(obj, "args")) {
5745 int res;
5746 Py_ssize_t len;
5747 Py_ssize_t i;
5748 tmp = PyObject_GetAttrString(obj, "args");
5749 if (tmp == NULL) goto failed;
5750 if (!PyList_Check(tmp)) {
5751 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5752 goto failed;
5753 }
5754 len = PyList_GET_SIZE(tmp);
5755 args = asdl_seq_new(len, arena);
5756 if (args == NULL) goto failed;
5757 for (i = 0; i < len; i++) {
5758 expr_ty value;
5759 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5760 if (res != 0) goto failed;
5761 asdl_seq_SET(args, i, value);
5762 }
5763 Py_XDECREF(tmp);
5764 tmp = NULL;
5765 } else {
5766 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5767 return 1;
5768 }
5769 if (PyObject_HasAttrString(obj, "vararg")) {
5770 int res;
5771 tmp = PyObject_GetAttrString(obj, "vararg");
5772 if (tmp == NULL) goto failed;
5773 res = obj2ast_identifier(tmp, &vararg, arena);
5774 if (res != 0) goto failed;
5775 Py_XDECREF(tmp);
5776 tmp = NULL;
5777 } else {
5778 vararg = NULL;
5779 }
5780 if (PyObject_HasAttrString(obj, "kwarg")) {
5781 int res;
5782 tmp = PyObject_GetAttrString(obj, "kwarg");
5783 if (tmp == NULL) goto failed;
5784 res = obj2ast_identifier(tmp, &kwarg, arena);
5785 if (res != 0) goto failed;
5786 Py_XDECREF(tmp);
5787 tmp = NULL;
5788 } else {
5789 kwarg = NULL;
5790 }
5791 if (PyObject_HasAttrString(obj, "defaults")) {
5792 int res;
5793 Py_ssize_t len;
5794 Py_ssize_t i;
5795 tmp = PyObject_GetAttrString(obj, "defaults");
5796 if (tmp == NULL) goto failed;
5797 if (!PyList_Check(tmp)) {
5798 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5799 goto failed;
5800 }
5801 len = PyList_GET_SIZE(tmp);
5802 defaults = asdl_seq_new(len, arena);
5803 if (defaults == NULL) goto failed;
5804 for (i = 0; i < len; i++) {
5805 expr_ty value;
5806 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5807 if (res != 0) goto failed;
5808 asdl_seq_SET(defaults, i, value);
5809 }
5810 Py_XDECREF(tmp);
5811 tmp = NULL;
5812 } else {
5813 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5814 return 1;
5815 }
5816 *out = arguments(args, vararg, kwarg, defaults, arena);
5817 return 0;
5818failed:
5819 Py_XDECREF(tmp);
5820 return 1;
5821}
5822
5823int
5824obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5825{
5826 PyObject* tmp = NULL;
5827 identifier arg;
5828 expr_ty value;
5829
5830 if (PyObject_HasAttrString(obj, "arg")) {
5831 int res;
5832 tmp = PyObject_GetAttrString(obj, "arg");
5833 if (tmp == NULL) goto failed;
5834 res = obj2ast_identifier(tmp, &arg, arena);
5835 if (res != 0) goto failed;
5836 Py_XDECREF(tmp);
5837 tmp = NULL;
5838 } else {
5839 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5840 return 1;
5841 }
5842 if (PyObject_HasAttrString(obj, "value")) {
5843 int res;
5844 tmp = PyObject_GetAttrString(obj, "value");
5845 if (tmp == NULL) goto failed;
5846 res = obj2ast_expr(tmp, &value, arena);
5847 if (res != 0) goto failed;
5848 Py_XDECREF(tmp);
5849 tmp = NULL;
5850 } else {
5851 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5852 return 1;
5853 }
5854 *out = keyword(arg, value, arena);
5855 return 0;
5856failed:
5857 Py_XDECREF(tmp);
5858 return 1;
5859}
5860
5861int
5862obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5863{
5864 PyObject* tmp = NULL;
5865 identifier name;
5866 identifier asname;
5867
5868 if (PyObject_HasAttrString(obj, "name")) {
5869 int res;
5870 tmp = PyObject_GetAttrString(obj, "name");
5871 if (tmp == NULL) goto failed;
5872 res = obj2ast_identifier(tmp, &name, arena);
5873 if (res != 0) goto failed;
5874 Py_XDECREF(tmp);
5875 tmp = NULL;
5876 } else {
5877 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5878 return 1;
5879 }
5880 if (PyObject_HasAttrString(obj, "asname")) {
5881 int res;
5882 tmp = PyObject_GetAttrString(obj, "asname");
5883 if (tmp == NULL) goto failed;
5884 res = obj2ast_identifier(tmp, &asname, arena);
5885 if (res != 0) goto failed;
5886 Py_XDECREF(tmp);
5887 tmp = NULL;
5888 } else {
5889 asname = NULL;
5890 }
5891 *out = alias(name, asname, arena);
5892 return 0;
5893failed:
5894 Py_XDECREF(tmp);
5895 return 1;
5896}
5897
5898
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005899PyMODINIT_FUNC
5900init_ast(void)
5901{
5902 PyObject *m, *d;
5903 if (!init_types()) return;
5904 m = Py_InitModule3("_ast", NULL, NULL);
5905 if (!m) return;
5906 d = PyModule_GetDict(m);
Georg Brandlc52ed592008-03-30 07:01:47 +00005907 if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005908 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5909 return;
Georg Brandlc52ed592008-03-30 07:01:47 +00005910 if (PyModule_AddStringConstant(m, "__version__", "62047") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00005911 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00005912 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5913 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5914 return;
5915 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5916 < 0) return;
5917 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5918 0) return;
5919 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5920 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5921 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5922 < 0) return;
5923 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5924 return;
5925 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5926 return;
5927 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5928 return;
5929 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5930 return;
5931 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5932 0) return;
5933 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5934 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5935 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5936 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5937 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5938 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5939 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5940 0) return;
5941 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5942 0) return;
5943 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5944 return;
5945 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5946 return;
5947 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5948 0) return;
5949 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5950 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5951 return;
5952 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5953 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5954 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5955 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5956 return;
5957 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5958 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5959 return;
5960 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5961 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5962 return;
5963 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
5964 return;
5965 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
5966 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
5967 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
5968 return;
5969 if (PyDict_SetItemString(d, "GeneratorExp",
5970 (PyObject*)GeneratorExp_type) < 0) return;
5971 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
5972 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
5973 return;
5974 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
5975 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
5976 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
5977 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
5978 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
5979 0) return;
5980 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
5981 0) return;
5982 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
5983 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
5984 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
5985 if (PyDict_SetItemString(d, "expr_context",
5986 (PyObject*)expr_context_type) < 0) return;
5987 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
5988 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
5989 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
5990 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
5991 return;
5992 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
5993 return;
5994 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
5995 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
5996 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
5997 return;
5998 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
5999 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
6000 return;
6001 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
6002 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
6003 return;
6004 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
6005 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
6006 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
6007 return;
6008 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
6009 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
6010 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
6011 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
6012 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
6013 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
6014 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
6015 return;
6016 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
6017 return;
6018 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
6019 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
6020 return;
6021 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
6022 return;
6023 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
6024 return;
6025 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
6026 return;
6027 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
6028 return;
6029 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
6030 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
6031 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
6032 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
6033 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
6034 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
6035 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
6036 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
6037 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
6038 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
6039 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
6040 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
6041 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
6042 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
6043 if (PyDict_SetItemString(d, "comprehension",
6044 (PyObject*)comprehension_type) < 0) return;
6045 if (PyDict_SetItemString(d, "excepthandler",
6046 (PyObject*)excepthandler_type) < 0) return;
Georg Brandla48f3ab2008-03-30 06:40:17 +00006047 if (PyDict_SetItemString(d, "ExceptHandler",
6048 (PyObject*)ExceptHandler_type) < 0) return;
Neal Norwitz19379f12006-04-03 04:50:58 +00006049 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
6050 0) return;
6051 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
6052 return;
6053 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00006054}
6055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006056
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006057PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006058{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00006059 init_types();
6060 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006061}
6062
Georg Brandlf2bfd542008-03-29 13:24:23 +00006063/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
6064mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00006065{
6066 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00006067 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
6068 (PyObject*)Interactive_type};
6069 char *req_name[] = {"Module", "Expression", "Interactive"};
6070 assert(0 <= mode && mode <= 2);
6071
Georg Brandlfc8eef32008-03-28 12:11:56 +00006072 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00006073
6074 if (!PyObject_IsInstance(ast, req_type[mode])) {
6075 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
6076 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006077 return NULL;
6078 }
6079 if (obj2ast_mod(ast, &res, arena) != 0)
6080 return NULL;
6081 else
6082 return res;
6083}
6084
6085int PyAST_Check(PyObject* obj)
6086{
6087 init_types();
Georg Brandlc52ed592008-03-30 07:01:47 +00006088 return PyObject_IsInstance(obj, (PyObject*)&AST_type);
Georg Brandlfc8eef32008-03-28 12:11:56 +00006089}
6090
Neal Norwitz7b5a6042005-11-13 19:14:20 +00006091