diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-07-23 19:50:21 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-07-23 19:50:21 (GMT) |
commit | d42c1d09e99d74f6b411bdb1d3c8b9a648dd6a79 (patch) | |
tree | ad9943d40fcf0ab23ae4d5ddeaacb71afab0d433 /Lib | |
parent | f23339a7bb9d9fe977de6d90c64e3c9a27e6b00b (diff) | |
parent | e96ec6810184f5daacb2d47ab8801365c99bb206 (diff) | |
download | cpython-d42c1d09e99d74f6b411bdb1d3c8b9a648dd6a79.zip cpython-d42c1d09e99d74f6b411bdb1d3c8b9a648dd6a79.tar.gz cpython-d42c1d09e99d74f6b411bdb1d3c8b9a648dd6a79.tar.bz2 |
Issue #12591: Allow io.TextIOWrapper to work with raw IO objects (without
a read1() method), and add a *write_through* parameter to
mandate unbuffered writes.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/_pyio.py | 8 | ||||
-rw-r--r-- | Lib/test/test_io.py | 21 |
2 files changed, 27 insertions, 2 deletions
diff --git a/Lib/_pyio.py b/Lib/_pyio.py index 265edab..a9c31d5 100644 --- a/Lib/_pyio.py +++ b/Lib/_pyio.py @@ -1478,7 +1478,7 @@ class TextIOWrapper(TextIOBase): _CHUNK_SIZE = 2048 def __init__(self, buffer, encoding=None, errors=None, newline=None, - line_buffering=False): + line_buffering=False, write_through=False): if newline is not None and not isinstance(newline, str): raise TypeError("illegal newline type: %r" % (type(newline),)) if newline not in (None, "", "\n", "\r", "\r\n"): @@ -1521,6 +1521,7 @@ class TextIOWrapper(TextIOBase): self._decoded_chars_used = 0 # offset into _decoded_chars for read() self._snapshot = None # info for reconstructing decoder state self._seekable = self._telling = self.buffer.seekable() + self._has_read1 = hasattr(self.buffer, 'read1') self._b2cratio = 0.0 if self._seekable and self.writable(): @@ -1687,7 +1688,10 @@ class TextIOWrapper(TextIOBase): # len(dec_buffer) bytes ago with decoder state (b'', dec_flags). # Read a chunk, decode it, and put the result in self._decoded_chars. - input_chunk = self.buffer.read1(self._CHUNK_SIZE) + if self._has_read1: + input_chunk = self.buffer.read1(self._CHUNK_SIZE) + else: + input_chunk = self.buffer.read(self._CHUNK_SIZE) eof = not input_chunk decoded_chars = self._decoder.decode(input_chunk, eof) self._set_decoded_chars(decoded_chars) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 419921f..0d68ca1 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2314,6 +2314,27 @@ class TextIOWrapperTest(unittest.TestCase): with self.assertRaises(AttributeError): txt.buffer = buf + def test_rawio(self): + # Issue #12591: TextIOWrapper must work with raw I/O objects, so + # that subprocess.Popen() can have the required unbuffered + # semantics with universal_newlines=True. + raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n']) + txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n') + # Reads + self.assertEqual(txt.read(4), 'abcd') + self.assertEqual(txt.readline(), 'efghi\n') + self.assertEqual(list(txt), ['jkl\n', 'opq\n']) + + def test_rawio_write_through(self): + # Issue #12591: with write_through=True, writes don't need a flush + raw = self.MockRawIO([b'abc', b'def', b'ghi\njkl\nopq\n']) + txt = self.TextIOWrapper(raw, encoding='ascii', newline='\n', + write_through=True) + txt.write('1') + txt.write('23\n4') + txt.write('5') + self.assertEqual(b''.join(raw._write_stack), b'123\n45') + class CTextIOWrapperTest(TextIOWrapperTest): def test_initialization(self): |