diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2014-04-29 08:14:02 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2014-04-29 08:14:02 (GMT) |
commit | b8503896ade8feab60406f616ed0b568cded43ff (patch) | |
tree | 13331276fe108c79334cdd7dd511fbaedb6d9a60 /Lib/test/test_io.py | |
parent | 92c4d4532662f2a584ecf5c14b8d692159cfd08c (diff) | |
download | cpython-b8503896ade8feab60406f616ed0b568cded43ff.zip cpython-b8503896ade8feab60406f616ed0b568cded43ff.tar.gz cpython-b8503896ade8feab60406f616ed0b568cded43ff.tar.bz2 |
Issue #21057: TextIOWrapper now allows the underlying binary stream's read() or read1() method to return an arbitrary bytes-like object (such as a memoryview).
Patch by Nikolaus Rath.
Diffstat (limited to 'Lib/test/test_io.py')
-rw-r--r-- | Lib/test/test_io.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 267537f..8e702db 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2681,6 +2681,34 @@ class TextIOWrapperTest(unittest.TestCase): self.assertFalse(err) self.assertEqual("ok", out.decode().strip()) + def test_read_byteslike(self): + r = MemviewBytesIO(b'Just some random string\n') + t = self.TextIOWrapper(r, 'utf-8') + + # TextIOwrapper will not read the full string, because + # we truncate it to a multiple of the native int size + # so that we can construct a more complex memoryview. + bytes_val = _to_memoryview(r.getvalue()).tobytes() + + self.assertEqual(t.read(200), bytes_val.decode('utf-8')) + +class MemviewBytesIO(io.BytesIO): + '''A BytesIO object whose read method returns memoryviews + rather than bytes''' + + def read1(self, len_): + return _to_memoryview(super().read1(len_)) + + def read(self, len_): + return _to_memoryview(super().read(len_)) + +def _to_memoryview(buf): + '''Convert bytes-object *buf* to a non-trivial memoryview''' + + arr = array.array('i') + idx = len(buf) - len(buf) % arr.itemsize + arr.frombytes(buf[:idx]) + return memoryview(arr) class CTextIOWrapperTest(TextIOWrapperTest): io = io |