diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py index 89c8196676d..027ac9452d0 100644 --- a/Lib/test/test_memoryio.py +++ b/Lib/test/test_memoryio.py @@ -392,6 +392,11 @@ def test_readinto(self): 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 b51e6e67b11..7794521353a 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -19,6 +19,11 @@ 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 #1682942: configparser supports alternative option/value delimiters. + Build ----- diff --git a/Modules/_io/bytesio.c b/Modules/_io/bytesio.c index 29fb1ead018..c8fb3eb95f4 100644 --- a/Modules/_io/bytesio.c +++ b/Modules/_io/bytesio.c @@ -391,15 +391,20 @@ static PyObject * bytesio_readinto(bytesio *self, PyObject *buffer) { void *raw_buffer; - Py_ssize_t len; + Py_ssize_t len, n; CHECK_CLOSED(self); if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1) return NULL; - 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(raw_buffer, self->buf + self->pos, len); assert(self->pos + len < PY_SSIZE_T_MAX);