summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-10-20 23:48:14 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-10-20 23:48:14 (GMT)
commitccb2c0e31056de091abdd62fc07ca6e4bb052f24 (patch)
tree84ff95e0152d152124b078a9f6e85f053b7147c1 /Lib
parentea8762cae64813788633b7d2a93c2c513c01fdea (diff)
downloadcpython-ccb2c0e31056de091abdd62fc07ca6e4bb052f24.zip
cpython-ccb2c0e31056de091abdd62fc07ca6e4bb052f24.tar.gz
cpython-ccb2c0e31056de091abdd62fc07ca6e4bb052f24.tar.bz2
Issue #23214: Implement optional BufferedReader, BytesIO read1() argument
Diffstat (limited to 'Lib')
-rw-r--r--Lib/_pyio.py18
-rw-r--r--Lib/test/test_io.py16
-rw-r--r--Lib/test/test_memoryio.py6
3 files changed, 25 insertions, 15 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py
index d0947f0..569527b 100644
--- a/Lib/_pyio.py
+++ b/Lib/_pyio.py
@@ -635,7 +635,7 @@ class BufferedIOBase(IOBase):
implementation, but wrap one.
"""
- def read(self, size=None):
+ def read(self, size=-1):
"""Read and return up to size bytes, where size is an int.
If the argument is omitted, None, or negative, reads and
@@ -655,7 +655,7 @@ class BufferedIOBase(IOBase):
"""
self._unsupported("read")
- def read1(self, size=None):
+ def read1(self, size=-1):
"""Read up to size bytes with at most one read() system call,
where size is an int.
"""
@@ -863,7 +863,7 @@ class BytesIO(BufferedIOBase):
self._buffer.clear()
super().close()
- def read(self, size=None):
+ def read(self, size=-1):
if self.closed:
raise ValueError("read from closed file")
if size is None:
@@ -877,7 +877,7 @@ class BytesIO(BufferedIOBase):
self._pos = newpos
return bytes(b)
- def read1(self, size):
+ def read1(self, size=-1):
"""This is the same as read.
"""
return self.read(size)
@@ -1073,12 +1073,12 @@ class BufferedReader(_BufferedIOMixin):
self._read_pos = 0
return self._read_buf[self._read_pos:]
- def read1(self, size):
+ def read1(self, size=-1):
"""Reads up to size bytes, with at most one read() system call."""
# Returns up to size bytes. If at least one byte is buffered, we
# only return buffered bytes. Otherwise, we do one raw read.
if size < 0:
- raise ValueError("number of bytes to read must be positive")
+ size = self.buffer_size
if size == 0:
return b""
with self._read_lock:
@@ -1270,7 +1270,7 @@ class BufferedRWPair(BufferedIOBase):
self.reader = BufferedReader(reader, buffer_size)
self.writer = BufferedWriter(writer, buffer_size)
- def read(self, size=None):
+ def read(self, size=-1):
if size is None:
size = -1
return self.reader.read(size)
@@ -1284,7 +1284,7 @@ class BufferedRWPair(BufferedIOBase):
def peek(self, size=0):
return self.reader.peek(size)
- def read1(self, size):
+ def read1(self, size=-1):
return self.reader.read1(size)
def readinto1(self, b):
@@ -1370,7 +1370,7 @@ class BufferedRandom(BufferedWriter, BufferedReader):
self.flush()
return BufferedReader.peek(self, size)
- def read1(self, size):
+ def read1(self, size=-1):
self.flush()
return BufferedReader.read1(self, size)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 8a2111c..877d3b5 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -1146,6 +1146,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
self.assertEqual(b"a", bufio.read(1))
self.assertEqual(b"b", bufio.read1(1))
self.assertEqual(rawio._reads, 1)
+ self.assertEqual(b"", bufio.read1(0))
self.assertEqual(b"c", bufio.read1(100))
self.assertEqual(rawio._reads, 1)
self.assertEqual(b"d", bufio.read1(100))
@@ -1154,8 +1155,17 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
self.assertEqual(rawio._reads, 3)
self.assertEqual(b"", bufio.read1(100))
self.assertEqual(rawio._reads, 4)
- # Invalid args
- self.assertRaises(ValueError, bufio.read1, -1)
+
+ def test_read1_arbitrary(self):
+ rawio = self.MockRawIO((b"abc", b"d", b"efg"))
+ bufio = self.tp(rawio)
+ self.assertEqual(b"a", bufio.read(1))
+ self.assertEqual(b"bc", bufio.read1())
+ self.assertEqual(b"d", bufio.read1())
+ self.assertEqual(b"efg", bufio.read1(-1))
+ self.assertEqual(rawio._reads, 3)
+ self.assertEqual(b"", bufio.read1())
+ self.assertEqual(rawio._reads, 4)
def test_readinto(self):
rawio = self.MockRawIO((b"abc", b"d", b"efg"))
@@ -1806,6 +1816,7 @@ class BufferedRWPairTest(unittest.TestCase):
pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
self.assertEqual(pair.read1(3), b"abc")
+ self.assertEqual(pair.read1(), b"def")
def test_readinto(self):
for method in ("readinto", "readinto1"):
@@ -3467,6 +3478,7 @@ class MiscIOTest(unittest.TestCase):
self.assertRaises(ValueError, f.read)
if hasattr(f, "read1"):
self.assertRaises(ValueError, f.read1, 1024)
+ self.assertRaises(ValueError, f.read1)
if hasattr(f, "readall"):
self.assertRaises(ValueError, f.readall)
if hasattr(f, "readinto"):
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 55b693e..80055ce 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -437,10 +437,8 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
def test_read1(self):
buf = self.buftype("1234567890")
- memio = self.ioclass(buf)
-
- self.assertRaises(TypeError, memio.read1)
- self.assertEqual(memio.read(), buf)
+ self.assertEqual(self.ioclass(buf).read1(), buf)
+ self.assertEqual(self.ioclass(buf).read1(-1), buf)
def test_readinto(self):
buf = self.buftype("1234567890")