@@ -603,6 +603,7 @@ def bad_flush():
603603 raise IOError ()
604604 f .flush = bad_flush
605605 self .assertRaises (IOError , f .close ) # exception not swallowed
606+ self .assertTrue (f .closed )
606607
607608 def test_multi_close (self ):
608609 f = self .open (support .TESTFN , "wb" , buffering = 0 )
@@ -780,6 +781,22 @@ def bad_flush():
780781 raw .flush = bad_flush
781782 b = self .tp (raw )
782783 self .assertRaises (IOError , b .close ) # exception not swallowed
784+ self .assertTrue (b .closed )
785+
786+ def test_close_error_on_close (self ):
787+ raw = self .MockRawIO ()
788+ def bad_flush ():
789+ raise IOError ('flush' )
790+ def bad_close ():
791+ raise IOError ('close' )
792+ raw .close = bad_close
793+ b = self .tp (raw )
794+ b .flush = bad_flush
795+ with self .assertRaises (IOError ) as err : # exception not swallowed
796+ b .close ()
797+ self .assertEqual (err .exception .args , ('close' ,))
798+ self .assertEqual (err .exception .__context__ .args , ('flush' ,))
799+ self .assertFalse (b .closed )
783800
784801 def test_multi_close (self ):
785802 raw = self .MockRawIO ()
@@ -1296,6 +1313,16 @@ def test_max_buffer_size_removal(self):
12961313 with self .assertRaises (TypeError ):
12971314 self .tp (self .MockRawIO (), 8 , 12 )
12981315
1316+ def test_write_error_on_close (self ):
1317+ raw = self .MockRawIO ()
1318+ def bad_write (b ):
1319+ raise IOError ()
1320+ raw .write = bad_write
1321+ b = self .tp (raw )
1322+ b .write (b'spam' )
1323+ self .assertRaises (IOError , b .close ) # exception not swallowed
1324+ self .assertTrue (b .closed )
1325+
12991326
13001327class CBufferedWriterTest (BufferedWriterTest , SizeofTest ):
13011328 tp = io .BufferedWriter
@@ -2465,6 +2492,7 @@ def bad_flush():
24652492 raise IOError ()
24662493 txt .flush = bad_flush
24672494 self .assertRaises (IOError , txt .close ) # exception not swallowed
2495+ self .assertTrue (txt .closed )
24682496
24692497 def test_multi_close (self ):
24702498 txt = self .TextIOWrapper (self .BytesIO (self .testdata ), encoding = "ascii" )
0 commit comments