diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-07-27 00:04:42 +0200 |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-07-27 00:04:42 +0200 |
commit | 70d0128cba691093d2cb863c26433429b485efb4 (patch) | |
tree | ef11a263b9f9ee1285ba81b2d98b48f7d662f3fb /Python/Python-ast.c | |
parent | f5b460e80170bc0f3affad1aaa4d43bb226967c6 (diff) | |
parent | 67a91bb80b4468b01151620a6b986a6c5c349f60 (diff) | |
download | cpython-70d0128cba691093d2cb863c26433429b485efb4.tar.gz |
(Merge 3.3) Parser/asdl_c.py: use Py_CLEAR()
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r-- | Python/Python-ast.c | 276 |
1 files changed, 172 insertions, 104 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c index d78657ce07..49d19dacd6 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -271,6 +271,10 @@ static PyTypeObject *Bytes_type; static char *Bytes_fields[]={ "s", }; +static PyTypeObject *NameConstant_type; +static char *NameConstant_fields[]={ + "value", +}; static PyTypeObject *Ellipsis_type; static PyTypeObject *Attribute_type; _Py_IDENTIFIER(attr); @@ -408,24 +412,24 @@ static char *ExceptHandler_fields[]={ static PyTypeObject *arguments_type; static PyObject* ast2obj_arguments(void*); _Py_IDENTIFIER(vararg); -_Py_IDENTIFIER(varargannotation); _Py_IDENTIFIER(kwonlyargs); +_Py_IDENTIFIER(kw_defaults); _Py_IDENTIFIER(kwarg); -_Py_IDENTIFIER(kwargannotation); _Py_IDENTIFIER(defaults); -_Py_IDENTIFIER(kw_defaults); static char *arguments_fields[]={ "args", "vararg", - "varargannotation", "kwonlyargs", + "kw_defaults", "kwarg", - "kwargannotation", "defaults", - "kw_defaults", }; static PyTypeObject *arg_type; static PyObject* ast2obj_arg(void*); +static char *arg_attributes[] = { + "lineno", + "col_offset", +}; _Py_IDENTIFIER(arg); _Py_IDENTIFIER(annotation); static char *arg_fields[]={ @@ -673,6 +677,7 @@ static PyObject* ast2obj_object(void *o) Py_INCREF((PyObject*)o); return (PyObject*)o; } +#define ast2obj_singleton ast2obj_object #define ast2obj_identifier ast2obj_object #define ast2obj_string ast2obj_object #define ast2obj_bytes ast2obj_object @@ -684,6 +689,17 @@ static PyObject* ast2obj_int(long b) /* Conversion Python -> AST */ +static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena) +{ + if (obj != Py_None && obj != Py_True && obj != Py_False) { + PyErr_SetString(PyExc_ValueError, + "AST singleton must be True, False, or None"); + return 1; + } + *out = obj; + return 0; +} + static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena) { if (obj == Py_None) @@ -754,6 +770,19 @@ static int add_ast_fields(void) return 0; } +static int exists_not_none(PyObject *obj, _Py_Identifier *id) +{ + int isnone; + PyObject *attr = _PyObject_GetAttrId(obj, id); + if (!attr) { + PyErr_Clear(); + return 0; + } + isnone = attr == Py_None; + Py_DECREF(attr); + return !isnone; +} + static int init_types(void) { @@ -858,6 +887,9 @@ static int init_types(void) if (!Str_type) return 0; Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1); if (!Bytes_type) return 0; + NameConstant_type = make_type("NameConstant", expr_type, + NameConstant_fields, 1); + if (!NameConstant_type) return 0; Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0); if (!Ellipsis_type) return 0; Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); @@ -1035,6 +1067,7 @@ static int init_types(void) comprehension_type = make_type("comprehension", &AST_type, comprehension_fields, 3); if (!comprehension_type) return 0; + if (!add_attributes(comprehension_type, NULL, 0)) return 0; excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0); if (!excepthandler_type) return 0; if (!add_attributes(excepthandler_type, excepthandler_attributes, 2)) @@ -1042,16 +1075,21 @@ static int init_types(void) ExceptHandler_type = make_type("ExceptHandler", excepthandler_type, ExceptHandler_fields, 3); if (!ExceptHandler_type) return 0; - arguments_type = make_type("arguments", &AST_type, arguments_fields, 8); + arguments_type = make_type("arguments", &AST_type, arguments_fields, 6); if (!arguments_type) return 0; + if (!add_attributes(arguments_type, NULL, 0)) return 0; arg_type = make_type("arg", &AST_type, arg_fields, 2); if (!arg_type) return 0; + if (!add_attributes(arg_type, arg_attributes, 2)) return 0; keyword_type = make_type("keyword", &AST_type, keyword_fields, 2); if (!keyword_type) return 0; + if (!add_attributes(keyword_type, NULL, 0)) return 0; alias_type = make_type("alias", &AST_type, alias_fields, 2); if (!alias_type) return 0; + if (!add_attributes(alias_type, NULL, 0)) return 0; withitem_type = make_type("withitem", &AST_type, withitem_fields, 2); if (!withitem_type) return 0; + if (!add_attributes(withitem_type, NULL, 0)) return 0; initialized = 1; return 1; } @@ -1919,6 +1957,25 @@ Bytes(bytes s, int lineno, int col_offset, PyArena *arena) } expr_ty +NameConstant(singleton value, int lineno, int col_offset, PyArena *arena) +{ + expr_ty p; + if (!value) { + PyErr_SetString(PyExc_ValueError, + "field value is required for NameConstant"); + return NULL; + } + p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); + if (!p) + return NULL; + p->kind = NameConstant_kind; + p->v.NameConstant.value = value; + p->lineno = lineno; + p->col_offset = col_offset; + return p; +} + +expr_ty Ellipsis(int lineno, int col_offset, PyArena *arena) { expr_ty p; @@ -2173,9 +2230,8 @@ ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int } arguments_ty -arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation, - asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation, - asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena) +arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq * + kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena) { arguments_ty p; p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p)); @@ -2183,12 +2239,10 @@ arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation, return NULL; p->args = args; p->vararg = vararg; - p->varargannotation = varargannotation; p->kwonlyargs = kwonlyargs; + p->kw_defaults = kw_defaults; p->kwarg = kwarg; - p->kwargannotation = kwargannotation; p->defaults = defaults; - p->kw_defaults = kw_defaults; return p; } @@ -2939,6 +2993,15 @@ ast2obj_expr(void* _o) goto failed; Py_DECREF(value); break; + case NameConstant_kind: + result = PyType_GenericNew(NameConstant_type, NULL, NULL); + if (!result) goto failed; + value = ast2obj_singleton(o->v.NameConstant.value); + if (!value) goto failed; + if (_PyObject_SetAttrId(result, &PyId_value, value) == -1) + goto failed; + Py_DECREF(value); + break; case Ellipsis_kind: result = PyType_GenericNew(Ellipsis_type, NULL, NULL); if (!result) goto failed; @@ -3357,29 +3420,24 @@ ast2obj_arguments(void* _o) if (_PyObject_SetAttrId(result, &PyId_args, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_identifier(o->vararg); + value = ast2obj_arg(o->vararg); if (!value) goto failed; if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_expr(o->varargannotation); - if (!value) goto failed; - if (_PyObject_SetAttrId(result, &PyId_varargannotation, value) == -1) - goto failed; - Py_DECREF(value); value = ast2obj_list(o->kwonlyargs, ast2obj_arg); if (!value) goto failed; if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_identifier(o->kwarg); + value = ast2obj_list(o->kw_defaults, ast2obj_expr); if (!value) goto failed; - if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1) + if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_expr(o->kwargannotation); + value = ast2obj_arg(o->kwarg); if (!value) goto failed; - if (_PyObject_SetAttrId(result, &PyId_kwargannotation, value) == -1) + if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1) goto failed; Py_DECREF(value); value = ast2obj_list(o->defaults, ast2obj_expr); @@ -3387,11 +3445,6 @@ ast2obj_arguments(void* _o) if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1) goto failed; Py_DECREF(value); - value = ast2obj_list(o->kw_defaults, ast2obj_expr); - if (!value) goto failed; - if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1) - goto failed; - Py_DECREF(value); return result; failed: Py_XDECREF(value); @@ -3421,6 +3474,16 @@ ast2obj_arg(void* _o) if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1) goto failed; Py_DECREF(value); + value = ast2obj_int(o->lineno); + if (!value) goto failed; + if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0) + goto failed; + Py_DECREF(value); + value = ast2obj_int(o->col_offset); + if (!value) goto failed; + if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0) + goto failed; + Py_DECREF(value); return result; failed: Py_XDECREF(value); @@ -3777,7 +3840,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_returns)) { + if (exists_not_none(obj, &PyId_returns)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_returns); if (tmp == NULL) goto failed; @@ -3864,7 +3927,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_starargs)) { + if (exists_not_none(obj, &PyId_starargs)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_starargs); if (tmp == NULL) goto failed; @@ -3874,7 +3937,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) } else { starargs = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_kwargs)) { + if (exists_not_none(obj, &PyId_kwargs)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_kwargs); if (tmp == NULL) goto failed; @@ -3944,7 +4007,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) if (isinstance) { expr_ty value; - if (_PyObject_HasAttrId(obj, &PyId_value)) { + if (exists_not_none(obj, &PyId_value)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_value); if (tmp == NULL) goto failed; @@ -4382,7 +4445,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) expr_ty exc; expr_ty cause; - if (_PyObject_HasAttrId(obj, &PyId_exc)) { + if (exists_not_none(obj, &PyId_exc)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_exc); if (tmp == NULL) goto failed; @@ -4392,7 +4455,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) } else { exc = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_cause)) { + if (exists_not_none(obj, &PyId_cause)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_cause); if (tmp == NULL) goto failed; @@ -4536,7 +4599,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_msg)) { + if (exists_not_none(obj, &PyId_msg)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_msg); if (tmp == NULL) goto failed; @@ -4594,7 +4657,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) asdl_seq* names; int level; - if (_PyObject_HasAttrId(obj, &PyId_module)) { + if (exists_not_none(obj, &PyId_module)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_module); if (tmp == NULL) goto failed; @@ -4628,7 +4691,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_level)) { + if (exists_not_none(obj, &PyId_level)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_level); if (tmp == NULL) goto failed; @@ -5315,7 +5378,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) if (isinstance) { expr_ty value; - if (_PyObject_HasAttrId(obj, &PyId_value)) { + if (exists_not_none(obj, &PyId_value)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_value); if (tmp == NULL) goto failed; @@ -5493,7 +5556,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_starargs)) { + if (exists_not_none(obj, &PyId_starargs)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_starargs); if (tmp == NULL) goto failed; @@ -5503,7 +5566,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) } else { starargs = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_kwargs)) { + if (exists_not_none(obj, &PyId_kwargs)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_kwargs); if (tmp == NULL) goto failed; @@ -5584,6 +5647,28 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) if (*out == NULL) goto failed; return 0; } + isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type); + if (isinstance == -1) { + return 1; + } + if (isinstance) { + singleton value; + + if (_PyObject_HasAttrId(obj, &PyId_value)) { + int res; + tmp = _PyObject_GetAttrId(obj, &PyId_value); + if (tmp == NULL) goto failed; + res = obj2ast_singleton(tmp, &value, arena); + if (res != 0) goto failed; + Py_CLEAR(tmp); + } else { + PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant"); + return 1; + } + *out = NameConstant(value, lineno, col_offset, arena); + if (*out == NULL) goto failed; + return 0; + } isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type); if (isinstance == -1) { return 1; @@ -5933,7 +6018,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena) expr_ty upper; expr_ty step; - if (_PyObject_HasAttrId(obj, &PyId_lower)) { + if (exists_not_none(obj, &PyId_lower)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_lower); if (tmp == NULL) goto failed; @@ -5943,7 +6028,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena) } else { lower = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_upper)) { + if (exists_not_none(obj, &PyId_upper)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_upper); if (tmp == NULL) goto failed; @@ -5953,7 +6038,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena) } else { upper = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_step)) { + if (exists_not_none(obj, &PyId_step)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_step); if (tmp == NULL) goto failed; @@ -6400,7 +6485,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) identifier name; asdl_seq* body; - if (_PyObject_HasAttrId(obj, &PyId_type)) { + if (exists_not_none(obj, &PyId_type)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_type); if (tmp == NULL) goto failed; @@ -6410,7 +6495,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena) } else { type = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_name)) { + if (exists_not_none(obj, &PyId_name)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_name); if (tmp == NULL) goto failed; @@ -6460,13 +6545,11 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena) { PyObject* tmp = NULL; asdl_seq* args; - identifier vararg; - expr_ty varargannotation; + arg_ty vararg; asdl_seq* kwonlyargs; - identifier kwarg; - expr_ty kwargannotation; - asdl_seq* defaults; asdl_seq* kw_defaults; + arg_ty kwarg; + asdl_seq* defaults; if (_PyObject_HasAttrId(obj, &PyId_args)) { int res; @@ -6492,26 +6575,16 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_vararg)) { + if (exists_not_none(obj, &PyId_vararg)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_vararg); if (tmp == NULL) goto failed; - res = obj2ast_identifier(tmp, &vararg, arena); + res = obj2ast_arg(tmp, &vararg, arena); if (res != 0) goto failed; Py_CLEAR(tmp); } else { vararg = NULL; } - if (_PyObject_HasAttrId(obj, &PyId_varargannotation)) { - int res; - tmp = _PyObject_GetAttrId(obj, &PyId_varargannotation); - if (tmp == NULL) goto failed; - res = obj2ast_expr(tmp, &varargannotation, arena); - if (res != 0) goto failed; - Py_CLEAR(tmp); - } else { - varargannotation = NULL; - } if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) { int res; Py_ssize_t len; @@ -6536,25 +6609,39 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_kwarg)) { + if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) { int res; - tmp = _PyObject_GetAttrId(obj, &PyId_kwarg); + Py_ssize_t len; + Py_ssize_t i; + tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults); if (tmp == NULL) goto failed; - res = obj2ast_identifier(tmp, &kwarg, arena); - if (res != 0) goto failed; + if (!PyList_Check(tmp)) { + PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name); + 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); + } Py_CLEAR(tmp); } else { - kwarg = NULL; + PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments"); + return 1; } - if (_PyObject_HasAttrId(obj, &PyId_kwargannotation)) { + if (exists_not_none(obj, &PyId_kwarg)) { int res; - tmp = _PyObject_GetAttrId(obj, &PyId_kwargannotation); + tmp = _PyObject_GetAttrId(obj, &PyId_kwarg); if (tmp == NULL) goto failed; - res = obj2ast_expr(tmp, &kwargannotation, arena); + res = obj2ast_arg(tmp, &kwarg, arena); if (res != 0) goto failed; Py_CLEAR(tmp); } else { - kwargannotation = NULL; + kwarg = NULL; } if (_PyObject_HasAttrId(obj, &PyId_defaults)) { int res; @@ -6580,32 +6667,8 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) { - int res; - Py_ssize_t len; - Py_ssize_t i; - tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults); - if (tmp == NULL) goto failed; - if (!PyList_Check(tmp)) { - PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name); - 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); - } - Py_CLEAR(tmp); - } else { - PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments"); - return 1; - } - *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg, - kwargannotation, defaults, kw_defaults, arena); + *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, defaults, + arena); return 0; failed: Py_XDECREF(tmp); @@ -6630,7 +6693,7 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_annotation)) { + if (exists_not_none(obj, &PyId_annotation)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_annotation); if (tmp == NULL) goto failed; @@ -6701,7 +6764,7 @@ obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_asname)) { + if (exists_not_none(obj, &PyId_asname)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_asname); if (tmp == NULL) goto failed; @@ -6736,7 +6799,7 @@ obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena) PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem"); return 1; } - if (_PyObject_HasAttrId(obj, &PyId_optional_vars)) { + if (exists_not_none(obj, &PyId_optional_vars)) { int res; tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars); if (tmp == NULL) goto failed; @@ -6766,7 +6829,7 @@ PyInit__ast(void) if (!m) return NULL; d = PyModule_GetDict(m); if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return NULL; - if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) + if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) return NULL; if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return NULL; if (PyDict_SetItemString(d, "Module", (PyObject*)Module_type) < 0) return @@ -6846,6 +6909,8 @@ PyInit__ast(void) 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, "NameConstant", (PyObject*)NameConstant_type) < + 0) return NULL; if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0) return NULL; if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) < 0) @@ -6945,7 +7010,8 @@ PyInit__ast(void) PyObject* PyAST_mod2obj(mod_ty t) { - init_types(); + if (!init_types()) + return NULL; return ast2obj_mod(t); } @@ -6959,7 +7025,8 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) int isinstance; assert(0 <= mode && mode <= 2); - init_types(); + if (!init_types()) + return NULL; isinstance = PyObject_IsInstance(ast, req_type[mode]); if (isinstance == -1) @@ -6977,7 +7044,8 @@ mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode) int PyAST_Check(PyObject* obj) { - init_types(); + if (!init_types()) + return -1; return PyObject_IsInstance(obj, (PyObject*)&AST_type); } |