]> granicus.if.org Git - python/commitdiff
correct logic when pos is after the string #10467
authorBenjamin Peterson <benjamin@python.org>
Sat, 20 Nov 2010 17:24:04 +0000 (17:24 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 20 Nov 2010 17:24:04 +0000 (17:24 +0000)
Lib/test/test_memoryio.py
Misc/NEWS
Modules/_io/bytesio.c

index dcf6d51575cfad1daba667be6f63c852cc53b027..13d7e1710011f8cd3ee4f1fbaa515a07976c3b8b 100644 (file)
@@ -452,6 +452,11 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin,
         self.assertEqual(a.tobytes(), 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")
index 76bf48126e971184450638135e4cf933b5ea2d36..709450458d959245e191bbf0ba121d320883913a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,9 @@ Core and Builtins
 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 #5412: configparser supports mapping protocol access.
index c5654040b45023c5e68b7c7eeed794f8af0a52fe..b40513f7a770c9fdd57a9fe83d2670789a29c068 100644 (file)
@@ -430,15 +430,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);