diff -r 336137a359ae Include/Python-ast.h --- a/Include/Python-ast.h Fri Mar 11 10:27:14 2011 -0500 +++ b/Include/Python-ast.h Mon Mar 14 23:22:44 2011 -0400 @@ -44,6 +44,7 @@ union { struct { asdl_seq *body; + string docstring; } Module; struct { @@ -77,6 +78,7 @@ asdl_seq *body; asdl_seq *decorator_list; expr_ty returns; + string docstring; } FunctionDef; struct { @@ -87,6 +89,7 @@ expr_ty kwargs; asdl_seq *body; asdl_seq *decorator_list; + string docstring; } ClassDef; struct { @@ -184,10 +187,9 @@ enum _expr_kind {BoolOp_kind=1, BinOp_kind=2, UnaryOp_kind=3, Lambda_kind=4, IfExp_kind=5, Dict_kind=6, Set_kind=7, ListComp_kind=8, SetComp_kind=9, DictComp_kind=10, GeneratorExp_kind=11, - Yield_kind=12, Compare_kind=13, Call_kind=14, Num_kind=15, - Str_kind=16, Bytes_kind=17, Ellipsis_kind=18, - Attribute_kind=19, Subscript_kind=20, Starred_kind=21, - Name_kind=22, List_kind=23, Tuple_kind=24}; + Yield_kind=12, Compare_kind=13, Call_kind=14, Lit_kind=15, + Attribute_kind=16, Subscript_kind=17, Starred_kind=18, + Name_kind=19, List_kind=20, Tuple_kind=21}; struct _expr { enum _expr_kind kind; union { @@ -267,16 +269,8 @@ } Call; struct { - object n; - } Num; - - struct { - string s; - } Str; - - struct { - string s; - } Bytes; + object v; + } Lit; struct { expr_ty value; @@ -384,23 +378,23 @@ }; -#define Module(a0, a1) _Py_Module(a0, a1) -mod_ty _Py_Module(asdl_seq * body, PyArena *arena); +#define Module(a0, a1, a2) _Py_Module(a0, a1, a2) +mod_ty _Py_Module(asdl_seq * body, string docstring, PyArena *arena); #define Interactive(a0, a1) _Py_Interactive(a0, a1) mod_ty _Py_Interactive(asdl_seq * body, PyArena *arena); #define Expression(a0, a1) _Py_Expression(a0, a1) mod_ty _Py_Expression(expr_ty body, PyArena *arena); #define Suite(a0, a1) _Py_Suite(a0, a1) mod_ty _Py_Suite(asdl_seq * body, PyArena *arena); -#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7) +#define FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) _Py_FunctionDef(a0, a1, a2, a3, a4, a5, a6, a7, a8) stmt_ty _Py_FunctionDef(identifier name, arguments_ty args, asdl_seq * body, - asdl_seq * decorator_list, expr_ty returns, int lineno, - int col_offset, PyArena *arena); -#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) + asdl_seq * decorator_list, expr_ty returns, string + docstring, int lineno, int col_offset, PyArena *arena); +#define ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) _Py_ClassDef(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) stmt_ty _Py_ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty starargs, expr_ty kwargs, asdl_seq * body, - asdl_seq * decorator_list, int lineno, int col_offset, - PyArena *arena); + asdl_seq * decorator_list, string docstring, int lineno, + int col_offset, PyArena *arena); #define Return(a0, a1, a2, a3) _Py_Return(a0, a1, a2, a3) stmt_ty _Py_Return(expr_ty value, int lineno, int col_offset, PyArena *arena); #define Delete(a0, a1, a2, a3) _Py_Delete(a0, a1, a2, a3) @@ -497,14 +491,8 @@ expr_ty _Py_Call(expr_ty func, asdl_seq * args, asdl_seq * keywords, expr_ty starargs, expr_ty kwargs, int lineno, int col_offset, PyArena *arena); -#define Num(a0, a1, a2, a3) _Py_Num(a0, a1, a2, a3) -expr_ty _Py_Num(object n, int lineno, int col_offset, PyArena *arena); -#define Str(a0, a1, a2, a3) _Py_Str(a0, a1, a2, a3) -expr_ty _Py_Str(string s, int lineno, int col_offset, PyArena *arena); -#define Bytes(a0, a1, a2, a3) _Py_Bytes(a0, a1, a2, a3) -expr_ty _Py_Bytes(string s, int lineno, int col_offset, PyArena *arena); -#define Ellipsis(a0, a1, a2) _Py_Ellipsis(a0, a1, a2) -expr_ty _Py_Ellipsis(int lineno, int col_offset, PyArena *arena); +#define Lit(a0, a1, a2, a3) _Py_Lit(a0, a1, a2, a3) +expr_ty _Py_Lit(object v, int lineno, int col_offset, PyArena *arena); #define Attribute(a0, a1, a2, a3, a4, a5) _Py_Attribute(a0, a1, a2, a3, a4, a5) expr_ty _Py_Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int col_offset, PyArena *arena); diff -r 336137a359ae Python/Python-ast.c --- a/Python/Python-ast.c Fri Mar 11 10:27:14 2011 -0500 +++ b/Python/Python-ast.c Mon Mar 14 23:22:44 2011 -0400 @@ -2,7 +2,7 @@ /* - __version__ 82163. + __version__ . This module must be committed separately after each AST grammar change; The __version__ number is set to the revision number of the commit @@ -18,6 +18,7 @@ static PyTypeObject *Module_type; static char *Module_fields[]={ "body", + "docstring", }; static PyTypeObject *Interactive_type; static char *Interactive_fields[]={ @@ -44,6 +45,7 @@ "body", "decorator_list", "returns", + "docstring", }; static PyTypeObject *ClassDef_type; static char *ClassDef_fields[]={ @@ -54,6 +56,7 @@ "kwargs", "body", "decorator_list", + "docstring", }; static PyTypeObject *Return_type; static char *Return_fields[]={ @@ -226,19 +229,10 @@ "starargs", "kwargs", }; -static PyTypeObject *Num_type; -static char *Num_fields[]={ - "n", +static PyTypeObject *Lit_type; +static char *Lit_fields[]={ + "v", }; -static PyTypeObject *Str_type; -static char *Str_fields[]={ - "s", -}; -static PyTypeObject *Bytes_type; -static char *Bytes_fields[]={ - "s", -}; -static PyTypeObject *Ellipsis_type; static PyTypeObject *Attribute_type; static char *Attribute_fields[]={ "value", @@ -648,7 +642,7 @@ mod_type = make_type("mod", &AST_type, NULL, 0); if (!mod_type) return 0; if (!add_attributes(mod_type, NULL, 0)) return 0; - Module_type = make_type("Module", mod_type, Module_fields, 1); + Module_type = make_type("Module", mod_type, Module_fields, 2); if (!Module_type) return 0; Interactive_type = make_type("Interactive", mod_type, Interactive_fields, 1); @@ -662,9 +656,9 @@ if (!stmt_type) return 0; if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0; FunctionDef_type = make_type("FunctionDef", stmt_type, - FunctionDef_fields, 5); + FunctionDef_fields, 6); if (!FunctionDef_type) return 0; - ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 7); + ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 8); if (!ClassDef_type) return 0; Return_type = make_type("Return", stmt_type, Return_fields, 1); if (!Return_type) return 0; @@ -740,14 +734,8 @@ if (!Compare_type) return 0; Call_type = make_type("Call", expr_type, Call_fields, 5); if (!Call_type) return 0; - Num_type = make_type("Num", expr_type, Num_fields, 1); - if (!Num_type) return 0; - Str_type = make_type("Str", expr_type, Str_fields, 1); - if (!Str_type) return 0; - Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1); - if (!Bytes_type) return 0; - Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0); - if (!Ellipsis_type) return 0; + Lit_type = make_type("Lit", expr_type, Lit_fields, 1); + if (!Lit_type) return 0; Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); if (!Attribute_type) return 0; Subscript_type = make_type("Subscript", expr_type, Subscript_fields, 3); @@ -962,7 +950,7 @@ static int obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena); mod_ty -Module(asdl_seq * body, PyArena *arena) +Module(asdl_seq * body, string docstring, PyArena *arena) { mod_ty p; p = (mod_ty)PyArena_Malloc(arena, sizeof(*p)); @@ -970,6 +958,7 @@ return NULL; p->kind = Module_kind; p->v.Module.body = body; + p->v.Module.docstring = docstring; return p; } @@ -1016,8 +1005,8 @@ stmt_ty FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq * - decorator_list, expr_ty returns, int lineno, int col_offset, - PyArena *arena) + decorator_list, expr_ty returns, string docstring, int lineno, int + col_offset, PyArena *arena) { stmt_ty p; if (!name) { @@ -1039,6 +1028,7 @@ p->v.FunctionDef.body = body; p->v.FunctionDef.decorator_list = decorator_list; p->v.FunctionDef.returns = returns; + p->v.FunctionDef.docstring = docstring; p->lineno = lineno; p->col_offset = col_offset; return p; @@ -1047,7 +1037,7 @@ stmt_ty ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, expr_ty starargs, expr_ty kwargs, asdl_seq * body, asdl_seq * decorator_list, - int lineno, int col_offset, PyArena *arena) + string docstring, int lineno, int col_offset, PyArena *arena) { stmt_ty p; if (!name) { @@ -1066,6 +1056,7 @@ p->v.ClassDef.kwargs = kwargs; p->v.ClassDef.body = body; p->v.ClassDef.decorator_list = decorator_list; + p->v.ClassDef.docstring = docstring; p->lineno = lineno; p->col_offset = col_offset; return p; @@ -1749,70 +1740,20 @@ } expr_ty -Num(object n, int lineno, int col_offset, PyArena *arena) +Lit(object v, int lineno, int col_offset, PyArena *arena) { expr_ty p; - if (!n) { + if (!v) v = Py_None; + if (!v) { PyErr_SetString(PyExc_ValueError, - "field n is required for Num"); + "field v is required for Lit"); return NULL; } p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); if (!p) return NULL; - p->kind = Num_kind; - p->v.Num.n = n; - p->lineno = lineno; - p->col_offset = col_offset; - return p; -} - -expr_ty -Str(string s, int lineno, int col_offset, PyArena *arena) -{ - expr_ty p; - if (!s) { - PyErr_SetString(PyExc_ValueError, - "field s is required for Str"); - return NULL; - } - p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) - return NULL; - p->kind = Str_kind; - p->v.Str.s = s; - p->lineno = lineno; - p->col_offset = col_offset; - return p; -} - -expr_ty -Bytes(string s, int lineno, int col_offset, PyArena *arena) -{ - expr_ty p; - if (!s) { - PyErr_SetString(PyExc_ValueError, - "field s is required for Bytes"); - return NULL; - } - p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) - return NULL; - p->kind = Bytes_kind; - p->v.Bytes.s = s; - p->lineno = lineno; - p->col_offset = col_offset; - return p; -} - -expr_ty -Ellipsis(int lineno, int col_offset, PyArena *arena) -{ - expr_ty p; - p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); - if (!p) - return NULL; - p->kind = Ellipsis_kind; + p->kind = Lit_kind; + p->v.Lit.v = v; p->lineno = lineno; p->col_offset = col_offset; return p; @@ -2155,6 +2096,11 @@ if (PyObject_SetAttrString(result, "body", value) == -1) goto failed; Py_DECREF(value); + value = ast2obj_string(o->v.Module.docstring); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "docstring", value) == -1) + goto failed; + Py_DECREF(value); break; case Interactive_kind: result = PyType_GenericNew(Interactive_type, NULL, NULL); @@ -2232,6 +2178,11 @@ if (PyObject_SetAttrString(result, "returns", value) == -1) goto failed; Py_DECREF(value); + value = ast2obj_string(o->v.FunctionDef.docstring); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "docstring", value) == -1) + goto failed; + Py_DECREF(value); break; case ClassDef_kind: result = PyType_GenericNew(ClassDef_type, NULL, NULL); @@ -2273,6 +2224,11 @@ -1) goto failed; Py_DECREF(value); + value = ast2obj_string(o->v.ClassDef.docstring); + if (!value) goto failed; + if (PyObject_SetAttrString(result, "docstring", value) == -1) + goto failed; + Py_DECREF(value); break; case Return_kind: result = PyType_GenericNew(Return_type, NULL, NULL); @@ -2796,37 +2752,15 @@ goto failed; Py_DECREF(value); break; - case Num_kind: - result = PyType_GenericNew(Num_type, NULL, NULL); + case Lit_kind: + result = PyType_GenericNew(Lit_type, NULL, NULL); if (!result) goto failed; - value = ast2obj_object(o->v.Num.n); + value = ast2obj_object(o->v.Lit.v); if (!value) goto failed; - if (PyObject_SetAttrString(result, "n", value) == -1) + if (PyObject_SetAttrString(result, "v", value) == -1) goto failed; Py_DECREF(value); break; - case Str_kind: - result = PyType_GenericNew(Str_type, NULL, NULL); - if (!result) goto failed; - value = ast2obj_string(o->v.Str.s); - if (!value) goto failed; - if (PyObject_SetAttrString(result, "s", value) == -1) - goto failed; - Py_DECREF(value); - break; - case Bytes_kind: - result = PyType_GenericNew(Bytes_type, NULL, NULL); - if (!result) goto failed; - value = ast2obj_string(o->v.Bytes.s); - if (!value) goto failed; - if (PyObject_SetAttrString(result, "s", value) == -1) - goto failed; - Py_DECREF(value); - break; - case Ellipsis_kind: - result = PyType_GenericNew(Ellipsis_type, NULL, NULL); - if (!result) goto failed; - break; case Attribute_kind: result = PyType_GenericNew(Attribute_type, NULL, NULL); if (!result) goto failed; @@ -3388,6 +3322,7 @@ } if (isinstance) { asdl_seq* body; + string docstring; if (PyObject_HasAttrString(obj, "body")) { int res; @@ -3400,13 +3335,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3414,7 +3353,18 @@ PyErr_SetString(PyExc_TypeError, "required field \"body\" missing from Module"); return 1; } - *out = Module(body, arena); + if (PyObject_HasAttrString(obj, "docstring")) { + int res; + tmp = PyObject_GetAttrString(obj, "docstring"); + if (tmp == NULL) goto failed; + res = obj2ast_string(tmp, &docstring, arena); + if (res != 0) goto failed; + Py_XDECREF(tmp); + tmp = NULL; + } else { + docstring = NULL; + } + *out = Module(body, docstring, arena); if (*out == NULL) goto failed; return 0; } @@ -3436,13 +3386,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3495,13 +3449,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3567,6 +3525,7 @@ asdl_seq* body; asdl_seq* decorator_list; expr_ty returns; + string docstring; if (PyObject_HasAttrString(obj, "name")) { int res; @@ -3603,13 +3562,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3628,13 +3591,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - decorator_list = asdl_seq_new(len, arena); - if (decorator_list == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(decorator_list, i, value); + if (len == 0) + decorator_list = NULL; + else { + decorator_list = asdl_seq_new(len, arena); + if (decorator_list == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(decorator_list, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3653,8 +3620,19 @@ } else { returns = NULL; } + if (PyObject_HasAttrString(obj, "docstring")) { + int res; + tmp = PyObject_GetAttrString(obj, "docstring"); + if (tmp == NULL) goto failed; + res = obj2ast_string(tmp, &docstring, arena); + if (res != 0) goto failed; + Py_XDECREF(tmp); + tmp = NULL; + } else { + docstring = NULL; + } *out = FunctionDef(name, args, body, decorator_list, returns, - lineno, col_offset, arena); + docstring, lineno, col_offset, arena); if (*out == NULL) goto failed; return 0; } @@ -3670,6 +3648,7 @@ expr_ty kwargs; asdl_seq* body; asdl_seq* decorator_list; + string docstring; if (PyObject_HasAttrString(obj, "name")) { int res; @@ -3694,13 +3673,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - bases = asdl_seq_new(len, arena); - if (bases == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(bases, i, value); + if (len == 0) + bases = NULL; + else { + bases = asdl_seq_new(len, arena); + if (bases == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(bases, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3719,13 +3702,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - keywords = asdl_seq_new(len, arena); - if (keywords == NULL) goto failed; - for (i = 0; i < len; i++) { - keyword_ty value; - res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(keywords, i, value); + if (len == 0) + keywords = NULL; + else { + keywords = asdl_seq_new(len, arena); + if (keywords == NULL) goto failed; + for (i = 0; i < len; i++) { + keyword_ty value; + res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(keywords, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3766,13 +3753,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3791,13 +3782,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - decorator_list = asdl_seq_new(len, arena); - if (decorator_list == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(decorator_list, i, value); + if (len == 0) + decorator_list = NULL; + else { + decorator_list = asdl_seq_new(len, arena); + if (decorator_list == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(decorator_list, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3805,8 +3800,20 @@ PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from ClassDef"); return 1; } + if (PyObject_HasAttrString(obj, "docstring")) { + int res; + tmp = PyObject_GetAttrString(obj, "docstring"); + if (tmp == NULL) goto failed; + res = obj2ast_string(tmp, &docstring, arena); + if (res != 0) goto failed; + Py_XDECREF(tmp); + tmp = NULL; + } else { + docstring = NULL; + } *out = ClassDef(name, bases, keywords, starargs, kwargs, body, - decorator_list, lineno, col_offset, arena); + decorator_list, docstring, lineno, col_offset, + arena); if (*out == NULL) goto failed; return 0; } @@ -3850,13 +3857,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - targets = asdl_seq_new(len, arena); - if (targets == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(targets, i, value); + if (len == 0) + targets = NULL; + else { + targets = asdl_seq_new(len, arena); + if (targets == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(targets, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -3887,13 +3898,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - targets = asdl_seq_new(len, arena); - if (targets == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(targets, i, value); + if (len == 0) + targets = NULL; + else { + targets = asdl_seq_new(len, arena); + if (targets == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(targets, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4011,13 +4026,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4036,13 +4055,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - orelse = asdl_seq_new(len, arena); - if (orelse == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(orelse, i, value); + if (len == 0) + orelse = NULL; + else { + orelse = asdl_seq_new(len, arena); + if (orelse == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(orelse, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4087,13 +4110,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4112,13 +4139,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - orelse = asdl_seq_new(len, arena); - if (orelse == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(orelse, i, value); + if (len == 0) + orelse = NULL; + else { + orelse = asdl_seq_new(len, arena); + if (orelse == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(orelse, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4162,13 +4193,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4187,13 +4222,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - orelse = asdl_seq_new(len, arena); - if (orelse == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(orelse, i, value); + if (len == 0) + orelse = NULL; + else { + orelse = asdl_seq_new(len, arena); + if (orelse == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(orelse, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4248,13 +4287,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4321,13 +4364,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4346,13 +4393,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - handlers = asdl_seq_new(len, arena); - if (handlers == NULL) goto failed; - for (i = 0; i < len; i++) { - excepthandler_ty value; - res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(handlers, i, value); + if (len == 0) + handlers = NULL; + else { + handlers = asdl_seq_new(len, arena); + if (handlers == NULL) goto failed; + for (i = 0; i < len; i++) { + excepthandler_ty value; + res = obj2ast_excepthandler(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(handlers, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4371,13 +4422,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - orelse = asdl_seq_new(len, arena); - if (orelse == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(orelse, i, value); + if (len == 0) + orelse = NULL; + else { + orelse = asdl_seq_new(len, arena); + if (orelse == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(orelse, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4409,13 +4464,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4434,13 +4493,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - finalbody = asdl_seq_new(len, arena); - if (finalbody == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(finalbody, i, value); + if (len == 0) + finalbody = NULL; + else { + finalbody = asdl_seq_new(len, arena); + if (finalbody == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(finalbody, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4505,13 +4568,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - names = asdl_seq_new(len, arena); - if (names == NULL) goto failed; - for (i = 0; i < len; i++) { - alias_ty value; - res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(names, i, value); + if (len == 0) + names = NULL; + else { + names = asdl_seq_new(len, arena); + if (names == NULL) goto failed; + for (i = 0; i < len; i++) { + alias_ty value; + res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(names, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4554,13 +4621,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - names = asdl_seq_new(len, arena); - if (names == NULL) goto failed; - for (i = 0; i < len; i++) { - alias_ty value; - res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(names, i, value); + if (len == 0) + names = NULL; + else { + names = asdl_seq_new(len, arena); + if (names == NULL) goto failed; + for (i = 0; i < len; i++) { + alias_ty value; + res = obj2ast_alias(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(names, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4602,13 +4673,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - names = asdl_seq_new(len, arena); - if (names == NULL) goto failed; - for (i = 0; i < len; i++) { - identifier value; - res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(names, i, value); + if (len == 0) + names = NULL; + else { + names = asdl_seq_new(len, arena); + if (names == NULL) goto failed; + for (i = 0; i < len; i++) { + identifier value; + res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(names, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4638,13 +4713,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - names = asdl_seq_new(len, arena); - if (names == NULL) goto failed; - for (i = 0; i < len; i++) { - identifier value; - res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(names, i, value); + if (len == 0) + names = NULL; + else { + names = asdl_seq_new(len, arena); + if (names == NULL) goto failed; + for (i = 0; i < len; i++) { + identifier value; + res = obj2ast_identifier(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(names, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4784,13 +4863,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - values = asdl_seq_new(len, arena); - if (values == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(values, i, value); + if (len == 0) + values = NULL; + else { + values = asdl_seq_new(len, arena); + if (values == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(values, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -4991,13 +5074,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - keys = asdl_seq_new(len, arena); - if (keys == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(keys, i, value); + if (len == 0) + keys = NULL; + else { + keys = asdl_seq_new(len, arena); + if (keys == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(keys, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5016,13 +5103,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - values = asdl_seq_new(len, arena); - if (values == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(values, i, value); + if (len == 0) + values = NULL; + else { + values = asdl_seq_new(len, arena); + if (values == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(values, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5052,13 +5143,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - elts = asdl_seq_new(len, arena); - if (elts == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(elts, i, value); + if (len == 0) + elts = NULL; + else { + elts = asdl_seq_new(len, arena); + if (elts == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(elts, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5101,13 +5196,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - generators = asdl_seq_new(len, arena); - if (generators == NULL) goto failed; - for (i = 0; i < len; i++) { - comprehension_ty value; - res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(generators, i, value); + if (len == 0) + generators = NULL; + else { + generators = asdl_seq_new(len, arena); + if (generators == NULL) goto failed; + for (i = 0; i < len; i++) { + comprehension_ty value; + res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(generators, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5150,13 +5249,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - generators = asdl_seq_new(len, arena); - if (generators == NULL) goto failed; - for (i = 0; i < len; i++) { - comprehension_ty value; - res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(generators, i, value); + if (len == 0) + generators = NULL; + else { + generators = asdl_seq_new(len, arena); + if (generators == NULL) goto failed; + for (i = 0; i < len; i++) { + comprehension_ty value; + res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(generators, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5212,13 +5315,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - generators = asdl_seq_new(len, arena); - if (generators == NULL) goto failed; - for (i = 0; i < len; i++) { - comprehension_ty value; - res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(generators, i, value); + if (len == 0) + generators = NULL; + else { + generators = asdl_seq_new(len, arena); + if (generators == NULL) goto failed; + for (i = 0; i < len; i++) { + comprehension_ty value; + res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(generators, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5262,13 +5369,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - generators = asdl_seq_new(len, arena); - if (generators == NULL) goto failed; - for (i = 0; i < len; i++) { - comprehension_ty value; - res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(generators, i, value); + if (len == 0) + generators = NULL; + else { + generators = asdl_seq_new(len, arena); + if (generators == NULL) goto failed; + for (i = 0; i < len; i++) { + comprehension_ty value; + res = obj2ast_comprehension(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(generators, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5334,13 +5445,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - ops = asdl_int_seq_new(len, arena); - if (ops == NULL) goto failed; - for (i = 0; i < len; i++) { - cmpop_ty value; - res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(ops, i, value); + if (len == 0) + ops = NULL; + else { + ops = asdl_int_seq_new(len, arena); + if (ops == NULL) goto failed; + for (i = 0; i < len; i++) { + cmpop_ty value; + res = obj2ast_cmpop(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(ops, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5359,13 +5474,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - comparators = asdl_seq_new(len, arena); - if (comparators == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(comparators, i, value); + if (len == 0) + comparators = NULL; + else { + comparators = asdl_seq_new(len, arena); + if (comparators == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(comparators, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5412,13 +5531,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - args = asdl_seq_new(len, arena); - if (args == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(args, i, value); + if (len == 0) + args = NULL; + else { + args = asdl_seq_new(len, arena); + if (args == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(args, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5437,13 +5560,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - keywords = asdl_seq_new(len, arena); - if (keywords == NULL) goto failed; - for (i = 0; i < len; i++) { - keyword_ty value; - res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(keywords, i, value); + if (len == 0) + keywords = NULL; + else { + keywords = asdl_seq_new(len, arena); + if (keywords == NULL) goto failed; + for (i = 0; i < len; i++) { + keyword_ty value; + res = obj2ast_keyword(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(keywords, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5478,82 +5605,26 @@ if (*out == NULL) goto failed; return 0; } - isinstance = PyObject_IsInstance(obj, (PyObject*)Num_type); + isinstance = PyObject_IsInstance(obj, (PyObject*)Lit_type); if (isinstance == -1) { return 1; } if (isinstance) { - object n; - - if (PyObject_HasAttrString(obj, "n")) { + object v; + + if (PyObject_HasAttrString(obj, "v")) { int res; - tmp = PyObject_GetAttrString(obj, "n"); + tmp = PyObject_GetAttrString(obj, "v"); if (tmp == NULL) goto failed; - res = obj2ast_object(tmp, &n, arena); + res = obj2ast_object(tmp, &v, arena); if (res != 0) goto failed; Py_XDECREF(tmp); tmp = NULL; } else { - PyErr_SetString(PyExc_TypeError, "required field \"n\" missing from Num"); + PyErr_SetString(PyExc_TypeError, "required field \"v\" missing from Lit"); return 1; } - *out = Num(n, lineno, col_offset, arena); - if (*out == NULL) goto failed; - return 0; - } - isinstance = PyObject_IsInstance(obj, (PyObject*)Str_type); - if (isinstance == -1) { - return 1; - } - if (isinstance) { - string s; - - if (PyObject_HasAttrString(obj, "s")) { - int res; - tmp = PyObject_GetAttrString(obj, "s"); - if (tmp == NULL) goto failed; - res = obj2ast_string(tmp, &s, arena); - if (res != 0) goto failed; - Py_XDECREF(tmp); - tmp = NULL; - } else { - PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str"); - return 1; - } - *out = Str(s, lineno, col_offset, arena); - if (*out == NULL) goto failed; - return 0; - } - isinstance = PyObject_IsInstance(obj, (PyObject*)Bytes_type); - if (isinstance == -1) { - return 1; - } - if (isinstance) { - string s; - - if (PyObject_HasAttrString(obj, "s")) { - int res; - tmp = PyObject_GetAttrString(obj, "s"); - if (tmp == NULL) goto failed; - res = obj2ast_string(tmp, &s, arena); - if (res != 0) goto failed; - Py_XDECREF(tmp); - tmp = NULL; - } else { - PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Bytes"); - return 1; - } - *out = Bytes(s, lineno, col_offset, arena); - if (*out == NULL) goto failed; - return 0; - } - isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type); - if (isinstance == -1) { - return 1; - } - if (isinstance) { - - *out = Ellipsis(lineno, col_offset, arena); + *out = Lit(v, lineno, col_offset, arena); if (*out == NULL) goto failed; return 0; } @@ -5746,13 +5817,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - elts = asdl_seq_new(len, arena); - if (elts == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(elts, i, value); + if (len == 0) + elts = NULL; + else { + elts = asdl_seq_new(len, arena); + if (elts == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(elts, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5795,13 +5870,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - elts = asdl_seq_new(len, arena); - if (elts == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(elts, i, value); + if (len == 0) + elts = NULL; + else { + elts = asdl_seq_new(len, arena); + if (elts == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(elts, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -5965,13 +6044,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - dims = asdl_seq_new(len, arena); - if (dims == NULL) goto failed; - for (i = 0; i < len; i++) { - slice_ty value; - res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(dims, i, value); + if (len == 0) + dims = NULL; + else { + dims = asdl_seq_new(len, arena); + if (dims == NULL) goto failed; + for (i = 0; i < len; i++) { + slice_ty value; + res = obj2ast_slice(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(dims, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6320,13 +6403,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - ifs = asdl_seq_new(len, arena); - if (ifs == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(ifs, i, value); + if (len == 0) + ifs = NULL; + else { + ifs = asdl_seq_new(len, arena); + if (ifs == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(ifs, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6420,13 +6507,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - body = asdl_seq_new(len, arena); - if (body == NULL) goto failed; - for (i = 0; i < len; i++) { - stmt_ty value; - res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(body, i, value); + if (len == 0) + body = NULL; + else { + body = asdl_seq_new(len, arena); + if (body == NULL) goto failed; + for (i = 0; i < len; i++) { + stmt_ty value; + res = obj2ast_stmt(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(body, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6470,13 +6561,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - args = asdl_seq_new(len, arena); - if (args == NULL) goto failed; - for (i = 0; i < len; i++) { - arg_ty value; - res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(args, i, value); + if (len == 0) + args = NULL; + else { + args = asdl_seq_new(len, arena); + if (args == NULL) goto failed; + for (i = 0; i < len; i++) { + arg_ty value; + res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(args, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6517,13 +6612,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - kwonlyargs = asdl_seq_new(len, arena); - if (kwonlyargs == NULL) goto failed; - for (i = 0; i < len; i++) { - arg_ty value; - res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(kwonlyargs, i, value); + if (len == 0) + kwonlyargs = NULL; + else { + kwonlyargs = asdl_seq_new(len, arena); + if (kwonlyargs == NULL) goto failed; + for (i = 0; i < len; i++) { + arg_ty value; + res = obj2ast_arg(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(kwonlyargs, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6564,13 +6663,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - defaults = asdl_seq_new(len, arena); - if (defaults == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(defaults, i, value); + if (len == 0) + defaults = NULL; + else { + defaults = asdl_seq_new(len, arena); + if (defaults == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(defaults, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6589,13 +6692,17 @@ goto failed; } len = PyList_GET_SIZE(tmp); - kw_defaults = asdl_seq_new(len, arena); - if (kw_defaults == NULL) goto failed; - for (i = 0; i < len; i++) { - expr_ty value; - res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); - if (res != 0) goto failed; - asdl_seq_SET(kw_defaults, i, value); + if (len == 0) + kw_defaults = NULL; + else { + kw_defaults = asdl_seq_new(len, arena); + if (kw_defaults == NULL) goto failed; + for (i = 0; i < len; i++) { + expr_ty value; + res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); + if (res != 0) goto failed; + asdl_seq_SET(kw_defaults, i, value); + } } Py_XDECREF(tmp); tmp = NULL; @@ -6739,7 +6846,7 @@ NULL; if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) return NULL; - if (PyModule_AddStringConstant(m, "__version__", "82163") < 0) + if (PyModule_AddStringConstant(m, "__version__", "") < 0) return NULL; if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL; @@ -6826,14 +6933,8 @@ return NULL; if (PyDict_SetItemString(d, "Call", (PyObject*)Call_type) < 0) return NULL; - if (PyDict_SetItemString(d, "Num", (PyObject*)Num_type) < 0) return + if (PyDict_SetItemString(d, "Lit", (PyObject*)Lit_type) < 0) return NULL; - if (PyDict_SetItemString(d, "Str", (PyObject*)Str_type) < 0) return - NULL; - if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return - NULL; - if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) - return NULL; if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0) return NULL; if (PyDict_SetItemString(d, "Subscript", (PyObject*)Subscript_type) < diff -r 336137a359ae Python/ast_opt.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Python/ast_opt.c Mon Mar 14 23:22:44 2011 -0400 @@ -0,0 +1,507 @@ +/* File automatically generated by Parser/asdl_ct.py. */ +#include "Python.h" +#include "Python-ast.h" + + +static int make_lit(expr_ty node, PyObject *val, PyArena *arena) +{ + if (val == NULL) { + if(!PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) + PyErr_Clear(); + return 1; + } + if (PyArena_AddPyObject(arena, val) < 0) { + Py_DECREF(val); + return 1; + } + node->kind = Lit_kind; + node->v.Lit.v = val; + return 1; +} + +#define MOVE_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr))) + +static PyObject *unary_not(PyObject *v) +{ + int r = PyObject_IsTrue(v); + if (r < 0) + return NULL; + return r ? Py_False : Py_True; +} + +static int fold_unaryop(expr_ty node, PyArena *arena) +{ + typedef PyObject *(*unary_op)(PyObject*); + static const unary_op ops[] = { + PyNumber_Invert, + unary_not, + PyNumber_Positive, + PyNumber_Negative + }; + /* Eq and NotEq are often implemented in terms of one another, so + folding not (self == other) into self != other breaks implementation + of !=. Detecting such cases doesn't seem worthwhile. + Python uses for 'is subset'/'is superset' operations on sets. + They don't satisfy not folding laws. */ + static const int negated_op[] = { + 0, 0, 0, 0, 0, 0, IsNot, Is, NotIn, In + }; + + expr_ty arg; + PyObject *newval; + + arg = node->v.UnaryOp.operand; + if (arg->kind != Lit_kind) { + /* Fold not into comparison */ + if (node->v.UnaryOp.op == Not && + arg->kind == Compare_kind && + asdl_seq_LEN(arg->v.Compare.ops) == 1) { + int op = asdl_seq_GET(arg->v.Compare.ops, 0); + op = negated_op[op-1]; + if (op) { + asdl_seq_SET(arg->v.Compare.ops, 0, op); + MOVE_NODE(node, arg); + return 1; + } + } + /* TODO: assume that all unary operations cancel out + themselves, i.e. op op x == x? */ + return 1; + } + + newval = ops[node->v.UnaryOp.op - 1](arg->v.Lit.v); + return make_lit(node, newval, arena); +} + +static PyObject *binary_power(PyObject *base, PyObject *power) +{ + return PyNumber_Power(base, power, Py_None); +} + +static int fold_binop(expr_ty node, PyArena *arena) +{ + typedef PyObject *(*bin_op)(PyObject*, PyObject*); + static const bin_op ops[] = { + PyNumber_Add, + PyNumber_Subtract, + PyNumber_Multiply, + PyNumber_TrueDivide, + PyNumber_Remainder, + binary_power, + PyNumber_Lshift, + PyNumber_Rshift, + PyNumber_Or, + PyNumber_Xor, + PyNumber_And, + PyNumber_FloorDivide + }; + + expr_ty lhs, rhs; + Py_ssize_t size; + PyObject *newval; + + lhs = node->v.BinOp.left; + rhs = node->v.BinOp.right; + if (lhs->kind != Lit_kind || rhs->kind != Lit_kind) + return 1; + + newval = ops[node->v.BinOp.op - 1](lhs->v.Lit.v, rhs->v.Lit.v); + /* Avoid creating large constants. */ + size = PyObject_Size(newval); + if (size == -1) { + if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) + return 1; + PyErr_Clear(); + } + else if (size > 20) { + Py_DECREF(newval); + return 1; + } + return make_lit(node, newval, arena); +} + +static PyObject *make_const_tuple(asdl_seq *elts, int make_set) +{ + PyObject *newval; + int i; + + for (i = 0; i < asdl_seq_LEN(elts); i++) { + expr_ty e = (expr_ty)asdl_seq_GET(elts, i); + if (e->kind != Lit_kind) + return NULL; + } + + newval = PyTuple_New(asdl_seq_LEN(elts)); + if (newval == NULL) + return NULL; + + for (i = 0; i < asdl_seq_LEN(elts); i++) { + expr_ty e = (expr_ty)asdl_seq_GET(elts, i); + Py_INCREF(e->v.Lit.v); + PyTuple_SET_ITEM(newval, i, e->v.Lit.v); + } + + /* Need to create frozen_set instead. */ + if (make_set) { + PyObject *old = newval; + newval = PyFrozenSet_New(old); + Py_DECREF(old); + } + return newval; +} + +static int fold_tuple(expr_ty node, PyArena *arena) +{ + PyObject *newval; + + if (node->v.Tuple.ctx != Load) + return 1; + + newval = make_const_tuple(node->v.Tuple.elts, 0); + return make_lit(node, newval, arena); +} + +static int fold_subscr(expr_ty node, PyArena *arena) +{ + PyObject *newval; + expr_ty arg, idx; + slice_ty slice; + + arg = node->v.Subscript.value; + slice = node->v.Subscript.slice; + if (node->v.Subscript.ctx != Load || + arg->kind != Lit_kind || + /* TODO: handle other types of slices */ + slice->kind != Index_kind || + slice->v.Index.value->kind != Lit_kind) + return 1; + + idx = slice->v.Index.value; + newval = PyObject_GetItem(arg->v.Lit.v, idx->v.Lit.v); + return make_lit(node, newval, arena); +} + +static int fold_compare(expr_ty node, PyArena *arena) +{ + asdl_int_seq *ops; + asdl_seq *args; + PyObject *newval; + int i; + + ops = node->v.Compare.ops; + args = node->v.Compare.comparators; + /* TODO: optimize cases with literal arguments. */ + for (i = 0; i < asdl_seq_LEN(ops); i++) { + int op; + expr_ty arg; + asdl_seq *elts; + + op = asdl_seq_GET(ops, i); + arg = (expr_ty)asdl_seq_GET(args, i); + /* Change literal list or set in 'in' or 'not in' into + tuple or frozenset respectively. */ + /* TODO: do the same when list or set is used as iterable + in for loop and comprehensions? */ + if (op != In && op != NotIn) + continue; + if (arg->kind == List_kind) + elts = arg->v.List.elts; + else if (arg->kind == Set_kind) + elts = arg->v.Set.elts; + else continue; + + newval = make_const_tuple(elts, arg->kind == Set_kind); + make_lit(arg, newval, arena); + } + return 1; +} + +static int astfold_slice(slice_ty node_, PyArena* ctx_); +static int astfold_keyword(keyword_ty node_, PyArena* ctx_); +static int astfold_expr(expr_ty node_, PyArena* ctx_); +static int astfold_stmt(stmt_ty node_, PyArena* ctx_); +static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_); +static int astfold_comprehension(comprehension_ty node_, PyArena* ctx_); +static int astfold_arg(arg_ty node_, PyArena* ctx_); +static int astfold_mod(mod_ty node_, PyArena* ctx_); +static int astfold_arguments(arguments_ty node_, PyArena* ctx_); +#define CALL(FUNC, TYPE, ARG) \ + if (!FUNC((ARG), ctx_)) \ + return 0; + +#define CALL_OPT(FUNC, TYPE, ARG) \ + if ((ARG) != NULL && !FUNC((ARG), ctx_)) \ + return 0; + +#define CALL_SEQ(FUNC, TYPE, ARG) { \ + int i; \ + asdl_seq *seq = (ARG); /* avoid variable capture */ \ + for (i = 0; i < asdl_seq_LEN(seq); i++) { \ + TYPE elt = (TYPE)asdl_seq_GET(seq, i); \ + /* XXX: kw_defaults has NULL elements, because it's \ + sized to the number of kw args */ \ + if (elt != NULL && !FUNC(elt, ctx_)) \ + return 0; \ + } \ +} + +static int astfold_mod(mod_ty node_, PyArena* ctx_) +{ + switch (node_->kind) { + case Module_kind: + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Module.body); + break; + case Interactive_kind: + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Interactive.body); + break; + case Expression_kind: + CALL(astfold_expr, expr_ty, node_->v.Expression.body); + break; + case Suite_kind: + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Suite.body); + break; + default: + break; + } + return 1; +} + +static int astfold_expr(expr_ty node_, PyArena* ctx_) +{ + switch (node_->kind) { + case BoolOp_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.BoolOp.values); + break; + case BinOp_kind: + CALL(astfold_expr, expr_ty, node_->v.BinOp.left); + CALL(astfold_expr, expr_ty, node_->v.BinOp.right); + CALL(fold_binop, expr_ty, node_); + break; + case UnaryOp_kind: + CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand); + CALL(fold_unaryop, expr_ty, node_); + break; + case Lambda_kind: + CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args); + CALL(astfold_expr, expr_ty, node_->v.Lambda.body); + break; + case IfExp_kind: + CALL(astfold_expr, expr_ty, node_->v.IfExp.test); + CALL(astfold_expr, expr_ty, node_->v.IfExp.body); + CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse); + break; + case Dict_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.keys); + CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.values); + break; + case Set_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.Set.elts); + break; + case ListComp_kind: + CALL(astfold_expr, expr_ty, node_->v.ListComp.elt); + CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.ListComp.generators); + break; + case SetComp_kind: + CALL(astfold_expr, expr_ty, node_->v.SetComp.elt); + CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.SetComp.generators); + break; + case DictComp_kind: + CALL(astfold_expr, expr_ty, node_->v.DictComp.key); + CALL(astfold_expr, expr_ty, node_->v.DictComp.value); + CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.DictComp.generators); + break; + case GeneratorExp_kind: + CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt); + CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.GeneratorExp.generators); + break; + case Yield_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value); + break; + case Compare_kind: + CALL(astfold_expr, expr_ty, node_->v.Compare.left); + CALL_SEQ(astfold_expr, expr_ty, node_->v.Compare.comparators); + CALL(fold_compare, expr_ty, node_); + break; + case Call_kind: + CALL(astfold_expr, expr_ty, node_->v.Call.func); + CALL_SEQ(astfold_expr, expr_ty, node_->v.Call.args); + CALL_SEQ(astfold_keyword, keyword_ty, node_->v.Call.keywords); + CALL_OPT(astfold_expr, expr_ty, node_->v.Call.starargs); + CALL_OPT(astfold_expr, expr_ty, node_->v.Call.kwargs); + break; + case Attribute_kind: + CALL(astfold_expr, expr_ty, node_->v.Attribute.value); + break; + case Subscript_kind: + CALL(astfold_expr, expr_ty, node_->v.Subscript.value); + CALL(astfold_slice, slice_ty, node_->v.Subscript.slice); + CALL(fold_subscr, expr_ty, node_); + break; + case Starred_kind: + CALL(astfold_expr, expr_ty, node_->v.Starred.value); + break; + case List_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.List.elts); + break; + case Tuple_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.Tuple.elts); + CALL(fold_tuple, expr_ty, node_); + break; + default: + break; + } + return 1; +} + +static int astfold_slice(slice_ty node_, PyArena* ctx_) +{ + switch (node_->kind) { + case Slice_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower); + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper); + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step); + break; + case ExtSlice_kind: + CALL_SEQ(astfold_slice, slice_ty, node_->v.ExtSlice.dims); + break; + case Index_kind: + CALL(astfold_expr, expr_ty, node_->v.Index.value); + break; + default: + break; + } + return 1; +} + +static int astfold_keyword(keyword_ty node_, PyArena* ctx_) +{ + CALL(astfold_expr, expr_ty, node_->value); + return 1; +} + +static int astfold_comprehension(comprehension_ty node_, PyArena* ctx_) +{ + CALL(astfold_expr, expr_ty, node_->target); + CALL(astfold_expr, expr_ty, node_->iter); + CALL_SEQ(astfold_expr, expr_ty, node_->ifs); + return 1; +} + +static int astfold_arguments(arguments_ty node_, PyArena* ctx_) +{ + CALL_SEQ(astfold_arg, arg_ty, node_->args); + CALL_OPT(astfold_expr, expr_ty, node_->varargannotation); + CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs); + CALL_OPT(astfold_expr, expr_ty, node_->kwargannotation); + CALL_SEQ(astfold_expr, expr_ty, node_->defaults); + CALL_SEQ(astfold_expr, expr_ty, node_->kw_defaults); + return 1; +} + +static int astfold_arg(arg_ty node_, PyArena* ctx_) +{ + CALL_OPT(astfold_expr, expr_ty, node_->annotation); + return 1; +} + +static int astfold_stmt(stmt_ty node_, PyArena* ctx_) +{ + switch (node_->kind) { + case FunctionDef_kind: + CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.FunctionDef.body); + CALL_SEQ(astfold_expr, expr_ty, node_->v.FunctionDef.decorator_list); + CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns); + break; + case ClassDef_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.bases); + CALL_SEQ(astfold_keyword, keyword_ty, node_->v.ClassDef.keywords); + CALL_OPT(astfold_expr, expr_ty, node_->v.ClassDef.starargs); + CALL_OPT(astfold_expr, expr_ty, node_->v.ClassDef.kwargs); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.ClassDef.body); + CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.decorator_list); + break; + case Return_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value); + break; + case Delete_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.Delete.targets); + break; + case Assign_kind: + CALL_SEQ(astfold_expr, expr_ty, node_->v.Assign.targets); + CALL(astfold_expr, expr_ty, node_->v.Assign.value); + break; + case AugAssign_kind: + CALL(astfold_expr, expr_ty, node_->v.AugAssign.target); + CALL(astfold_expr, expr_ty, node_->v.AugAssign.value); + break; + case For_kind: + CALL(astfold_expr, expr_ty, node_->v.For.target); + CALL(astfold_expr, expr_ty, node_->v.For.iter); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.body); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.orelse); + break; + case While_kind: + CALL(astfold_expr, expr_ty, node_->v.While.test); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.body); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.orelse); + break; + case If_kind: + CALL(astfold_expr, expr_ty, node_->v.If.test); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.body); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.orelse); + break; + case With_kind: + CALL(astfold_expr, expr_ty, node_->v.With.context_expr); + CALL_OPT(astfold_expr, expr_ty, node_->v.With.optional_vars); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.With.body); + break; + case Raise_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc); + CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause); + break; + case TryExcept_kind: + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.TryExcept.body); + CALL_SEQ(astfold_excepthandler, excepthandler_ty, node_->v.TryExcept.handlers); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.TryExcept.orelse); + break; + case TryFinally_kind: + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.TryFinally.body); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.TryFinally.finalbody); + break; + case Assert_kind: + CALL(astfold_expr, expr_ty, node_->v.Assert.test); + CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg); + break; + case Expr_kind: + CALL(astfold_expr, expr_ty, node_->v.Expr.value); + break; + default: + break; + } + return 1; +} + +static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_) +{ + switch (node_->kind) { + case ExceptHandler_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type); + CALL_SEQ(astfold_stmt, stmt_ty, node_->v.ExceptHandler.body); + break; + default: + break; + } + return 1; +} + + +#undef CALL +#undef CALL_OPT +#undef CALL_SEQ + +int _PyAST_Optimize(mod_ty mod, PyArena *arena) +{ + return astfold_mod(mod, arena); +} +