]> granicus.if.org Git - python/commitdiff
merge 3.4 (#22849)
authorBenjamin Peterson <benjamin@python.org>
Wed, 12 Nov 2014 15:23:35 +0000 (10:23 -0500)
committerBenjamin Peterson <benjamin@python.org>
Wed, 12 Nov 2014 15:23:35 +0000 (10:23 -0500)
1  2 
Lib/test/test_io.py
Misc/NEWS
Modules/_io/textio.c

index a3567fad209b0c0f427a33cc55a357b3963c040d,940e921e54d2ae712ebab5a71fa51262d4238169..746f59b0e728047f39d9aa9e29b4e2cfc65fc48a
@@@ -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 1c72d964796d09994abb56ee58cf4b6f82cd9fd5,bb58965623557f26207f95b86592bbc5d965fc12..ef98134c16d9dfb8397703c7a4219f136df3ba2d
+++ 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.
  
Simple merge