Skip to content

Commit

Permalink
bpo-37960: Silence only necessary errors in repr() of buffered and te…
Browse files Browse the repository at this point in the history
…xt streams. (pythonGH-15543)
  • Loading branch information
serhiy-storchaka authored Aug 29, 2019
1 parent f5896a0 commit b235a1b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
8 changes: 4 additions & 4 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def __del__(self):
"""Destructor. Calls close()."""
try:
closed = self.closed
except Exception:
except AttributeError:
# If getting closed fails, then the object is probably
# in an unusable state, so ignore.
return
Expand Down Expand Up @@ -867,7 +867,7 @@ def __repr__(self):
clsname = self.__class__.__qualname__
try:
name = self.name
except Exception:
except AttributeError:
return "<{}.{}>".format(modname, clsname)
else:
return "<{}.{} name={!r}>".format(modname, clsname, name)
Expand Down Expand Up @@ -2083,13 +2083,13 @@ def __repr__(self):
self.__class__.__qualname__)
try:
name = self.name
except Exception:
except AttributeError:
pass
else:
result += " name={0!r}".format(name)
try:
mode = self.mode
except Exception:
except AttributeError:
pass
else:
result += " mode={0!r}".format(mode)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``repr()`` of buffered and text streams now silences only expected
exceptions when get the value of "name" and "mode" attributes.
12 changes: 7 additions & 5 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 9 additions & 13 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2899,14 +2899,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)
Expand All @@ -2915,14 +2915,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)
Expand Down

0 comments on commit b235a1b

Please sign in to comment.