Skip to content

Commit b235a1b

Browse files
bpo-37960: Silence only necessary errors in repr() of buffered and text streams. (GH-15543)
1 parent f5896a0 commit b235a1b

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
@@ -409,7 +409,7 @@ def __del__(self):
409409
"""Destructor. Calls close()."""
410410
try:
411411
closed = self.closed
412-
except Exception:
412+
except AttributeError:
413413
# If getting closed fails, then the object is probably
414414
# in an unusable state, so ignore.
415415
return
@@ -867,7 +867,7 @@ def __repr__(self):
867867
clsname = self.__class__.__qualname__
868868
try:
869869
name = self.name
870-
except Exception:
870+
except AttributeError:
871871
return "<{}.{}>".format(modname, clsname)
872872
else:
873873
return "<{}.{} name={!r}>".format(modname, clsname, name)
@@ -2083,13 +2083,13 @@ def __repr__(self):
20832083
self.__class__.__qualname__)
20842084
try:
20852085
name = self.name
2086-
except Exception:
2086+
except AttributeError:
20872087
pass
20882088
else:
20892089
result += " name={0!r}".format(name)
20902090
try:
20912091
mode = self.mode
2092-
except Exception:
2092+
except AttributeError:
20932093
pass
20942094
else:
20952095
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
@@ -2899,14 +2899,14 @@ textiowrapper_repr(textio *self)
28992899
}
29002900
goto error;
29012901
}
2902-
nameobj = _PyObject_GetAttrId((PyObject *) self, &PyId_name);
2903-
if (nameobj == NULL) {
2904-
if (PyErr_ExceptionMatches(PyExc_Exception))
2905-
PyErr_Clear();
2906-
else
2902+
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_name, &nameobj) < 0) {
2903+
if (!PyErr_ExceptionMatches(PyExc_ValueError)) {
29072904
goto error;
2905+
}
2906+
/* Ignore ValueError raised if the underlying stream was detached */
2907+
PyErr_Clear();
29082908
}
2909-
else {
2909+
if (nameobj != NULL) {
29102910
s = PyUnicode_FromFormat(" name=%R", nameobj);
29112911
Py_DECREF(nameobj);
29122912
if (s == NULL)
@@ -2915,14 +2915,10 @@ textiowrapper_repr(textio *self)
29152915
if (res == NULL)
29162916
goto error;
29172917
}
2918-
modeobj = _PyObject_GetAttrId((PyObject *) self, &PyId_mode);
2919-
if (modeobj == NULL) {
2920-
if (PyErr_ExceptionMatches(PyExc_Exception))
2921-
PyErr_Clear();
2922-
else
2923-
goto error;
2918+
if (_PyObject_LookupAttrId((PyObject *) self, &PyId_mode, &modeobj) < 0) {
2919+
goto error;
29242920
}
2925-
else {
2921+
if (modeobj != NULL) {
29262922
s = PyUnicode_FromFormat(" mode=%R", modeobj);
29272923
Py_DECREF(modeobj);
29282924
if (s == NULL)

0 commit comments

Comments
 (0)