@@ -67,6 +67,11 @@ class EmptyStruct(ctypes.Structure):
67
67
'--with-memory-sanitizer' in _config_args
68
68
)
69
69
70
+ # Does io.IOBase logs unhandled exceptions on calling close()?
71
+ # They are silenced by default in release build.
72
+ DESTRUCTOR_LOG_ERRORS = (hasattr (sys , "gettotalrefcount" ) or sys .flags .dev_mode )
73
+
74
+
70
75
def _default_chunk_size ():
71
76
"""Get the default TextIOWrapper chunk size"""
72
77
with open (__file__ , "r" , encoding = "latin-1" ) as f :
@@ -1097,9 +1102,16 @@ def f():
1097
1102
s = s .getvalue ().strip ()
1098
1103
if s :
1099
1104
# The destructor *may* have printed an unraisable error, check it
1100
- self .assertEqual (len (s .splitlines ()), 1 )
1101
- self .assertTrue (s .startswith ("Exception OSError: " ), s )
1102
- self .assertTrue (s .endswith (" ignored" ), s )
1105
+ lines = s .splitlines ()
1106
+ if DESTRUCTOR_LOG_ERRORS :
1107
+ self .assertEqual (len (lines ), 5 )
1108
+ self .assertTrue (lines [0 ].startswith ("Exception ignored in: " ), lines )
1109
+ self .assertEqual (lines [1 ], "Traceback (most recent call last):" , lines )
1110
+ self .assertEqual (lines [4 ], 'OSError:' , lines )
1111
+ else :
1112
+ self .assertEqual (len (lines ), 1 )
1113
+ self .assertTrue (lines [- 1 ].startswith ("Exception OSError: " ), lines )
1114
+ self .assertTrue (lines [- 1 ].endswith (" ignored" ), lines )
1103
1115
1104
1116
def test_repr (self ):
1105
1117
raw = self .MockRawIO ()
@@ -2833,9 +2845,16 @@ def f():
2833
2845
s = s .getvalue ().strip ()
2834
2846
if s :
2835
2847
# The destructor *may* have printed an unraisable error, check it
2836
- self .assertEqual (len (s .splitlines ()), 1 )
2837
- self .assertTrue (s .startswith ("Exception OSError: " ), s )
2838
- self .assertTrue (s .endswith (" ignored" ), s )
2848
+ lines = s .splitlines ()
2849
+ if DESTRUCTOR_LOG_ERRORS :
2850
+ self .assertEqual (len (lines ), 5 )
2851
+ self .assertTrue (lines [0 ].startswith ("Exception ignored in: " ), lines )
2852
+ self .assertEqual (lines [1 ], "Traceback (most recent call last):" , lines )
2853
+ self .assertEqual (lines [4 ], 'OSError:' , lines )
2854
+ else :
2855
+ self .assertEqual (len (lines ), 1 )
2856
+ self .assertTrue (lines [- 1 ].startswith ("Exception OSError: " ), lines )
2857
+ self .assertTrue (lines [- 1 ].endswith (" ignored" ), lines )
2839
2858
2840
2859
# Systematic tests of the text I/O API
2841
2860
0 commit comments