@@ -2615,6 +2615,38 @@ def test_rawio_write_through(self):
26152615 txt .write ('5' )
26162616 self .assertEqual (b'' .join (raw ._write_stack ), b'123\n 45' )
26172617
2618+ def test_bufio_write_through (self ):
2619+ # Issue #21396: write_through=True doesn't force a flush()
2620+ # on the underlying binary buffered object.
2621+ flush_called , write_called = [], []
2622+ class BufferedWriter (self .BufferedWriter ):
2623+ def flush (self , * args , ** kwargs ):
2624+ flush_called .append (True )
2625+ return super ().flush (* args , ** kwargs )
2626+ def write (self , * args , ** kwargs ):
2627+ write_called .append (True )
2628+ return super ().write (* args , ** kwargs )
2629+
2630+ rawio = self .BytesIO ()
2631+ data = b"a"
2632+ bufio = BufferedWriter (rawio , len (data )* 2 )
2633+ textio = self .TextIOWrapper (bufio , encoding = 'ascii' ,
2634+ write_through = True )
2635+ # write to the buffered io but don't overflow the buffer
2636+ text = data .decode ('ascii' )
2637+ textio .write (text )
2638+
2639+ # buffer.flush is not called with write_through=True
2640+ self .assertFalse (flush_called )
2641+ # buffer.write *is* called with write_through=True
2642+ self .assertTrue (write_called )
2643+ self .assertEqual (rawio .getvalue (), b"" ) # no flush
2644+
2645+ write_called = [] # reset
2646+ textio .write (text * 10 ) # total content is larger than bufio buffer
2647+ self .assertTrue (write_called )
2648+ self .assertEqual (rawio .getvalue (), data * 11 ) # all flushed
2649+
26182650 def test_read_nonbytes (self ):
26192651 # Issue #17106
26202652 # Crash when underlying read() returns non-bytes
0 commit comments