[Python-3000-checkins] r54194 - in python/branches/p3yk/Lib: io.py test/test_io.py

guido.van.rossum python-3000-checkins at python.org
Wed Mar 7 06:23:26 CET 2007


Author: guido.van.rossum
Date: Wed Mar  7 06:23:25 2007
New Revision: 54194

Modified:
   python/branches/p3yk/Lib/io.py
   python/branches/p3yk/Lib/test/test_io.py
Log:
Change the specs for readinto() -- it should *not* shorten the buffer to
the amount of data read.


Modified: python/branches/p3yk/Lib/io.py
==============================================================================
--- python/branches/p3yk/Lib/io.py	(original)
+++ python/branches/p3yk/Lib/io.py	Wed Mar  7 06:23:25 2007
@@ -132,7 +132,8 @@
         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 @@
 
     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 @@
         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)

Modified: python/branches/p3yk/Lib/test/test_io.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_io.py	(original)
+++ python/branches/p3yk/Lib/test/test_io.py	Wed Mar  7 06:23:25 2007
@@ -70,10 +70,13 @@
     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)


More information about the Python-3000-checkins mailing list