summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_memoryio.py5
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/_io/bytesio.c11
3 files changed, 16 insertions, 3 deletions
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
index 2b4c76e..8d95a64 100644
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -438,6 +438,11 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase):
self.assertEqual(a.tostring(), b"1234567890d")
memio.close()
self.assertRaises(ValueError, memio.readinto, b)
+ memio = self.ioclass(b"123")
+ b = bytearray()
+ memio.seek(42)
+ memio.readinto(b)
+ self.assertEqual(b, b"")
def test_relative_seek(self):
buf = self.buftype("1234567890")
diff --git a/Misc/NEWS b/Misc/NEWS
index 3a984f4..b04fc6a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -16,6 +16,9 @@ Library
- Issue #10198: fix duplicate header written to wave files when writeframes()
is called without data.
+- Issue #10467: Fix BytesIO.readinto() after seeking into a position after the
+ end of the file.
+
- Issue #5111: IPv6 Host in the Header is wrapped inside [ ]. Patch by Chandru.
Build
diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c
index 15dbc33..51fad21 100644
--- a/Modules/_io/bytesio.c
+++ b/Modules/_io/bytesio.c
@@ -391,7 +391,7 @@ static PyObject *
bytesio_readinto(bytesio *self, PyObject *args)
{
Py_buffer buf;
- Py_ssize_t len;
+ Py_ssize_t len, n;
CHECK_CLOSED(self);
@@ -399,8 +399,13 @@ bytesio_readinto(bytesio *self, PyObject *args)
return NULL;
len = buf.len;
- if (self->pos + len > self->string_size)
- len = self->string_size - self->pos;
+ /* adjust invalid sizes */
+ n = self->string_size - self->pos;
+ if (len > n) {
+ len = n;
+ if (len < 0)
+ len = 0;
+ }
memcpy(buf.buf, self->buf + self->pos, len);
assert(self->pos + len < PY_SSIZE_T_MAX);