@@ -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 ()
@@ -1304,6 +1321,16 @@ def test_max_buffer_size_removal(self):
13041321 with self .assertRaises (TypeError ):
13051322 self .tp (self .MockRawIO (), 8 , 12 )
13061323
1324+ def test_write_error_on_close (self ):
1325+ raw = self .MockRawIO ()
1326+ def bad_write (b ):
1327+ raise IOError ()
1328+ raw .write = bad_write
1329+ b = self .tp (raw )
1330+ b .write (b'spam' )
1331+ self .assertRaises (IOError , b .close ) # exception not swallowed
1332+ self .assertTrue (b .closed )
1333+
13071334
13081335class CBufferedWriterTest (BufferedWriterTest , SizeofTest ):
13091336 tp = io .BufferedWriter
@@ -2473,6 +2500,7 @@ def bad_flush():
24732500 raise IOError ()
24742501 txt .flush = bad_flush
24752502 self .assertRaises (IOError , txt .close ) # exception not swallowed
2503+ self .assertTrue (txt .closed )
24762504
24772505 def test_multi_close (self ):
24782506 txt = self .TextIOWrapper (self .BytesIO (self .testdata ), encoding = "ascii" )
0 commit comments