bpo-22671: Clarify and test default read method implementations (GH-4… · python/cpython@0aa2a1d (original) (raw)
`@@ -796,8 +796,8 @@ def test_multi_close(self):
`
796
796
`self.assertRaises(ValueError, f.flush)
`
797
797
``
798
798
`def test_RawIOBase_read(self):
`
799
``
`-
Exercise the default RawIOBase.read() implementation (which calls
`
800
``
`-
readinto() internally).
`
``
799
`+
Exercise the default limited RawIOBase.read(n) implementation (which
`
``
800
`+
calls readinto() internally).
`
801
801
`rawio = self.MockRawIOWithoutRead((b"abc", b"d", None, b"efg", None))
`
802
802
`self.assertEqual(rawio.read(2), b"ab")
`
803
803
`self.assertEqual(rawio.read(2), b"c")
`
`@@ -905,6 +905,55 @@ def check_path_succeeds(path):
`
905
905
`with self.assertRaisesRegex(ValueError, 'read/write/append mode'):
`
906
906
`self.open(PathLike(support.TESTFN), 'rwxa')
`
907
907
``
``
908
`+
def test_RawIOBase_readall(self):
`
``
909
`+
Exercise the default unlimited RawIOBase.read() and readall()
`
``
910
`+
implementations.
`
``
911
`+
rawio = self.MockRawIOWithoutRead((b"abc", b"d", b"efg"))
`
``
912
`+
self.assertEqual(rawio.read(), b"abcdefg")
`
``
913
`+
rawio = self.MockRawIOWithoutRead((b"abc", b"d", b"efg"))
`
``
914
`+
self.assertEqual(rawio.readall(), b"abcdefg")
`
``
915
+
``
916
`+
def test_BufferedIOBase_readinto(self):
`
``
917
`+
Exercise the default BufferedIOBase.readinto() and readinto1()
`
``
918
`+
implementations (which call read() or read1() internally).
`
``
919
`+
class Reader(self.BufferedIOBase):
`
``
920
`+
def init(self, avail):
`
``
921
`+
self.avail = avail
`
``
922
`+
def read(self, size):
`
``
923
`+
result = self.avail[:size]
`
``
924
`+
self.avail = self.avail[size:]
`
``
925
`+
return result
`
``
926
`+
def read1(self, size):
`
``
927
`+
"""Returns no more than 5 bytes at once"""
`
``
928
`+
return self.read(min(size, 5))
`
``
929
`+
tests = (
`
``
930
`+
(test method, total data available, read buffer size, expected
`
``
931
`+
read size)
`
``
932
`+
("readinto", 10, 5, 5),
`
``
933
`+
("readinto", 10, 6, 6), # More than read1() can return
`
``
934
`+
("readinto", 5, 6, 5), # Buffer larger than total available
`
``
935
`+
("readinto", 6, 7, 6),
`
``
936
`+
("readinto", 10, 0, 0), # Empty buffer
`
``
937
`+
("readinto1", 10, 5, 5), # Result limited to single read1() call
`
``
938
`+
("readinto1", 10, 6, 5), # Buffer larger than read1() can return
`
``
939
`+
("readinto1", 5, 6, 5), # Buffer larger than total available
`
``
940
`+
("readinto1", 6, 7, 5),
`
``
941
`+
("readinto1", 10, 0, 0), # Empty buffer
`
``
942
`+
)
`
``
943
`+
UNUSED_BYTE = 0x81
`
``
944
`+
for test in tests:
`
``
945
`+
with self.subTest(test):
`
``
946
`+
method, avail, request, result = test
`
``
947
`+
reader = Reader(bytes(range(avail)))
`
``
948
`+
buffer = bytearray((UNUSED_BYTE,) * request)
`
``
949
`+
method = getattr(reader, method)
`
``
950
`+
self.assertEqual(method(buffer), result)
`
``
951
`+
self.assertEqual(len(buffer), request)
`
``
952
`+
self.assertSequenceEqual(buffer[:result], range(result))
`
``
953
`+
unused = (UNUSED_BYTE,) * (request - result)
`
``
954
`+
self.assertSequenceEqual(buffer[result:], unused)
`
``
955
`+
self.assertEqual(len(reader.avail), avail - result)
`
``
956
+
908
957
``
909
958
`class CIOTest(IOTest):
`
910
959
``