diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-08-29 08:13:29 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-29 08:13:29 (GMT) |
commit | 102130a63c93fde7bcabbbf4fae1ebce38888981 (patch) | |
tree | 87cca8cb9f7d74436fd7e6710e9995256c82cd11 /Modules/_io | |
parent | 43b7ed77a8224c378b436ad3385733454198be41 (diff) | |
download | cpython-102130a63c93fde7bcabbbf4fae1ebce38888981.zip cpython-102130a63c93fde7bcabbbf4fae1ebce38888981.tar.gz cpython-102130a63c93fde7bcabbbf4fae1ebce38888981.tar.bz2 |
bpo-37960: Silence only necessary errors in repr() of buffered and text streams. (GH-15543)
(cherry picked from commit b235a1b47394eedc5f8ea4cf214f56c4c6932e59)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/bufferedio.c | 12 | ||||
-rw-r--r-- | Modules/_io/textio.c | 22 |
2 files changed, 16 insertions, 18 deletions
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 44e12db..8e8ff97 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -1378,12 +1378,14 @@ buffered_repr(buffered *self) { PyObject *nameobj, *res; - nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name); - if (nameobj == NULL) { - if (PyErr_ExceptionMatches(PyExc_Exception)) - PyErr_Clear(); - else + if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { + if (!PyErr_ExceptionMatches(PyExc_ValueError)) { return NULL; + } + /* Ignore ValueError raised if the underlying stream was detached */ + PyErr_Clear(); + } + if (nameobj == NULL) { res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name); } else { diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 73b2756..4fe1b29 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -2860,14 +2860,14 @@ textiowrapper_repr(textio *self) } goto error; } - nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name); - if (nameobj == NULL) { - if (PyErr_ExceptionMatches(PyExc_Exception)) - PyErr_Clear(); - else + if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) { + if (!PyErr_ExceptionMatches(PyExc_ValueError)) { goto error; + } + /* Ignore ValueError raised if the underlying stream was detached */ + PyErr_Clear(); } - else { + if (nameobj != NULL) { s = PyUnicode_FromFormat(" name=%R", nameobj); Py_DECREF(nameobj); if (s == NULL) @@ -2876,14 +2876,10 @@ textiowrapper_repr(textio *self) if (res == NULL) goto error; } - modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode); - if (modeobj == NULL) { - if (PyErr_ExceptionMatches(PyExc_Exception)) - PyErr_Clear(); - else - goto error; + if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) { + goto error; } - else { + if (modeobj != NULL) { s = PyUnicode_FromFormat(" mode=%R", modeobj); Py_DECREF(modeobj); if (s == NULL) |