summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2007-04-12 05:44:49 (GMT)
committerGuido van Rossum <guido@python.org>2007-04-12 05:44:49 (GMT)
commitd410395ea7febe1d9daf3cd6a57dad36c31d3fb7 (patch)
treecc003f6694928e7c32d8b809ba518a01167d6d47 /Lib
parentaa43ed95cd14366e95a724cb94eb9369195fb27b (diff)
downloadcpython-d410395ea7febe1d9daf3cd6a57dad36c31d3fb7.zip
cpython-d410395ea7febe1d9daf3cd6a57dad36c31d3fb7.tar.gz
cpython-d410395ea7febe1d9daf3cd6a57dad36c31d3fb7.tar.bz2
Make sure that writing an array instance returns the number of bytes,
not the number of array elements.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/io.py7
-rw-r--r--Lib/test/test_io.py11
2 files changed, 17 insertions, 1 deletions
diff --git a/Lib/io.py b/Lib/io.py
index ccdb3fb..4465e9e 100644
--- a/Lib/io.py
+++ b/Lib/io.py
@@ -17,6 +17,7 @@ XXX need to support 1 meaning line-buffered
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@python.org>, "
@@ -205,6 +206,7 @@ class IOBase:
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 @@ class BufferedIOBase(IOBase):
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 @@ class BufferedWriter(_BufferedIOMixin):
# 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 @@ class BufferedWriter(_BufferedIOMixin):
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
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
index 688f6dc..737dfab 100644
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -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 @@ class IOTest(unittest.TestCase):
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: