@@ -47,22 +47,16 @@ def _default_chunk_size():
4747 return f ._CHUNK_SIZE
4848
4949
50- class MockRawIO :
50+ class MockRawIOWithoutRead :
51+ """A RawIO implementation without read(), so as to exercise the default
52+ RawIO.read() which calls readinto()."""
5153
5254 def __init__ (self , read_stack = ()):
5355 self ._read_stack = list (read_stack )
5456 self ._write_stack = []
5557 self ._reads = 0
5658 self ._extraneous_reads = 0
5759
58- def read (self , n = None ):
59- self ._reads += 1
60- try :
61- return self ._read_stack .pop (0 )
62- except :
63- self ._extraneous_reads += 1
64- return b""
65-
6660 def write (self , b ):
6761 self ._write_stack .append (bytes (b ))
6862 return len (b )
@@ -109,6 +103,23 @@ def readinto(self, buf):
109103 def truncate (self , pos = None ):
110104 return pos
111105
106+ class CMockRawIOWithoutRead (MockRawIOWithoutRead , io .RawIOBase ):
107+ pass
108+
109+ class PyMockRawIOWithoutRead (MockRawIOWithoutRead , pyio .RawIOBase ):
110+ pass
111+
112+
113+ class MockRawIO (MockRawIOWithoutRead ):
114+
115+ def read (self , n = None ):
116+ self ._reads += 1
117+ try :
118+ return self ._read_stack .pop (0 )
119+ except :
120+ self ._extraneous_reads += 1
121+ return b""
122+
112123class CMockRawIO (MockRawIO , io .RawIOBase ):
113124 pass
114125
@@ -554,6 +565,19 @@ def test_multi_close(self):
554565 f .close ()
555566 self .assertRaises (ValueError , f .flush )
556567
568+ def test_RawIOBase_read (self ):
569+ # Exercise the default RawIOBase.read() implementation (which calls
570+ # readinto() internally).
571+ rawio = self .MockRawIOWithoutRead ((b"abc" , b"d" , None , b"efg" , None ))
572+ self .assertEqual (rawio .read (2 ), b"ab" )
573+ self .assertEqual (rawio .read (2 ), b"c" )
574+ self .assertEqual (rawio .read (2 ), b"d" )
575+ self .assertEqual (rawio .read (2 ), None )
576+ self .assertEqual (rawio .read (2 ), b"ef" )
577+ self .assertEqual (rawio .read (2 ), b"g" )
578+ self .assertEqual (rawio .read (2 ), None )
579+ self .assertEqual (rawio .read (2 ), b"" )
580+
557581class CIOTest (IOTest ):
558582 pass
559583
@@ -2557,7 +2581,7 @@ def test_main():
25572581 # Put the namespaces of the IO module we are testing and some useful mock
25582582 # classes in the __dict__ of each test.
25592583 mocks = (MockRawIO , MisbehavedRawIO , MockFileIO , CloseFailureIO ,
2560- MockNonBlockWriterIO )
2584+ MockNonBlockWriterIO , MockRawIOWithoutRead )
25612585 all_members = io .__all__ + ["IncrementalNewlineDecoder" ]
25622586 c_io_ns = {name : getattr (io , name ) for name in all_members }
25632587 py_io_ns = {name : getattr (pyio , name ) for name in all_members }
0 commit comments