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 @@ -407,7 +407,7 @@ def __del__(self):
407
407
"""Destructor. Calls close()."""
408
408
try :
409
409
closed = self .closed
410
- except Exception :
410
+ except AttributeError :
411
411
# If getting closed fails, then the object is probably
412
412
# in an unusable state, so ignore.
413
413
return
@@ -865,7 +865,7 @@ def __repr__(self):
865
865
clsname = self .__class__ .__qualname__
866
866
try :
867
867
name = self .name
868
- except Exception :
868
+ except AttributeError :
869
869
return "<{}.{}>" .format (modname , clsname )
870
870
else :
871
871
return "<{}.{} name={!r}>" .format (modname , clsname , name )
@@ -2079,13 +2079,13 @@ def __repr__(self):
2079
2079
self .__class__ .__qualname__ )
2080
2080
try :
2081
2081
name = self .name
2082
- except Exception :
2082
+ except AttributeError :
2083
2083
pass
2084
2084
else :
2085
2085
result += " name={0!r}" .format (name )
2086
2086
try :
2087
2087
mode = self .mode
2088
- except Exception :
2088
+ except AttributeError :
2089
2089
pass
2090
2090
else :
2091
2091
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 @@ -2860,14 +2860,14 @@ textiowrapper_repr(textio *self)
2860
2860
}
2861
2861
goto error ;
2862
2862
}
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 )) {
2868
2865
goto error ;
2866
+ }
2867
+ /* Ignore ValueError raised if the underlying stream was detached */
2868
+ PyErr_Clear ();
2869
2869
}
2870
- else {
2870
+ if ( nameobj != NULL ) {
2871
2871
s = PyUnicode_FromFormat (" name=%R" , nameobj );
2872
2872
Py_DECREF (nameobj );
2873
2873
if (s == NULL )
@@ -2876,14 +2876,10 @@ textiowrapper_repr(textio *self)
2876
2876
if (res == NULL )
2877
2877
goto error ;
2878
2878
}
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 ;
2885
2881
}
2886
- else {
2882
+ if ( modeobj != NULL ) {
2887
2883
s = PyUnicode_FromFormat (" mode=%R" , modeobj );
2888
2884
Py_DECREF (modeobj );
2889
2885
if (s == NULL )
You can’t perform that action at this time.
0 commit comments