Skip to content

Commit e1f76eb

Browse files
committed
test(io): share readonly readinto test setup and cases
1 parent 5c5f966 commit e1f76eb

3 files changed

Lines changed: 28 additions & 31 deletions

File tree

‎Lib/test/test_io/test_bufferedio.py‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -314,20 +314,9 @@ def test_readinto(self):
314314
self.assertEqual(bufio.readinto(b), 1)
315315
self.assertEqual(b, b"cb")
316316

317-
@support.subTests("method", ("readinto", "readinto1"))
318-
@support.subTests("buffer", (
319-
b"xxx", memoryview(b"xxx"),
320-
))
321-
@support.subTests("position", (0, 4))
322-
@support.subTests("buffered", (False, True))
323-
def test_readinto_readonly_buffer(self, method, buffer, position, buffered):
324-
data = b"abcdef"
325-
with self.tp(self.BytesIO(data)) as bufio:
326-
bufio.seek(position)
327-
if buffered:
328-
bufio.peek(1)
329-
check_readinto_readonly_buffer(
330-
self, bufio, method, buffer, position, data[position:])
317+
def test_readinto_readonly_buffer(self):
318+
check_readinto_readonly_buffer(
319+
self, lambda data: self.tp(self.BytesIO(data)), prefill=True)
331320

332321
def test_readinto1(self):
333322
buffer_size = 10

‎Lib/test/test_io/test_memoryio.py‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -569,17 +569,8 @@ def test_readinto(self):
569569
memio.readinto(b)
570570
self.assertEqual(b, b"")
571571

572-
@support.subTests("method", ("readinto", "readinto1"))
573-
@support.subTests("buffer", (
574-
b"xxx", memoryview(b"xxx"), b"", memoryview(b""),
575-
))
576-
@support.subTests("position", (0, 4, 10))
577-
def test_readinto_readonly_buffer(self, method, buffer, position):
578-
data = b"abcdef"
579-
with self.ioclass(data) as memio:
580-
memio.seek(position)
581-
check_readinto_readonly_buffer(
582-
self, memio, method, buffer, position, data[position:])
572+
def test_readinto_readonly_buffer(self):
573+
check_readinto_readonly_buffer(self, self.ioclass, empty_and_eof=True)
583574

584575
def test_relative_seek(self):
585576
buf = self.buftype("1234567890")

‎Lib/test/test_io/utils.py‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import threading
33
import time
44
import unittest
5+
from itertools import product
56

67
import io # C implementation of io
78
import _pyio as pyio # Python implementation of io
@@ -26,12 +27,28 @@ def byteslike(*pos, **kw):
2627
return obj
2728

2829

29-
def check_readinto_readonly_buffer(testcase, stream, method, buffer,
30-
position, remaining):
31-
op = getattr(stream, method)
32-
testcase.assertRaises(TypeError, op, buffer)
33-
testcase.assertEqual(stream.tell(), position)
34-
testcase.assertEqual(stream.read(), remaining)
30+
def check_readinto_readonly_buffer(testcase, stream_factory, *,
31+
empty_and_eof=False, prefill=False):
32+
data = b"abcdef"
33+
buffers = [b"xxx", memoryview(b"xxx")]
34+
positions = [0, 4]
35+
if empty_and_eof:
36+
buffers.extend([b"", memoryview(b"")])
37+
positions.append(10)
38+
methods = ("readinto", "readinto1")
39+
prefilled = (False, True) if prefill else (False,)
40+
for method, buffer, position, buffered in product(
41+
methods, buffers, positions, prefilled):
42+
with testcase.subTest(method=method, buffer=buffer, position=position,
43+
buffered=buffered):
44+
with stream_factory(data) as stream:
45+
stream.seek(position)
46+
if buffered:
47+
stream.peek(1)
48+
op = getattr(stream, method)
49+
testcase.assertRaises(TypeError, op, buffer)
50+
testcase.assertEqual(stream.tell(), position)
51+
testcase.assertEqual(stream.read(), data[position:])
3552

3653

3754
class MockRawIOWithoutRead:

0 commit comments

Comments
 (0)