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

guido.van.rossum python-3000-checkins at python.org
Thu Apr 12 07:44:49 CEST 2007


Author: guido.van.rossum
Date: Thu Apr 12 07:44:49 2007
New Revision: 54777

Modified:
   python/branches/p3yk/Lib/io.py
   python/branches/p3yk/Lib/test/test_io.py
Log:
Make sure that writing an array instance returns the number of bytes,
not the number of array elements.


Modified: python/branches/p3yk/Lib/io.py
==============================================================================
--- python/branches/p3yk/Lib/io.py	(original)
+++ python/branches/p3yk/Lib/io.py	Thu Apr 12 07:44:49 2007
@@ -17,6 +17,7 @@
 XXX don't use assert to validate input requirements
 XXX whenever an argument is None, use the default value
 XXX read/write ops should check readable/writable
+XXX buffered readinto should work with arbitrary buffer objects
 """
 
 __author__ = ("Guido van Rossum <guido at python.org>, "
@@ -205,6 +206,7 @@
 
         This is a no-op for read-only and non-blocking streams.
         """
+        # XXX Should this return the number of bytes written???
 
     __closed = False
 
@@ -431,6 +433,7 @@
         Raises BlockingIOError if the underlying raw stream has no
         data at the moment.
         """
+        # XXX This ought to work with anything that supports the buffer API
         data = self.read(len(b))
         n = len(data)
         b[:n] = data
@@ -676,7 +679,9 @@
                 # We can't accept anything else.
                 # XXX Why not just let the exception pass through?
                 raise BlockingIOError(e.errno, e.strerror, 0)
+        before = len(self._write_buf)
         self._write_buf.extend(b)
+        written = len(self._write_buf) - before
         if len(self._write_buf) > self.buffer_size:
             try:
                 self.flush()
@@ -687,7 +692,7 @@
                     overage = len(self._write_buf) - self.max_buffer_size
                     self._write_buf = self._write_buf[:self.max_buffer_size]
                     raise BlockingIOError(e.errno, e.strerror, overage)
-        return len(b)
+        return written
 
     def flush(self):
         written = 0

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	Thu Apr 12 07:44:49 2007
@@ -2,6 +2,7 @@
 
 import sys
 import time
+import array
 import unittest
 from itertools import chain
 from test import test_support
@@ -235,6 +236,16 @@
         self.assertEqual(f.read(), b"xxx")
         f.close()
 
+    def test_array_writes(self):
+        a = array.array('i', range(10))
+        n = len(buffer(a))
+        f = io.open(test_support.TESTFN, "wb", 0)
+        self.assertEqual(f.write(a), n)
+        f.close()
+        f = io.open(test_support.TESTFN, "wb")
+        self.assertEqual(f.write(a), n)
+        f.close()
+
 
 class MemorySeekTestMixin:
 


More information about the Python-3000-checkins mailing list