Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: py3.5
Changeset: r88785:58cf1903ac70
Date: 2016-12-01 06:34 +0000
http://bitbucket.org/pypy/pypy/changeset/58cf1903ac70/

Log:    Make sure that BufferedReader passes down a valid memoryview to
        reader.raw.readinto()

diff --git a/pypy/module/_io/interp_bufferedio.py 
b/pypy/module/_io/interp_bufferedio.py
--- a/pypy/module/_io/interp_bufferedio.py
+++ b/pypy/module/_io/interp_bufferedio.py
@@ -158,6 +158,9 @@
     def getlength(self):
         return self.length
 
+    def getitem(self, index):
+        return self.buf[index]
+
     def setitem(self, index, char):
         self.buf[self.start + index] = char
 
diff --git a/pypy/module/_io/test/test_bufferedio.py 
b/pypy/module/_io/test/test_bufferedio.py
--- a/pypy/module/_io/test/test_bufferedio.py
+++ b/pypy/module/_io/test/test_bufferedio.py
@@ -65,6 +65,23 @@
         bufio = _io.BufferedReader(MockIO())
         assert bufio.read(9000) == b"abcdefg"
 
+    def test_valid_buffer(self):
+        import _io
+
+        class MockIO(_io._IOBase):
+            def readable(self):
+                return True
+
+            def readinto(self, buf):
+                # Check that `buf` is a valid memoryview object
+                assert buf.itemsize == 1
+                assert buf.strides == (1,)
+                assert buf.shape == (len(buf),)
+                return len(bytes(buf))
+
+        bufio = _io.BufferedReader(MockIO())
+        assert len(bufio.read(5)) == 5  # Note: PyPy zeros the buffer, CPython 
does not
+
     def test_buffering(self):
         import _io
         data = b"abcdefghi"
@@ -695,7 +712,7 @@
                 expected[j] = 2
                 expected[i] = 1
                 assert raw.getvalue() == expected
-        
+
     def test_interleaved_read_write(self):
         import _io as io
         # Test for issue #12213
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to