]> granicus.if.org Git - python/commitdiff
Change the specs for readinto() -- it should *not* shorten the buffer to
authorGuido van Rossum <guido@python.org>
Wed, 7 Mar 2007 05:23:25 +0000 (05:23 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 7 Mar 2007 05:23:25 +0000 (05:23 +0000)
the amount of data read.

Lib/io.py
Lib/test/test_io.py

index d6ee186b9780a6788859125d01684752f3a5cebd..db0ba7e87d97476230359a199df04b173795e222 100644 (file)
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -132,7 +132,8 @@ class RawIOBase:
         set not to block and has no data to read.
         """
         b = bytes(n.__index__())
-        self.readinto(b)
+        n = self.readinto(b)
+        del b[n:]
         return b
 
     def readinto(self, b):
@@ -200,8 +201,10 @@ class FileIO(RawIOBase):
 
     def readinto(self, b):
         # XXX We really should have os.readinto()
-        b[:] = os.read(self._fd, len(b))
-        return len(b)
+        tmp = os.read(self._fd, len(b))
+        n = len(tmp)
+        b[:n] = tmp
+        return n
 
     def write(self, b):
         return os.write(self._fd, b)
@@ -303,7 +306,10 @@ class BytesIO(BufferedIOBase):
         return b
 
     def readinto(self, b):
-        b[:] = self.read(len(b))
+        tmp = self.read(len(b))
+        n = len(tmp)
+        b[:n] = tmp
+        return n
 
     def write(self, b):
         n = len(b)
index 1be1b714e547d6b30d898398dfadd5d65a475558..03cdfef322413b249c4b0fbabf8be873369fbdef 100644 (file)
@@ -70,10 +70,13 @@ class IOTest(unittest.TestCase):
     def read_ops(self, f):
         data = f.read(5)
         self.assertEqual(data, b"hello")
-        f.readinto(data)
+        n = f.readinto(data)
+        self.assertEqual(n, 5)
         self.assertEqual(data, b" worl")
-        f.readinto(data)
-        self.assertEqual(data, b"d\n")
+        n = f.readinto(data)
+        self.assertEqual(n, 2)
+        self.assertEqual(len(data), 5)
+        self.assertEqual(data[:2], b"d\n")
         f.seek(0)
         self.assertEqual(f.read(20), b"hello world\n")
         f.seek(-6, 2)