mirror of
https://github.com/python/cpython.git
synced 2025-12-03 08:04:34 +00:00
bpo-37960: Silence only necessary errors in repr() of buffered and text streams. (GH-15543)
This commit is contained in:
parent
f5896a05ed
commit
b235a1b473
4 changed files with 22 additions and 22 deletions
|
|
@ -409,7 +409,7 @@ class IOBase(metaclass=abc.ABCMeta):
|
||||||
"""Destructor. Calls close()."""
|
"""Destructor. Calls close()."""
|
||||||
try:
|
try:
|
||||||
closed = self.closed
|
closed = self.closed
|
||||||
except Exception:
|
except AttributeError:
|
||||||
# If getting closed fails, then the object is probably
|
# If getting closed fails, then the object is probably
|
||||||
# in an unusable state, so ignore.
|
# in an unusable state, so ignore.
|
||||||
return
|
return
|
||||||
|
|
@ -867,7 +867,7 @@ class _BufferedIOMixin(BufferedIOBase):
|
||||||
clsname = self.__class__.__qualname__
|
clsname = self.__class__.__qualname__
|
||||||
try:
|
try:
|
||||||
name = self.name
|
name = self.name
|
||||||
except Exception:
|
except AttributeError:
|
||||||
return "<{}.{}>".format(modname, clsname)
|
return "<{}.{}>".format(modname, clsname)
|
||||||
else:
|
else:
|
||||||
return "<{}.{} name={!r}>".format(modname, clsname, name)
|
return "<{}.{} name={!r}>".format(modname, clsname, name)
|
||||||
|
|
@ -2083,13 +2083,13 @@ class TextIOWrapper(TextIOBase):
|
||||||
self.__class__.__qualname__)
|
self.__class__.__qualname__)
|
||||||
try:
|
try:
|
||||||
name = self.name
|
name = self.name
|
||||||
except Exception:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
result += " name={0!r}".format(name)
|
result += " name={0!r}".format(name)
|
||||||
try:
|
try:
|
||||||
mode = self.mode
|
mode = self.mode
|
||||||
except Exception:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
result += " mode={0!r}".format(mode)
|
result += " mode={0!r}".format(mode)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
``repr()`` of buffered and text streams now silences only expected
|
||||||
|
exceptions when get the value of "name" and "mode" attributes.
|
||||||
|
|
@ -1378,12 +1378,14 @@ buffered_repr(buffered *self)
|
||||||
{
|
{
|
||||||
PyObject *nameobj, *res;
|
PyObject *nameobj, *res;
|
||||||
|
|
||||||
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
|
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
|
||||||
if (nameobj == NULL) {
|
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_Exception))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
return NULL;
|
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);
|
res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -2899,14 +2899,14 @@ textiowrapper_repr(textio *self)
|
||||||
}
|
}
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
|
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
|
||||||
if (nameobj == NULL) {
|
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
|
||||||
if (PyErr_ExceptionMatches(PyExc_Exception))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
else {
|
/* Ignore ValueError raised if the underlying stream was detached */
|
||||||
|
PyErr_Clear();
|
||||||
|
}
|
||||||
|
if (nameobj != NULL) {
|
||||||
s = PyUnicode_FromFormat(" name=%R", nameobj);
|
s = PyUnicode_FromFormat(" name=%R", nameobj);
|
||||||
Py_DECREF(nameobj);
|
Py_DECREF(nameobj);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
|
|
@ -2915,14 +2915,10 @@ textiowrapper_repr(textio *self)
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
|
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) {
|
||||||
if (modeobj == NULL) {
|
|
||||||
if (PyErr_ExceptionMatches(PyExc_Exception))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
else {
|
if (modeobj != NULL) {
|
||||||
s = PyUnicode_FromFormat(" mode=%R", modeobj);
|
s = PyUnicode_FromFormat(" mode=%R", modeobj);
|
||||||
Py_DECREF(modeobj);
|
Py_DECREF(modeobj);
|
||||||
if (s == NULL)
|
if (s == NULL)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue