# HG changeset patch # User Martin Panter # Date 1464933560 0 # Fri Jun 03 05:59:20 2016 +0000 # Branch 2.7 # Node ID 8b5f9b47a58d3070d5aa1acaeed4d6cdc2f1c51d # Parent 0df93ab07a8f053996950873b82ff5cc90217da7 Issue #20699: Document that “io” methods accept sequences of bytes This matches the usage by BufferedReader, BufferedWriter, etc. Return values are still required to be bytes() objects however. Also document and test that the write() methods should only access their argument before they return. The _pyio implementation is not general enough because it assumes len(), iteration and slice assignment work. But it is probably safer to leave that alone. diff -r 0df93ab07a8f -r 8b5f9b47a58d Doc/library/io.rst --- a/Doc/library/io.rst Fri May 27 11:20:21 2016 +0000 +++ b/Doc/library/io.rst Fri Jun 03 05:59:20 2016 +0000 @@ -225,8 +225,11 @@ support are called. The basic type used for binary data read from or written to a file is - :class:`bytes` (also known as :class:`str`). :class:`bytearray`\s are - accepted too, and in some cases (such as :class:`readinto`) required. + :class:`bytes` (also known as :class:`str`). + Other sequences of bytes which are usable with :class:`memoryview` are + accepted as method arguments too. In some cases, such as + :meth:`~RawIOBase.readinto`, a writable object such as :class:`bytearray` + is required. Text I/O classes work with :class:`unicode` data. Note that calling any method (even inquiries) on a closed stream is @@ -383,18 +386,23 @@ .. method:: readinto(b) - Read up to len(b) bytes into bytearray *b* and return the number - of bytes read. If the object is in non-blocking mode and no + Read bytes into *b*, and return the number of bytes read. + The object *b* should be a pre-allocated, writable sequence of bytes + usable with :class:`memoryview`, such as :class:`bytearray`. + If the object is in non-blocking mode and no bytes are available, ``None`` is returned. .. method:: write(b) - Write the given bytes or bytearray object, *b*, to the underlying raw - stream and return the number of bytes written. This can be less than - ``len(b)``, depending on specifics of the underlying raw stream, and + Write the given sequence of bytes, *b*, to the + underlying raw stream, and return the number of + bytes written. This can be less than the length of *b* in + bytes, depending on specifics of the underlying raw stream, and especially if it is in non-blocking mode. ``None`` is returned if the raw stream is set not to block and no single byte could be readily - written to it. + written to it. The caller may release or mutate *b* after + this method returns, so the implementation should only access *b* + during the method call. .. class:: BufferedIOBase @@ -465,8 +473,9 @@ .. method:: readinto(b) - Read up to len(b) bytes into bytearray *b* and return the number of bytes - read. + Read bytes into *b* and return the number of bytes read. + The object *b* should be a pre-allocated, writable sequence of bytes + usable with :class:`memoryview`, such as :class:`bytearray`. Like :meth:`read`, multiple reads may be issued to the underlying raw stream, unless the latter is 'interactive'. @@ -476,8 +485,9 @@ .. method:: write(b) - Write the given bytes or bytearray object, *b* and return the number - of bytes written (never less than ``len(b)``, since if the write fails + Write the given sequence of bytes, *b*, and return the number + of bytes written (always equal to the length + of *b* in bytes, since if the write fails an :exc:`IOError` will be raised). Depending on the actual implementation, these bytes may be readily written to the underlying stream, or held in a buffer for performance and latency reasons. @@ -486,6 +496,9 @@ data needed to be written to the raw stream but it couldn't accept all the data without blocking. + The caller may release or mutate *b* after this method returns, + so the implementation should only access *b* during the method call. + Raw File I/O ------------ @@ -535,7 +548,8 @@ A stream implementation using an in-memory bytes buffer. It inherits :class:`BufferedIOBase`. - The argument *initial_bytes* is an optional initial :class:`bytes`. + The optional argument *initial_bytes* is a :class:`str` object that + contains initial data. :class:`BytesIO` provides or overrides these methods in addition to those from :class:`BufferedIOBase` and :class:`IOBase`: @@ -611,7 +625,7 @@ .. method:: write(b) - Write the bytes or bytearray object, *b* and return the number of bytes + Write the sequence of bytes, *b*, and return the number of bytes written. When in non-blocking mode, a :exc:`BlockingIOError` is raised if the buffer needs to be written out but the raw stream blocks. diff -r 0df93ab07a8f -r 8b5f9b47a58d Lib/_pyio.py --- a/Lib/_pyio.py Fri May 27 11:20:21 2016 +0000 +++ b/Lib/_pyio.py Fri Jun 03 05:59:20 2016 +0000 @@ -278,7 +278,7 @@ The basic type used for binary data read from or written to a file is bytes. bytearrays are accepted too, and in some cases (such as - readinto) needed. Text I/O classes work with str data. + readinto), needed. Text I/O classes work with unicode data. Note that calling any method (even inquiries) on a closed stream is undefined. Implementations may raise IOError in this case. @@ -588,7 +588,8 @@ def write(self, b): """Write the given buffer to the IO stream. - Returns the number of bytes written, which may be less than len(b). + Returns the number of bytes written, which may be less than the + length of b in bytes. """ self._unsupported("write") @@ -649,7 +650,8 @@ 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 + # XXX This ought to work with any sequence of bytes that supports + # memoryview() data = self.read(len(b)) n = len(data) try: @@ -664,8 +666,8 @@ def write(self, b): """Write the given buffer to the IO stream. - Return the number of bytes written, which is never less than - len(b). + Return the number of bytes written, which is always the length of b + in bytes. Raises BlockingIOError if the buffer is full and the underlying raw stream cannot accept more data at the moment. diff -r 0df93ab07a8f -r 8b5f9b47a58d Lib/test/test_io.py --- a/Lib/test/test_io.py Fri May 27 11:20:21 2016 +0000 +++ b/Lib/test/test_io.py Fri Jun 03 05:59:20 2016 +0000 @@ -54,6 +54,8 @@ __metaclass__ = type bytes = support.py3k_bytes +byteslike = bytearray # Compatibility with the Python 3 test code + def _default_chunk_size(): """Get the default TextIOWrapper chunk size""" with io.open(__file__, "r", encoding="latin1") as f: @@ -273,7 +275,9 @@ self.assertEqual(f.tell(), 6) self.assertEqual(f.seek(-1, 1), 5) self.assertEqual(f.tell(), 5) - self.assertEqual(f.write(bytearray(b" world\n\n\n")), 9) + buffer = bytearray(b" world\n\n\n") + self.assertEqual(f.write(buffer), 9) + buffer[:] = b"*" * 9 # Overwrite our copy of the data self.assertEqual(f.seek(0), 0) self.assertEqual(f.write(b"h"), 1) self.assertEqual(f.seek(-1, 2), 13) @@ -286,20 +290,21 @@ def read_ops(self, f, buffered=False): data = f.read(5) self.assertEqual(data, b"hello") - data = bytearray(data) + data = byteslike(data) self.assertEqual(f.readinto(data), 5) - self.assertEqual(data, b" worl") + self.assertEqual(bytes(data), b" worl") + data = bytearray(5) self.assertEqual(f.readinto(data), 2) self.assertEqual(len(data), 5) self.assertEqual(data[:2], b"d\n") self.assertEqual(f.seek(0), 0) self.assertEqual(f.read(20), b"hello world\n") self.assertEqual(f.read(1), b"") - self.assertEqual(f.readinto(bytearray(b"x")), 0) + self.assertEqual(f.readinto(byteslike(b"x")), 0) self.assertEqual(f.seek(-6, 2), 6) self.assertEqual(f.read(5), b"world") self.assertEqual(f.read(0), b"") - self.assertEqual(f.readinto(bytearray()), 0) + self.assertEqual(f.readinto(byteslike()), 0) self.assertEqual(f.seek(-6, 1), 5) self.assertEqual(f.read(5), b" worl") self.assertEqual(f.tell(), 10) @@ -649,6 +654,16 @@ support.gc_collect() self.assertEqual(recorded, []) + def test_buffered_readinto_mixin(self): + # Test the implementation provided by BufferedIOBase + class Stream(self.BufferedIOBase): + def read(self, size): + return b"12345" + stream = Stream() + buffer = byteslike(5) + self.assertEqual(stream.readinto(buffer), 5) + self.assertEqual(bytes(buffer), b"12345") + class CIOTest(IOTest): @@ -1111,6 +1126,11 @@ bufio = self.tp(writer, 8) bufio.write(b"abc") self.assertFalse(writer._write_stack) + buffer = bytearray(b"def") + bufio.write(buffer) + buffer[:] = b"***" # Overwrite our copy of the data + bufio.flush() + self.assertEqual(b"".join(writer._write_stack), b"abcdef") def test_write_overflow(self): writer = self.MockRawIO() @@ -1440,9 +1460,9 @@ def test_readinto(self): pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO()) - data = bytearray(5) + data = byteslike(5) self.assertEqual(pair.readinto(data), 5) - self.assertEqual(data, b"abcde") + self.assertEqual(bytes(data), b"abcde") def test_write(self): w = self.MockRawIO() @@ -1450,7 +1470,9 @@ pair.write(b"abc") pair.flush() - pair.write(b"def") + buffer = bytearray(b"def") + pair.write(buffer) + buffer[:] = b"***" # Overwrite our copy of the data pair.flush() self.assertEqual(w._write_stack, [b"abc", b"def"]) diff -r 0df93ab07a8f -r 8b5f9b47a58d Lib/test/test_memoryio.py --- a/Lib/test/test_memoryio.py Fri May 27 11:20:21 2016 +0000 +++ b/Lib/test/test_memoryio.py Fri Jun 03 05:59:20 2016 +0000 @@ -396,6 +396,7 @@ class PyBytesIOTest(MemoryTestMixin, MemorySeekTestMixin, unittest.TestCase): + # Test _pyio.BytesIO; class also inherited for testing C implementation UnsupportedOperation = pyio.UnsupportedOperation diff -r 0df93ab07a8f -r 8b5f9b47a58d Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Fri May 27 11:20:21 2016 +0000 +++ b/Modules/_io/bufferedio.c Fri Jun 03 05:59:20 2016 +0000 @@ -125,8 +125,8 @@ PyDoc_STRVAR(bufferediobase_write_doc, "Write the given buffer to the IO stream.\n" "\n" - "Returns the number of bytes written, which is never less than\n" - "len(b).\n" + "Returns the number of bytes written, which is always the length of b\n" + "in bytes.\n" "\n" "Raises BlockingIOError if the buffer is full and the\n" "underlying raw stream cannot accept more data at the moment.\n"); diff -r 0df93ab07a8f -r 8b5f9b47a58d Modules/_io/bytesio.c --- a/Modules/_io/bytesio.c Fri May 27 11:20:21 2016 +0000 +++ b/Modules/_io/bytesio.c Fri Jun 03 05:59:20 2016 +0000 @@ -392,7 +392,7 @@ } PyDoc_STRVAR(readinto_doc, -"readinto(bytearray) -> int. Read up to len(b) bytes into b.\n" +"readinto(b) -> int. Read bytes into b.\n" "\n" "Returns number of bytes read (0 for EOF), or None if the object\n" "is set not to block and has no data to read."); diff -r 0df93ab07a8f -r 8b5f9b47a58d Modules/_io/fileio.c --- a/Modules/_io/fileio.c Fri May 27 11:20:21 2016 +0000 +++ b/Modules/_io/fileio.c Fri Jun 03 05:59:20 2016 +0000 @@ -969,7 +969,7 @@ "or None if no data is available. On end-of-file, returns ''."); PyDoc_STRVAR(write_doc, -"write(b: bytes) -> int. Write bytes b to file, return number written.\n" +"write(b) -> int. Write sequence of bytes b, return number written.\n" "\n" "Only makes one system call, so not all of the data may be written.\n" "The number of bytes actually written is returned. In non-blocking mode,\n" diff -r 0df93ab07a8f -r 8b5f9b47a58d Modules/_io/iobase.c --- a/Modules/_io/iobase.c Fri May 27 11:20:21 2016 +0000 +++ b/Modules/_io/iobase.c Fri Jun 03 05:59:20 2016 +0000 @@ -38,8 +38,9 @@ "may raise a IOError when operations they do not support are called.\n" "\n" "The basic type used for binary data read from or written to a file is\n" - "bytes. bytearrays are accepted too, and in some cases (such as\n" - "readinto) needed. Text I/O classes work with str data.\n" + "the bytes type. Other sequences of bytes usable with memoryview are\n" + "accepted as method arguments too. In some cases (such as readinto), a\n" + "writable object is required. Text I/O classes work with unicode data.\n" "\n" "Note that calling any method (except additional calls to close(),\n" "which are ignored) on a closed stream should raise a ValueError.\n"