Skip to content

Commit 102130a

Browse files
bpo-37960: Silence only necessary errors in repr() of buffered and text streams. (GH-15543)
(cherry picked from commit b235a1b) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 43b7ed7 commit 102130a

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

Lib/_pyio.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def __del__(self):
407407
"""Destructor. Calls close()."""
408408
try:
409409
closed = self.closed
410-
except Exception:
410+
except AttributeError:
411411
# If getting closed fails, then the object is probably
412412
# in an unusable state, so ignore.
413413
return
@@ -865,7 +865,7 @@ def __repr__(self):
865865
clsname = self.__class__.__qualname__
866866
try:
867867
name = self.name
868-
except Exception:
868+
except AttributeError:
869869
return "<{}.{}>".format(modname, clsname)
870870
else:
871871
return "<{}.{} name={!r}>".format(modname, clsname, name)
@@ -2079,13 +2079,13 @@ def __repr__(self):
20792079
self.__class__.__qualname__)
20802080
try:
20812081
name = self.name
2082-
except Exception:
2082+
except AttributeError:
20832083
pass
20842084
else:
20852085
result += " name={0!r}".format(name)
20862086
try:
20872087
mode = self.mode
2088-
except Exception:
2088+
except AttributeError:
20892089
pass
20902090
else:
20912091
result += " mode={0!r}".format(mode)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``repr()`` of buffered and text streams now silences only expected
2+
exceptions when get the value of "name" and "mode" attributes.

Modules/_io/bufferedio.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,12 +1378,14 @@ buffered_repr(buffered *self)
13781378
{
13791379
PyObject *nameobj, *res;
13801380

1381-
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
1382-
if (nameobj == NULL) {
1383-
if (PyErr_ExceptionMatches(PyExc_Exception))
1384-
PyErr_Clear();
1385-
else
1381+
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
1382+
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
13861383
return NULL;
1384+
}
1385+
/* Ignore ValueError raised if the underlying stream was detached */
1386+
PyErr_Clear();
1387+
}
1388+
if (nameobj == NULL) {
13871389
res = PyUnicode_FromFormat("<%s>", Py_TYPE(self)->tp_name);
13881390
}
13891391
else {

Modules/_io/textio.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2860,14 +2860,14 @@ textiowrapper_repr(textio *self)
28602860
}
28612861
goto error;
28622862
}
2863-
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
2864-
if (nameobj == NULL) {
2865-
if (PyErr_ExceptionMatches(PyExc_Exception))
2866-
PyErr_Clear();
2867-
else
2863+
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
2864+
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
28682865
goto error;
2866+
}
2867+
/* Ignore ValueError raised if the underlying stream was detached */
2868+
PyErr_Clear();
28692869
}
2870-
else {
2870+
if (nameobj != NULL) {
28712871
s = PyUnicode_FromFormat(" name=%R", nameobj);
28722872
Py_DECREF(nameobj);
28732873
if (s == NULL)
@@ -2876,14 +2876,10 @@ textiowrapper_repr(textio *self)
28762876
if (res == NULL)
28772877
goto error;
28782878
}
2879-
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
2880-
if (modeobj == NULL) {
2881-
if (PyErr_ExceptionMatches(PyExc_Exception))
2882-
PyErr_Clear();
2883-
else
2884-
goto error;
2879+
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) {
2880+
goto error;
28852881
}
2886-
else {
2882+
if (modeobj != NULL) {
28872883
s = PyUnicode_FromFormat(" mode=%R", modeobj);
28882884
Py_DECREF(modeobj);
28892885
if (s == NULL)

0 commit comments

Comments
 (0)