diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-01-25 08:49:40 (GMT) |
---|---|---|
committer | INADA Naoki <methane@users.noreply.github.com> | 2018-01-25 08:49:40 (GMT) |
commit | f320be77ffb73e3b9e7fc98c37b8df3975d84b40 (patch) | |
tree | 552338f0200938249233fa4aa7b00add61965337 /Objects/genobject.c | |
parent | 2b822a0bb1de2612c85d8f75e3ce89eda2ac9f68 (diff) | |
download | cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.zip cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.tar.gz cpython-f320be77ffb73e3b9e7fc98c37b8df3975d84b40.tar.bz2 |
bpo-32571: Avoid raising unneeded AttributeError and silencing it in C code (GH-5222)
Add two new private APIs: _PyObject_LookupAttr() and _PyObject_LookupAttrId()
Diffstat (limited to 'Objects/genobject.c')
-rw-r--r-- | Objects/genobject.c | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/Objects/genobject.c b/Objects/genobject.c index 0a34c1f..7baffa7 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; } |