bpo-22671: Clarify and test default read method implementations (#4568) · python/cpython@1b74f9b (original) (raw)

`@@ -807,8 +807,8 @@ def test_multi_close(self):

`

807

807

`self.assertRaises(ValueError, f.flush)

`

808

808

``

809

809

`def test_RawIOBase_read(self):

`

810

``

`-

Exercise the default RawIOBase.read() implementation (which calls

`

811

``

`-

readinto() internally).

`

``

810

`+

Exercise the default limited RawIOBase.read(n) implementation (which

`

``

811

`+

calls readinto() internally).

`

812

812

`rawio = self.MockRawIOWithoutRead((b"abc", b"d", None, b"efg", None))

`

813

813

`self.assertEqual(rawio.read(2), b"ab")

`

814

814

`self.assertEqual(rawio.read(2), b"c")

`

`@@ -916,6 +916,55 @@ def check_path_succeeds(path):

`

916

916

`with self.assertRaisesRegex(ValueError, 'read/write/append mode'):

`

917

917

`self.open(PathLike(support.TESTFN), 'rwxa')

`

918

918

``

``

919

`+

def test_RawIOBase_readall(self):

`

``

920

`+

Exercise the default unlimited RawIOBase.read() and readall()

`

``

921

`+

implementations.

`

``

922

`+

rawio = self.MockRawIOWithoutRead((b"abc", b"d", b"efg"))

`

``

923

`+

self.assertEqual(rawio.read(), b"abcdefg")

`

``

924

`+

rawio = self.MockRawIOWithoutRead((b"abc", b"d", b"efg"))

`

``

925

`+

self.assertEqual(rawio.readall(), b"abcdefg")

`

``

926

+

``

927

`+

def test_BufferedIOBase_readinto(self):

`

``

928

`+

Exercise the default BufferedIOBase.readinto() and readinto1()

`

``

929

`+

implementations (which call read() or read1() internally).

`

``

930

`+

class Reader(self.BufferedIOBase):

`

``

931

`+

def init(self, avail):

`

``

932

`+

self.avail = avail

`

``

933

`+

def read(self, size):

`

``

934

`+

result = self.avail[:size]

`

``

935

`+

self.avail = self.avail[size:]

`

``

936

`+

return result

`

``

937

`+

def read1(self, size):

`

``

938

`+

"""Returns no more than 5 bytes at once"""

`

``

939

`+

return self.read(min(size, 5))

`

``

940

`+

tests = (

`

``

941

`+

(test method, total data available, read buffer size, expected

`

``

942

`+

read size)

`

``

943

`+

("readinto", 10, 5, 5),

`

``

944

`+

("readinto", 10, 6, 6), # More than read1() can return

`

``

945

`+

("readinto", 5, 6, 5), # Buffer larger than total available

`

``

946

`+

("readinto", 6, 7, 6),

`

``

947

`+

("readinto", 10, 0, 0), # Empty buffer

`

``

948

`+

("readinto1", 10, 5, 5), # Result limited to single read1() call

`

``

949

`+

("readinto1", 10, 6, 5), # Buffer larger than read1() can return

`

``

950

`+

("readinto1", 5, 6, 5), # Buffer larger than total available

`

``

951

`+

("readinto1", 6, 7, 5),

`

``

952

`+

("readinto1", 10, 0, 0), # Empty buffer

`

``

953

`+

)

`

``

954

`+

UNUSED_BYTE = 0x81

`

``

955

`+

for test in tests:

`

``

956

`+

with self.subTest(test):

`

``

957

`+

method, avail, request, result = test

`

``

958

`+

reader = Reader(bytes(range(avail)))

`

``

959

`+

buffer = bytearray((UNUSED_BYTE,) * request)

`

``

960

`+

method = getattr(reader, method)

`

``

961

`+

self.assertEqual(method(buffer), result)

`

``

962

`+

self.assertEqual(len(buffer), request)

`

``

963

`+

self.assertSequenceEqual(buffer[:result], range(result))

`

``

964

`+

unused = (UNUSED_BYTE,) * (request - result)

`

``

965

`+

self.assertSequenceEqual(buffer[result:], unused)

`

``

966

`+

self.assertEqual(len(reader.avail), avail - result)

`

``

967

+

919

968

``

920

969

`class CIOTest(IOTest):

`

921

970

``