Kaydet (Commit) fa73555c authored tarafından Benjamin Peterson's avatar Benjamin Peterson

correct logic when pos is after the string #10467

üst 6bcfadec
...@@ -452,6 +452,11 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, ...@@ -452,6 +452,11 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin,
self.assertEqual(a.tobytes(), b"1234567890d") self.assertEqual(a.tobytes(), b"1234567890d")
memio.close() memio.close()
self.assertRaises(ValueError, memio.readinto, b) 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): def test_relative_seek(self):
buf = self.buftype("1234567890") buf = self.buftype("1234567890")
......
...@@ -24,6 +24,9 @@ Core and Builtins ...@@ -24,6 +24,9 @@ Core and Builtins
Library Library
------- -------
- Issue #10467: Fix BytesIO.readinto() after seeking into a position after the
end of the file.
- Issue #1682942: configparser supports alternative option/value delimiters. - Issue #1682942: configparser supports alternative option/value delimiters.
- Issue #5412: configparser supports mapping protocol access. - Issue #5412: configparser supports mapping protocol access.
......
...@@ -430,15 +430,20 @@ static PyObject * ...@@ -430,15 +430,20 @@ static PyObject *
bytesio_readinto(bytesio *self, PyObject *buffer) bytesio_readinto(bytesio *self, PyObject *buffer)
{ {
void *raw_buffer; void *raw_buffer;
Py_ssize_t len; Py_ssize_t len, n;
CHECK_CLOSED(self); CHECK_CLOSED(self);
if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1) if (PyObject_AsWriteBuffer(buffer, &raw_buffer, &len) == -1)
return NULL; return NULL;
if (self->pos + len > self->string_size) /* adjust invalid sizes */
len = self->string_size - self->pos; n = self->string_size - self->pos;
if (len > n) {
len = n;
if (len < 0)
len = 0;
}
memcpy(raw_buffer, self->buf + self->pos, len); memcpy(raw_buffer, self->buf + self->pos, len);
assert(self->pos + len < PY_SSIZE_T_MAX); assert(self->pos + len < PY_SSIZE_T_MAX);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment