File tree Expand file tree Collapse file tree 4 files changed +22
-22
lines changed Expand file tree Collapse file tree 4 files changed +22
-22
lines changed Original file line number Diff line number Diff line change @@ -409,7 +409,7 @@ def __del__(self):
409
409
"""Destructor. Calls close()."""
410
410
try :
411
411
closed = self .closed
412
- except Exception :
412
+ except AttributeError :
413
413
# If getting closed fails, then the object is probably
414
414
# in an unusable state, so ignore.
415
415
return
@@ -867,7 +867,7 @@ def __repr__(self):
867
867
clsname = self .__class__ .__qualname__
868
868
try :
869
869
name = self .name
870
- except Exception :
870
+ except AttributeError :
871
871
return "<{}.{}>" .format (modname , clsname )
872
872
else :
873
873
return "<{}.{} name={!r}>" .format (modname , clsname , name )
@@ -2083,13 +2083,13 @@ def __repr__(self):
2083
2083
self .__class__ .__qualname__ )
2084
2084
try :
2085
2085
name = self .name
2086
- except Exception :
2086
+ except AttributeError :
2087
2087
pass
2088
2088
else :
2089
2089
result += " name={0!r}" .format (name )
2090
2090
try :
2091
2091
mode = self .mode
2092
- except Exception :
2092
+ except AttributeError :
2093
2093
pass
2094
2094
else :
2095
2095
result += " mode={0!r}" .format (mode )
Original file line number Diff line number Diff line change
1
+ ``repr() `` of buffered and text streams now silences only expected
2
+ exceptions when get the value of "name" and "mode" attributes.
Original file line number Diff line number Diff line change @@ -1378,12 +1378,14 @@ buffered_repr(buffered *self)
1378
1378
{
1379
1379
PyObject * nameobj , * res ;
1380
1380
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 )) {
1386
1383
return NULL ;
1384
+ }
1385
+ /* Ignore ValueError raised if the underlying stream was detached */
1386
+ PyErr_Clear ();
1387
+ }
1388
+ if (nameobj == NULL ) {
1387
1389
res = PyUnicode_FromFormat ("<%s>" , Py_TYPE (self )-> tp_name );
1388
1390
}
1389
1391
else {
Original file line number Diff line number Diff line change @@ -2899,14 +2899,14 @@ textiowrapper_repr(textio *self)
2899
2899
}
2900
2900
goto error ;
2901
2901
}
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 )) {
2907
2904
goto error ;
2905
+ }
2906
+ /* Ignore ValueError raised if the underlying stream was detached */
2907
+ PyErr_Clear ();
2908
2908
}
2909
- else {
2909
+ if ( nameobj != NULL ) {
2910
2910
s = PyUnicode_FromFormat (" name=%R" , nameobj );
2911
2911
Py_DECREF (nameobj );
2912
2912
if (s == NULL )
@@ -2915,14 +2915,10 @@ textiowrapper_repr(textio *self)
2915
2915
if (res == NULL )
2916
2916
goto error ;
2917
2917
}
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 ;
2924
2920
}
2925
- else {
2921
+ if ( modeobj != NULL ) {
2926
2922
s = PyUnicode_FromFormat (" mode=%R" , modeobj );
2927
2923
Py_DECREF (modeobj );
2928
2924
if (s == NULL )
You can’t perform that action at this time.
0 commit comments