From f320be77ffb73e3b9e7fc98c37b8df3975d84b40 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 25 Jan 2018 10:49:40 +0200 Subject: bpo-32571: Avoid raising unneeded AttributeError and silencing it in C code (GH-5222) Add two new private APIs: _PyObject_LookupAttr() and _PyObject_LookupAttrId() --- Objects/genobject.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'Objects/genobject.c') diff --git a/Objects/genobject.c b/Objects/genobject.c index 0a34c1f6a9..7baffa7fc0 100644 --- a/Objects/genobject.c +++ b/Objects/genobject.c @@ -352,13 +352,11 @@ gen_close_iter(PyObject *yf) return -1; } else { - PyObject *meth = _PyObject_GetAttrId(yf, &PyId_close); - if (meth == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) - PyErr_WriteUnraisable(yf); - PyErr_Clear(); + PyObject *meth; + if (_PyObject_LookupAttrId(yf, &PyId_close, &meth) < 0) { + PyErr_WriteUnraisable(yf); } - else { + if (meth) { retval = _PyObject_CallNoArg(meth); Py_DECREF(meth); if (retval == NULL) @@ -471,13 +469,12 @@ _gen_throw(PyGenObject *gen, int close_on_genexit, gen->gi_running = 0; } else { /* `yf` is an iterator or a coroutine-like object. */ - PyObject *meth = _PyObject_GetAttrId(yf, &PyId_throw); + PyObject *meth; + if (_PyObject_LookupAttrId(yf, &PyId_throw, &meth) < 0) { + Py_DECREF(yf); + return NULL; + } if (meth == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { - Py_DECREF(yf); - return NULL; - } - PyErr_Clear(); Py_DECREF(yf); goto throw_here; } -- cgit v1.2.1