blob: 0703d515e8da8e05457ab658671e5102176f582a [file] [log] [blame]
Brett Cannon0db62aa2007-02-12 03:51:02 +00001/* File automatically generated by Parser/asdl_c.py. */
2
3
4/*
Christian Heimes40346852008-02-23 17:52:07 +00005 __version__ 60978.
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
Neal Norwitz53d960c2006-02-28 22:47:29 +000015static PyTypeObject* AST_type;
16static 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;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000339static PyObject* ast2obj_excepthandler(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000340static char *excepthandler_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000341 "type",
342 "name",
343 "body",
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000344 "lineno",
345 "col_offset",
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000346};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000347static PyTypeObject *arguments_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000348static PyObject* ast2obj_arguments(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000349static char *arguments_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000350 "args",
351 "vararg",
352 "kwarg",
353 "defaults",
354};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000355static PyTypeObject *keyword_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000356static PyObject* ast2obj_keyword(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000357static char *keyword_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000358 "arg",
359 "value",
360};
Neal Norwitz53d960c2006-02-28 22:47:29 +0000361static PyTypeObject *alias_type;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000362static PyObject* ast2obj_alias(void*);
Neal Norwitz53d960c2006-02-28 22:47:29 +0000363static char *alias_fields[]={
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000364 "name",
365 "asname",
366};
367
368
369static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields)
370{
371 PyObject *fnames, *result;
372 int i;
373 if (num_fields) {
374 fnames = PyTuple_New(num_fields);
375 if (!fnames) return NULL;
376 } else {
377 fnames = Py_None;
378 Py_INCREF(Py_None);
379 }
380 for(i=0; i < num_fields; i++) {
381 PyObject *field = PyString_FromString(fields[i]);
382 if (!field) {
383 Py_DECREF(fnames);
384 return NULL;
385 }
386 PyTuple_SET_ITEM(fnames, i, field);
387 }
Thomas Wouters34aa7ba2006-02-28 19:02:24 +0000388 result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000389 type, base, "_fields", fnames, "__module__", "_ast");
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000390 Py_DECREF(fnames);
391 return (PyTypeObject*)result;
392}
393
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000394static int add_attributes(PyTypeObject* type, char**attrs, int num_fields)
395{
Neal Norwitz19379f12006-04-03 04:50:58 +0000396 int i, result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000397 PyObject *s, *l = PyList_New(num_fields);
398 if (!l) return 0;
Neal Norwitz19379f12006-04-03 04:50:58 +0000399 for(i = 0; i < num_fields; i++) {
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000400 s = PyString_FromString(attrs[i]);
401 if (!s) {
402 Py_DECREF(l);
403 return 0;
404 }
405 PyList_SET_ITEM(l, i, s);
406 }
Neal Norwitz19379f12006-04-03 04:50:58 +0000407 result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0;
408 Py_DECREF(l);
409 return result;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000410}
411
Georg Brandlfc8eef32008-03-28 12:11:56 +0000412/* Conversion AST -> Python */
413
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000414static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*))
415{
416 int i, n = asdl_seq_LEN(seq);
417 PyObject *result = PyList_New(n);
418 PyObject *value;
419 if (!result)
420 return NULL;
421 for (i = 0; i < n; i++) {
422 value = func(asdl_seq_GET(seq, i));
423 if (!value) {
424 Py_DECREF(result);
425 return NULL;
426 }
427 PyList_SET_ITEM(result, i, value);
428 }
429 return result;
430}
431
432static PyObject* ast2obj_object(void *o)
433{
434 if (!o)
435 o = Py_None;
436 Py_INCREF((PyObject*)o);
437 return (PyObject*)o;
438}
439#define ast2obj_identifier ast2obj_object
440#define ast2obj_string ast2obj_object
441static PyObject* ast2obj_bool(bool b)
442{
443 return PyBool_FromLong(b);
444}
445
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000446static PyObject* ast2obj_int(long b)
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000447{
448 return PyInt_FromLong(b);
449}
450
Georg Brandlfc8eef32008-03-28 12:11:56 +0000451/* Conversion Python -> AST */
452
453static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
454{
455 if (obj == Py_None)
456 obj = NULL;
457 if (obj)
458 PyArena_AddPyObject(arena, obj);
459 Py_XINCREF(obj);
460 *out = obj;
461 return 0;
462}
463
464#define obj2ast_identifier obj2ast_object
465#define obj2ast_string obj2ast_object
466
467static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
468{
469 int i;
470 if (!PyInt_Check(obj) && !PyLong_Check(obj)) {
471 PyObject *s = PyObject_Repr(obj);
472 if (s == NULL) return 1;
473 PyErr_Format(PyExc_ValueError, "invalid integer value: %.400s",
474 PyString_AS_STRING(s));
475 Py_DECREF(s);
476 return 1;
477 }
478
479 i = (int)PyLong_AsLong(obj);
480 if (i == -1 && PyErr_Occurred())
481 return 1;
482 *out = i;
483 return 0;
484}
485
486static int obj2ast_bool(PyObject* obj, bool* out, PyArena* arena)
487{
488 if (!PyBool_Check(obj)) {
489 PyObject *s = PyObject_Repr(obj);
490 if (s == NULL) return 1;
491 PyErr_Format(PyExc_ValueError, "invalid boolean value: %.400s",
492 PyString_AS_STRING(s));
493 Py_DECREF(s);
494 return 1;
495 }
496
497 *out = (obj == Py_True);
498 return 0;
499}
500
501
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000502static int init_types(void)
503{
Neal Norwitz19379f12006-04-03 04:50:58 +0000504 static int initialized;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000505 if (initialized) return 1;
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000506 AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);
507 mod_type = make_type("mod", AST_type, NULL, 0);
508 if (!mod_type) return 0;
509 if (!add_attributes(mod_type, NULL, 0)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000510 Module_type = make_type("Module", mod_type, Module_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000511 if (!Module_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000512 Interactive_type = make_type("Interactive", mod_type,
513 Interactive_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000514 if (!Interactive_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000515 Expression_type = make_type("Expression", mod_type, Expression_fields,
516 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000517 if (!Expression_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000518 Suite_type = make_type("Suite", mod_type, Suite_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000519 if (!Suite_type) return 0;
520 stmt_type = make_type("stmt", AST_type, NULL, 0);
521 if (!stmt_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000522 if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000523 FunctionDef_type = make_type("FunctionDef", stmt_type,
524 FunctionDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000525 if (!FunctionDef_type) return 0;
Christian Heimes5224d282008-02-23 15:01:05 +0000526 ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000527 if (!ClassDef_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000528 Return_type = make_type("Return", stmt_type, Return_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000529 if (!Return_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000530 Delete_type = make_type("Delete", stmt_type, Delete_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000531 if (!Delete_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000532 Assign_type = make_type("Assign", stmt_type, Assign_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000533 if (!Assign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000534 AugAssign_type = make_type("AugAssign", stmt_type, AugAssign_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000535 if (!AugAssign_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000536 Print_type = make_type("Print", stmt_type, Print_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000537 if (!Print_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000538 For_type = make_type("For", stmt_type, For_fields, 4);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000539 if (!For_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000540 While_type = make_type("While", stmt_type, While_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000541 if (!While_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000542 If_type = make_type("If", stmt_type, If_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000543 if (!If_type) return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000544 With_type = make_type("With", stmt_type, With_fields, 3);
545 if (!With_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000546 Raise_type = make_type("Raise", stmt_type, Raise_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000547 if (!Raise_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000548 TryExcept_type = make_type("TryExcept", stmt_type, TryExcept_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000549 if (!TryExcept_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000550 TryFinally_type = make_type("TryFinally", stmt_type, TryFinally_fields,
551 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000552 if (!TryFinally_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000553 Assert_type = make_type("Assert", stmt_type, Assert_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000554 if (!Assert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000555 Import_type = make_type("Import", stmt_type, Import_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000556 if (!Import_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000557 ImportFrom_type = make_type("ImportFrom", stmt_type, ImportFrom_fields,
Thomas Woutersf7f438b2006-02-28 16:09:29 +0000558 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000559 if (!ImportFrom_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000560 Exec_type = make_type("Exec", stmt_type, Exec_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000561 if (!Exec_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000562 Global_type = make_type("Global", stmt_type, Global_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000563 if (!Global_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000564 Expr_type = make_type("Expr", stmt_type, Expr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000565 if (!Expr_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000566 Pass_type = make_type("Pass", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000567 if (!Pass_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000568 Break_type = make_type("Break", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000569 if (!Break_type) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000570 Continue_type = make_type("Continue", stmt_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000571 if (!Continue_type) return 0;
572 expr_type = make_type("expr", AST_type, NULL, 0);
573 if (!expr_type) return 0;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000574 if (!add_attributes(expr_type, expr_attributes, 2)) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000575 BoolOp_type = make_type("BoolOp", expr_type, BoolOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000576 if (!BoolOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000577 BinOp_type = make_type("BinOp", expr_type, BinOp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000578 if (!BinOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000579 UnaryOp_type = make_type("UnaryOp", expr_type, UnaryOp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000580 if (!UnaryOp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000581 Lambda_type = make_type("Lambda", expr_type, Lambda_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000582 if (!Lambda_type) return 0;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +0000583 IfExp_type = make_type("IfExp", expr_type, IfExp_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000584 if (!IfExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000585 Dict_type = make_type("Dict", expr_type, Dict_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000586 if (!Dict_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000587 ListComp_type = make_type("ListComp", expr_type, ListComp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000588 if (!ListComp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000589 GeneratorExp_type = make_type("GeneratorExp", expr_type,
590 GeneratorExp_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000591 if (!GeneratorExp_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000592 Yield_type = make_type("Yield", expr_type, Yield_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000593 if (!Yield_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000594 Compare_type = make_type("Compare", expr_type, Compare_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000595 if (!Compare_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000596 Call_type = make_type("Call", expr_type, Call_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000597 if (!Call_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000598 Repr_type = make_type("Repr", expr_type, Repr_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000599 if (!Repr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000600 Num_type = make_type("Num", expr_type, Num_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000601 if (!Num_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000602 Str_type = make_type("Str", expr_type, Str_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000603 if (!Str_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000604 Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000605 if (!Attribute_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000606 Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000607 if (!Subscript_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000608 Name_type = make_type("Name", expr_type, Name_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000609 if (!Name_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000610 List_type = make_type("List", expr_type, List_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000611 if (!List_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000612 Tuple_type = make_type("Tuple", expr_type, Tuple_fields, 2);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000613 if (!Tuple_type) return 0;
614 expr_context_type = make_type("expr_context", AST_type, NULL, 0);
615 if (!expr_context_type) return 0;
616 if (!add_attributes(expr_context_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000617 Load_type = make_type("Load", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000618 if (!Load_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000619 Load_singleton = PyType_GenericNew(Load_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000620 if (!Load_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000621 Store_type = make_type("Store", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000622 if (!Store_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000623 Store_singleton = PyType_GenericNew(Store_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000624 if (!Store_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000625 Del_type = make_type("Del", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000626 if (!Del_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000627 Del_singleton = PyType_GenericNew(Del_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000628 if (!Del_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000629 AugLoad_type = make_type("AugLoad", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000630 if (!AugLoad_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000631 AugLoad_singleton = PyType_GenericNew(AugLoad_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000632 if (!AugLoad_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000633 AugStore_type = make_type("AugStore", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000634 if (!AugStore_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000635 AugStore_singleton = PyType_GenericNew(AugStore_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000636 if (!AugStore_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000637 Param_type = make_type("Param", expr_context_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000638 if (!Param_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000639 Param_singleton = PyType_GenericNew(Param_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000640 if (!Param_singleton) return 0;
641 slice_type = make_type("slice", AST_type, NULL, 0);
642 if (!slice_type) return 0;
643 if (!add_attributes(slice_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000644 Ellipsis_type = make_type("Ellipsis", slice_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000645 if (!Ellipsis_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000646 Slice_type = make_type("Slice", slice_type, Slice_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000647 if (!Slice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000648 ExtSlice_type = make_type("ExtSlice", slice_type, ExtSlice_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000649 if (!ExtSlice_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000650 Index_type = make_type("Index", slice_type, Index_fields, 1);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000651 if (!Index_type) return 0;
652 boolop_type = make_type("boolop", AST_type, NULL, 0);
653 if (!boolop_type) return 0;
654 if (!add_attributes(boolop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000655 And_type = make_type("And", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000656 if (!And_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000657 And_singleton = PyType_GenericNew(And_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000658 if (!And_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000659 Or_type = make_type("Or", boolop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000660 if (!Or_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000661 Or_singleton = PyType_GenericNew(Or_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000662 if (!Or_singleton) return 0;
663 operator_type = make_type("operator", AST_type, NULL, 0);
664 if (!operator_type) return 0;
665 if (!add_attributes(operator_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000666 Add_type = make_type("Add", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000667 if (!Add_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000668 Add_singleton = PyType_GenericNew(Add_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000669 if (!Add_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000670 Sub_type = make_type("Sub", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000671 if (!Sub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000672 Sub_singleton = PyType_GenericNew(Sub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000673 if (!Sub_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000674 Mult_type = make_type("Mult", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000675 if (!Mult_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000676 Mult_singleton = PyType_GenericNew(Mult_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000677 if (!Mult_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000678 Div_type = make_type("Div", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000679 if (!Div_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000680 Div_singleton = PyType_GenericNew(Div_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000681 if (!Div_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000682 Mod_type = make_type("Mod", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000683 if (!Mod_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000684 Mod_singleton = PyType_GenericNew(Mod_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000685 if (!Mod_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000686 Pow_type = make_type("Pow", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000687 if (!Pow_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000688 Pow_singleton = PyType_GenericNew(Pow_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000689 if (!Pow_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000690 LShift_type = make_type("LShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000691 if (!LShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000692 LShift_singleton = PyType_GenericNew(LShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000693 if (!LShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000694 RShift_type = make_type("RShift", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000695 if (!RShift_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000696 RShift_singleton = PyType_GenericNew(RShift_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000697 if (!RShift_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000698 BitOr_type = make_type("BitOr", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000699 if (!BitOr_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000700 BitOr_singleton = PyType_GenericNew(BitOr_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000701 if (!BitOr_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000702 BitXor_type = make_type("BitXor", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000703 if (!BitXor_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000704 BitXor_singleton = PyType_GenericNew(BitXor_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000705 if (!BitXor_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000706 BitAnd_type = make_type("BitAnd", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000707 if (!BitAnd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000708 BitAnd_singleton = PyType_GenericNew(BitAnd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000709 if (!BitAnd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000710 FloorDiv_type = make_type("FloorDiv", operator_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000711 if (!FloorDiv_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000712 FloorDiv_singleton = PyType_GenericNew(FloorDiv_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000713 if (!FloorDiv_singleton) return 0;
714 unaryop_type = make_type("unaryop", AST_type, NULL, 0);
715 if (!unaryop_type) return 0;
716 if (!add_attributes(unaryop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000717 Invert_type = make_type("Invert", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000718 if (!Invert_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000719 Invert_singleton = PyType_GenericNew(Invert_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000720 if (!Invert_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000721 Not_type = make_type("Not", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000722 if (!Not_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000723 Not_singleton = PyType_GenericNew(Not_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000724 if (!Not_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000725 UAdd_type = make_type("UAdd", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000726 if (!UAdd_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000727 UAdd_singleton = PyType_GenericNew(UAdd_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000728 if (!UAdd_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000729 USub_type = make_type("USub", unaryop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000730 if (!USub_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000731 USub_singleton = PyType_GenericNew(USub_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000732 if (!USub_singleton) return 0;
733 cmpop_type = make_type("cmpop", AST_type, NULL, 0);
734 if (!cmpop_type) return 0;
735 if (!add_attributes(cmpop_type, NULL, 0)) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000736 Eq_type = make_type("Eq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000737 if (!Eq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000738 Eq_singleton = PyType_GenericNew(Eq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000739 if (!Eq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000740 NotEq_type = make_type("NotEq", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000741 if (!NotEq_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000742 NotEq_singleton = PyType_GenericNew(NotEq_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000743 if (!NotEq_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000744 Lt_type = make_type("Lt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000745 if (!Lt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000746 Lt_singleton = PyType_GenericNew(Lt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000747 if (!Lt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000748 LtE_type = make_type("LtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000749 if (!LtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000750 LtE_singleton = PyType_GenericNew(LtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000751 if (!LtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000752 Gt_type = make_type("Gt", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000753 if (!Gt_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000754 Gt_singleton = PyType_GenericNew(Gt_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000755 if (!Gt_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000756 GtE_type = make_type("GtE", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000757 if (!GtE_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000758 GtE_singleton = PyType_GenericNew(GtE_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000759 if (!GtE_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000760 Is_type = make_type("Is", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000761 if (!Is_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000762 Is_singleton = PyType_GenericNew(Is_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000763 if (!Is_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000764 IsNot_type = make_type("IsNot", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000765 if (!IsNot_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000766 IsNot_singleton = PyType_GenericNew(IsNot_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000767 if (!IsNot_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000768 In_type = make_type("In", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000769 if (!In_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000770 In_singleton = PyType_GenericNew(In_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000771 if (!In_singleton) return 0;
Martin v. Löwis8d0701d2006-02-26 23:40:20 +0000772 NotIn_type = make_type("NotIn", cmpop_type, NULL, 0);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000773 if (!NotIn_type) return 0;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000774 NotIn_singleton = PyType_GenericNew(NotIn_type, NULL, NULL);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000775 if (!NotIn_singleton) return 0;
776 comprehension_type = make_type("comprehension", AST_type,
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000777 comprehension_fields, 3);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000778 if (!comprehension_type) return 0;
779 excepthandler_type = make_type("excepthandler", AST_type,
Jeremy Hylton2f327c12006-04-04 04:00:23 +0000780 excepthandler_fields, 5);
Martin v. Löwis577b5b92006-02-27 15:23:19 +0000781 if (!excepthandler_type) return 0;
782 arguments_type = make_type("arguments", AST_type, arguments_fields, 4);
783 if (!arguments_type) return 0;
784 keyword_type = make_type("keyword", AST_type, keyword_fields, 2);
785 if (!keyword_type) return 0;
786 alias_type = make_type("alias", AST_type, alias_fields, 2);
787 if (!alias_type) return 0;
788 initialized = 1;
789 return 1;
Martin v. Löwisbd260da2006-02-26 19:42:26 +0000790}
Neal Norwitz7b5a6042005-11-13 19:14:20 +0000791
Georg Brandlfc8eef32008-03-28 12:11:56 +0000792static int obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena);
793static int obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena);
794static int obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena);
795static int obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena*
796 arena);
797static int obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena);
798static int obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena);
799static int obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena);
800static int obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena);
801static int obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena);
802static int obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena*
803 arena);
804static int obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena*
805 arena);
806static int obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena);
807static int obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena);
808static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena);
809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000811Module(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812{
813 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000814 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000815 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817 p->kind = Module_kind;
818 p->v.Module.body = body;
819 return p;
820}
821
822mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000823Interactive(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824{
825 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000826 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000827 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000829 p->kind = Interactive_kind;
830 p->v.Interactive.body = body;
831 return p;
832}
833
834mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000835Expression(expr_ty body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836{
837 mod_ty p;
838 if (!body) {
839 PyErr_SetString(PyExc_ValueError,
840 "field body is required for Expression");
841 return NULL;
842 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000843 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000844 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 p->kind = Expression_kind;
847 p->v.Expression.body = body;
848 return p;
849}
850
851mod_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000852Suite(asdl_seq * body, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853{
854 mod_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000855 p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000856 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 p->kind = Suite_kind;
859 p->v.Suite.body = body;
860 return p;
861}
862
863stmt_ty
864FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
Christian Heimes5224d282008-02-23 15:01:05 +0000865 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866{
867 stmt_ty p;
868 if (!name) {
869 PyErr_SetString(PyExc_ValueError,
870 "field name is required for FunctionDef");
871 return NULL;
872 }
873 if (!args) {
874 PyErr_SetString(PyExc_ValueError,
875 "field args is required for FunctionDef");
876 return NULL;
877 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000878 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000879 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000880 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881 p->kind = FunctionDef_kind;
882 p->v.FunctionDef.name = name;
883 p->v.FunctionDef.args = args;
884 p->v.FunctionDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +0000885 p->v.FunctionDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000886 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000887 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888 return p;
889}
890
891stmt_ty
Christian Heimes5224d282008-02-23 15:01:05 +0000892ClassDef(identifier name, asdl_seq * bases, asdl_seq * body, asdl_seq *
893 decorator_list, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894{
895 stmt_ty p;
896 if (!name) {
897 PyErr_SetString(PyExc_ValueError,
898 "field name is required for ClassDef");
899 return NULL;
900 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000901 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000902 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000903 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000904 p->kind = ClassDef_kind;
905 p->v.ClassDef.name = name;
906 p->v.ClassDef.bases = bases;
907 p->v.ClassDef.body = body;
Christian Heimes5224d282008-02-23 15:01:05 +0000908 p->v.ClassDef.decorator_list = decorator_list;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000909 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000910 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 return p;
912}
913
914stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000915Return(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916{
917 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000918 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000919 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 p->kind = Return_kind;
922 p->v.Return.value = value;
923 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000924 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925 return p;
926}
927
928stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000929Delete(asdl_seq * targets, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930{
931 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000932 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000933 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935 p->kind = Delete_kind;
936 p->v.Delete.targets = targets;
937 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000938 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 return p;
940}
941
942stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000943Assign(asdl_seq * targets, expr_ty value, int lineno, int col_offset, PyArena
944 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945{
946 stmt_ty p;
947 if (!value) {
948 PyErr_SetString(PyExc_ValueError,
949 "field value is required for Assign");
950 return NULL;
951 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000952 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000953 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955 p->kind = Assign_kind;
956 p->v.Assign.targets = targets;
957 p->v.Assign.value = value;
958 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000959 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960 return p;
961}
962
963stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000964AugAssign(expr_ty target, operator_ty op, expr_ty value, int lineno, int
965 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966{
967 stmt_ty p;
968 if (!target) {
969 PyErr_SetString(PyExc_ValueError,
970 "field target is required for AugAssign");
971 return NULL;
972 }
973 if (!op) {
974 PyErr_SetString(PyExc_ValueError,
975 "field op is required for AugAssign");
976 return NULL;
977 }
978 if (!value) {
979 PyErr_SetString(PyExc_ValueError,
980 "field value is required for AugAssign");
981 return NULL;
982 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000983 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +0000984 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986 p->kind = AugAssign_kind;
987 p->v.AugAssign.target = target;
988 p->v.AugAssign.op = op;
989 p->v.AugAssign.value = value;
990 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000991 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 return p;
993}
994
995stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +0000996Print(expr_ty dest, asdl_seq * values, bool nl, int lineno, int col_offset,
997 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998{
999 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001000 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001001 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 p->kind = Print_kind;
1004 p->v.Print.dest = dest;
1005 p->v.Print.values = values;
1006 p->v.Print.nl = nl;
1007 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001008 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 return p;
1010}
1011
1012stmt_ty
1013For(expr_ty target, expr_ty iter, asdl_seq * body, asdl_seq * orelse, int
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001014 lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015{
1016 stmt_ty p;
1017 if (!target) {
1018 PyErr_SetString(PyExc_ValueError,
1019 "field target is required for For");
1020 return NULL;
1021 }
1022 if (!iter) {
1023 PyErr_SetString(PyExc_ValueError,
1024 "field iter is required for For");
1025 return NULL;
1026 }
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 = For_kind;
1031 p->v.For.target = target;
1032 p->v.For.iter = iter;
1033 p->v.For.body = body;
1034 p->v.For.orelse = orelse;
1035 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001036 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001037 return p;
1038}
1039
1040stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001041While(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1042 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043{
1044 stmt_ty p;
1045 if (!test) {
1046 PyErr_SetString(PyExc_ValueError,
1047 "field test is required for While");
1048 return NULL;
1049 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001050 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001051 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053 p->kind = While_kind;
1054 p->v.While.test = test;
1055 p->v.While.body = body;
1056 p->v.While.orelse = orelse;
1057 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001058 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 return p;
1060}
1061
1062stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001063If(expr_ty test, asdl_seq * body, asdl_seq * orelse, int lineno, int
1064 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065{
1066 stmt_ty p;
1067 if (!test) {
1068 PyErr_SetString(PyExc_ValueError,
1069 "field test is required for If");
1070 return NULL;
1071 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001072 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001073 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 p->kind = If_kind;
1076 p->v.If.test = test;
1077 p->v.If.body = body;
1078 p->v.If.orelse = orelse;
1079 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001080 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 return p;
1082}
1083
1084stmt_ty
Guido van Rossumc2e20742006-02-27 22:32:47 +00001085With(expr_ty context_expr, expr_ty optional_vars, asdl_seq * body, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001086 int col_offset, PyArena *arena)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001087{
1088 stmt_ty p;
1089 if (!context_expr) {
1090 PyErr_SetString(PyExc_ValueError,
1091 "field context_expr is required for With");
1092 return NULL;
1093 }
1094 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001095 if (!p)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001096 return NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001097 p->kind = With_kind;
1098 p->v.With.context_expr = context_expr;
1099 p->v.With.optional_vars = optional_vars;
1100 p->v.With.body = body;
1101 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001102 p->col_offset = col_offset;
Guido van Rossumc2e20742006-02-27 22:32:47 +00001103 return p;
1104}
1105
1106stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001107Raise(expr_ty type, expr_ty inst, expr_ty tback, int lineno, int col_offset,
1108 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109{
1110 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001111 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001112 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 p->kind = Raise_kind;
1115 p->v.Raise.type = type;
1116 p->v.Raise.inst = inst;
1117 p->v.Raise.tback = tback;
1118 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001119 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return p;
1121}
1122
1123stmt_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001124TryExcept(asdl_seq * body, asdl_seq * handlers, asdl_seq * orelse, int lineno,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001125 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126{
1127 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001128 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001129 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131 p->kind = TryExcept_kind;
1132 p->v.TryExcept.body = body;
1133 p->v.TryExcept.handlers = handlers;
1134 p->v.TryExcept.orelse = orelse;
1135 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001136 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 return p;
1138}
1139
1140stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001141TryFinally(asdl_seq * body, asdl_seq * finalbody, int lineno, int col_offset,
1142 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143{
1144 stmt_ty p;
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 = TryFinally_kind;
1149 p->v.TryFinally.body = body;
1150 p->v.TryFinally.finalbody = finalbody;
1151 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001152 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 return p;
1154}
1155
1156stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001157Assert(expr_ty test, expr_ty msg, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158{
1159 stmt_ty p;
1160 if (!test) {
1161 PyErr_SetString(PyExc_ValueError,
1162 "field test is required for Assert");
1163 return NULL;
1164 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001165 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001166 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 p->kind = Assert_kind;
1169 p->v.Assert.test = test;
1170 p->v.Assert.msg = msg;
1171 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001172 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return p;
1174}
1175
1176stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001177Import(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178{
1179 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001180 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001181 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183 p->kind = Import_kind;
1184 p->v.Import.names = names;
1185 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001186 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return p;
1188}
1189
1190stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001191ImportFrom(identifier module, asdl_seq * names, int level, int lineno, int
1192 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193{
1194 stmt_ty p;
1195 if (!module) {
1196 PyErr_SetString(PyExc_ValueError,
1197 "field module is required for ImportFrom");
1198 return NULL;
1199 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001200 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001201 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 p->kind = ImportFrom_kind;
1204 p->v.ImportFrom.module = module;
1205 p->v.ImportFrom.names = names;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001206 p->v.ImportFrom.level = level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001208 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return p;
1210}
1211
1212stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001213Exec(expr_ty body, expr_ty globals, expr_ty locals, int lineno, int col_offset,
1214 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215{
1216 stmt_ty p;
1217 if (!body) {
1218 PyErr_SetString(PyExc_ValueError,
1219 "field body is required for Exec");
1220 return NULL;
1221 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001222 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001223 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 p->kind = Exec_kind;
1226 p->v.Exec.body = body;
1227 p->v.Exec.globals = globals;
1228 p->v.Exec.locals = locals;
1229 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001230 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 return p;
1232}
1233
1234stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001235Global(asdl_seq * names, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236{
1237 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001238 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001239 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241 p->kind = Global_kind;
1242 p->v.Global.names = names;
1243 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001244 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 return p;
1246}
1247
1248stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001249Expr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250{
1251 stmt_ty p;
1252 if (!value) {
1253 PyErr_SetString(PyExc_ValueError,
1254 "field value is required for Expr");
1255 return NULL;
1256 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001257 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001258 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 p->kind = Expr_kind;
1261 p->v.Expr.value = value;
1262 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001263 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return p;
1265}
1266
1267stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001268Pass(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269{
1270 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001271 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001272 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 p->kind = Pass_kind;
1275 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001276 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 return p;
1278}
1279
1280stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001281Break(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282{
1283 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001284 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001285 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 p->kind = Break_kind;
1288 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001289 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001290 return p;
1291}
1292
1293stmt_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001294Continue(int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295{
1296 stmt_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001297 p = (stmt_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001298 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 p->kind = Continue_kind;
1301 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001302 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303 return p;
1304}
1305
1306expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001307BoolOp(boolop_ty op, asdl_seq * values, int lineno, int col_offset, PyArena
1308 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309{
1310 expr_ty p;
1311 if (!op) {
1312 PyErr_SetString(PyExc_ValueError,
1313 "field op is required for BoolOp");
1314 return NULL;
1315 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001316 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001317 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319 p->kind = BoolOp_kind;
1320 p->v.BoolOp.op = op;
1321 p->v.BoolOp.values = values;
1322 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001323 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001324 return p;
1325}
1326
1327expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001328BinOp(expr_ty left, operator_ty op, expr_ty right, int lineno, int col_offset,
1329 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330{
1331 expr_ty p;
1332 if (!left) {
1333 PyErr_SetString(PyExc_ValueError,
1334 "field left is required for BinOp");
1335 return NULL;
1336 }
1337 if (!op) {
1338 PyErr_SetString(PyExc_ValueError,
1339 "field op is required for BinOp");
1340 return NULL;
1341 }
1342 if (!right) {
1343 PyErr_SetString(PyExc_ValueError,
1344 "field right is required for BinOp");
1345 return NULL;
1346 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001347 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001348 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001350 p->kind = BinOp_kind;
1351 p->v.BinOp.left = left;
1352 p->v.BinOp.op = op;
1353 p->v.BinOp.right = right;
1354 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001355 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 return p;
1357}
1358
1359expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001360UnaryOp(unaryop_ty op, expr_ty operand, int lineno, int col_offset, PyArena
1361 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362{
1363 expr_ty p;
1364 if (!op) {
1365 PyErr_SetString(PyExc_ValueError,
1366 "field op is required for UnaryOp");
1367 return NULL;
1368 }
1369 if (!operand) {
1370 PyErr_SetString(PyExc_ValueError,
1371 "field operand is required for UnaryOp");
1372 return NULL;
1373 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001374 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001375 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 p->kind = UnaryOp_kind;
1378 p->v.UnaryOp.op = op;
1379 p->v.UnaryOp.operand = operand;
1380 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001381 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001382 return p;
1383}
1384
1385expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001386Lambda(arguments_ty args, expr_ty body, int lineno, int col_offset, PyArena
1387 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388{
1389 expr_ty p;
1390 if (!args) {
1391 PyErr_SetString(PyExc_ValueError,
1392 "field args is required for Lambda");
1393 return NULL;
1394 }
1395 if (!body) {
1396 PyErr_SetString(PyExc_ValueError,
1397 "field body is required for Lambda");
1398 return NULL;
1399 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001400 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001401 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403 p->kind = Lambda_kind;
1404 p->v.Lambda.args = args;
1405 p->v.Lambda.body = body;
1406 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001407 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 return p;
1409}
1410
1411expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001412IfExp(expr_ty test, expr_ty body, expr_ty orelse, int lineno, int col_offset,
1413 PyArena *arena)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001414{
1415 expr_ty p;
1416 if (!test) {
1417 PyErr_SetString(PyExc_ValueError,
1418 "field test is required for IfExp");
1419 return NULL;
1420 }
1421 if (!body) {
1422 PyErr_SetString(PyExc_ValueError,
1423 "field body is required for IfExp");
1424 return NULL;
1425 }
1426 if (!orelse) {
1427 PyErr_SetString(PyExc_ValueError,
1428 "field orelse is required for IfExp");
1429 return NULL;
1430 }
1431 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001432 if (!p)
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001433 return NULL;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001434 p->kind = IfExp_kind;
1435 p->v.IfExp.test = test;
1436 p->v.IfExp.body = body;
1437 p->v.IfExp.orelse = orelse;
1438 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001439 p->col_offset = col_offset;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001440 return p;
1441}
1442
1443expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001444Dict(asdl_seq * keys, asdl_seq * values, int lineno, int col_offset, PyArena
1445 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446{
1447 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001448 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001449 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 p->kind = Dict_kind;
1452 p->v.Dict.keys = keys;
1453 p->v.Dict.values = values;
1454 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001455 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 return p;
1457}
1458
1459expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001460ListComp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1461 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462{
1463 expr_ty p;
1464 if (!elt) {
1465 PyErr_SetString(PyExc_ValueError,
1466 "field elt is required for ListComp");
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 = ListComp_kind;
1473 p->v.ListComp.elt = elt;
1474 p->v.ListComp.generators = generators;
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 +00001481GeneratorExp(expr_ty elt, asdl_seq * generators, int lineno, int col_offset,
1482 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483{
1484 expr_ty p;
1485 if (!elt) {
1486 PyErr_SetString(PyExc_ValueError,
1487 "field elt is required for GeneratorExp");
1488 return NULL;
1489 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001490 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001491 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001492 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 p->kind = GeneratorExp_kind;
1494 p->v.GeneratorExp.elt = elt;
1495 p->v.GeneratorExp.generators = generators;
1496 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001497 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498 return p;
1499}
1500
1501expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001502Yield(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503{
1504 expr_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001505 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001506 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 p->kind = Yield_kind;
1509 p->v.Yield.value = value;
1510 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001511 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 return p;
1513}
1514
1515expr_ty
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00001516Compare(expr_ty left, asdl_int_seq * ops, asdl_seq * comparators, int lineno,
1517 int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518{
1519 expr_ty p;
1520 if (!left) {
1521 PyErr_SetString(PyExc_ValueError,
1522 "field left is required for Compare");
1523 return NULL;
1524 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001525 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001526 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528 p->kind = Compare_kind;
1529 p->v.Compare.left = left;
1530 p->v.Compare.ops = ops;
1531 p->v.Compare.comparators = comparators;
1532 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001533 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 return p;
1535}
1536
1537expr_ty
1538Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001539 expr_ty kwargs, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540{
1541 expr_ty p;
1542 if (!func) {
1543 PyErr_SetString(PyExc_ValueError,
1544 "field func is required for Call");
1545 return NULL;
1546 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001547 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001548 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001549 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550 p->kind = Call_kind;
1551 p->v.Call.func = func;
1552 p->v.Call.args = args;
1553 p->v.Call.keywords = keywords;
1554 p->v.Call.starargs = starargs;
1555 p->v.Call.kwargs = kwargs;
1556 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001557 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558 return p;
1559}
1560
1561expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001562Repr(expr_ty value, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563{
1564 expr_ty p;
1565 if (!value) {
1566 PyErr_SetString(PyExc_ValueError,
1567 "field value is required for Repr");
1568 return NULL;
1569 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001570 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001571 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001573 p->kind = Repr_kind;
1574 p->v.Repr.value = value;
1575 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001576 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001577 return p;
1578}
1579
1580expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001581Num(object n, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582{
1583 expr_ty p;
1584 if (!n) {
1585 PyErr_SetString(PyExc_ValueError,
1586 "field n is required for Num");
1587 return NULL;
1588 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001589 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001590 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592 p->kind = Num_kind;
1593 p->v.Num.n = n;
1594 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001595 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 return p;
1597}
1598
1599expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001600Str(string s, int lineno, int col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601{
1602 expr_ty p;
1603 if (!s) {
1604 PyErr_SetString(PyExc_ValueError,
1605 "field s is required for Str");
1606 return NULL;
1607 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001608 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001609 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001610 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 p->kind = Str_kind;
1612 p->v.Str.s = s;
1613 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001614 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 return p;
1616}
1617
1618expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001619Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
1620 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621{
1622 expr_ty p;
1623 if (!value) {
1624 PyErr_SetString(PyExc_ValueError,
1625 "field value is required for Attribute");
1626 return NULL;
1627 }
1628 if (!attr) {
1629 PyErr_SetString(PyExc_ValueError,
1630 "field attr is required for Attribute");
1631 return NULL;
1632 }
1633 if (!ctx) {
1634 PyErr_SetString(PyExc_ValueError,
1635 "field ctx is required for Attribute");
1636 return NULL;
1637 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001638 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001639 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 p->kind = Attribute_kind;
1642 p->v.Attribute.value = value;
1643 p->v.Attribute.attr = attr;
1644 p->v.Attribute.ctx = ctx;
1645 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001646 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return p;
1648}
1649
1650expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001651Subscript(expr_ty value, slice_ty slice, expr_context_ty ctx, int lineno, int
1652 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653{
1654 expr_ty p;
1655 if (!value) {
1656 PyErr_SetString(PyExc_ValueError,
1657 "field value is required for Subscript");
1658 return NULL;
1659 }
1660 if (!slice) {
1661 PyErr_SetString(PyExc_ValueError,
1662 "field slice is required for Subscript");
1663 return NULL;
1664 }
1665 if (!ctx) {
1666 PyErr_SetString(PyExc_ValueError,
1667 "field ctx is required for Subscript");
1668 return NULL;
1669 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001670 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001671 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 p->kind = Subscript_kind;
1674 p->v.Subscript.value = value;
1675 p->v.Subscript.slice = slice;
1676 p->v.Subscript.ctx = ctx;
1677 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001678 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 return p;
1680}
1681
1682expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001683Name(identifier id, expr_context_ty ctx, int lineno, int col_offset, PyArena
1684 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685{
1686 expr_ty p;
1687 if (!id) {
1688 PyErr_SetString(PyExc_ValueError,
1689 "field id is required for Name");
1690 return NULL;
1691 }
1692 if (!ctx) {
1693 PyErr_SetString(PyExc_ValueError,
1694 "field ctx is required for Name");
1695 return NULL;
1696 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001697 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001698 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 p->kind = Name_kind;
1701 p->v.Name.id = id;
1702 p->v.Name.ctx = ctx;
1703 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001704 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705 return p;
1706}
1707
1708expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001709List(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1710 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711{
1712 expr_ty p;
1713 if (!ctx) {
1714 PyErr_SetString(PyExc_ValueError,
1715 "field ctx is required for List");
1716 return NULL;
1717 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001718 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001719 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721 p->kind = List_kind;
1722 p->v.List.elts = elts;
1723 p->v.List.ctx = ctx;
1724 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001725 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 return p;
1727}
1728
1729expr_ty
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001730Tuple(asdl_seq * elts, expr_context_ty ctx, int lineno, int col_offset, PyArena
1731 *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732{
1733 expr_ty p;
1734 if (!ctx) {
1735 PyErr_SetString(PyExc_ValueError,
1736 "field ctx is required for Tuple");
1737 return NULL;
1738 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001739 p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001740 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742 p->kind = Tuple_kind;
1743 p->v.Tuple.elts = elts;
1744 p->v.Tuple.ctx = ctx;
1745 p->lineno = lineno;
Martin v. Löwis49c5da12006-03-01 22:49:05 +00001746 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 return p;
1748}
1749
1750slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001751Ellipsis(PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752{
1753 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001754 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001755 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757 p->kind = Ellipsis_kind;
1758 return p;
1759}
1760
1761slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001762Slice(expr_ty lower, expr_ty upper, expr_ty step, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763{
1764 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001765 p = (slice_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 = Slice_kind;
1769 p->v.Slice.lower = lower;
1770 p->v.Slice.upper = upper;
1771 p->v.Slice.step = step;
1772 return p;
1773}
1774
1775slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001776ExtSlice(asdl_seq * dims, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777{
1778 slice_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001779 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001780 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 p->kind = ExtSlice_kind;
1783 p->v.ExtSlice.dims = dims;
1784 return p;
1785}
1786
1787slice_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001788Index(expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789{
1790 slice_ty p;
1791 if (!value) {
1792 PyErr_SetString(PyExc_ValueError,
1793 "field value is required for Index");
1794 return NULL;
1795 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001796 p = (slice_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001797 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 p->kind = Index_kind;
1800 p->v.Index.value = value;
1801 return p;
1802}
1803
1804comprehension_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001805comprehension(expr_ty target, expr_ty iter, asdl_seq * ifs, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806{
1807 comprehension_ty p;
1808 if (!target) {
1809 PyErr_SetString(PyExc_ValueError,
1810 "field target is required for comprehension");
1811 return NULL;
1812 }
1813 if (!iter) {
1814 PyErr_SetString(PyExc_ValueError,
1815 "field iter is required for comprehension");
1816 return NULL;
1817 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001818 p = (comprehension_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001819 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 p->target = target;
1822 p->iter = iter;
1823 p->ifs = ifs;
1824 return p;
1825}
1826
1827excepthandler_ty
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001828excepthandler(expr_ty type, expr_ty name, asdl_seq * body, int lineno, int
1829 col_offset, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830{
1831 excepthandler_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001832 p = (excepthandler_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001833 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001835 p->type = type;
1836 p->name = name;
1837 p->body = body;
Jeremy Hylton2f327c12006-04-04 04:00:23 +00001838 p->lineno = lineno;
1839 p->col_offset = col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 return p;
1841}
1842
1843arguments_ty
1844arguments(asdl_seq * args, identifier vararg, identifier kwarg, asdl_seq *
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001845 defaults, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846{
1847 arguments_ty p;
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001848 p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001849 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 p->args = args;
1852 p->vararg = vararg;
1853 p->kwarg = kwarg;
1854 p->defaults = defaults;
1855 return p;
1856}
1857
1858keyword_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001859keyword(identifier arg, expr_ty value, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860{
1861 keyword_ty p;
1862 if (!arg) {
1863 PyErr_SetString(PyExc_ValueError,
1864 "field arg is required for keyword");
1865 return NULL;
1866 }
1867 if (!value) {
1868 PyErr_SetString(PyExc_ValueError,
1869 "field value is required for keyword");
1870 return NULL;
1871 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001872 p = (keyword_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001873 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 p->arg = arg;
1876 p->value = value;
1877 return p;
1878}
1879
1880alias_ty
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001881alias(identifier name, identifier asname, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882{
1883 alias_ty p;
1884 if (!name) {
1885 PyErr_SetString(PyExc_ValueError,
1886 "field name is required for alias");
1887 return NULL;
1888 }
Neal Norwitzadb69fc2005-12-17 20:54:49 +00001889 p = (alias_ty)PyArena_Malloc(arena, sizeof(*p));
Neal Norwitz7b7d1c82007-02-26 18:10:47 +00001890 if (!p)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892 p->name = name;
1893 p->asname = asname;
1894 return p;
1895}
1896
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001897
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001898PyObject*
1899ast2obj_mod(void* _o)
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001900{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001901 mod_ty o = (mod_ty)_o;
1902 PyObject *result = NULL, *value = NULL;
1903 if (!o) {
1904 Py_INCREF(Py_None);
1905 return Py_None;
1906 }
Neal Norwitz7b5a6042005-11-13 19:14:20 +00001907
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 switch (o->kind) {
1909 case Module_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001910 result = PyType_GenericNew(Module_type, NULL, NULL);
1911 if (!result) goto failed;
1912 value = ast2obj_list(o->v.Module.body, ast2obj_stmt);
1913 if (!value) goto failed;
1914 if (PyObject_SetAttrString(result, "body", value) == -1)
1915 goto failed;
1916 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 break;
1918 case Interactive_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001919 result = PyType_GenericNew(Interactive_type, NULL, NULL);
1920 if (!result) goto failed;
1921 value = ast2obj_list(o->v.Interactive.body, ast2obj_stmt);
1922 if (!value) goto failed;
1923 if (PyObject_SetAttrString(result, "body", value) == -1)
1924 goto failed;
1925 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001926 break;
1927 case Expression_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001928 result = PyType_GenericNew(Expression_type, NULL, NULL);
1929 if (!result) goto failed;
1930 value = ast2obj_expr(o->v.Expression.body);
1931 if (!value) goto failed;
1932 if (PyObject_SetAttrString(result, "body", value) == -1)
1933 goto failed;
1934 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 break;
1936 case Suite_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001937 result = PyType_GenericNew(Suite_type, NULL, NULL);
1938 if (!result) goto failed;
1939 value = ast2obj_list(o->v.Suite.body, ast2obj_stmt);
1940 if (!value) goto failed;
1941 if (PyObject_SetAttrString(result, "body", value) == -1)
1942 goto failed;
1943 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 break;
1945 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001946 return result;
1947failed:
1948 Py_XDECREF(value);
1949 Py_XDECREF(result);
1950 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951}
1952
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001953PyObject*
1954ast2obj_stmt(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001955{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001956 stmt_ty o = (stmt_ty)_o;
1957 PyObject *result = NULL, *value = NULL;
1958 if (!o) {
1959 Py_INCREF(Py_None);
1960 return Py_None;
1961 }
1962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 switch (o->kind) {
1964 case FunctionDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001965 result = PyType_GenericNew(FunctionDef_type, NULL, NULL);
1966 if (!result) goto failed;
1967 value = ast2obj_identifier(o->v.FunctionDef.name);
1968 if (!value) goto failed;
1969 if (PyObject_SetAttrString(result, "name", value) == -1)
1970 goto failed;
1971 Py_DECREF(value);
1972 value = ast2obj_arguments(o->v.FunctionDef.args);
1973 if (!value) goto failed;
1974 if (PyObject_SetAttrString(result, "args", value) == -1)
1975 goto failed;
1976 Py_DECREF(value);
1977 value = ast2obj_list(o->v.FunctionDef.body, ast2obj_stmt);
1978 if (!value) goto failed;
1979 if (PyObject_SetAttrString(result, "body", value) == -1)
1980 goto failed;
1981 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00001982 value = ast2obj_list(o->v.FunctionDef.decorator_list,
1983 ast2obj_expr);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001984 if (!value) goto failed;
Christian Heimes5224d282008-02-23 15:01:05 +00001985 if (PyObject_SetAttrString(result, "decorator_list", value) ==
1986 -1)
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001987 goto failed;
1988 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 break;
1990 case ClassDef_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00001991 result = PyType_GenericNew(ClassDef_type, NULL, NULL);
1992 if (!result) goto failed;
1993 value = ast2obj_identifier(o->v.ClassDef.name);
1994 if (!value) goto failed;
1995 if (PyObject_SetAttrString(result, "name", value) == -1)
1996 goto failed;
1997 Py_DECREF(value);
1998 value = ast2obj_list(o->v.ClassDef.bases, ast2obj_expr);
1999 if (!value) goto failed;
2000 if (PyObject_SetAttrString(result, "bases", value) == -1)
2001 goto failed;
2002 Py_DECREF(value);
2003 value = ast2obj_list(o->v.ClassDef.body, ast2obj_stmt);
2004 if (!value) goto failed;
2005 if (PyObject_SetAttrString(result, "body", value) == -1)
2006 goto failed;
2007 Py_DECREF(value);
Christian Heimes5224d282008-02-23 15:01:05 +00002008 value = ast2obj_list(o->v.ClassDef.decorator_list,
2009 ast2obj_expr);
2010 if (!value) goto failed;
2011 if (PyObject_SetAttrString(result, "decorator_list", value) ==
2012 -1)
2013 goto failed;
2014 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 break;
2016 case Return_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002017 result = PyType_GenericNew(Return_type, NULL, NULL);
2018 if (!result) goto failed;
2019 value = ast2obj_expr(o->v.Return.value);
2020 if (!value) goto failed;
2021 if (PyObject_SetAttrString(result, "value", value) == -1)
2022 goto failed;
2023 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 break;
2025 case Delete_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002026 result = PyType_GenericNew(Delete_type, NULL, NULL);
2027 if (!result) goto failed;
2028 value = ast2obj_list(o->v.Delete.targets, ast2obj_expr);
2029 if (!value) goto failed;
2030 if (PyObject_SetAttrString(result, "targets", value) == -1)
2031 goto failed;
2032 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 break;
2034 case Assign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002035 result = PyType_GenericNew(Assign_type, NULL, NULL);
2036 if (!result) goto failed;
2037 value = ast2obj_list(o->v.Assign.targets, ast2obj_expr);
2038 if (!value) goto failed;
2039 if (PyObject_SetAttrString(result, "targets", value) == -1)
2040 goto failed;
2041 Py_DECREF(value);
2042 value = ast2obj_expr(o->v.Assign.value);
2043 if (!value) goto failed;
2044 if (PyObject_SetAttrString(result, "value", value) == -1)
2045 goto failed;
2046 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 break;
2048 case AugAssign_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002049 result = PyType_GenericNew(AugAssign_type, NULL, NULL);
2050 if (!result) goto failed;
2051 value = ast2obj_expr(o->v.AugAssign.target);
2052 if (!value) goto failed;
2053 if (PyObject_SetAttrString(result, "target", value) == -1)
2054 goto failed;
2055 Py_DECREF(value);
2056 value = ast2obj_operator(o->v.AugAssign.op);
2057 if (!value) goto failed;
2058 if (PyObject_SetAttrString(result, "op", value) == -1)
2059 goto failed;
2060 Py_DECREF(value);
2061 value = ast2obj_expr(o->v.AugAssign.value);
2062 if (!value) goto failed;
2063 if (PyObject_SetAttrString(result, "value", value) == -1)
2064 goto failed;
2065 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 break;
2067 case Print_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002068 result = PyType_GenericNew(Print_type, NULL, NULL);
2069 if (!result) goto failed;
2070 value = ast2obj_expr(o->v.Print.dest);
2071 if (!value) goto failed;
2072 if (PyObject_SetAttrString(result, "dest", value) == -1)
2073 goto failed;
2074 Py_DECREF(value);
2075 value = ast2obj_list(o->v.Print.values, ast2obj_expr);
2076 if (!value) goto failed;
2077 if (PyObject_SetAttrString(result, "values", value) == -1)
2078 goto failed;
2079 Py_DECREF(value);
2080 value = ast2obj_bool(o->v.Print.nl);
2081 if (!value) goto failed;
2082 if (PyObject_SetAttrString(result, "nl", value) == -1)
2083 goto failed;
2084 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 break;
2086 case For_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002087 result = PyType_GenericNew(For_type, NULL, NULL);
2088 if (!result) goto failed;
2089 value = ast2obj_expr(o->v.For.target);
2090 if (!value) goto failed;
2091 if (PyObject_SetAttrString(result, "target", value) == -1)
2092 goto failed;
2093 Py_DECREF(value);
2094 value = ast2obj_expr(o->v.For.iter);
2095 if (!value) goto failed;
2096 if (PyObject_SetAttrString(result, "iter", value) == -1)
2097 goto failed;
2098 Py_DECREF(value);
2099 value = ast2obj_list(o->v.For.body, ast2obj_stmt);
2100 if (!value) goto failed;
2101 if (PyObject_SetAttrString(result, "body", value) == -1)
2102 goto failed;
2103 Py_DECREF(value);
2104 value = ast2obj_list(o->v.For.orelse, ast2obj_stmt);
2105 if (!value) goto failed;
2106 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2107 goto failed;
2108 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 break;
2110 case While_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002111 result = PyType_GenericNew(While_type, NULL, NULL);
2112 if (!result) goto failed;
2113 value = ast2obj_expr(o->v.While.test);
2114 if (!value) goto failed;
2115 if (PyObject_SetAttrString(result, "test", value) == -1)
2116 goto failed;
2117 Py_DECREF(value);
2118 value = ast2obj_list(o->v.While.body, ast2obj_stmt);
2119 if (!value) goto failed;
2120 if (PyObject_SetAttrString(result, "body", value) == -1)
2121 goto failed;
2122 Py_DECREF(value);
2123 value = ast2obj_list(o->v.While.orelse, ast2obj_stmt);
2124 if (!value) goto failed;
2125 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2126 goto failed;
2127 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 break;
2129 case If_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002130 result = PyType_GenericNew(If_type, NULL, NULL);
2131 if (!result) goto failed;
2132 value = ast2obj_expr(o->v.If.test);
2133 if (!value) goto failed;
2134 if (PyObject_SetAttrString(result, "test", value) == -1)
2135 goto failed;
2136 Py_DECREF(value);
2137 value = ast2obj_list(o->v.If.body, ast2obj_stmt);
2138 if (!value) goto failed;
2139 if (PyObject_SetAttrString(result, "body", value) == -1)
2140 goto failed;
2141 Py_DECREF(value);
2142 value = ast2obj_list(o->v.If.orelse, ast2obj_stmt);
2143 if (!value) goto failed;
2144 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2145 goto failed;
2146 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 break;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002148 case With_kind:
2149 result = PyType_GenericNew(With_type, NULL, NULL);
2150 if (!result) goto failed;
2151 value = ast2obj_expr(o->v.With.context_expr);
2152 if (!value) goto failed;
2153 if (PyObject_SetAttrString(result, "context_expr", value) == -1)
2154 goto failed;
2155 Py_DECREF(value);
2156 value = ast2obj_expr(o->v.With.optional_vars);
2157 if (!value) goto failed;
2158 if (PyObject_SetAttrString(result, "optional_vars", value) ==
2159 -1)
2160 goto failed;
2161 Py_DECREF(value);
2162 value = ast2obj_list(o->v.With.body, ast2obj_stmt);
2163 if (!value) goto failed;
2164 if (PyObject_SetAttrString(result, "body", value) == -1)
2165 goto failed;
2166 Py_DECREF(value);
2167 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 case Raise_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002169 result = PyType_GenericNew(Raise_type, NULL, NULL);
2170 if (!result) goto failed;
2171 value = ast2obj_expr(o->v.Raise.type);
2172 if (!value) goto failed;
2173 if (PyObject_SetAttrString(result, "type", value) == -1)
2174 goto failed;
2175 Py_DECREF(value);
2176 value = ast2obj_expr(o->v.Raise.inst);
2177 if (!value) goto failed;
2178 if (PyObject_SetAttrString(result, "inst", value) == -1)
2179 goto failed;
2180 Py_DECREF(value);
2181 value = ast2obj_expr(o->v.Raise.tback);
2182 if (!value) goto failed;
2183 if (PyObject_SetAttrString(result, "tback", value) == -1)
2184 goto failed;
2185 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 break;
2187 case TryExcept_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002188 result = PyType_GenericNew(TryExcept_type, NULL, NULL);
2189 if (!result) goto failed;
2190 value = ast2obj_list(o->v.TryExcept.body, ast2obj_stmt);
2191 if (!value) goto failed;
2192 if (PyObject_SetAttrString(result, "body", value) == -1)
2193 goto failed;
2194 Py_DECREF(value);
2195 value = ast2obj_list(o->v.TryExcept.handlers,
2196 ast2obj_excepthandler);
2197 if (!value) goto failed;
2198 if (PyObject_SetAttrString(result, "handlers", value) == -1)
2199 goto failed;
2200 Py_DECREF(value);
2201 value = ast2obj_list(o->v.TryExcept.orelse, ast2obj_stmt);
2202 if (!value) goto failed;
2203 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2204 goto failed;
2205 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 break;
2207 case TryFinally_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002208 result = PyType_GenericNew(TryFinally_type, NULL, NULL);
2209 if (!result) goto failed;
2210 value = ast2obj_list(o->v.TryFinally.body, ast2obj_stmt);
2211 if (!value) goto failed;
2212 if (PyObject_SetAttrString(result, "body", value) == -1)
2213 goto failed;
2214 Py_DECREF(value);
2215 value = ast2obj_list(o->v.TryFinally.finalbody, ast2obj_stmt);
2216 if (!value) goto failed;
2217 if (PyObject_SetAttrString(result, "finalbody", value) == -1)
2218 goto failed;
2219 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 break;
2221 case Assert_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002222 result = PyType_GenericNew(Assert_type, NULL, NULL);
2223 if (!result) goto failed;
2224 value = ast2obj_expr(o->v.Assert.test);
2225 if (!value) goto failed;
2226 if (PyObject_SetAttrString(result, "test", value) == -1)
2227 goto failed;
2228 Py_DECREF(value);
2229 value = ast2obj_expr(o->v.Assert.msg);
2230 if (!value) goto failed;
2231 if (PyObject_SetAttrString(result, "msg", value) == -1)
2232 goto failed;
2233 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 break;
2235 case Import_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002236 result = PyType_GenericNew(Import_type, NULL, NULL);
2237 if (!result) goto failed;
2238 value = ast2obj_list(o->v.Import.names, ast2obj_alias);
2239 if (!value) goto failed;
2240 if (PyObject_SetAttrString(result, "names", value) == -1)
2241 goto failed;
2242 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 break;
2244 case ImportFrom_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002245 result = PyType_GenericNew(ImportFrom_type, NULL, NULL);
2246 if (!result) goto failed;
2247 value = ast2obj_identifier(o->v.ImportFrom.module);
2248 if (!value) goto failed;
2249 if (PyObject_SetAttrString(result, "module", value) == -1)
2250 goto failed;
2251 Py_DECREF(value);
2252 value = ast2obj_list(o->v.ImportFrom.names, ast2obj_alias);
2253 if (!value) goto failed;
2254 if (PyObject_SetAttrString(result, "names", value) == -1)
2255 goto failed;
2256 Py_DECREF(value);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002257 value = ast2obj_int(o->v.ImportFrom.level);
2258 if (!value) goto failed;
2259 if (PyObject_SetAttrString(result, "level", value) == -1)
2260 goto failed;
2261 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 break;
2263 case Exec_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002264 result = PyType_GenericNew(Exec_type, NULL, NULL);
2265 if (!result) goto failed;
2266 value = ast2obj_expr(o->v.Exec.body);
2267 if (!value) goto failed;
2268 if (PyObject_SetAttrString(result, "body", value) == -1)
2269 goto failed;
2270 Py_DECREF(value);
2271 value = ast2obj_expr(o->v.Exec.globals);
2272 if (!value) goto failed;
2273 if (PyObject_SetAttrString(result, "globals", value) == -1)
2274 goto failed;
2275 Py_DECREF(value);
2276 value = ast2obj_expr(o->v.Exec.locals);
2277 if (!value) goto failed;
2278 if (PyObject_SetAttrString(result, "locals", value) == -1)
2279 goto failed;
2280 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 break;
2282 case Global_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002283 result = PyType_GenericNew(Global_type, NULL, NULL);
2284 if (!result) goto failed;
2285 value = ast2obj_list(o->v.Global.names, ast2obj_identifier);
2286 if (!value) goto failed;
2287 if (PyObject_SetAttrString(result, "names", value) == -1)
2288 goto failed;
2289 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 break;
2291 case Expr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002292 result = PyType_GenericNew(Expr_type, NULL, NULL);
2293 if (!result) goto failed;
2294 value = ast2obj_expr(o->v.Expr.value);
2295 if (!value) goto failed;
2296 if (PyObject_SetAttrString(result, "value", value) == -1)
2297 goto failed;
2298 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 break;
2300 case Pass_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002301 result = PyType_GenericNew(Pass_type, NULL, NULL);
2302 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 break;
2304 case Break_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002305 result = PyType_GenericNew(Break_type, NULL, NULL);
2306 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002307 break;
2308 case Continue_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002309 result = PyType_GenericNew(Continue_type, NULL, NULL);
2310 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 break;
2312 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002313 value = ast2obj_int(o->lineno);
2314 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002315 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2316 goto failed;
2317 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002318 value = ast2obj_int(o->col_offset);
2319 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002320 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2321 goto failed;
2322 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002323 return result;
2324failed:
2325 Py_XDECREF(value);
2326 Py_XDECREF(result);
2327 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328}
2329
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002330PyObject*
2331ast2obj_expr(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002333 expr_ty o = (expr_ty)_o;
2334 PyObject *result = NULL, *value = NULL;
2335 if (!o) {
2336 Py_INCREF(Py_None);
2337 return Py_None;
2338 }
2339
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 switch (o->kind) {
2341 case BoolOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002342 result = PyType_GenericNew(BoolOp_type, NULL, NULL);
2343 if (!result) goto failed;
2344 value = ast2obj_boolop(o->v.BoolOp.op);
2345 if (!value) goto failed;
2346 if (PyObject_SetAttrString(result, "op", value) == -1)
2347 goto failed;
2348 Py_DECREF(value);
2349 value = ast2obj_list(o->v.BoolOp.values, ast2obj_expr);
2350 if (!value) goto failed;
2351 if (PyObject_SetAttrString(result, "values", value) == -1)
2352 goto failed;
2353 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 break;
2355 case BinOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002356 result = PyType_GenericNew(BinOp_type, NULL, NULL);
2357 if (!result) goto failed;
2358 value = ast2obj_expr(o->v.BinOp.left);
2359 if (!value) goto failed;
2360 if (PyObject_SetAttrString(result, "left", value) == -1)
2361 goto failed;
2362 Py_DECREF(value);
2363 value = ast2obj_operator(o->v.BinOp.op);
2364 if (!value) goto failed;
2365 if (PyObject_SetAttrString(result, "op", value) == -1)
2366 goto failed;
2367 Py_DECREF(value);
2368 value = ast2obj_expr(o->v.BinOp.right);
2369 if (!value) goto failed;
2370 if (PyObject_SetAttrString(result, "right", value) == -1)
2371 goto failed;
2372 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 break;
2374 case UnaryOp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002375 result = PyType_GenericNew(UnaryOp_type, NULL, NULL);
2376 if (!result) goto failed;
2377 value = ast2obj_unaryop(o->v.UnaryOp.op);
2378 if (!value) goto failed;
2379 if (PyObject_SetAttrString(result, "op", value) == -1)
2380 goto failed;
2381 Py_DECREF(value);
2382 value = ast2obj_expr(o->v.UnaryOp.operand);
2383 if (!value) goto failed;
2384 if (PyObject_SetAttrString(result, "operand", value) == -1)
2385 goto failed;
2386 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 break;
2388 case Lambda_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002389 result = PyType_GenericNew(Lambda_type, NULL, NULL);
2390 if (!result) goto failed;
2391 value = ast2obj_arguments(o->v.Lambda.args);
2392 if (!value) goto failed;
2393 if (PyObject_SetAttrString(result, "args", value) == -1)
2394 goto failed;
2395 Py_DECREF(value);
2396 value = ast2obj_expr(o->v.Lambda.body);
2397 if (!value) goto failed;
2398 if (PyObject_SetAttrString(result, "body", value) == -1)
2399 goto failed;
2400 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 break;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002402 case IfExp_kind:
2403 result = PyType_GenericNew(IfExp_type, NULL, NULL);
2404 if (!result) goto failed;
2405 value = ast2obj_expr(o->v.IfExp.test);
2406 if (!value) goto failed;
2407 if (PyObject_SetAttrString(result, "test", value) == -1)
2408 goto failed;
2409 Py_DECREF(value);
2410 value = ast2obj_expr(o->v.IfExp.body);
2411 if (!value) goto failed;
2412 if (PyObject_SetAttrString(result, "body", value) == -1)
2413 goto failed;
2414 Py_DECREF(value);
2415 value = ast2obj_expr(o->v.IfExp.orelse);
2416 if (!value) goto failed;
2417 if (PyObject_SetAttrString(result, "orelse", value) == -1)
2418 goto failed;
2419 Py_DECREF(value);
2420 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 case Dict_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002422 result = PyType_GenericNew(Dict_type, NULL, NULL);
2423 if (!result) goto failed;
2424 value = ast2obj_list(o->v.Dict.keys, ast2obj_expr);
2425 if (!value) goto failed;
2426 if (PyObject_SetAttrString(result, "keys", value) == -1)
2427 goto failed;
2428 Py_DECREF(value);
2429 value = ast2obj_list(o->v.Dict.values, ast2obj_expr);
2430 if (!value) goto failed;
2431 if (PyObject_SetAttrString(result, "values", value) == -1)
2432 goto failed;
2433 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 break;
2435 case ListComp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002436 result = PyType_GenericNew(ListComp_type, NULL, NULL);
2437 if (!result) goto failed;
2438 value = ast2obj_expr(o->v.ListComp.elt);
2439 if (!value) goto failed;
2440 if (PyObject_SetAttrString(result, "elt", value) == -1)
2441 goto failed;
2442 Py_DECREF(value);
2443 value = ast2obj_list(o->v.ListComp.generators,
2444 ast2obj_comprehension);
2445 if (!value) goto failed;
2446 if (PyObject_SetAttrString(result, "generators", value) == -1)
2447 goto failed;
2448 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 break;
2450 case GeneratorExp_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002451 result = PyType_GenericNew(GeneratorExp_type, NULL, NULL);
2452 if (!result) goto failed;
2453 value = ast2obj_expr(o->v.GeneratorExp.elt);
2454 if (!value) goto failed;
2455 if (PyObject_SetAttrString(result, "elt", value) == -1)
2456 goto failed;
2457 Py_DECREF(value);
2458 value = ast2obj_list(o->v.GeneratorExp.generators,
2459 ast2obj_comprehension);
2460 if (!value) goto failed;
2461 if (PyObject_SetAttrString(result, "generators", value) == -1)
2462 goto failed;
2463 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 break;
2465 case Yield_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002466 result = PyType_GenericNew(Yield_type, NULL, NULL);
2467 if (!result) goto failed;
2468 value = ast2obj_expr(o->v.Yield.value);
2469 if (!value) goto failed;
2470 if (PyObject_SetAttrString(result, "value", value) == -1)
2471 goto failed;
2472 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 break;
2474 case Compare_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002475 result = PyType_GenericNew(Compare_type, NULL, NULL);
2476 if (!result) goto failed;
2477 value = ast2obj_expr(o->v.Compare.left);
2478 if (!value) goto failed;
2479 if (PyObject_SetAttrString(result, "left", value) == -1)
2480 goto failed;
2481 Py_DECREF(value);
Martin v. Löwisce1d5d22006-02-26 20:51:25 +00002482 {
2483 int i, n = asdl_seq_LEN(o->v.Compare.ops);
2484 value = PyList_New(n);
2485 if (!value) goto failed;
2486 for(i = 0; i < n; i++)
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002487 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 +00002488 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002489 if (!value) goto failed;
2490 if (PyObject_SetAttrString(result, "ops", value) == -1)
2491 goto failed;
2492 Py_DECREF(value);
2493 value = ast2obj_list(o->v.Compare.comparators, ast2obj_expr);
2494 if (!value) goto failed;
2495 if (PyObject_SetAttrString(result, "comparators", value) == -1)
2496 goto failed;
2497 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 break;
2499 case Call_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002500 result = PyType_GenericNew(Call_type, NULL, NULL);
2501 if (!result) goto failed;
2502 value = ast2obj_expr(o->v.Call.func);
2503 if (!value) goto failed;
2504 if (PyObject_SetAttrString(result, "func", value) == -1)
2505 goto failed;
2506 Py_DECREF(value);
2507 value = ast2obj_list(o->v.Call.args, ast2obj_expr);
2508 if (!value) goto failed;
2509 if (PyObject_SetAttrString(result, "args", value) == -1)
2510 goto failed;
2511 Py_DECREF(value);
2512 value = ast2obj_list(o->v.Call.keywords, ast2obj_keyword);
2513 if (!value) goto failed;
2514 if (PyObject_SetAttrString(result, "keywords", value) == -1)
2515 goto failed;
2516 Py_DECREF(value);
2517 value = ast2obj_expr(o->v.Call.starargs);
2518 if (!value) goto failed;
2519 if (PyObject_SetAttrString(result, "starargs", value) == -1)
2520 goto failed;
2521 Py_DECREF(value);
2522 value = ast2obj_expr(o->v.Call.kwargs);
2523 if (!value) goto failed;
2524 if (PyObject_SetAttrString(result, "kwargs", value) == -1)
2525 goto failed;
2526 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 break;
2528 case Repr_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002529 result = PyType_GenericNew(Repr_type, NULL, NULL);
2530 if (!result) goto failed;
2531 value = ast2obj_expr(o->v.Repr.value);
2532 if (!value) goto failed;
2533 if (PyObject_SetAttrString(result, "value", value) == -1)
2534 goto failed;
2535 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 break;
2537 case Num_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002538 result = PyType_GenericNew(Num_type, NULL, NULL);
2539 if (!result) goto failed;
2540 value = ast2obj_object(o->v.Num.n);
2541 if (!value) goto failed;
2542 if (PyObject_SetAttrString(result, "n", value) == -1)
2543 goto failed;
2544 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 break;
2546 case Str_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002547 result = PyType_GenericNew(Str_type, NULL, NULL);
2548 if (!result) goto failed;
2549 value = ast2obj_string(o->v.Str.s);
2550 if (!value) goto failed;
2551 if (PyObject_SetAttrString(result, "s", value) == -1)
2552 goto failed;
2553 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554 break;
2555 case Attribute_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002556 result = PyType_GenericNew(Attribute_type, NULL, NULL);
2557 if (!result) goto failed;
2558 value = ast2obj_expr(o->v.Attribute.value);
2559 if (!value) goto failed;
2560 if (PyObject_SetAttrString(result, "value", value) == -1)
2561 goto failed;
2562 Py_DECREF(value);
2563 value = ast2obj_identifier(o->v.Attribute.attr);
2564 if (!value) goto failed;
2565 if (PyObject_SetAttrString(result, "attr", value) == -1)
2566 goto failed;
2567 Py_DECREF(value);
2568 value = ast2obj_expr_context(o->v.Attribute.ctx);
2569 if (!value) goto failed;
2570 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2571 goto failed;
2572 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 break;
2574 case Subscript_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002575 result = PyType_GenericNew(Subscript_type, NULL, NULL);
2576 if (!result) goto failed;
2577 value = ast2obj_expr(o->v.Subscript.value);
2578 if (!value) goto failed;
2579 if (PyObject_SetAttrString(result, "value", value) == -1)
2580 goto failed;
2581 Py_DECREF(value);
2582 value = ast2obj_slice(o->v.Subscript.slice);
2583 if (!value) goto failed;
2584 if (PyObject_SetAttrString(result, "slice", value) == -1)
2585 goto failed;
2586 Py_DECREF(value);
2587 value = ast2obj_expr_context(o->v.Subscript.ctx);
2588 if (!value) goto failed;
2589 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2590 goto failed;
2591 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 break;
2593 case Name_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002594 result = PyType_GenericNew(Name_type, NULL, NULL);
2595 if (!result) goto failed;
2596 value = ast2obj_identifier(o->v.Name.id);
2597 if (!value) goto failed;
2598 if (PyObject_SetAttrString(result, "id", value) == -1)
2599 goto failed;
2600 Py_DECREF(value);
2601 value = ast2obj_expr_context(o->v.Name.ctx);
2602 if (!value) goto failed;
2603 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2604 goto failed;
2605 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 break;
2607 case List_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002608 result = PyType_GenericNew(List_type, NULL, NULL);
2609 if (!result) goto failed;
2610 value = ast2obj_list(o->v.List.elts, ast2obj_expr);
2611 if (!value) goto failed;
2612 if (PyObject_SetAttrString(result, "elts", value) == -1)
2613 goto failed;
2614 Py_DECREF(value);
2615 value = ast2obj_expr_context(o->v.List.ctx);
2616 if (!value) goto failed;
2617 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2618 goto failed;
2619 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 break;
2621 case Tuple_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002622 result = PyType_GenericNew(Tuple_type, NULL, NULL);
2623 if (!result) goto failed;
2624 value = ast2obj_list(o->v.Tuple.elts, ast2obj_expr);
2625 if (!value) goto failed;
2626 if (PyObject_SetAttrString(result, "elts", value) == -1)
2627 goto failed;
2628 Py_DECREF(value);
2629 value = ast2obj_expr_context(o->v.Tuple.ctx);
2630 if (!value) goto failed;
2631 if (PyObject_SetAttrString(result, "ctx", value) == -1)
2632 goto failed;
2633 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 break;
2635 }
Martin v. Löwis577b5b92006-02-27 15:23:19 +00002636 value = ast2obj_int(o->lineno);
2637 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002638 if (PyObject_SetAttrString(result, "lineno", value) < 0)
2639 goto failed;
2640 Py_DECREF(value);
Martin v. Löwis49c5da12006-03-01 22:49:05 +00002641 value = ast2obj_int(o->col_offset);
2642 if (!value) goto failed;
Martin v. Löwis03e5bc02006-03-02 00:31:27 +00002643 if (PyObject_SetAttrString(result, "col_offset", value) < 0)
2644 goto failed;
2645 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002646 return result;
2647failed:
2648 Py_XDECREF(value);
2649 Py_XDECREF(result);
2650 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651}
2652
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002653PyObject* ast2obj_expr_context(expr_context_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002655 switch(o) {
2656 case Load:
2657 Py_INCREF(Load_singleton);
2658 return Load_singleton;
2659 case Store:
2660 Py_INCREF(Store_singleton);
2661 return Store_singleton;
2662 case Del:
2663 Py_INCREF(Del_singleton);
2664 return Del_singleton;
2665 case AugLoad:
2666 Py_INCREF(AugLoad_singleton);
2667 return AugLoad_singleton;
2668 case AugStore:
2669 Py_INCREF(AugStore_singleton);
2670 return AugStore_singleton;
2671 case Param:
2672 Py_INCREF(Param_singleton);
2673 return Param_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002674 default:
2675 /* should never happen, but just in case ... */
2676 PyErr_Format(PyExc_SystemError, "unknown expr_context found");
2677 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679}
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002680PyObject*
2681ast2obj_slice(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002683 slice_ty o = (slice_ty)_o;
2684 PyObject *result = NULL, *value = NULL;
2685 if (!o) {
2686 Py_INCREF(Py_None);
2687 return Py_None;
2688 }
2689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 switch (o->kind) {
2691 case Ellipsis_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002692 result = PyType_GenericNew(Ellipsis_type, NULL, NULL);
2693 if (!result) goto failed;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 break;
2695 case Slice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002696 result = PyType_GenericNew(Slice_type, NULL, NULL);
2697 if (!result) goto failed;
2698 value = ast2obj_expr(o->v.Slice.lower);
2699 if (!value) goto failed;
2700 if (PyObject_SetAttrString(result, "lower", value) == -1)
2701 goto failed;
2702 Py_DECREF(value);
2703 value = ast2obj_expr(o->v.Slice.upper);
2704 if (!value) goto failed;
2705 if (PyObject_SetAttrString(result, "upper", value) == -1)
2706 goto failed;
2707 Py_DECREF(value);
2708 value = ast2obj_expr(o->v.Slice.step);
2709 if (!value) goto failed;
2710 if (PyObject_SetAttrString(result, "step", value) == -1)
2711 goto failed;
2712 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 break;
2714 case ExtSlice_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002715 result = PyType_GenericNew(ExtSlice_type, NULL, NULL);
2716 if (!result) goto failed;
2717 value = ast2obj_list(o->v.ExtSlice.dims, ast2obj_slice);
2718 if (!value) goto failed;
2719 if (PyObject_SetAttrString(result, "dims", value) == -1)
2720 goto failed;
2721 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 break;
2723 case Index_kind:
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002724 result = PyType_GenericNew(Index_type, NULL, NULL);
2725 if (!result) goto failed;
2726 value = ast2obj_expr(o->v.Index.value);
2727 if (!value) goto failed;
2728 if (PyObject_SetAttrString(result, "value", value) == -1)
2729 goto failed;
2730 Py_DECREF(value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 break;
2732 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002733 return result;
2734failed:
2735 Py_XDECREF(value);
2736 Py_XDECREF(result);
2737 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738}
2739
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002740PyObject* ast2obj_boolop(boolop_ty o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002742 switch(o) {
2743 case And:
2744 Py_INCREF(And_singleton);
2745 return And_singleton;
2746 case Or:
2747 Py_INCREF(Or_singleton);
2748 return Or_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002749 default:
2750 /* should never happen, but just in case ... */
2751 PyErr_Format(PyExc_SystemError, "unknown boolop found");
2752 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002754}
2755PyObject* ast2obj_operator(operator_ty o)
2756{
2757 switch(o) {
2758 case Add:
2759 Py_INCREF(Add_singleton);
2760 return Add_singleton;
2761 case Sub:
2762 Py_INCREF(Sub_singleton);
2763 return Sub_singleton;
2764 case Mult:
2765 Py_INCREF(Mult_singleton);
2766 return Mult_singleton;
2767 case Div:
2768 Py_INCREF(Div_singleton);
2769 return Div_singleton;
2770 case Mod:
2771 Py_INCREF(Mod_singleton);
2772 return Mod_singleton;
2773 case Pow:
2774 Py_INCREF(Pow_singleton);
2775 return Pow_singleton;
2776 case LShift:
2777 Py_INCREF(LShift_singleton);
2778 return LShift_singleton;
2779 case RShift:
2780 Py_INCREF(RShift_singleton);
2781 return RShift_singleton;
2782 case BitOr:
2783 Py_INCREF(BitOr_singleton);
2784 return BitOr_singleton;
2785 case BitXor:
2786 Py_INCREF(BitXor_singleton);
2787 return BitXor_singleton;
2788 case BitAnd:
2789 Py_INCREF(BitAnd_singleton);
2790 return BitAnd_singleton;
2791 case FloorDiv:
2792 Py_INCREF(FloorDiv_singleton);
2793 return FloorDiv_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002794 default:
2795 /* should never happen, but just in case ... */
2796 PyErr_Format(PyExc_SystemError, "unknown operator found");
2797 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002798 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002799}
2800PyObject* ast2obj_unaryop(unaryop_ty o)
2801{
2802 switch(o) {
2803 case Invert:
2804 Py_INCREF(Invert_singleton);
2805 return Invert_singleton;
2806 case Not:
2807 Py_INCREF(Not_singleton);
2808 return Not_singleton;
2809 case UAdd:
2810 Py_INCREF(UAdd_singleton);
2811 return UAdd_singleton;
2812 case USub:
2813 Py_INCREF(USub_singleton);
2814 return USub_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002815 default:
2816 /* should never happen, but just in case ... */
2817 PyErr_Format(PyExc_SystemError, "unknown unaryop found");
2818 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002819 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002820}
2821PyObject* ast2obj_cmpop(cmpop_ty o)
2822{
2823 switch(o) {
2824 case Eq:
2825 Py_INCREF(Eq_singleton);
2826 return Eq_singleton;
2827 case NotEq:
2828 Py_INCREF(NotEq_singleton);
2829 return NotEq_singleton;
2830 case Lt:
2831 Py_INCREF(Lt_singleton);
2832 return Lt_singleton;
2833 case LtE:
2834 Py_INCREF(LtE_singleton);
2835 return LtE_singleton;
2836 case Gt:
2837 Py_INCREF(Gt_singleton);
2838 return Gt_singleton;
2839 case GtE:
2840 Py_INCREF(GtE_singleton);
2841 return GtE_singleton;
2842 case Is:
2843 Py_INCREF(Is_singleton);
2844 return Is_singleton;
2845 case IsNot:
2846 Py_INCREF(IsNot_singleton);
2847 return IsNot_singleton;
2848 case In:
2849 Py_INCREF(In_singleton);
2850 return In_singleton;
2851 case NotIn:
2852 Py_INCREF(NotIn_singleton);
2853 return NotIn_singleton;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002854 default:
2855 /* should never happen, but just in case ... */
2856 PyErr_Format(PyExc_SystemError, "unknown cmpop found");
2857 return NULL;
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002858 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002859}
2860PyObject*
2861ast2obj_comprehension(void* _o)
2862{
2863 comprehension_ty o = (comprehension_ty)_o;
2864 PyObject *result = NULL, *value = NULL;
2865 if (!o) {
2866 Py_INCREF(Py_None);
2867 return Py_None;
2868 }
2869
2870 result = PyType_GenericNew(comprehension_type, NULL, NULL);
2871 if (!result) return NULL;
2872 value = ast2obj_expr(o->target);
2873 if (!value) goto failed;
2874 if (PyObject_SetAttrString(result, "target", value) == -1)
2875 goto failed;
2876 Py_DECREF(value);
2877 value = ast2obj_expr(o->iter);
2878 if (!value) goto failed;
2879 if (PyObject_SetAttrString(result, "iter", value) == -1)
2880 goto failed;
2881 Py_DECREF(value);
2882 value = ast2obj_list(o->ifs, ast2obj_expr);
2883 if (!value) goto failed;
2884 if (PyObject_SetAttrString(result, "ifs", value) == -1)
2885 goto failed;
2886 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002887 return result;
2888failed:
2889 Py_XDECREF(value);
2890 Py_XDECREF(result);
2891 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002894PyObject*
2895ast2obj_excepthandler(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002897 excepthandler_ty o = (excepthandler_ty)_o;
2898 PyObject *result = NULL, *value = NULL;
2899 if (!o) {
2900 Py_INCREF(Py_None);
2901 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002902 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002903
2904 result = PyType_GenericNew(excepthandler_type, NULL, NULL);
2905 if (!result) return NULL;
2906 value = ast2obj_expr(o->type);
2907 if (!value) goto failed;
2908 if (PyObject_SetAttrString(result, "type", value) == -1)
2909 goto failed;
2910 Py_DECREF(value);
2911 value = ast2obj_expr(o->name);
2912 if (!value) goto failed;
2913 if (PyObject_SetAttrString(result, "name", value) == -1)
2914 goto failed;
2915 Py_DECREF(value);
2916 value = ast2obj_list(o->body, ast2obj_stmt);
2917 if (!value) goto failed;
2918 if (PyObject_SetAttrString(result, "body", value) == -1)
2919 goto failed;
2920 Py_DECREF(value);
Jeremy Hylton2f327c12006-04-04 04:00:23 +00002921 value = ast2obj_int(o->lineno);
2922 if (!value) goto failed;
2923 if (PyObject_SetAttrString(result, "lineno", value) == -1)
2924 goto failed;
2925 Py_DECREF(value);
2926 value = ast2obj_int(o->col_offset);
2927 if (!value) goto failed;
2928 if (PyObject_SetAttrString(result, "col_offset", value) == -1)
2929 goto failed;
2930 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002931 return result;
2932failed:
2933 Py_XDECREF(value);
2934 Py_XDECREF(result);
2935 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936}
2937
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002938PyObject*
2939ast2obj_arguments(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002940{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002941 arguments_ty o = (arguments_ty)_o;
2942 PyObject *result = NULL, *value = NULL;
2943 if (!o) {
2944 Py_INCREF(Py_None);
2945 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002947
2948 result = PyType_GenericNew(arguments_type, NULL, NULL);
2949 if (!result) return NULL;
2950 value = ast2obj_list(o->args, ast2obj_expr);
2951 if (!value) goto failed;
2952 if (PyObject_SetAttrString(result, "args", value) == -1)
2953 goto failed;
2954 Py_DECREF(value);
2955 value = ast2obj_identifier(o->vararg);
2956 if (!value) goto failed;
2957 if (PyObject_SetAttrString(result, "vararg", value) == -1)
2958 goto failed;
2959 Py_DECREF(value);
2960 value = ast2obj_identifier(o->kwarg);
2961 if (!value) goto failed;
2962 if (PyObject_SetAttrString(result, "kwarg", value) == -1)
2963 goto failed;
2964 Py_DECREF(value);
2965 value = ast2obj_list(o->defaults, ast2obj_expr);
2966 if (!value) goto failed;
2967 if (PyObject_SetAttrString(result, "defaults", value) == -1)
2968 goto failed;
2969 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002970 return result;
2971failed:
2972 Py_XDECREF(value);
2973 Py_XDECREF(result);
2974 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975}
2976
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002977PyObject*
2978ast2obj_keyword(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002980 keyword_ty o = (keyword_ty)_o;
2981 PyObject *result = NULL, *value = NULL;
2982 if (!o) {
2983 Py_INCREF(Py_None);
2984 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002986
2987 result = PyType_GenericNew(keyword_type, NULL, NULL);
2988 if (!result) return NULL;
2989 value = ast2obj_identifier(o->arg);
2990 if (!value) goto failed;
2991 if (PyObject_SetAttrString(result, "arg", value) == -1)
2992 goto failed;
2993 Py_DECREF(value);
2994 value = ast2obj_expr(o->value);
2995 if (!value) goto failed;
2996 if (PyObject_SetAttrString(result, "value", value) == -1)
2997 goto failed;
2998 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00002999 return result;
3000failed:
3001 Py_XDECREF(value);
3002 Py_XDECREF(result);
3003 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004}
3005
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003006PyObject*
3007ast2obj_alias(void* _o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003009 alias_ty o = (alias_ty)_o;
3010 PyObject *result = NULL, *value = NULL;
3011 if (!o) {
3012 Py_INCREF(Py_None);
3013 return Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 }
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003015
3016 result = PyType_GenericNew(alias_type, NULL, NULL);
3017 if (!result) return NULL;
3018 value = ast2obj_identifier(o->name);
3019 if (!value) goto failed;
3020 if (PyObject_SetAttrString(result, "name", value) == -1)
3021 goto failed;
3022 Py_DECREF(value);
3023 value = ast2obj_identifier(o->asname);
3024 if (!value) goto failed;
3025 if (PyObject_SetAttrString(result, "asname", value) == -1)
3026 goto failed;
3027 Py_DECREF(value);
Martin v. Löwisbd260da2006-02-26 19:42:26 +00003028 return result;
3029failed:
3030 Py_XDECREF(value);
3031 Py_XDECREF(result);
3032 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033}
3034
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035
Georg Brandlfc8eef32008-03-28 12:11:56 +00003036int
3037obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
3038{
3039 PyObject* tmp = NULL;
3040
3041
3042 if (obj == Py_None) {
3043 *out = NULL;
3044 return 0;
3045 }
3046 if (PyObject_IsInstance(obj, (PyObject*)Module_type)) {
3047 asdl_seq* body;
3048
3049 if (PyObject_HasAttrString(obj, "body")) {
3050 int res;
3051 Py_ssize_t len;
3052 Py_ssize_t i;
3053 tmp = PyObject_GetAttrString(obj, "body");
3054 if (tmp == NULL) goto failed;
3055 if (!PyList_Check(tmp)) {
3056 PyErr_Format(PyExc_TypeError, "Module field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3057 goto failed;
3058 }
3059 len = PyList_GET_SIZE(tmp);
3060 body = asdl_seq_new(len, arena);
3061 if (body == NULL) goto failed;
3062 for (i = 0; i < len; i++) {
3063 stmt_ty value;
3064 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3065 if (res != 0) goto failed;
3066 asdl_seq_SET(body, i, value);
3067 }
3068 Py_XDECREF(tmp);
3069 tmp = NULL;
3070 } else {
3071 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module");
3072 return 1;
3073 }
3074 *out = Module(body, arena);
3075 if (*out == NULL) goto failed;
3076 return 0;
3077 }
3078 if (PyObject_IsInstance(obj, (PyObject*)Interactive_type)) {
3079 asdl_seq* body;
3080
3081 if (PyObject_HasAttrString(obj, "body")) {
3082 int res;
3083 Py_ssize_t len;
3084 Py_ssize_t i;
3085 tmp = PyObject_GetAttrString(obj, "body");
3086 if (tmp == NULL) goto failed;
3087 if (!PyList_Check(tmp)) {
3088 PyErr_Format(PyExc_TypeError, "Interactive field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3089 goto failed;
3090 }
3091 len = PyList_GET_SIZE(tmp);
3092 body = asdl_seq_new(len, arena);
3093 if (body == NULL) goto failed;
3094 for (i = 0; i < len; i++) {
3095 stmt_ty value;
3096 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3097 if (res != 0) goto failed;
3098 asdl_seq_SET(body, i, value);
3099 }
3100 Py_XDECREF(tmp);
3101 tmp = NULL;
3102 } else {
3103 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Interactive");
3104 return 1;
3105 }
3106 *out = Interactive(body, arena);
3107 if (*out == NULL) goto failed;
3108 return 0;
3109 }
3110 if (PyObject_IsInstance(obj, (PyObject*)Expression_type)) {
3111 expr_ty body;
3112
3113 if (PyObject_HasAttrString(obj, "body")) {
3114 int res;
3115 tmp = PyObject_GetAttrString(obj, "body");
3116 if (tmp == NULL) goto failed;
3117 res = obj2ast_expr(tmp, &body, arena);
3118 if (res != 0) goto failed;
3119 Py_XDECREF(tmp);
3120 tmp = NULL;
3121 } else {
3122 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Expression");
3123 return 1;
3124 }
3125 *out = Expression(body, arena);
3126 if (*out == NULL) goto failed;
3127 return 0;
3128 }
3129 if (PyObject_IsInstance(obj, (PyObject*)Suite_type)) {
3130 asdl_seq* body;
3131
3132 if (PyObject_HasAttrString(obj, "body")) {
3133 int res;
3134 Py_ssize_t len;
3135 Py_ssize_t i;
3136 tmp = PyObject_GetAttrString(obj, "body");
3137 if (tmp == NULL) goto failed;
3138 if (!PyList_Check(tmp)) {
3139 PyErr_Format(PyExc_TypeError, "Suite field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3140 goto failed;
3141 }
3142 len = PyList_GET_SIZE(tmp);
3143 body = asdl_seq_new(len, arena);
3144 if (body == NULL) goto failed;
3145 for (i = 0; i < len; i++) {
3146 stmt_ty value;
3147 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3148 if (res != 0) goto failed;
3149 asdl_seq_SET(body, i, value);
3150 }
3151 Py_XDECREF(tmp);
3152 tmp = NULL;
3153 } else {
3154 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Suite");
3155 return 1;
3156 }
3157 *out = Suite(body, arena);
3158 if (*out == NULL) goto failed;
3159 return 0;
3160 }
3161
3162 tmp = PyObject_Repr(obj);
3163 if (tmp == NULL) goto failed;
3164 PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyString_AS_STRING(tmp));
3165failed:
3166 Py_XDECREF(tmp);
3167 return 1;
3168}
3169
3170int
3171obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
3172{
3173 PyObject* tmp = NULL;
3174
3175 int lineno;
3176 int col_offset;
3177
3178 if (obj == Py_None) {
3179 *out = NULL;
3180 return 0;
3181 }
3182 if (PyObject_HasAttrString(obj, "lineno")) {
3183 int res;
3184 tmp = PyObject_GetAttrString(obj, "lineno");
3185 if (tmp == NULL) goto failed;
3186 res = obj2ast_int(tmp, &lineno, arena);
3187 if (res != 0) goto failed;
3188 Py_XDECREF(tmp);
3189 tmp = NULL;
3190 } else {
3191 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from stmt");
3192 return 1;
3193 }
3194 if (PyObject_HasAttrString(obj, "col_offset")) {
3195 int res;
3196 tmp = PyObject_GetAttrString(obj, "col_offset");
3197 if (tmp == NULL) goto failed;
3198 res = obj2ast_int(tmp, &col_offset, arena);
3199 if (res != 0) goto failed;
3200 Py_XDECREF(tmp);
3201 tmp = NULL;
3202 } else {
3203 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from stmt");
3204 return 1;
3205 }
3206 if (PyObject_IsInstance(obj, (PyObject*)FunctionDef_type)) {
3207 identifier name;
3208 arguments_ty args;
3209 asdl_seq* body;
3210 asdl_seq* decorator_list;
3211
3212 if (PyObject_HasAttrString(obj, "name")) {
3213 int res;
3214 tmp = PyObject_GetAttrString(obj, "name");
3215 if (tmp == NULL) goto failed;
3216 res = obj2ast_identifier(tmp, &name, arena);
3217 if (res != 0) goto failed;
3218 Py_XDECREF(tmp);
3219 tmp = NULL;
3220 } else {
3221 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from FunctionDef");
3222 return 1;
3223 }
3224 if (PyObject_HasAttrString(obj, "args")) {
3225 int res;
3226 tmp = PyObject_GetAttrString(obj, "args");
3227 if (tmp == NULL) goto failed;
3228 res = obj2ast_arguments(tmp, &args, arena);
3229 if (res != 0) goto failed;
3230 Py_XDECREF(tmp);
3231 tmp = NULL;
3232 } else {
3233 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from FunctionDef");
3234 return 1;
3235 }
3236 if (PyObject_HasAttrString(obj, "body")) {
3237 int res;
3238 Py_ssize_t len;
3239 Py_ssize_t i;
3240 tmp = PyObject_GetAttrString(obj, "body");
3241 if (tmp == NULL) goto failed;
3242 if (!PyList_Check(tmp)) {
3243 PyErr_Format(PyExc_TypeError, "FunctionDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3244 goto failed;
3245 }
3246 len = PyList_GET_SIZE(tmp);
3247 body = asdl_seq_new(len, arena);
3248 if (body == NULL) goto failed;
3249 for (i = 0; i < len; i++) {
3250 stmt_ty value;
3251 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3252 if (res != 0) goto failed;
3253 asdl_seq_SET(body, i, value);
3254 }
3255 Py_XDECREF(tmp);
3256 tmp = NULL;
3257 } else {
3258 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from FunctionDef");
3259 return 1;
3260 }
3261 if (PyObject_HasAttrString(obj, "decorator_list")) {
3262 int res;
3263 Py_ssize_t len;
3264 Py_ssize_t i;
3265 tmp = PyObject_GetAttrString(obj, "decorator_list");
3266 if (tmp == NULL) goto failed;
3267 if (!PyList_Check(tmp)) {
3268 PyErr_Format(PyExc_TypeError, "FunctionDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3269 goto failed;
3270 }
3271 len = PyList_GET_SIZE(tmp);
3272 decorator_list = asdl_seq_new(len, arena);
3273 if (decorator_list == NULL) goto failed;
3274 for (i = 0; i < len; i++) {
3275 expr_ty value;
3276 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3277 if (res != 0) goto failed;
3278 asdl_seq_SET(decorator_list, i, value);
3279 }
3280 Py_XDECREF(tmp);
3281 tmp = NULL;
3282 } else {
3283 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");
3284 return 1;
3285 }
3286 *out = FunctionDef(name, args, body, decorator_list, lineno,
3287 col_offset, arena);
3288 if (*out == NULL) goto failed;
3289 return 0;
3290 }
3291 if (PyObject_IsInstance(obj, (PyObject*)ClassDef_type)) {
3292 identifier name;
3293 asdl_seq* bases;
3294 asdl_seq* body;
3295 asdl_seq* decorator_list;
3296
3297 if (PyObject_HasAttrString(obj, "name")) {
3298 int res;
3299 tmp = PyObject_GetAttrString(obj, "name");
3300 if (tmp == NULL) goto failed;
3301 res = obj2ast_identifier(tmp, &name, arena);
3302 if (res != 0) goto failed;
3303 Py_XDECREF(tmp);
3304 tmp = NULL;
3305 } else {
3306 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from ClassDef");
3307 return 1;
3308 }
3309 if (PyObject_HasAttrString(obj, "bases")) {
3310 int res;
3311 Py_ssize_t len;
3312 Py_ssize_t i;
3313 tmp = PyObject_GetAttrString(obj, "bases");
3314 if (tmp == NULL) goto failed;
3315 if (!PyList_Check(tmp)) {
3316 PyErr_Format(PyExc_TypeError, "ClassDef field \"bases\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3317 goto failed;
3318 }
3319 len = PyList_GET_SIZE(tmp);
3320 bases = asdl_seq_new(len, arena);
3321 if (bases == NULL) goto failed;
3322 for (i = 0; i < len; i++) {
3323 expr_ty value;
3324 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3325 if (res != 0) goto failed;
3326 asdl_seq_SET(bases, i, value);
3327 }
3328 Py_XDECREF(tmp);
3329 tmp = NULL;
3330 } else {
3331 PyErr_SetString(PyExc_TypeError, "required field \"bases\" missing from ClassDef");
3332 return 1;
3333 }
3334 if (PyObject_HasAttrString(obj, "body")) {
3335 int res;
3336 Py_ssize_t len;
3337 Py_ssize_t i;
3338 tmp = PyObject_GetAttrString(obj, "body");
3339 if (tmp == NULL) goto failed;
3340 if (!PyList_Check(tmp)) {
3341 PyErr_Format(PyExc_TypeError, "ClassDef field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3342 goto failed;
3343 }
3344 len = PyList_GET_SIZE(tmp);
3345 body = asdl_seq_new(len, arena);
3346 if (body == NULL) goto failed;
3347 for (i = 0; i < len; i++) {
3348 stmt_ty value;
3349 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3350 if (res != 0) goto failed;
3351 asdl_seq_SET(body, i, value);
3352 }
3353 Py_XDECREF(tmp);
3354 tmp = NULL;
3355 } else {
3356 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from ClassDef");
3357 return 1;
3358 }
3359 if (PyObject_HasAttrString(obj, "decorator_list")) {
3360 int res;
3361 Py_ssize_t len;
3362 Py_ssize_t i;
3363 tmp = PyObject_GetAttrString(obj, "decorator_list");
3364 if (tmp == NULL) goto failed;
3365 if (!PyList_Check(tmp)) {
3366 PyErr_Format(PyExc_TypeError, "ClassDef field \"decorator_list\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3367 goto failed;
3368 }
3369 len = PyList_GET_SIZE(tmp);
3370 decorator_list = asdl_seq_new(len, arena);
3371 if (decorator_list == NULL) goto failed;
3372 for (i = 0; i < len; i++) {
3373 expr_ty value;
3374 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3375 if (res != 0) goto failed;
3376 asdl_seq_SET(decorator_list, i, value);
3377 }
3378 Py_XDECREF(tmp);
3379 tmp = NULL;
3380 } else {
3381 PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef");
3382 return 1;
3383 }
3384 *out = ClassDef(name, bases, body, decorator_list, lineno,
3385 col_offset, arena);
3386 if (*out == NULL) goto failed;
3387 return 0;
3388 }
3389 if (PyObject_IsInstance(obj, (PyObject*)Return_type)) {
3390 expr_ty value;
3391
3392 if (PyObject_HasAttrString(obj, "value")) {
3393 int res;
3394 tmp = PyObject_GetAttrString(obj, "value");
3395 if (tmp == NULL) goto failed;
3396 res = obj2ast_expr(tmp, &value, arena);
3397 if (res != 0) goto failed;
3398 Py_XDECREF(tmp);
3399 tmp = NULL;
3400 } else {
3401 value = NULL;
3402 }
3403 *out = Return(value, lineno, col_offset, arena);
3404 if (*out == NULL) goto failed;
3405 return 0;
3406 }
3407 if (PyObject_IsInstance(obj, (PyObject*)Delete_type)) {
3408 asdl_seq* targets;
3409
3410 if (PyObject_HasAttrString(obj, "targets")) {
3411 int res;
3412 Py_ssize_t len;
3413 Py_ssize_t i;
3414 tmp = PyObject_GetAttrString(obj, "targets");
3415 if (tmp == NULL) goto failed;
3416 if (!PyList_Check(tmp)) {
3417 PyErr_Format(PyExc_TypeError, "Delete field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3418 goto failed;
3419 }
3420 len = PyList_GET_SIZE(tmp);
3421 targets = asdl_seq_new(len, arena);
3422 if (targets == NULL) goto failed;
3423 for (i = 0; i < len; i++) {
3424 expr_ty value;
3425 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3426 if (res != 0) goto failed;
3427 asdl_seq_SET(targets, i, value);
3428 }
3429 Py_XDECREF(tmp);
3430 tmp = NULL;
3431 } else {
3432 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Delete");
3433 return 1;
3434 }
3435 *out = Delete(targets, lineno, col_offset, arena);
3436 if (*out == NULL) goto failed;
3437 return 0;
3438 }
3439 if (PyObject_IsInstance(obj, (PyObject*)Assign_type)) {
3440 asdl_seq* targets;
3441 expr_ty value;
3442
3443 if (PyObject_HasAttrString(obj, "targets")) {
3444 int res;
3445 Py_ssize_t len;
3446 Py_ssize_t i;
3447 tmp = PyObject_GetAttrString(obj, "targets");
3448 if (tmp == NULL) goto failed;
3449 if (!PyList_Check(tmp)) {
3450 PyErr_Format(PyExc_TypeError, "Assign field \"targets\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3451 goto failed;
3452 }
3453 len = PyList_GET_SIZE(tmp);
3454 targets = asdl_seq_new(len, arena);
3455 if (targets == NULL) goto failed;
3456 for (i = 0; i < len; i++) {
3457 expr_ty value;
3458 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3459 if (res != 0) goto failed;
3460 asdl_seq_SET(targets, i, value);
3461 }
3462 Py_XDECREF(tmp);
3463 tmp = NULL;
3464 } else {
3465 PyErr_SetString(PyExc_TypeError, "required field \"targets\" missing from Assign");
3466 return 1;
3467 }
3468 if (PyObject_HasAttrString(obj, "value")) {
3469 int res;
3470 tmp = PyObject_GetAttrString(obj, "value");
3471 if (tmp == NULL) goto failed;
3472 res = obj2ast_expr(tmp, &value, arena);
3473 if (res != 0) goto failed;
3474 Py_XDECREF(tmp);
3475 tmp = NULL;
3476 } else {
3477 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Assign");
3478 return 1;
3479 }
3480 *out = Assign(targets, value, lineno, col_offset, arena);
3481 if (*out == NULL) goto failed;
3482 return 0;
3483 }
3484 if (PyObject_IsInstance(obj, (PyObject*)AugAssign_type)) {
3485 expr_ty target;
3486 operator_ty op;
3487 expr_ty value;
3488
3489 if (PyObject_HasAttrString(obj, "target")) {
3490 int res;
3491 tmp = PyObject_GetAttrString(obj, "target");
3492 if (tmp == NULL) goto failed;
3493 res = obj2ast_expr(tmp, &target, arena);
3494 if (res != 0) goto failed;
3495 Py_XDECREF(tmp);
3496 tmp = NULL;
3497 } else {
3498 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from AugAssign");
3499 return 1;
3500 }
3501 if (PyObject_HasAttrString(obj, "op")) {
3502 int res;
3503 tmp = PyObject_GetAttrString(obj, "op");
3504 if (tmp == NULL) goto failed;
3505 res = obj2ast_operator(tmp, &op, arena);
3506 if (res != 0) goto failed;
3507 Py_XDECREF(tmp);
3508 tmp = NULL;
3509 } else {
3510 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from AugAssign");
3511 return 1;
3512 }
3513 if (PyObject_HasAttrString(obj, "value")) {
3514 int res;
3515 tmp = PyObject_GetAttrString(obj, "value");
3516 if (tmp == NULL) goto failed;
3517 res = obj2ast_expr(tmp, &value, arena);
3518 if (res != 0) goto failed;
3519 Py_XDECREF(tmp);
3520 tmp = NULL;
3521 } else {
3522 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from AugAssign");
3523 return 1;
3524 }
3525 *out = AugAssign(target, op, value, lineno, col_offset, arena);
3526 if (*out == NULL) goto failed;
3527 return 0;
3528 }
3529 if (PyObject_IsInstance(obj, (PyObject*)Print_type)) {
3530 expr_ty dest;
3531 asdl_seq* values;
3532 bool nl;
3533
3534 if (PyObject_HasAttrString(obj, "dest")) {
3535 int res;
3536 tmp = PyObject_GetAttrString(obj, "dest");
3537 if (tmp == NULL) goto failed;
3538 res = obj2ast_expr(tmp, &dest, arena);
3539 if (res != 0) goto failed;
3540 Py_XDECREF(tmp);
3541 tmp = NULL;
3542 } else {
3543 dest = NULL;
3544 }
3545 if (PyObject_HasAttrString(obj, "values")) {
3546 int res;
3547 Py_ssize_t len;
3548 Py_ssize_t i;
3549 tmp = PyObject_GetAttrString(obj, "values");
3550 if (tmp == NULL) goto failed;
3551 if (!PyList_Check(tmp)) {
3552 PyErr_Format(PyExc_TypeError, "Print field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3553 goto failed;
3554 }
3555 len = PyList_GET_SIZE(tmp);
3556 values = asdl_seq_new(len, arena);
3557 if (values == NULL) goto failed;
3558 for (i = 0; i < len; i++) {
3559 expr_ty value;
3560 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
3561 if (res != 0) goto failed;
3562 asdl_seq_SET(values, i, value);
3563 }
3564 Py_XDECREF(tmp);
3565 tmp = NULL;
3566 } else {
3567 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Print");
3568 return 1;
3569 }
3570 if (PyObject_HasAttrString(obj, "nl")) {
3571 int res;
3572 tmp = PyObject_GetAttrString(obj, "nl");
3573 if (tmp == NULL) goto failed;
3574 res = obj2ast_bool(tmp, &nl, arena);
3575 if (res != 0) goto failed;
3576 Py_XDECREF(tmp);
3577 tmp = NULL;
3578 } else {
3579 PyErr_SetString(PyExc_TypeError, "required field \"nl\" missing from Print");
3580 return 1;
3581 }
3582 *out = Print(dest, values, nl, lineno, col_offset, arena);
3583 if (*out == NULL) goto failed;
3584 return 0;
3585 }
3586 if (PyObject_IsInstance(obj, (PyObject*)For_type)) {
3587 expr_ty target;
3588 expr_ty iter;
3589 asdl_seq* body;
3590 asdl_seq* orelse;
3591
3592 if (PyObject_HasAttrString(obj, "target")) {
3593 int res;
3594 tmp = PyObject_GetAttrString(obj, "target");
3595 if (tmp == NULL) goto failed;
3596 res = obj2ast_expr(tmp, &target, arena);
3597 if (res != 0) goto failed;
3598 Py_XDECREF(tmp);
3599 tmp = NULL;
3600 } else {
3601 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from For");
3602 return 1;
3603 }
3604 if (PyObject_HasAttrString(obj, "iter")) {
3605 int res;
3606 tmp = PyObject_GetAttrString(obj, "iter");
3607 if (tmp == NULL) goto failed;
3608 res = obj2ast_expr(tmp, &iter, arena);
3609 if (res != 0) goto failed;
3610 Py_XDECREF(tmp);
3611 tmp = NULL;
3612 } else {
3613 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from For");
3614 return 1;
3615 }
3616 if (PyObject_HasAttrString(obj, "body")) {
3617 int res;
3618 Py_ssize_t len;
3619 Py_ssize_t i;
3620 tmp = PyObject_GetAttrString(obj, "body");
3621 if (tmp == NULL) goto failed;
3622 if (!PyList_Check(tmp)) {
3623 PyErr_Format(PyExc_TypeError, "For field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3624 goto failed;
3625 }
3626 len = PyList_GET_SIZE(tmp);
3627 body = asdl_seq_new(len, arena);
3628 if (body == NULL) goto failed;
3629 for (i = 0; i < len; i++) {
3630 stmt_ty value;
3631 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3632 if (res != 0) goto failed;
3633 asdl_seq_SET(body, i, value);
3634 }
3635 Py_XDECREF(tmp);
3636 tmp = NULL;
3637 } else {
3638 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from For");
3639 return 1;
3640 }
3641 if (PyObject_HasAttrString(obj, "orelse")) {
3642 int res;
3643 Py_ssize_t len;
3644 Py_ssize_t i;
3645 tmp = PyObject_GetAttrString(obj, "orelse");
3646 if (tmp == NULL) goto failed;
3647 if (!PyList_Check(tmp)) {
3648 PyErr_Format(PyExc_TypeError, "For field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3649 goto failed;
3650 }
3651 len = PyList_GET_SIZE(tmp);
3652 orelse = asdl_seq_new(len, arena);
3653 if (orelse == NULL) goto failed;
3654 for (i = 0; i < len; i++) {
3655 stmt_ty value;
3656 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3657 if (res != 0) goto failed;
3658 asdl_seq_SET(orelse, i, value);
3659 }
3660 Py_XDECREF(tmp);
3661 tmp = NULL;
3662 } else {
3663 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from For");
3664 return 1;
3665 }
3666 *out = For(target, iter, body, orelse, lineno, col_offset,
3667 arena);
3668 if (*out == NULL) goto failed;
3669 return 0;
3670 }
3671 if (PyObject_IsInstance(obj, (PyObject*)While_type)) {
3672 expr_ty test;
3673 asdl_seq* body;
3674 asdl_seq* orelse;
3675
3676 if (PyObject_HasAttrString(obj, "test")) {
3677 int res;
3678 tmp = PyObject_GetAttrString(obj, "test");
3679 if (tmp == NULL) goto failed;
3680 res = obj2ast_expr(tmp, &test, arena);
3681 if (res != 0) goto failed;
3682 Py_XDECREF(tmp);
3683 tmp = NULL;
3684 } else {
3685 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from While");
3686 return 1;
3687 }
3688 if (PyObject_HasAttrString(obj, "body")) {
3689 int res;
3690 Py_ssize_t len;
3691 Py_ssize_t i;
3692 tmp = PyObject_GetAttrString(obj, "body");
3693 if (tmp == NULL) goto failed;
3694 if (!PyList_Check(tmp)) {
3695 PyErr_Format(PyExc_TypeError, "While field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3696 goto failed;
3697 }
3698 len = PyList_GET_SIZE(tmp);
3699 body = asdl_seq_new(len, arena);
3700 if (body == NULL) goto failed;
3701 for (i = 0; i < len; i++) {
3702 stmt_ty value;
3703 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3704 if (res != 0) goto failed;
3705 asdl_seq_SET(body, i, value);
3706 }
3707 Py_XDECREF(tmp);
3708 tmp = NULL;
3709 } else {
3710 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from While");
3711 return 1;
3712 }
3713 if (PyObject_HasAttrString(obj, "orelse")) {
3714 int res;
3715 Py_ssize_t len;
3716 Py_ssize_t i;
3717 tmp = PyObject_GetAttrString(obj, "orelse");
3718 if (tmp == NULL) goto failed;
3719 if (!PyList_Check(tmp)) {
3720 PyErr_Format(PyExc_TypeError, "While field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3721 goto failed;
3722 }
3723 len = PyList_GET_SIZE(tmp);
3724 orelse = asdl_seq_new(len, arena);
3725 if (orelse == NULL) goto failed;
3726 for (i = 0; i < len; i++) {
3727 stmt_ty value;
3728 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3729 if (res != 0) goto failed;
3730 asdl_seq_SET(orelse, i, value);
3731 }
3732 Py_XDECREF(tmp);
3733 tmp = NULL;
3734 } else {
3735 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from While");
3736 return 1;
3737 }
3738 *out = While(test, body, orelse, lineno, col_offset, arena);
3739 if (*out == NULL) goto failed;
3740 return 0;
3741 }
3742 if (PyObject_IsInstance(obj, (PyObject*)If_type)) {
3743 expr_ty test;
3744 asdl_seq* body;
3745 asdl_seq* orelse;
3746
3747 if (PyObject_HasAttrString(obj, "test")) {
3748 int res;
3749 tmp = PyObject_GetAttrString(obj, "test");
3750 if (tmp == NULL) goto failed;
3751 res = obj2ast_expr(tmp, &test, arena);
3752 if (res != 0) goto failed;
3753 Py_XDECREF(tmp);
3754 tmp = NULL;
3755 } else {
3756 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from If");
3757 return 1;
3758 }
3759 if (PyObject_HasAttrString(obj, "body")) {
3760 int res;
3761 Py_ssize_t len;
3762 Py_ssize_t i;
3763 tmp = PyObject_GetAttrString(obj, "body");
3764 if (tmp == NULL) goto failed;
3765 if (!PyList_Check(tmp)) {
3766 PyErr_Format(PyExc_TypeError, "If field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3767 goto failed;
3768 }
3769 len = PyList_GET_SIZE(tmp);
3770 body = asdl_seq_new(len, arena);
3771 if (body == NULL) goto failed;
3772 for (i = 0; i < len; i++) {
3773 stmt_ty value;
3774 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3775 if (res != 0) goto failed;
3776 asdl_seq_SET(body, i, value);
3777 }
3778 Py_XDECREF(tmp);
3779 tmp = NULL;
3780 } else {
3781 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from If");
3782 return 1;
3783 }
3784 if (PyObject_HasAttrString(obj, "orelse")) {
3785 int res;
3786 Py_ssize_t len;
3787 Py_ssize_t i;
3788 tmp = PyObject_GetAttrString(obj, "orelse");
3789 if (tmp == NULL) goto failed;
3790 if (!PyList_Check(tmp)) {
3791 PyErr_Format(PyExc_TypeError, "If field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3792 goto failed;
3793 }
3794 len = PyList_GET_SIZE(tmp);
3795 orelse = asdl_seq_new(len, arena);
3796 if (orelse == NULL) goto failed;
3797 for (i = 0; i < len; i++) {
3798 stmt_ty value;
3799 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3800 if (res != 0) goto failed;
3801 asdl_seq_SET(orelse, i, value);
3802 }
3803 Py_XDECREF(tmp);
3804 tmp = NULL;
3805 } else {
3806 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from If");
3807 return 1;
3808 }
3809 *out = If(test, body, orelse, lineno, col_offset, arena);
3810 if (*out == NULL) goto failed;
3811 return 0;
3812 }
3813 if (PyObject_IsInstance(obj, (PyObject*)With_type)) {
3814 expr_ty context_expr;
3815 expr_ty optional_vars;
3816 asdl_seq* body;
3817
3818 if (PyObject_HasAttrString(obj, "context_expr")) {
3819 int res;
3820 tmp = PyObject_GetAttrString(obj, "context_expr");
3821 if (tmp == NULL) goto failed;
3822 res = obj2ast_expr(tmp, &context_expr, arena);
3823 if (res != 0) goto failed;
3824 Py_XDECREF(tmp);
3825 tmp = NULL;
3826 } else {
3827 PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from With");
3828 return 1;
3829 }
3830 if (PyObject_HasAttrString(obj, "optional_vars")) {
3831 int res;
3832 tmp = PyObject_GetAttrString(obj, "optional_vars");
3833 if (tmp == NULL) goto failed;
3834 res = obj2ast_expr(tmp, &optional_vars, arena);
3835 if (res != 0) goto failed;
3836 Py_XDECREF(tmp);
3837 tmp = NULL;
3838 } else {
3839 optional_vars = NULL;
3840 }
3841 if (PyObject_HasAttrString(obj, "body")) {
3842 int res;
3843 Py_ssize_t len;
3844 Py_ssize_t i;
3845 tmp = PyObject_GetAttrString(obj, "body");
3846 if (tmp == NULL) goto failed;
3847 if (!PyList_Check(tmp)) {
3848 PyErr_Format(PyExc_TypeError, "With field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3849 goto failed;
3850 }
3851 len = PyList_GET_SIZE(tmp);
3852 body = asdl_seq_new(len, arena);
3853 if (body == NULL) goto failed;
3854 for (i = 0; i < len; i++) {
3855 stmt_ty value;
3856 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3857 if (res != 0) goto failed;
3858 asdl_seq_SET(body, i, value);
3859 }
3860 Py_XDECREF(tmp);
3861 tmp = NULL;
3862 } else {
3863 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from With");
3864 return 1;
3865 }
3866 *out = With(context_expr, optional_vars, body, lineno,
3867 col_offset, arena);
3868 if (*out == NULL) goto failed;
3869 return 0;
3870 }
3871 if (PyObject_IsInstance(obj, (PyObject*)Raise_type)) {
3872 expr_ty type;
3873 expr_ty inst;
3874 expr_ty tback;
3875
3876 if (PyObject_HasAttrString(obj, "type")) {
3877 int res;
3878 tmp = PyObject_GetAttrString(obj, "type");
3879 if (tmp == NULL) goto failed;
3880 res = obj2ast_expr(tmp, &type, arena);
3881 if (res != 0) goto failed;
3882 Py_XDECREF(tmp);
3883 tmp = NULL;
3884 } else {
3885 type = NULL;
3886 }
3887 if (PyObject_HasAttrString(obj, "inst")) {
3888 int res;
3889 tmp = PyObject_GetAttrString(obj, "inst");
3890 if (tmp == NULL) goto failed;
3891 res = obj2ast_expr(tmp, &inst, arena);
3892 if (res != 0) goto failed;
3893 Py_XDECREF(tmp);
3894 tmp = NULL;
3895 } else {
3896 inst = NULL;
3897 }
3898 if (PyObject_HasAttrString(obj, "tback")) {
3899 int res;
3900 tmp = PyObject_GetAttrString(obj, "tback");
3901 if (tmp == NULL) goto failed;
3902 res = obj2ast_expr(tmp, &tback, arena);
3903 if (res != 0) goto failed;
3904 Py_XDECREF(tmp);
3905 tmp = NULL;
3906 } else {
3907 tback = NULL;
3908 }
3909 *out = Raise(type, inst, tback, lineno, col_offset, arena);
3910 if (*out == NULL) goto failed;
3911 return 0;
3912 }
3913 if (PyObject_IsInstance(obj, (PyObject*)TryExcept_type)) {
3914 asdl_seq* body;
3915 asdl_seq* handlers;
3916 asdl_seq* orelse;
3917
3918 if (PyObject_HasAttrString(obj, "body")) {
3919 int res;
3920 Py_ssize_t len;
3921 Py_ssize_t i;
3922 tmp = PyObject_GetAttrString(obj, "body");
3923 if (tmp == NULL) goto failed;
3924 if (!PyList_Check(tmp)) {
3925 PyErr_Format(PyExc_TypeError, "TryExcept field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3926 goto failed;
3927 }
3928 len = PyList_GET_SIZE(tmp);
3929 body = asdl_seq_new(len, arena);
3930 if (body == NULL) goto failed;
3931 for (i = 0; i < len; i++) {
3932 stmt_ty value;
3933 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3934 if (res != 0) goto failed;
3935 asdl_seq_SET(body, i, value);
3936 }
3937 Py_XDECREF(tmp);
3938 tmp = NULL;
3939 } else {
3940 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryExcept");
3941 return 1;
3942 }
3943 if (PyObject_HasAttrString(obj, "handlers")) {
3944 int res;
3945 Py_ssize_t len;
3946 Py_ssize_t i;
3947 tmp = PyObject_GetAttrString(obj, "handlers");
3948 if (tmp == NULL) goto failed;
3949 if (!PyList_Check(tmp)) {
3950 PyErr_Format(PyExc_TypeError, "TryExcept field \"handlers\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3951 goto failed;
3952 }
3953 len = PyList_GET_SIZE(tmp);
3954 handlers = asdl_seq_new(len, arena);
3955 if (handlers == NULL) goto failed;
3956 for (i = 0; i < len; i++) {
3957 excepthandler_ty value;
3958 res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena);
3959 if (res != 0) goto failed;
3960 asdl_seq_SET(handlers, i, value);
3961 }
3962 Py_XDECREF(tmp);
3963 tmp = NULL;
3964 } else {
3965 PyErr_SetString(PyExc_TypeError, "required field \"handlers\" missing from TryExcept");
3966 return 1;
3967 }
3968 if (PyObject_HasAttrString(obj, "orelse")) {
3969 int res;
3970 Py_ssize_t len;
3971 Py_ssize_t i;
3972 tmp = PyObject_GetAttrString(obj, "orelse");
3973 if (tmp == NULL) goto failed;
3974 if (!PyList_Check(tmp)) {
3975 PyErr_Format(PyExc_TypeError, "TryExcept field \"orelse\" must be a list, not a %.200s", tmp->ob_type->tp_name);
3976 goto failed;
3977 }
3978 len = PyList_GET_SIZE(tmp);
3979 orelse = asdl_seq_new(len, arena);
3980 if (orelse == NULL) goto failed;
3981 for (i = 0; i < len; i++) {
3982 stmt_ty value;
3983 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
3984 if (res != 0) goto failed;
3985 asdl_seq_SET(orelse, i, value);
3986 }
3987 Py_XDECREF(tmp);
3988 tmp = NULL;
3989 } else {
3990 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from TryExcept");
3991 return 1;
3992 }
3993 *out = TryExcept(body, handlers, orelse, lineno, col_offset,
3994 arena);
3995 if (*out == NULL) goto failed;
3996 return 0;
3997 }
3998 if (PyObject_IsInstance(obj, (PyObject*)TryFinally_type)) {
3999 asdl_seq* body;
4000 asdl_seq* finalbody;
4001
4002 if (PyObject_HasAttrString(obj, "body")) {
4003 int res;
4004 Py_ssize_t len;
4005 Py_ssize_t i;
4006 tmp = PyObject_GetAttrString(obj, "body");
4007 if (tmp == NULL) goto failed;
4008 if (!PyList_Check(tmp)) {
4009 PyErr_Format(PyExc_TypeError, "TryFinally field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4010 goto failed;
4011 }
4012 len = PyList_GET_SIZE(tmp);
4013 body = asdl_seq_new(len, arena);
4014 if (body == NULL) goto failed;
4015 for (i = 0; i < len; i++) {
4016 stmt_ty value;
4017 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4018 if (res != 0) goto failed;
4019 asdl_seq_SET(body, i, value);
4020 }
4021 Py_XDECREF(tmp);
4022 tmp = NULL;
4023 } else {
4024 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from TryFinally");
4025 return 1;
4026 }
4027 if (PyObject_HasAttrString(obj, "finalbody")) {
4028 int res;
4029 Py_ssize_t len;
4030 Py_ssize_t i;
4031 tmp = PyObject_GetAttrString(obj, "finalbody");
4032 if (tmp == NULL) goto failed;
4033 if (!PyList_Check(tmp)) {
4034 PyErr_Format(PyExc_TypeError, "TryFinally field \"finalbody\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4035 goto failed;
4036 }
4037 len = PyList_GET_SIZE(tmp);
4038 finalbody = asdl_seq_new(len, arena);
4039 if (finalbody == NULL) goto failed;
4040 for (i = 0; i < len; i++) {
4041 stmt_ty value;
4042 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
4043 if (res != 0) goto failed;
4044 asdl_seq_SET(finalbody, i, value);
4045 }
4046 Py_XDECREF(tmp);
4047 tmp = NULL;
4048 } else {
4049 PyErr_SetString(PyExc_TypeError, "required field \"finalbody\" missing from TryFinally");
4050 return 1;
4051 }
4052 *out = TryFinally(body, finalbody, lineno, col_offset, arena);
4053 if (*out == NULL) goto failed;
4054 return 0;
4055 }
4056 if (PyObject_IsInstance(obj, (PyObject*)Assert_type)) {
4057 expr_ty test;
4058 expr_ty msg;
4059
4060 if (PyObject_HasAttrString(obj, "test")) {
4061 int res;
4062 tmp = PyObject_GetAttrString(obj, "test");
4063 if (tmp == NULL) goto failed;
4064 res = obj2ast_expr(tmp, &test, arena);
4065 if (res != 0) goto failed;
4066 Py_XDECREF(tmp);
4067 tmp = NULL;
4068 } else {
4069 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");
4070 return 1;
4071 }
4072 if (PyObject_HasAttrString(obj, "msg")) {
4073 int res;
4074 tmp = PyObject_GetAttrString(obj, "msg");
4075 if (tmp == NULL) goto failed;
4076 res = obj2ast_expr(tmp, &msg, arena);
4077 if (res != 0) goto failed;
4078 Py_XDECREF(tmp);
4079 tmp = NULL;
4080 } else {
4081 msg = NULL;
4082 }
4083 *out = Assert(test, msg, lineno, col_offset, arena);
4084 if (*out == NULL) goto failed;
4085 return 0;
4086 }
4087 if (PyObject_IsInstance(obj, (PyObject*)Import_type)) {
4088 asdl_seq* names;
4089
4090 if (PyObject_HasAttrString(obj, "names")) {
4091 int res;
4092 Py_ssize_t len;
4093 Py_ssize_t i;
4094 tmp = PyObject_GetAttrString(obj, "names");
4095 if (tmp == NULL) goto failed;
4096 if (!PyList_Check(tmp)) {
4097 PyErr_Format(PyExc_TypeError, "Import field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4098 goto failed;
4099 }
4100 len = PyList_GET_SIZE(tmp);
4101 names = asdl_seq_new(len, arena);
4102 if (names == NULL) goto failed;
4103 for (i = 0; i < len; i++) {
4104 alias_ty value;
4105 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4106 if (res != 0) goto failed;
4107 asdl_seq_SET(names, i, value);
4108 }
4109 Py_XDECREF(tmp);
4110 tmp = NULL;
4111 } else {
4112 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Import");
4113 return 1;
4114 }
4115 *out = Import(names, lineno, col_offset, arena);
4116 if (*out == NULL) goto failed;
4117 return 0;
4118 }
4119 if (PyObject_IsInstance(obj, (PyObject*)ImportFrom_type)) {
4120 identifier module;
4121 asdl_seq* names;
4122 int level;
4123
4124 if (PyObject_HasAttrString(obj, "module")) {
4125 int res;
4126 tmp = PyObject_GetAttrString(obj, "module");
4127 if (tmp == NULL) goto failed;
4128 res = obj2ast_identifier(tmp, &module, arena);
4129 if (res != 0) goto failed;
4130 Py_XDECREF(tmp);
4131 tmp = NULL;
4132 } else {
4133 PyErr_SetString(PyExc_TypeError, "required field \"module\" missing from ImportFrom");
4134 return 1;
4135 }
4136 if (PyObject_HasAttrString(obj, "names")) {
4137 int res;
4138 Py_ssize_t len;
4139 Py_ssize_t i;
4140 tmp = PyObject_GetAttrString(obj, "names");
4141 if (tmp == NULL) goto failed;
4142 if (!PyList_Check(tmp)) {
4143 PyErr_Format(PyExc_TypeError, "ImportFrom field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4144 goto failed;
4145 }
4146 len = PyList_GET_SIZE(tmp);
4147 names = asdl_seq_new(len, arena);
4148 if (names == NULL) goto failed;
4149 for (i = 0; i < len; i++) {
4150 alias_ty value;
4151 res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena);
4152 if (res != 0) goto failed;
4153 asdl_seq_SET(names, i, value);
4154 }
4155 Py_XDECREF(tmp);
4156 tmp = NULL;
4157 } else {
4158 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");
4159 return 1;
4160 }
4161 if (PyObject_HasAttrString(obj, "level")) {
4162 int res;
4163 tmp = PyObject_GetAttrString(obj, "level");
4164 if (tmp == NULL) goto failed;
4165 res = obj2ast_int(tmp, &level, arena);
4166 if (res != 0) goto failed;
4167 Py_XDECREF(tmp);
4168 tmp = NULL;
4169 } else {
4170 level = 0;
4171 }
4172 *out = ImportFrom(module, names, level, lineno, col_offset,
4173 arena);
4174 if (*out == NULL) goto failed;
4175 return 0;
4176 }
4177 if (PyObject_IsInstance(obj, (PyObject*)Exec_type)) {
4178 expr_ty body;
4179 expr_ty globals;
4180 expr_ty locals;
4181
4182 if (PyObject_HasAttrString(obj, "body")) {
4183 int res;
4184 tmp = PyObject_GetAttrString(obj, "body");
4185 if (tmp == NULL) goto failed;
4186 res = obj2ast_expr(tmp, &body, arena);
4187 if (res != 0) goto failed;
4188 Py_XDECREF(tmp);
4189 tmp = NULL;
4190 } else {
4191 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Exec");
4192 return 1;
4193 }
4194 if (PyObject_HasAttrString(obj, "globals")) {
4195 int res;
4196 tmp = PyObject_GetAttrString(obj, "globals");
4197 if (tmp == NULL) goto failed;
4198 res = obj2ast_expr(tmp, &globals, arena);
4199 if (res != 0) goto failed;
4200 Py_XDECREF(tmp);
4201 tmp = NULL;
4202 } else {
4203 globals = NULL;
4204 }
4205 if (PyObject_HasAttrString(obj, "locals")) {
4206 int res;
4207 tmp = PyObject_GetAttrString(obj, "locals");
4208 if (tmp == NULL) goto failed;
4209 res = obj2ast_expr(tmp, &locals, arena);
4210 if (res != 0) goto failed;
4211 Py_XDECREF(tmp);
4212 tmp = NULL;
4213 } else {
4214 locals = NULL;
4215 }
4216 *out = Exec(body, globals, locals, lineno, col_offset, arena);
4217 if (*out == NULL) goto failed;
4218 return 0;
4219 }
4220 if (PyObject_IsInstance(obj, (PyObject*)Global_type)) {
4221 asdl_seq* names;
4222
4223 if (PyObject_HasAttrString(obj, "names")) {
4224 int res;
4225 Py_ssize_t len;
4226 Py_ssize_t i;
4227 tmp = PyObject_GetAttrString(obj, "names");
4228 if (tmp == NULL) goto failed;
4229 if (!PyList_Check(tmp)) {
4230 PyErr_Format(PyExc_TypeError, "Global field \"names\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4231 goto failed;
4232 }
4233 len = PyList_GET_SIZE(tmp);
4234 names = asdl_seq_new(len, arena);
4235 if (names == NULL) goto failed;
4236 for (i = 0; i < len; i++) {
4237 identifier value;
4238 res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena);
4239 if (res != 0) goto failed;
4240 asdl_seq_SET(names, i, value);
4241 }
4242 Py_XDECREF(tmp);
4243 tmp = NULL;
4244 } else {
4245 PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from Global");
4246 return 1;
4247 }
4248 *out = Global(names, lineno, col_offset, arena);
4249 if (*out == NULL) goto failed;
4250 return 0;
4251 }
4252 if (PyObject_IsInstance(obj, (PyObject*)Expr_type)) {
4253 expr_ty value;
4254
4255 if (PyObject_HasAttrString(obj, "value")) {
4256 int res;
4257 tmp = PyObject_GetAttrString(obj, "value");
4258 if (tmp == NULL) goto failed;
4259 res = obj2ast_expr(tmp, &value, arena);
4260 if (res != 0) goto failed;
4261 Py_XDECREF(tmp);
4262 tmp = NULL;
4263 } else {
4264 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Expr");
4265 return 1;
4266 }
4267 *out = Expr(value, lineno, col_offset, arena);
4268 if (*out == NULL) goto failed;
4269 return 0;
4270 }
4271 if (PyObject_IsInstance(obj, (PyObject*)Pass_type)) {
4272
4273 *out = Pass(lineno, col_offset, arena);
4274 if (*out == NULL) goto failed;
4275 return 0;
4276 }
4277 if (PyObject_IsInstance(obj, (PyObject*)Break_type)) {
4278
4279 *out = Break(lineno, col_offset, arena);
4280 if (*out == NULL) goto failed;
4281 return 0;
4282 }
4283 if (PyObject_IsInstance(obj, (PyObject*)Continue_type)) {
4284
4285 *out = Continue(lineno, col_offset, arena);
4286 if (*out == NULL) goto failed;
4287 return 0;
4288 }
4289
4290 tmp = PyObject_Repr(obj);
4291 if (tmp == NULL) goto failed;
4292 PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyString_AS_STRING(tmp));
4293failed:
4294 Py_XDECREF(tmp);
4295 return 1;
4296}
4297
4298int
4299obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
4300{
4301 PyObject* tmp = NULL;
4302
4303 int lineno;
4304 int col_offset;
4305
4306 if (obj == Py_None) {
4307 *out = NULL;
4308 return 0;
4309 }
4310 if (PyObject_HasAttrString(obj, "lineno")) {
4311 int res;
4312 tmp = PyObject_GetAttrString(obj, "lineno");
4313 if (tmp == NULL) goto failed;
4314 res = obj2ast_int(tmp, &lineno, arena);
4315 if (res != 0) goto failed;
4316 Py_XDECREF(tmp);
4317 tmp = NULL;
4318 } else {
4319 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from expr");
4320 return 1;
4321 }
4322 if (PyObject_HasAttrString(obj, "col_offset")) {
4323 int res;
4324 tmp = PyObject_GetAttrString(obj, "col_offset");
4325 if (tmp == NULL) goto failed;
4326 res = obj2ast_int(tmp, &col_offset, arena);
4327 if (res != 0) goto failed;
4328 Py_XDECREF(tmp);
4329 tmp = NULL;
4330 } else {
4331 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from expr");
4332 return 1;
4333 }
4334 if (PyObject_IsInstance(obj, (PyObject*)BoolOp_type)) {
4335 boolop_ty op;
4336 asdl_seq* values;
4337
4338 if (PyObject_HasAttrString(obj, "op")) {
4339 int res;
4340 tmp = PyObject_GetAttrString(obj, "op");
4341 if (tmp == NULL) goto failed;
4342 res = obj2ast_boolop(tmp, &op, arena);
4343 if (res != 0) goto failed;
4344 Py_XDECREF(tmp);
4345 tmp = NULL;
4346 } else {
4347 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BoolOp");
4348 return 1;
4349 }
4350 if (PyObject_HasAttrString(obj, "values")) {
4351 int res;
4352 Py_ssize_t len;
4353 Py_ssize_t i;
4354 tmp = PyObject_GetAttrString(obj, "values");
4355 if (tmp == NULL) goto failed;
4356 if (!PyList_Check(tmp)) {
4357 PyErr_Format(PyExc_TypeError, "BoolOp field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4358 goto failed;
4359 }
4360 len = PyList_GET_SIZE(tmp);
4361 values = asdl_seq_new(len, arena);
4362 if (values == NULL) goto failed;
4363 for (i = 0; i < len; i++) {
4364 expr_ty value;
4365 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4366 if (res != 0) goto failed;
4367 asdl_seq_SET(values, i, value);
4368 }
4369 Py_XDECREF(tmp);
4370 tmp = NULL;
4371 } else {
4372 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from BoolOp");
4373 return 1;
4374 }
4375 *out = BoolOp(op, values, lineno, col_offset, arena);
4376 if (*out == NULL) goto failed;
4377 return 0;
4378 }
4379 if (PyObject_IsInstance(obj, (PyObject*)BinOp_type)) {
4380 expr_ty left;
4381 operator_ty op;
4382 expr_ty right;
4383
4384 if (PyObject_HasAttrString(obj, "left")) {
4385 int res;
4386 tmp = PyObject_GetAttrString(obj, "left");
4387 if (tmp == NULL) goto failed;
4388 res = obj2ast_expr(tmp, &left, arena);
4389 if (res != 0) goto failed;
4390 Py_XDECREF(tmp);
4391 tmp = NULL;
4392 } else {
4393 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from BinOp");
4394 return 1;
4395 }
4396 if (PyObject_HasAttrString(obj, "op")) {
4397 int res;
4398 tmp = PyObject_GetAttrString(obj, "op");
4399 if (tmp == NULL) goto failed;
4400 res = obj2ast_operator(tmp, &op, arena);
4401 if (res != 0) goto failed;
4402 Py_XDECREF(tmp);
4403 tmp = NULL;
4404 } else {
4405 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from BinOp");
4406 return 1;
4407 }
4408 if (PyObject_HasAttrString(obj, "right")) {
4409 int res;
4410 tmp = PyObject_GetAttrString(obj, "right");
4411 if (tmp == NULL) goto failed;
4412 res = obj2ast_expr(tmp, &right, arena);
4413 if (res != 0) goto failed;
4414 Py_XDECREF(tmp);
4415 tmp = NULL;
4416 } else {
4417 PyErr_SetString(PyExc_TypeError, "required field \"right\" missing from BinOp");
4418 return 1;
4419 }
4420 *out = BinOp(left, op, right, lineno, col_offset, arena);
4421 if (*out == NULL) goto failed;
4422 return 0;
4423 }
4424 if (PyObject_IsInstance(obj, (PyObject*)UnaryOp_type)) {
4425 unaryop_ty op;
4426 expr_ty operand;
4427
4428 if (PyObject_HasAttrString(obj, "op")) {
4429 int res;
4430 tmp = PyObject_GetAttrString(obj, "op");
4431 if (tmp == NULL) goto failed;
4432 res = obj2ast_unaryop(tmp, &op, arena);
4433 if (res != 0) goto failed;
4434 Py_XDECREF(tmp);
4435 tmp = NULL;
4436 } else {
4437 PyErr_SetString(PyExc_TypeError, "required field \"op\" missing from UnaryOp");
4438 return 1;
4439 }
4440 if (PyObject_HasAttrString(obj, "operand")) {
4441 int res;
4442 tmp = PyObject_GetAttrString(obj, "operand");
4443 if (tmp == NULL) goto failed;
4444 res = obj2ast_expr(tmp, &operand, arena);
4445 if (res != 0) goto failed;
4446 Py_XDECREF(tmp);
4447 tmp = NULL;
4448 } else {
4449 PyErr_SetString(PyExc_TypeError, "required field \"operand\" missing from UnaryOp");
4450 return 1;
4451 }
4452 *out = UnaryOp(op, operand, lineno, col_offset, arena);
4453 if (*out == NULL) goto failed;
4454 return 0;
4455 }
4456 if (PyObject_IsInstance(obj, (PyObject*)Lambda_type)) {
4457 arguments_ty args;
4458 expr_ty body;
4459
4460 if (PyObject_HasAttrString(obj, "args")) {
4461 int res;
4462 tmp = PyObject_GetAttrString(obj, "args");
4463 if (tmp == NULL) goto failed;
4464 res = obj2ast_arguments(tmp, &args, arena);
4465 if (res != 0) goto failed;
4466 Py_XDECREF(tmp);
4467 tmp = NULL;
4468 } else {
4469 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Lambda");
4470 return 1;
4471 }
4472 if (PyObject_HasAttrString(obj, "body")) {
4473 int res;
4474 tmp = PyObject_GetAttrString(obj, "body");
4475 if (tmp == NULL) goto failed;
4476 res = obj2ast_expr(tmp, &body, arena);
4477 if (res != 0) goto failed;
4478 Py_XDECREF(tmp);
4479 tmp = NULL;
4480 } else {
4481 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Lambda");
4482 return 1;
4483 }
4484 *out = Lambda(args, body, lineno, col_offset, arena);
4485 if (*out == NULL) goto failed;
4486 return 0;
4487 }
4488 if (PyObject_IsInstance(obj, (PyObject*)IfExp_type)) {
4489 expr_ty test;
4490 expr_ty body;
4491 expr_ty orelse;
4492
4493 if (PyObject_HasAttrString(obj, "test")) {
4494 int res;
4495 tmp = PyObject_GetAttrString(obj, "test");
4496 if (tmp == NULL) goto failed;
4497 res = obj2ast_expr(tmp, &test, arena);
4498 if (res != 0) goto failed;
4499 Py_XDECREF(tmp);
4500 tmp = NULL;
4501 } else {
4502 PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from IfExp");
4503 return 1;
4504 }
4505 if (PyObject_HasAttrString(obj, "body")) {
4506 int res;
4507 tmp = PyObject_GetAttrString(obj, "body");
4508 if (tmp == NULL) goto failed;
4509 res = obj2ast_expr(tmp, &body, arena);
4510 if (res != 0) goto failed;
4511 Py_XDECREF(tmp);
4512 tmp = NULL;
4513 } else {
4514 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from IfExp");
4515 return 1;
4516 }
4517 if (PyObject_HasAttrString(obj, "orelse")) {
4518 int res;
4519 tmp = PyObject_GetAttrString(obj, "orelse");
4520 if (tmp == NULL) goto failed;
4521 res = obj2ast_expr(tmp, &orelse, arena);
4522 if (res != 0) goto failed;
4523 Py_XDECREF(tmp);
4524 tmp = NULL;
4525 } else {
4526 PyErr_SetString(PyExc_TypeError, "required field \"orelse\" missing from IfExp");
4527 return 1;
4528 }
4529 *out = IfExp(test, body, orelse, lineno, col_offset, arena);
4530 if (*out == NULL) goto failed;
4531 return 0;
4532 }
4533 if (PyObject_IsInstance(obj, (PyObject*)Dict_type)) {
4534 asdl_seq* keys;
4535 asdl_seq* values;
4536
4537 if (PyObject_HasAttrString(obj, "keys")) {
4538 int res;
4539 Py_ssize_t len;
4540 Py_ssize_t i;
4541 tmp = PyObject_GetAttrString(obj, "keys");
4542 if (tmp == NULL) goto failed;
4543 if (!PyList_Check(tmp)) {
4544 PyErr_Format(PyExc_TypeError, "Dict field \"keys\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4545 goto failed;
4546 }
4547 len = PyList_GET_SIZE(tmp);
4548 keys = asdl_seq_new(len, arena);
4549 if (keys == NULL) goto failed;
4550 for (i = 0; i < len; i++) {
4551 expr_ty value;
4552 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4553 if (res != 0) goto failed;
4554 asdl_seq_SET(keys, i, value);
4555 }
4556 Py_XDECREF(tmp);
4557 tmp = NULL;
4558 } else {
4559 PyErr_SetString(PyExc_TypeError, "required field \"keys\" missing from Dict");
4560 return 1;
4561 }
4562 if (PyObject_HasAttrString(obj, "values")) {
4563 int res;
4564 Py_ssize_t len;
4565 Py_ssize_t i;
4566 tmp = PyObject_GetAttrString(obj, "values");
4567 if (tmp == NULL) goto failed;
4568 if (!PyList_Check(tmp)) {
4569 PyErr_Format(PyExc_TypeError, "Dict field \"values\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4570 goto failed;
4571 }
4572 len = PyList_GET_SIZE(tmp);
4573 values = asdl_seq_new(len, arena);
4574 if (values == NULL) goto failed;
4575 for (i = 0; i < len; i++) {
4576 expr_ty value;
4577 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4578 if (res != 0) goto failed;
4579 asdl_seq_SET(values, i, value);
4580 }
4581 Py_XDECREF(tmp);
4582 tmp = NULL;
4583 } else {
4584 PyErr_SetString(PyExc_TypeError, "required field \"values\" missing from Dict");
4585 return 1;
4586 }
4587 *out = Dict(keys, values, lineno, col_offset, arena);
4588 if (*out == NULL) goto failed;
4589 return 0;
4590 }
4591 if (PyObject_IsInstance(obj, (PyObject*)ListComp_type)) {
4592 expr_ty elt;
4593 asdl_seq* generators;
4594
4595 if (PyObject_HasAttrString(obj, "elt")) {
4596 int res;
4597 tmp = PyObject_GetAttrString(obj, "elt");
4598 if (tmp == NULL) goto failed;
4599 res = obj2ast_expr(tmp, &elt, arena);
4600 if (res != 0) goto failed;
4601 Py_XDECREF(tmp);
4602 tmp = NULL;
4603 } else {
4604 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from ListComp");
4605 return 1;
4606 }
4607 if (PyObject_HasAttrString(obj, "generators")) {
4608 int res;
4609 Py_ssize_t len;
4610 Py_ssize_t i;
4611 tmp = PyObject_GetAttrString(obj, "generators");
4612 if (tmp == NULL) goto failed;
4613 if (!PyList_Check(tmp)) {
4614 PyErr_Format(PyExc_TypeError, "ListComp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4615 goto failed;
4616 }
4617 len = PyList_GET_SIZE(tmp);
4618 generators = asdl_seq_new(len, arena);
4619 if (generators == NULL) goto failed;
4620 for (i = 0; i < len; i++) {
4621 comprehension_ty value;
4622 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4623 if (res != 0) goto failed;
4624 asdl_seq_SET(generators, i, value);
4625 }
4626 Py_XDECREF(tmp);
4627 tmp = NULL;
4628 } else {
4629 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from ListComp");
4630 return 1;
4631 }
4632 *out = ListComp(elt, generators, lineno, col_offset, arena);
4633 if (*out == NULL) goto failed;
4634 return 0;
4635 }
4636 if (PyObject_IsInstance(obj, (PyObject*)GeneratorExp_type)) {
4637 expr_ty elt;
4638 asdl_seq* generators;
4639
4640 if (PyObject_HasAttrString(obj, "elt")) {
4641 int res;
4642 tmp = PyObject_GetAttrString(obj, "elt");
4643 if (tmp == NULL) goto failed;
4644 res = obj2ast_expr(tmp, &elt, arena);
4645 if (res != 0) goto failed;
4646 Py_XDECREF(tmp);
4647 tmp = NULL;
4648 } else {
4649 PyErr_SetString(PyExc_TypeError, "required field \"elt\" missing from GeneratorExp");
4650 return 1;
4651 }
4652 if (PyObject_HasAttrString(obj, "generators")) {
4653 int res;
4654 Py_ssize_t len;
4655 Py_ssize_t i;
4656 tmp = PyObject_GetAttrString(obj, "generators");
4657 if (tmp == NULL) goto failed;
4658 if (!PyList_Check(tmp)) {
4659 PyErr_Format(PyExc_TypeError, "GeneratorExp field \"generators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4660 goto failed;
4661 }
4662 len = PyList_GET_SIZE(tmp);
4663 generators = asdl_seq_new(len, arena);
4664 if (generators == NULL) goto failed;
4665 for (i = 0; i < len; i++) {
4666 comprehension_ty value;
4667 res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena);
4668 if (res != 0) goto failed;
4669 asdl_seq_SET(generators, i, value);
4670 }
4671 Py_XDECREF(tmp);
4672 tmp = NULL;
4673 } else {
4674 PyErr_SetString(PyExc_TypeError, "required field \"generators\" missing from GeneratorExp");
4675 return 1;
4676 }
4677 *out = GeneratorExp(elt, generators, lineno, col_offset, arena);
4678 if (*out == NULL) goto failed;
4679 return 0;
4680 }
4681 if (PyObject_IsInstance(obj, (PyObject*)Yield_type)) {
4682 expr_ty value;
4683
4684 if (PyObject_HasAttrString(obj, "value")) {
4685 int res;
4686 tmp = PyObject_GetAttrString(obj, "value");
4687 if (tmp == NULL) goto failed;
4688 res = obj2ast_expr(tmp, &value, arena);
4689 if (res != 0) goto failed;
4690 Py_XDECREF(tmp);
4691 tmp = NULL;
4692 } else {
4693 value = NULL;
4694 }
4695 *out = Yield(value, lineno, col_offset, arena);
4696 if (*out == NULL) goto failed;
4697 return 0;
4698 }
4699 if (PyObject_IsInstance(obj, (PyObject*)Compare_type)) {
4700 expr_ty left;
4701 asdl_int_seq* ops;
4702 asdl_seq* comparators;
4703
4704 if (PyObject_HasAttrString(obj, "left")) {
4705 int res;
4706 tmp = PyObject_GetAttrString(obj, "left");
4707 if (tmp == NULL) goto failed;
4708 res = obj2ast_expr(tmp, &left, arena);
4709 if (res != 0) goto failed;
4710 Py_XDECREF(tmp);
4711 tmp = NULL;
4712 } else {
4713 PyErr_SetString(PyExc_TypeError, "required field \"left\" missing from Compare");
4714 return 1;
4715 }
4716 if (PyObject_HasAttrString(obj, "ops")) {
4717 int res;
4718 Py_ssize_t len;
4719 Py_ssize_t i;
4720 tmp = PyObject_GetAttrString(obj, "ops");
4721 if (tmp == NULL) goto failed;
4722 if (!PyList_Check(tmp)) {
4723 PyErr_Format(PyExc_TypeError, "Compare field \"ops\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4724 goto failed;
4725 }
4726 len = PyList_GET_SIZE(tmp);
4727 ops = asdl_int_seq_new(len, arena);
4728 if (ops == NULL) goto failed;
4729 for (i = 0; i < len; i++) {
4730 cmpop_ty value;
4731 res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena);
4732 if (res != 0) goto failed;
4733 asdl_seq_SET(ops, i, value);
4734 }
4735 Py_XDECREF(tmp);
4736 tmp = NULL;
4737 } else {
4738 PyErr_SetString(PyExc_TypeError, "required field \"ops\" missing from Compare");
4739 return 1;
4740 }
4741 if (PyObject_HasAttrString(obj, "comparators")) {
4742 int res;
4743 Py_ssize_t len;
4744 Py_ssize_t i;
4745 tmp = PyObject_GetAttrString(obj, "comparators");
4746 if (tmp == NULL) goto failed;
4747 if (!PyList_Check(tmp)) {
4748 PyErr_Format(PyExc_TypeError, "Compare field \"comparators\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4749 goto failed;
4750 }
4751 len = PyList_GET_SIZE(tmp);
4752 comparators = asdl_seq_new(len, arena);
4753 if (comparators == NULL) goto failed;
4754 for (i = 0; i < len; i++) {
4755 expr_ty value;
4756 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4757 if (res != 0) goto failed;
4758 asdl_seq_SET(comparators, i, value);
4759 }
4760 Py_XDECREF(tmp);
4761 tmp = NULL;
4762 } else {
4763 PyErr_SetString(PyExc_TypeError, "required field \"comparators\" missing from Compare");
4764 return 1;
4765 }
4766 *out = Compare(left, ops, comparators, lineno, col_offset,
4767 arena);
4768 if (*out == NULL) goto failed;
4769 return 0;
4770 }
4771 if (PyObject_IsInstance(obj, (PyObject*)Call_type)) {
4772 expr_ty func;
4773 asdl_seq* args;
4774 asdl_seq* keywords;
4775 expr_ty starargs;
4776 expr_ty kwargs;
4777
4778 if (PyObject_HasAttrString(obj, "func")) {
4779 int res;
4780 tmp = PyObject_GetAttrString(obj, "func");
4781 if (tmp == NULL) goto failed;
4782 res = obj2ast_expr(tmp, &func, arena);
4783 if (res != 0) goto failed;
4784 Py_XDECREF(tmp);
4785 tmp = NULL;
4786 } else {
4787 PyErr_SetString(PyExc_TypeError, "required field \"func\" missing from Call");
4788 return 1;
4789 }
4790 if (PyObject_HasAttrString(obj, "args")) {
4791 int res;
4792 Py_ssize_t len;
4793 Py_ssize_t i;
4794 tmp = PyObject_GetAttrString(obj, "args");
4795 if (tmp == NULL) goto failed;
4796 if (!PyList_Check(tmp)) {
4797 PyErr_Format(PyExc_TypeError, "Call field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4798 goto failed;
4799 }
4800 len = PyList_GET_SIZE(tmp);
4801 args = asdl_seq_new(len, arena);
4802 if (args == NULL) goto failed;
4803 for (i = 0; i < len; i++) {
4804 expr_ty value;
4805 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
4806 if (res != 0) goto failed;
4807 asdl_seq_SET(args, i, value);
4808 }
4809 Py_XDECREF(tmp);
4810 tmp = NULL;
4811 } else {
4812 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from Call");
4813 return 1;
4814 }
4815 if (PyObject_HasAttrString(obj, "keywords")) {
4816 int res;
4817 Py_ssize_t len;
4818 Py_ssize_t i;
4819 tmp = PyObject_GetAttrString(obj, "keywords");
4820 if (tmp == NULL) goto failed;
4821 if (!PyList_Check(tmp)) {
4822 PyErr_Format(PyExc_TypeError, "Call field \"keywords\" must be a list, not a %.200s", tmp->ob_type->tp_name);
4823 goto failed;
4824 }
4825 len = PyList_GET_SIZE(tmp);
4826 keywords = asdl_seq_new(len, arena);
4827 if (keywords == NULL) goto failed;
4828 for (i = 0; i < len; i++) {
4829 keyword_ty value;
4830 res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena);
4831 if (res != 0) goto failed;
4832 asdl_seq_SET(keywords, i, value);
4833 }
4834 Py_XDECREF(tmp);
4835 tmp = NULL;
4836 } else {
4837 PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");
4838 return 1;
4839 }
4840 if (PyObject_HasAttrString(obj, "starargs")) {
4841 int res;
4842 tmp = PyObject_GetAttrString(obj, "starargs");
4843 if (tmp == NULL) goto failed;
4844 res = obj2ast_expr(tmp, &starargs, arena);
4845 if (res != 0) goto failed;
4846 Py_XDECREF(tmp);
4847 tmp = NULL;
4848 } else {
4849 starargs = NULL;
4850 }
4851 if (PyObject_HasAttrString(obj, "kwargs")) {
4852 int res;
4853 tmp = PyObject_GetAttrString(obj, "kwargs");
4854 if (tmp == NULL) goto failed;
4855 res = obj2ast_expr(tmp, &kwargs, arena);
4856 if (res != 0) goto failed;
4857 Py_XDECREF(tmp);
4858 tmp = NULL;
4859 } else {
4860 kwargs = NULL;
4861 }
4862 *out = Call(func, args, keywords, starargs, kwargs, lineno,
4863 col_offset, arena);
4864 if (*out == NULL) goto failed;
4865 return 0;
4866 }
4867 if (PyObject_IsInstance(obj, (PyObject*)Repr_type)) {
4868 expr_ty value;
4869
4870 if (PyObject_HasAttrString(obj, "value")) {
4871 int res;
4872 tmp = PyObject_GetAttrString(obj, "value");
4873 if (tmp == NULL) goto failed;
4874 res = obj2ast_expr(tmp, &value, arena);
4875 if (res != 0) goto failed;
4876 Py_XDECREF(tmp);
4877 tmp = NULL;
4878 } else {
4879 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Repr");
4880 return 1;
4881 }
4882 *out = Repr(value, lineno, col_offset, arena);
4883 if (*out == NULL) goto failed;
4884 return 0;
4885 }
4886 if (PyObject_IsInstance(obj, (PyObject*)Num_type)) {
4887 object n;
4888
4889 if (PyObject_HasAttrString(obj, "n")) {
4890 int res;
4891 tmp = PyObject_GetAttrString(obj, "n");
4892 if (tmp == NULL) goto failed;
4893 res = obj2ast_object(tmp, &n, arena);
4894 if (res != 0) goto failed;
4895 Py_XDECREF(tmp);
4896 tmp = NULL;
4897 } else {
4898 PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num");
4899 return 1;
4900 }
4901 *out = Num(n, lineno, col_offset, arena);
4902 if (*out == NULL) goto failed;
4903 return 0;
4904 }
4905 if (PyObject_IsInstance(obj, (PyObject*)Str_type)) {
4906 string s;
4907
4908 if (PyObject_HasAttrString(obj, "s")) {
4909 int res;
4910 tmp = PyObject_GetAttrString(obj, "s");
4911 if (tmp == NULL) goto failed;
4912 res = obj2ast_string(tmp, &s, arena);
4913 if (res != 0) goto failed;
4914 Py_XDECREF(tmp);
4915 tmp = NULL;
4916 } else {
4917 PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
4918 return 1;
4919 }
4920 *out = Str(s, lineno, col_offset, arena);
4921 if (*out == NULL) goto failed;
4922 return 0;
4923 }
4924 if (PyObject_IsInstance(obj, (PyObject*)Attribute_type)) {
4925 expr_ty value;
4926 identifier attr;
4927 expr_context_ty ctx;
4928
4929 if (PyObject_HasAttrString(obj, "value")) {
4930 int res;
4931 tmp = PyObject_GetAttrString(obj, "value");
4932 if (tmp == NULL) goto failed;
4933 res = obj2ast_expr(tmp, &value, arena);
4934 if (res != 0) goto failed;
4935 Py_XDECREF(tmp);
4936 tmp = NULL;
4937 } else {
4938 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Attribute");
4939 return 1;
4940 }
4941 if (PyObject_HasAttrString(obj, "attr")) {
4942 int res;
4943 tmp = PyObject_GetAttrString(obj, "attr");
4944 if (tmp == NULL) goto failed;
4945 res = obj2ast_identifier(tmp, &attr, arena);
4946 if (res != 0) goto failed;
4947 Py_XDECREF(tmp);
4948 tmp = NULL;
4949 } else {
4950 PyErr_SetString(PyExc_TypeError, "required field \"attr\" missing from Attribute");
4951 return 1;
4952 }
4953 if (PyObject_HasAttrString(obj, "ctx")) {
4954 int res;
4955 tmp = PyObject_GetAttrString(obj, "ctx");
4956 if (tmp == NULL) goto failed;
4957 res = obj2ast_expr_context(tmp, &ctx, arena);
4958 if (res != 0) goto failed;
4959 Py_XDECREF(tmp);
4960 tmp = NULL;
4961 } else {
4962 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Attribute");
4963 return 1;
4964 }
4965 *out = Attribute(value, attr, ctx, lineno, col_offset, arena);
4966 if (*out == NULL) goto failed;
4967 return 0;
4968 }
4969 if (PyObject_IsInstance(obj, (PyObject*)Subscript_type)) {
4970 expr_ty value;
4971 slice_ty slice;
4972 expr_context_ty ctx;
4973
4974 if (PyObject_HasAttrString(obj, "value")) {
4975 int res;
4976 tmp = PyObject_GetAttrString(obj, "value");
4977 if (tmp == NULL) goto failed;
4978 res = obj2ast_expr(tmp, &value, arena);
4979 if (res != 0) goto failed;
4980 Py_XDECREF(tmp);
4981 tmp = NULL;
4982 } else {
4983 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Subscript");
4984 return 1;
4985 }
4986 if (PyObject_HasAttrString(obj, "slice")) {
4987 int res;
4988 tmp = PyObject_GetAttrString(obj, "slice");
4989 if (tmp == NULL) goto failed;
4990 res = obj2ast_slice(tmp, &slice, arena);
4991 if (res != 0) goto failed;
4992 Py_XDECREF(tmp);
4993 tmp = NULL;
4994 } else {
4995 PyErr_SetString(PyExc_TypeError, "required field \"slice\" missing from Subscript");
4996 return 1;
4997 }
4998 if (PyObject_HasAttrString(obj, "ctx")) {
4999 int res;
5000 tmp = PyObject_GetAttrString(obj, "ctx");
5001 if (tmp == NULL) goto failed;
5002 res = obj2ast_expr_context(tmp, &ctx, arena);
5003 if (res != 0) goto failed;
5004 Py_XDECREF(tmp);
5005 tmp = NULL;
5006 } else {
5007 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Subscript");
5008 return 1;
5009 }
5010 *out = Subscript(value, slice, ctx, lineno, col_offset, arena);
5011 if (*out == NULL) goto failed;
5012 return 0;
5013 }
5014 if (PyObject_IsInstance(obj, (PyObject*)Name_type)) {
5015 identifier id;
5016 expr_context_ty ctx;
5017
5018 if (PyObject_HasAttrString(obj, "id")) {
5019 int res;
5020 tmp = PyObject_GetAttrString(obj, "id");
5021 if (tmp == NULL) goto failed;
5022 res = obj2ast_identifier(tmp, &id, arena);
5023 if (res != 0) goto failed;
5024 Py_XDECREF(tmp);
5025 tmp = NULL;
5026 } else {
5027 PyErr_SetString(PyExc_TypeError, "required field \"id\" missing from Name");
5028 return 1;
5029 }
5030 if (PyObject_HasAttrString(obj, "ctx")) {
5031 int res;
5032 tmp = PyObject_GetAttrString(obj, "ctx");
5033 if (tmp == NULL) goto failed;
5034 res = obj2ast_expr_context(tmp, &ctx, arena);
5035 if (res != 0) goto failed;
5036 Py_XDECREF(tmp);
5037 tmp = NULL;
5038 } else {
5039 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Name");
5040 return 1;
5041 }
5042 *out = Name(id, ctx, lineno, col_offset, arena);
5043 if (*out == NULL) goto failed;
5044 return 0;
5045 }
5046 if (PyObject_IsInstance(obj, (PyObject*)List_type)) {
5047 asdl_seq* elts;
5048 expr_context_ty ctx;
5049
5050 if (PyObject_HasAttrString(obj, "elts")) {
5051 int res;
5052 Py_ssize_t len;
5053 Py_ssize_t i;
5054 tmp = PyObject_GetAttrString(obj, "elts");
5055 if (tmp == NULL) goto failed;
5056 if (!PyList_Check(tmp)) {
5057 PyErr_Format(PyExc_TypeError, "List field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5058 goto failed;
5059 }
5060 len = PyList_GET_SIZE(tmp);
5061 elts = asdl_seq_new(len, arena);
5062 if (elts == NULL) goto failed;
5063 for (i = 0; i < len; i++) {
5064 expr_ty value;
5065 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5066 if (res != 0) goto failed;
5067 asdl_seq_SET(elts, i, value);
5068 }
5069 Py_XDECREF(tmp);
5070 tmp = NULL;
5071 } else {
5072 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from List");
5073 return 1;
5074 }
5075 if (PyObject_HasAttrString(obj, "ctx")) {
5076 int res;
5077 tmp = PyObject_GetAttrString(obj, "ctx");
5078 if (tmp == NULL) goto failed;
5079 res = obj2ast_expr_context(tmp, &ctx, arena);
5080 if (res != 0) goto failed;
5081 Py_XDECREF(tmp);
5082 tmp = NULL;
5083 } else {
5084 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from List");
5085 return 1;
5086 }
5087 *out = List(elts, ctx, lineno, col_offset, arena);
5088 if (*out == NULL) goto failed;
5089 return 0;
5090 }
5091 if (PyObject_IsInstance(obj, (PyObject*)Tuple_type)) {
5092 asdl_seq* elts;
5093 expr_context_ty ctx;
5094
5095 if (PyObject_HasAttrString(obj, "elts")) {
5096 int res;
5097 Py_ssize_t len;
5098 Py_ssize_t i;
5099 tmp = PyObject_GetAttrString(obj, "elts");
5100 if (tmp == NULL) goto failed;
5101 if (!PyList_Check(tmp)) {
5102 PyErr_Format(PyExc_TypeError, "Tuple field \"elts\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5103 goto failed;
5104 }
5105 len = PyList_GET_SIZE(tmp);
5106 elts = asdl_seq_new(len, arena);
5107 if (elts == NULL) goto failed;
5108 for (i = 0; i < len; i++) {
5109 expr_ty value;
5110 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5111 if (res != 0) goto failed;
5112 asdl_seq_SET(elts, i, value);
5113 }
5114 Py_XDECREF(tmp);
5115 tmp = NULL;
5116 } else {
5117 PyErr_SetString(PyExc_TypeError, "required field \"elts\" missing from Tuple");
5118 return 1;
5119 }
5120 if (PyObject_HasAttrString(obj, "ctx")) {
5121 int res;
5122 tmp = PyObject_GetAttrString(obj, "ctx");
5123 if (tmp == NULL) goto failed;
5124 res = obj2ast_expr_context(tmp, &ctx, arena);
5125 if (res != 0) goto failed;
5126 Py_XDECREF(tmp);
5127 tmp = NULL;
5128 } else {
5129 PyErr_SetString(PyExc_TypeError, "required field \"ctx\" missing from Tuple");
5130 return 1;
5131 }
5132 *out = Tuple(elts, ctx, lineno, col_offset, arena);
5133 if (*out == NULL) goto failed;
5134 return 0;
5135 }
5136
5137 tmp = PyObject_Repr(obj);
5138 if (tmp == NULL) goto failed;
5139 PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyString_AS_STRING(tmp));
5140failed:
5141 Py_XDECREF(tmp);
5142 return 1;
5143}
5144
5145int
5146obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
5147{
5148 PyObject* tmp = NULL;
5149
5150 if (PyObject_IsInstance(obj, (PyObject*)Load_type)) {
5151 *out = Load;
5152 return 0;
5153 }
5154 if (PyObject_IsInstance(obj, (PyObject*)Store_type)) {
5155 *out = Store;
5156 return 0;
5157 }
5158 if (PyObject_IsInstance(obj, (PyObject*)Del_type)) {
5159 *out = Del;
5160 return 0;
5161 }
5162 if (PyObject_IsInstance(obj, (PyObject*)AugLoad_type)) {
5163 *out = AugLoad;
5164 return 0;
5165 }
5166 if (PyObject_IsInstance(obj, (PyObject*)AugStore_type)) {
5167 *out = AugStore;
5168 return 0;
5169 }
5170 if (PyObject_IsInstance(obj, (PyObject*)Param_type)) {
5171 *out = Param;
5172 return 0;
5173 }
5174
5175 tmp = PyObject_Repr(obj);
5176 if (tmp == NULL) goto failed;
5177 PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyString_AS_STRING(tmp));
5178failed:
5179 Py_XDECREF(tmp);
5180 return 1;
5181}
5182
5183int
5184obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
5185{
5186 PyObject* tmp = NULL;
5187
5188
5189 if (obj == Py_None) {
5190 *out = NULL;
5191 return 0;
5192 }
5193 if (PyObject_IsInstance(obj, (PyObject*)Ellipsis_type)) {
5194
5195 *out = Ellipsis(arena);
5196 if (*out == NULL) goto failed;
5197 return 0;
5198 }
5199 if (PyObject_IsInstance(obj, (PyObject*)Slice_type)) {
5200 expr_ty lower;
5201 expr_ty upper;
5202 expr_ty step;
5203
5204 if (PyObject_HasAttrString(obj, "lower")) {
5205 int res;
5206 tmp = PyObject_GetAttrString(obj, "lower");
5207 if (tmp == NULL) goto failed;
5208 res = obj2ast_expr(tmp, &lower, arena);
5209 if (res != 0) goto failed;
5210 Py_XDECREF(tmp);
5211 tmp = NULL;
5212 } else {
5213 lower = NULL;
5214 }
5215 if (PyObject_HasAttrString(obj, "upper")) {
5216 int res;
5217 tmp = PyObject_GetAttrString(obj, "upper");
5218 if (tmp == NULL) goto failed;
5219 res = obj2ast_expr(tmp, &upper, arena);
5220 if (res != 0) goto failed;
5221 Py_XDECREF(tmp);
5222 tmp = NULL;
5223 } else {
5224 upper = NULL;
5225 }
5226 if (PyObject_HasAttrString(obj, "step")) {
5227 int res;
5228 tmp = PyObject_GetAttrString(obj, "step");
5229 if (tmp == NULL) goto failed;
5230 res = obj2ast_expr(tmp, &step, arena);
5231 if (res != 0) goto failed;
5232 Py_XDECREF(tmp);
5233 tmp = NULL;
5234 } else {
5235 step = NULL;
5236 }
5237 *out = Slice(lower, upper, step, arena);
5238 if (*out == NULL) goto failed;
5239 return 0;
5240 }
5241 if (PyObject_IsInstance(obj, (PyObject*)ExtSlice_type)) {
5242 asdl_seq* dims;
5243
5244 if (PyObject_HasAttrString(obj, "dims")) {
5245 int res;
5246 Py_ssize_t len;
5247 Py_ssize_t i;
5248 tmp = PyObject_GetAttrString(obj, "dims");
5249 if (tmp == NULL) goto failed;
5250 if (!PyList_Check(tmp)) {
5251 PyErr_Format(PyExc_TypeError, "ExtSlice field \"dims\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5252 goto failed;
5253 }
5254 len = PyList_GET_SIZE(tmp);
5255 dims = asdl_seq_new(len, arena);
5256 if (dims == NULL) goto failed;
5257 for (i = 0; i < len; i++) {
5258 slice_ty value;
5259 res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena);
5260 if (res != 0) goto failed;
5261 asdl_seq_SET(dims, i, value);
5262 }
5263 Py_XDECREF(tmp);
5264 tmp = NULL;
5265 } else {
5266 PyErr_SetString(PyExc_TypeError, "required field \"dims\" missing from ExtSlice");
5267 return 1;
5268 }
5269 *out = ExtSlice(dims, arena);
5270 if (*out == NULL) goto failed;
5271 return 0;
5272 }
5273 if (PyObject_IsInstance(obj, (PyObject*)Index_type)) {
5274 expr_ty value;
5275
5276 if (PyObject_HasAttrString(obj, "value")) {
5277 int res;
5278 tmp = PyObject_GetAttrString(obj, "value");
5279 if (tmp == NULL) goto failed;
5280 res = obj2ast_expr(tmp, &value, arena);
5281 if (res != 0) goto failed;
5282 Py_XDECREF(tmp);
5283 tmp = NULL;
5284 } else {
5285 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from Index");
5286 return 1;
5287 }
5288 *out = Index(value, arena);
5289 if (*out == NULL) goto failed;
5290 return 0;
5291 }
5292
5293 tmp = PyObject_Repr(obj);
5294 if (tmp == NULL) goto failed;
5295 PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyString_AS_STRING(tmp));
5296failed:
5297 Py_XDECREF(tmp);
5298 return 1;
5299}
5300
5301int
5302obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
5303{
5304 PyObject* tmp = NULL;
5305
5306 if (PyObject_IsInstance(obj, (PyObject*)And_type)) {
5307 *out = And;
5308 return 0;
5309 }
5310 if (PyObject_IsInstance(obj, (PyObject*)Or_type)) {
5311 *out = Or;
5312 return 0;
5313 }
5314
5315 tmp = PyObject_Repr(obj);
5316 if (tmp == NULL) goto failed;
5317 PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyString_AS_STRING(tmp));
5318failed:
5319 Py_XDECREF(tmp);
5320 return 1;
5321}
5322
5323int
5324obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
5325{
5326 PyObject* tmp = NULL;
5327
5328 if (PyObject_IsInstance(obj, (PyObject*)Add_type)) {
5329 *out = Add;
5330 return 0;
5331 }
5332 if (PyObject_IsInstance(obj, (PyObject*)Sub_type)) {
5333 *out = Sub;
5334 return 0;
5335 }
5336 if (PyObject_IsInstance(obj, (PyObject*)Mult_type)) {
5337 *out = Mult;
5338 return 0;
5339 }
5340 if (PyObject_IsInstance(obj, (PyObject*)Div_type)) {
5341 *out = Div;
5342 return 0;
5343 }
5344 if (PyObject_IsInstance(obj, (PyObject*)Mod_type)) {
5345 *out = Mod;
5346 return 0;
5347 }
5348 if (PyObject_IsInstance(obj, (PyObject*)Pow_type)) {
5349 *out = Pow;
5350 return 0;
5351 }
5352 if (PyObject_IsInstance(obj, (PyObject*)LShift_type)) {
5353 *out = LShift;
5354 return 0;
5355 }
5356 if (PyObject_IsInstance(obj, (PyObject*)RShift_type)) {
5357 *out = RShift;
5358 return 0;
5359 }
5360 if (PyObject_IsInstance(obj, (PyObject*)BitOr_type)) {
5361 *out = BitOr;
5362 return 0;
5363 }
5364 if (PyObject_IsInstance(obj, (PyObject*)BitXor_type)) {
5365 *out = BitXor;
5366 return 0;
5367 }
5368 if (PyObject_IsInstance(obj, (PyObject*)BitAnd_type)) {
5369 *out = BitAnd;
5370 return 0;
5371 }
5372 if (PyObject_IsInstance(obj, (PyObject*)FloorDiv_type)) {
5373 *out = FloorDiv;
5374 return 0;
5375 }
5376
5377 tmp = PyObject_Repr(obj);
5378 if (tmp == NULL) goto failed;
5379 PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyString_AS_STRING(tmp));
5380failed:
5381 Py_XDECREF(tmp);
5382 return 1;
5383}
5384
5385int
5386obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
5387{
5388 PyObject* tmp = NULL;
5389
5390 if (PyObject_IsInstance(obj, (PyObject*)Invert_type)) {
5391 *out = Invert;
5392 return 0;
5393 }
5394 if (PyObject_IsInstance(obj, (PyObject*)Not_type)) {
5395 *out = Not;
5396 return 0;
5397 }
5398 if (PyObject_IsInstance(obj, (PyObject*)UAdd_type)) {
5399 *out = UAdd;
5400 return 0;
5401 }
5402 if (PyObject_IsInstance(obj, (PyObject*)USub_type)) {
5403 *out = USub;
5404 return 0;
5405 }
5406
5407 tmp = PyObject_Repr(obj);
5408 if (tmp == NULL) goto failed;
5409 PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyString_AS_STRING(tmp));
5410failed:
5411 Py_XDECREF(tmp);
5412 return 1;
5413}
5414
5415int
5416obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
5417{
5418 PyObject* tmp = NULL;
5419
5420 if (PyObject_IsInstance(obj, (PyObject*)Eq_type)) {
5421 *out = Eq;
5422 return 0;
5423 }
5424 if (PyObject_IsInstance(obj, (PyObject*)NotEq_type)) {
5425 *out = NotEq;
5426 return 0;
5427 }
5428 if (PyObject_IsInstance(obj, (PyObject*)Lt_type)) {
5429 *out = Lt;
5430 return 0;
5431 }
5432 if (PyObject_IsInstance(obj, (PyObject*)LtE_type)) {
5433 *out = LtE;
5434 return 0;
5435 }
5436 if (PyObject_IsInstance(obj, (PyObject*)Gt_type)) {
5437 *out = Gt;
5438 return 0;
5439 }
5440 if (PyObject_IsInstance(obj, (PyObject*)GtE_type)) {
5441 *out = GtE;
5442 return 0;
5443 }
5444 if (PyObject_IsInstance(obj, (PyObject*)Is_type)) {
5445 *out = Is;
5446 return 0;
5447 }
5448 if (PyObject_IsInstance(obj, (PyObject*)IsNot_type)) {
5449 *out = IsNot;
5450 return 0;
5451 }
5452 if (PyObject_IsInstance(obj, (PyObject*)In_type)) {
5453 *out = In;
5454 return 0;
5455 }
5456 if (PyObject_IsInstance(obj, (PyObject*)NotIn_type)) {
5457 *out = NotIn;
5458 return 0;
5459 }
5460
5461 tmp = PyObject_Repr(obj);
5462 if (tmp == NULL) goto failed;
5463 PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyString_AS_STRING(tmp));
5464failed:
5465 Py_XDECREF(tmp);
5466 return 1;
5467}
5468
5469int
5470obj2ast_comprehension(PyObject* obj, comprehension_ty* out, PyArena* arena)
5471{
5472 PyObject* tmp = NULL;
5473 expr_ty target;
5474 expr_ty iter;
5475 asdl_seq* ifs;
5476
5477 if (PyObject_HasAttrString(obj, "target")) {
5478 int res;
5479 tmp = PyObject_GetAttrString(obj, "target");
5480 if (tmp == NULL) goto failed;
5481 res = obj2ast_expr(tmp, &target, arena);
5482 if (res != 0) goto failed;
5483 Py_XDECREF(tmp);
5484 tmp = NULL;
5485 } else {
5486 PyErr_SetString(PyExc_TypeError, "required field \"target\" missing from comprehension");
5487 return 1;
5488 }
5489 if (PyObject_HasAttrString(obj, "iter")) {
5490 int res;
5491 tmp = PyObject_GetAttrString(obj, "iter");
5492 if (tmp == NULL) goto failed;
5493 res = obj2ast_expr(tmp, &iter, arena);
5494 if (res != 0) goto failed;
5495 Py_XDECREF(tmp);
5496 tmp = NULL;
5497 } else {
5498 PyErr_SetString(PyExc_TypeError, "required field \"iter\" missing from comprehension");
5499 return 1;
5500 }
5501 if (PyObject_HasAttrString(obj, "ifs")) {
5502 int res;
5503 Py_ssize_t len;
5504 Py_ssize_t i;
5505 tmp = PyObject_GetAttrString(obj, "ifs");
5506 if (tmp == NULL) goto failed;
5507 if (!PyList_Check(tmp)) {
5508 PyErr_Format(PyExc_TypeError, "comprehension field \"ifs\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5509 goto failed;
5510 }
5511 len = PyList_GET_SIZE(tmp);
5512 ifs = asdl_seq_new(len, arena);
5513 if (ifs == NULL) goto failed;
5514 for (i = 0; i < len; i++) {
5515 expr_ty value;
5516 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5517 if (res != 0) goto failed;
5518 asdl_seq_SET(ifs, i, value);
5519 }
5520 Py_XDECREF(tmp);
5521 tmp = NULL;
5522 } else {
5523 PyErr_SetString(PyExc_TypeError, "required field \"ifs\" missing from comprehension");
5524 return 1;
5525 }
5526 *out = comprehension(target, iter, ifs, arena);
5527 return 0;
5528failed:
5529 Py_XDECREF(tmp);
5530 return 1;
5531}
5532
5533int
5534obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
5535{
5536 PyObject* tmp = NULL;
5537 expr_ty type;
5538 expr_ty name;
5539 asdl_seq* body;
5540 int lineno;
5541 int col_offset;
5542
5543 if (PyObject_HasAttrString(obj, "type")) {
5544 int res;
5545 tmp = PyObject_GetAttrString(obj, "type");
5546 if (tmp == NULL) goto failed;
5547 res = obj2ast_expr(tmp, &type, arena);
5548 if (res != 0) goto failed;
5549 Py_XDECREF(tmp);
5550 tmp = NULL;
5551 } else {
5552 type = NULL;
5553 }
5554 if (PyObject_HasAttrString(obj, "name")) {
5555 int res;
5556 tmp = PyObject_GetAttrString(obj, "name");
5557 if (tmp == NULL) goto failed;
5558 res = obj2ast_expr(tmp, &name, arena);
5559 if (res != 0) goto failed;
5560 Py_XDECREF(tmp);
5561 tmp = NULL;
5562 } else {
5563 name = NULL;
5564 }
5565 if (PyObject_HasAttrString(obj, "body")) {
5566 int res;
5567 Py_ssize_t len;
5568 Py_ssize_t i;
5569 tmp = PyObject_GetAttrString(obj, "body");
5570 if (tmp == NULL) goto failed;
5571 if (!PyList_Check(tmp)) {
5572 PyErr_Format(PyExc_TypeError, "excepthandler field \"body\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5573 goto failed;
5574 }
5575 len = PyList_GET_SIZE(tmp);
5576 body = asdl_seq_new(len, arena);
5577 if (body == NULL) goto failed;
5578 for (i = 0; i < len; i++) {
5579 stmt_ty value;
5580 res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena);
5581 if (res != 0) goto failed;
5582 asdl_seq_SET(body, i, value);
5583 }
5584 Py_XDECREF(tmp);
5585 tmp = NULL;
5586 } else {
5587 PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from excepthandler");
5588 return 1;
5589 }
5590 if (PyObject_HasAttrString(obj, "lineno")) {
5591 int res;
5592 tmp = PyObject_GetAttrString(obj, "lineno");
5593 if (tmp == NULL) goto failed;
5594 res = obj2ast_int(tmp, &lineno, arena);
5595 if (res != 0) goto failed;
5596 Py_XDECREF(tmp);
5597 tmp = NULL;
5598 } else {
5599 PyErr_SetString(PyExc_TypeError, "required field \"lineno\" missing from excepthandler");
5600 return 1;
5601 }
5602 if (PyObject_HasAttrString(obj, "col_offset")) {
5603 int res;
5604 tmp = PyObject_GetAttrString(obj, "col_offset");
5605 if (tmp == NULL) goto failed;
5606 res = obj2ast_int(tmp, &col_offset, arena);
5607 if (res != 0) goto failed;
5608 Py_XDECREF(tmp);
5609 tmp = NULL;
5610 } else {
5611 PyErr_SetString(PyExc_TypeError, "required field \"col_offset\" missing from excepthandler");
5612 return 1;
5613 }
5614 *out = excepthandler(type, name, body, lineno, col_offset, arena);
5615 return 0;
5616failed:
5617 Py_XDECREF(tmp);
5618 return 1;
5619}
5620
5621int
5622obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)
5623{
5624 PyObject* tmp = NULL;
5625 asdl_seq* args;
5626 identifier vararg;
5627 identifier kwarg;
5628 asdl_seq* defaults;
5629
5630 if (PyObject_HasAttrString(obj, "args")) {
5631 int res;
5632 Py_ssize_t len;
5633 Py_ssize_t i;
5634 tmp = PyObject_GetAttrString(obj, "args");
5635 if (tmp == NULL) goto failed;
5636 if (!PyList_Check(tmp)) {
5637 PyErr_Format(PyExc_TypeError, "arguments field \"args\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5638 goto failed;
5639 }
5640 len = PyList_GET_SIZE(tmp);
5641 args = asdl_seq_new(len, arena);
5642 if (args == NULL) goto failed;
5643 for (i = 0; i < len; i++) {
5644 expr_ty value;
5645 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5646 if (res != 0) goto failed;
5647 asdl_seq_SET(args, i, value);
5648 }
5649 Py_XDECREF(tmp);
5650 tmp = NULL;
5651 } else {
5652 PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");
5653 return 1;
5654 }
5655 if (PyObject_HasAttrString(obj, "vararg")) {
5656 int res;
5657 tmp = PyObject_GetAttrString(obj, "vararg");
5658 if (tmp == NULL) goto failed;
5659 res = obj2ast_identifier(tmp, &vararg, arena);
5660 if (res != 0) goto failed;
5661 Py_XDECREF(tmp);
5662 tmp = NULL;
5663 } else {
5664 vararg = NULL;
5665 }
5666 if (PyObject_HasAttrString(obj, "kwarg")) {
5667 int res;
5668 tmp = PyObject_GetAttrString(obj, "kwarg");
5669 if (tmp == NULL) goto failed;
5670 res = obj2ast_identifier(tmp, &kwarg, arena);
5671 if (res != 0) goto failed;
5672 Py_XDECREF(tmp);
5673 tmp = NULL;
5674 } else {
5675 kwarg = NULL;
5676 }
5677 if (PyObject_HasAttrString(obj, "defaults")) {
5678 int res;
5679 Py_ssize_t len;
5680 Py_ssize_t i;
5681 tmp = PyObject_GetAttrString(obj, "defaults");
5682 if (tmp == NULL) goto failed;
5683 if (!PyList_Check(tmp)) {
5684 PyErr_Format(PyExc_TypeError, "arguments field \"defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name);
5685 goto failed;
5686 }
5687 len = PyList_GET_SIZE(tmp);
5688 defaults = asdl_seq_new(len, arena);
5689 if (defaults == NULL) goto failed;
5690 for (i = 0; i < len; i++) {
5691 expr_ty value;
5692 res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena);
5693 if (res != 0) goto failed;
5694 asdl_seq_SET(defaults, i, value);
5695 }
5696 Py_XDECREF(tmp);
5697 tmp = NULL;
5698 } else {
5699 PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");
5700 return 1;
5701 }
5702 *out = arguments(args, vararg, kwarg, defaults, arena);
5703 return 0;
5704failed:
5705 Py_XDECREF(tmp);
5706 return 1;
5707}
5708
5709int
5710obj2ast_keyword(PyObject* obj, keyword_ty* out, PyArena* arena)
5711{
5712 PyObject* tmp = NULL;
5713 identifier arg;
5714 expr_ty value;
5715
5716 if (PyObject_HasAttrString(obj, "arg")) {
5717 int res;
5718 tmp = PyObject_GetAttrString(obj, "arg");
5719 if (tmp == NULL) goto failed;
5720 res = obj2ast_identifier(tmp, &arg, arena);
5721 if (res != 0) goto failed;
5722 Py_XDECREF(tmp);
5723 tmp = NULL;
5724 } else {
5725 PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from keyword");
5726 return 1;
5727 }
5728 if (PyObject_HasAttrString(obj, "value")) {
5729 int res;
5730 tmp = PyObject_GetAttrString(obj, "value");
5731 if (tmp == NULL) goto failed;
5732 res = obj2ast_expr(tmp, &value, arena);
5733 if (res != 0) goto failed;
5734 Py_XDECREF(tmp);
5735 tmp = NULL;
5736 } else {
5737 PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from keyword");
5738 return 1;
5739 }
5740 *out = keyword(arg, value, arena);
5741 return 0;
5742failed:
5743 Py_XDECREF(tmp);
5744 return 1;
5745}
5746
5747int
5748obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)
5749{
5750 PyObject* tmp = NULL;
5751 identifier name;
5752 identifier asname;
5753
5754 if (PyObject_HasAttrString(obj, "name")) {
5755 int res;
5756 tmp = PyObject_GetAttrString(obj, "name");
5757 if (tmp == NULL) goto failed;
5758 res = obj2ast_identifier(tmp, &name, arena);
5759 if (res != 0) goto failed;
5760 Py_XDECREF(tmp);
5761 tmp = NULL;
5762 } else {
5763 PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");
5764 return 1;
5765 }
5766 if (PyObject_HasAttrString(obj, "asname")) {
5767 int res;
5768 tmp = PyObject_GetAttrString(obj, "asname");
5769 if (tmp == NULL) goto failed;
5770 res = obj2ast_identifier(tmp, &asname, arena);
5771 if (res != 0) goto failed;
5772 Py_XDECREF(tmp);
5773 tmp = NULL;
5774 } else {
5775 asname = NULL;
5776 }
5777 *out = alias(name, asname, arena);
5778 return 0;
5779failed:
5780 Py_XDECREF(tmp);
5781 return 1;
5782}
5783
5784
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005785PyMODINIT_FUNC
5786init_ast(void)
5787{
5788 PyObject *m, *d;
5789 if (!init_types()) return;
5790 m = Py_InitModule3("_ast", NULL, NULL);
5791 if (!m) return;
5792 d = PyModule_GetDict(m);
5793 if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;
5794 if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
5795 return;
Christian Heimes40346852008-02-23 17:52:07 +00005796 if (PyModule_AddStringConstant(m, "__version__", "60978") < 0)
Martin v. Löwisace990c2006-02-28 00:32:31 +00005797 return;
Neal Norwitz19379f12006-04-03 04:50:58 +00005798 if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return;
5799 if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0)
5800 return;
5801 if (PyDict_SetItemString(d, "Interactive", (PyObject*)Interactive_type)
5802 < 0) return;
5803 if (PyDict_SetItemString(d, "Expression", (PyObject*)Expression_type) <
5804 0) return;
5805 if (PyDict_SetItemString(d, "Suite", (PyObject*)Suite_type) < 0) return;
5806 if (PyDict_SetItemString(d, "stmt", (PyObject*)stmt_type) < 0) return;
5807 if (PyDict_SetItemString(d, "FunctionDef", (PyObject*)FunctionDef_type)
5808 < 0) return;
5809 if (PyDict_SetItemString(d, "ClassDef", (PyObject*)ClassDef_type) < 0)
5810 return;
5811 if (PyDict_SetItemString(d, "Return", (PyObject*)Return_type) < 0)
5812 return;
5813 if (PyDict_SetItemString(d, "Delete", (PyObject*)Delete_type) < 0)
5814 return;
5815 if (PyDict_SetItemString(d, "Assign", (PyObject*)Assign_type) < 0)
5816 return;
5817 if (PyDict_SetItemString(d, "AugAssign", (PyObject*)AugAssign_type) <
5818 0) return;
5819 if (PyDict_SetItemString(d, "Print", (PyObject*)Print_type) < 0) return;
5820 if (PyDict_SetItemString(d, "For", (PyObject*)For_type) < 0) return;
5821 if (PyDict_SetItemString(d, "While", (PyObject*)While_type) < 0) return;
5822 if (PyDict_SetItemString(d, "If", (PyObject*)If_type) < 0) return;
5823 if (PyDict_SetItemString(d, "With", (PyObject*)With_type) < 0) return;
5824 if (PyDict_SetItemString(d, "Raise", (PyObject*)Raise_type) < 0) return;
5825 if (PyDict_SetItemString(d, "TryExcept", (PyObject*)TryExcept_type) <
5826 0) return;
5827 if (PyDict_SetItemString(d, "TryFinally", (PyObject*)TryFinally_type) <
5828 0) return;
5829 if (PyDict_SetItemString(d, "Assert", (PyObject*)Assert_type) < 0)
5830 return;
5831 if (PyDict_SetItemString(d, "Import", (PyObject*)Import_type) < 0)
5832 return;
5833 if (PyDict_SetItemString(d, "ImportFrom", (PyObject*)ImportFrom_type) <
5834 0) return;
5835 if (PyDict_SetItemString(d, "Exec", (PyObject*)Exec_type) < 0) return;
5836 if (PyDict_SetItemString(d, "Global", (PyObject*)Global_type) < 0)
5837 return;
5838 if (PyDict_SetItemString(d, "Expr", (PyObject*)Expr_type) < 0) return;
5839 if (PyDict_SetItemString(d, "Pass", (PyObject*)Pass_type) < 0) return;
5840 if (PyDict_SetItemString(d, "Break", (PyObject*)Break_type) < 0) return;
5841 if (PyDict_SetItemString(d, "Continue", (PyObject*)Continue_type) < 0)
5842 return;
5843 if (PyDict_SetItemString(d, "expr", (PyObject*)expr_type) < 0) return;
5844 if (PyDict_SetItemString(d, "BoolOp", (PyObject*)BoolOp_type) < 0)
5845 return;
5846 if (PyDict_SetItemString(d, "BinOp", (PyObject*)BinOp_type) < 0) return;
5847 if (PyDict_SetItemString(d, "UnaryOp", (PyObject*)UnaryOp_type) < 0)
5848 return;
5849 if (PyDict_SetItemString(d, "Lambda", (PyObject*)Lambda_type) < 0)
5850 return;
5851 if (PyDict_SetItemString(d, "IfExp", (PyObject*)IfExp_type) < 0) return;
5852 if (PyDict_SetItemString(d, "Dict", (PyObject*)Dict_type) < 0) return;
5853 if (PyDict_SetItemString(d, "ListComp", (PyObject*)ListComp_type) < 0)
5854 return;
5855 if (PyDict_SetItemString(d, "GeneratorExp",
5856 (PyObject*)GeneratorExp_type) < 0) return;
5857 if (PyDict_SetItemString(d, "Yield", (PyObject*)Yield_type) < 0) return;
5858 if (PyDict_SetItemString(d, "Compare", (PyObject*)Compare_type) < 0)
5859 return;
5860 if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return;
5861 if (PyDict_SetItemString(d, "Repr", (PyObject*)Repr_type) < 0) return;
5862 if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return;
5863 if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return;
5864 if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <
5865 0) return;
5866 if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) <
5867 0) return;
5868 if (PyDict_SetItemString(d, "Name", (PyObject*)Name_type) < 0) return;
5869 if (PyDict_SetItemString(d, "List", (PyObject*)List_type) < 0) return;
5870 if (PyDict_SetItemString(d, "Tuple", (PyObject*)Tuple_type) < 0) return;
5871 if (PyDict_SetItemString(d, "expr_context",
5872 (PyObject*)expr_context_type) < 0) return;
5873 if (PyDict_SetItemString(d, "Load", (PyObject*)Load_type) < 0) return;
5874 if (PyDict_SetItemString(d, "Store", (PyObject*)Store_type) < 0) return;
5875 if (PyDict_SetItemString(d, "Del", (PyObject*)Del_type) < 0) return;
5876 if (PyDict_SetItemString(d, "AugLoad", (PyObject*)AugLoad_type) < 0)
5877 return;
5878 if (PyDict_SetItemString(d, "AugStore", (PyObject*)AugStore_type) < 0)
5879 return;
5880 if (PyDict_SetItemString(d, "Param", (PyObject*)Param_type) < 0) return;
5881 if (PyDict_SetItemString(d, "slice", (PyObject*)slice_type) < 0) return;
5882 if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)
5883 return;
5884 if (PyDict_SetItemString(d, "Slice", (PyObject*)Slice_type) < 0) return;
5885 if (PyDict_SetItemString(d, "ExtSlice", (PyObject*)ExtSlice_type) < 0)
5886 return;
5887 if (PyDict_SetItemString(d, "Index", (PyObject*)Index_type) < 0) return;
5888 if (PyDict_SetItemString(d, "boolop", (PyObject*)boolop_type) < 0)
5889 return;
5890 if (PyDict_SetItemString(d, "And", (PyObject*)And_type) < 0) return;
5891 if (PyDict_SetItemString(d, "Or", (PyObject*)Or_type) < 0) return;
5892 if (PyDict_SetItemString(d, "operator", (PyObject*)operator_type) < 0)
5893 return;
5894 if (PyDict_SetItemString(d, "Add", (PyObject*)Add_type) < 0) return;
5895 if (PyDict_SetItemString(d, "Sub", (PyObject*)Sub_type) < 0) return;
5896 if (PyDict_SetItemString(d, "Mult", (PyObject*)Mult_type) < 0) return;
5897 if (PyDict_SetItemString(d, "Div", (PyObject*)Div_type) < 0) return;
5898 if (PyDict_SetItemString(d, "Mod", (PyObject*)Mod_type) < 0) return;
5899 if (PyDict_SetItemString(d, "Pow", (PyObject*)Pow_type) < 0) return;
5900 if (PyDict_SetItemString(d, "LShift", (PyObject*)LShift_type) < 0)
5901 return;
5902 if (PyDict_SetItemString(d, "RShift", (PyObject*)RShift_type) < 0)
5903 return;
5904 if (PyDict_SetItemString(d, "BitOr", (PyObject*)BitOr_type) < 0) return;
5905 if (PyDict_SetItemString(d, "BitXor", (PyObject*)BitXor_type) < 0)
5906 return;
5907 if (PyDict_SetItemString(d, "BitAnd", (PyObject*)BitAnd_type) < 0)
5908 return;
5909 if (PyDict_SetItemString(d, "FloorDiv", (PyObject*)FloorDiv_type) < 0)
5910 return;
5911 if (PyDict_SetItemString(d, "unaryop", (PyObject*)unaryop_type) < 0)
5912 return;
5913 if (PyDict_SetItemString(d, "Invert", (PyObject*)Invert_type) < 0)
5914 return;
5915 if (PyDict_SetItemString(d, "Not", (PyObject*)Not_type) < 0) return;
5916 if (PyDict_SetItemString(d, "UAdd", (PyObject*)UAdd_type) < 0) return;
5917 if (PyDict_SetItemString(d, "USub", (PyObject*)USub_type) < 0) return;
5918 if (PyDict_SetItemString(d, "cmpop", (PyObject*)cmpop_type) < 0) return;
5919 if (PyDict_SetItemString(d, "Eq", (PyObject*)Eq_type) < 0) return;
5920 if (PyDict_SetItemString(d, "NotEq", (PyObject*)NotEq_type) < 0) return;
5921 if (PyDict_SetItemString(d, "Lt", (PyObject*)Lt_type) < 0) return;
5922 if (PyDict_SetItemString(d, "LtE", (PyObject*)LtE_type) < 0) return;
5923 if (PyDict_SetItemString(d, "Gt", (PyObject*)Gt_type) < 0) return;
5924 if (PyDict_SetItemString(d, "GtE", (PyObject*)GtE_type) < 0) return;
5925 if (PyDict_SetItemString(d, "Is", (PyObject*)Is_type) < 0) return;
5926 if (PyDict_SetItemString(d, "IsNot", (PyObject*)IsNot_type) < 0) return;
5927 if (PyDict_SetItemString(d, "In", (PyObject*)In_type) < 0) return;
5928 if (PyDict_SetItemString(d, "NotIn", (PyObject*)NotIn_type) < 0) return;
5929 if (PyDict_SetItemString(d, "comprehension",
5930 (PyObject*)comprehension_type) < 0) return;
5931 if (PyDict_SetItemString(d, "excepthandler",
5932 (PyObject*)excepthandler_type) < 0) return;
5933 if (PyDict_SetItemString(d, "arguments", (PyObject*)arguments_type) <
5934 0) return;
5935 if (PyDict_SetItemString(d, "keyword", (PyObject*)keyword_type) < 0)
5936 return;
5937 if (PyDict_SetItemString(d, "alias", (PyObject*)alias_type) < 0) return;
Martin v. Löwis577b5b92006-02-27 15:23:19 +00005938}
5939
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005940
Martin v. Löwisbd260da2006-02-26 19:42:26 +00005941PyObject* PyAST_mod2obj(mod_ty t)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005942{
Martin v. Löwisbd260da2006-02-26 19:42:26 +00005943 init_types();
5944 return ast2obj_mod(t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005945}
5946
Georg Brandlf2bfd542008-03-29 13:24:23 +00005947/* mode is 0 for "exec", 1 for "eval" and 2 for "single" input */
5948mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode)
Georg Brandlfc8eef32008-03-28 12:11:56 +00005949{
5950 mod_ty res;
Georg Brandlf2bfd542008-03-29 13:24:23 +00005951 PyObject *req_type[] = {(PyObject*)Module_type, (PyObject*)Expression_type,
5952 (PyObject*)Interactive_type};
5953 char *req_name[] = {"Module", "Expression", "Interactive"};
5954 assert(0 <= mode && mode <= 2);
5955
Georg Brandlfc8eef32008-03-28 12:11:56 +00005956 init_types();
Georg Brandlf2bfd542008-03-29 13:24:23 +00005957
5958 if (!PyObject_IsInstance(ast, req_type[mode])) {
5959 PyErr_Format(PyExc_TypeError, "expected %s node, got %.400s",
5960 req_name[mode], Py_TYPE(ast)->tp_name);
Georg Brandlfc8eef32008-03-28 12:11:56 +00005961 return NULL;
5962 }
5963 if (obj2ast_mod(ast, &res, arena) != 0)
5964 return NULL;
5965 else
5966 return res;
5967}
5968
5969int PyAST_Check(PyObject* obj)
5970{
5971 init_types();
5972 return PyObject_IsInstance(obj, (PyObject*)AST_type);
5973}
5974
Neal Norwitz7b5a6042005-11-13 19:14:20 +00005975