From: Benjamin Peterson Date: Wed, 12 Nov 2014 15:23:35 +0000 (-0500) Subject: merge 3.4 (#22849) X-Git-Tag: v3.5.0a1~487 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=994c7f76a3cffd123183b2437580d3d4f09ab197;p=python merge 3.4 (#22849) --- 994c7f76a3cffd123183b2437580d3d4f09ab197 diff --cc Lib/test/test_io.py index a3567fad20,940e921e54..746f59b0e7 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@@ -2845,35 -2784,22 +2845,51 @@@ class TextIOWrapperTest(unittest.TestCa 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')) + + def test_issue22849(self): + class F(object): + def readable(self): return True + def writable(self): return True + def seekable(self): return True + + for i in range(10): + try: + self.TextIOWrapper(F(), encoding='utf-8') + except Exception: + pass + + F.tell = lambda x: 0 + t = self.TextIOWrapper(F(), encoding='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 shutdown_error = "RuntimeError: could not find io module state" diff --cc Misc/NEWS index 1c72d96479,bb58965623..ef98134c16 --- a/Misc/NEWS +++ b/Misc/NEWS @@@ -183,11 -36,8 +183,13 @@@ Core and Builtin Library ------- +- Issue #19494: Added urllib.request.HTTPBasicPriorAuthHandler. Patch by + Matej Cepl. + +- Issue #22578: Added attributes to the re.error class. + + - Issue #22849: Fix possible double free in the io.TextIOWrapper constructor. + - Issue #12728: Different Unicode characters having the same uppercase but different lowercase are now matched in case-insensitive regular expressions.