[pypy-commit] pypy py3.5: readinto1(), and fix expected error messages for readinto() test

arigo pypy.commits at gmail.com
Tue Jan 10 06:48:15 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r89473:26d691d3df32
Date: 2017-01-10 12:42 +0100
http://bitbucket.org/pypy/pypy/changeset/26d691d3df32/

Log:	readinto1(), and fix expected error messages for readinto() test

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
@@ -107,18 +107,25 @@
         self._unsupportedoperation(space, "detach")
 
     def readinto_w(self, space, w_buffer):
+        return self._readinto(space, w_buffer, "read")
+
+    def readinto1_w(self, space, w_buffer):
+        return self._readinto(space, w_buffer, "read1")
+
+    def _readinto(self, space, w_buffer, methodname):
         rwbuffer = space.getarg_w('w*', w_buffer)
         length = rwbuffer.getlength()
-        w_data = space.call_method(self, "read", space.wrap(length))
+        w_data = space.call_method(self, methodname, space.wrap(length))
 
         if not space.isinstance_w(w_data, space.w_str):
-            raise oefmt(space.w_TypeError, "read() should return bytes")
+            raise oefmt(space.w_TypeError, "%s() should return bytes",
+                        methodname)
         data = space.bytes_w(w_data)
         if len(data) > length:
             raise oefmt(space.w_ValueError,
-                        "read() returned too much data: "
+                        "%s() returned too much data: "
                         "%d bytes requested, %d returned",
-                        length, len(data))
+                        methodname, length, len(data))
         rwbuffer.setslice(0, data)
         return space.wrap(len(data))
 
@@ -144,6 +151,7 @@
     write = interp2app(W_BufferedIOBase.write_w),
     detach = interp2app(W_BufferedIOBase.detach_w),
     readinto = interp2app(W_BufferedIOBase.readinto_w),
+    readinto1 = interp2app(W_BufferedIOBase.readinto1_w),
 )
 
 class RawBuffer(Buffer):
diff --git a/pypy/module/_io/interp_bytesio.py b/pypy/module/_io/interp_bytesio.py
--- a/pypy/module/_io/interp_bytesio.py
+++ b/pypy/module/_io/interp_bytesio.py
@@ -83,9 +83,6 @@
         limit = convert_size(space, w_limit)
         return space.newbytes(self.readline(limit))
 
-    def read1_w(self, space, w_size):
-        return self.read_w(space, w_size)
-
     def readinto_w(self, space, w_buffer):
         self._check_closed(space)
         rwbuffer = space.getarg_w('w*', w_buffer)
@@ -203,9 +200,10 @@
     __init__  = interp2app(W_BytesIO.descr_init),
 
     read = interp2app(W_BytesIO.read_w),
-    read1 = interp2app(W_BytesIO.read1_w),
+    read1 = interp2app(W_BytesIO.read_w),
     readline = interp2app(W_BytesIO.readline_w),
     readinto = interp2app(W_BytesIO.readinto_w),
+    readinto1 = interp2app(W_BytesIO.readinto_w),
     write = interp2app(W_BytesIO.write_w),
     truncate = interp2app(W_BytesIO.truncate_w),
     getbuffer = interp2app(W_BytesIO.getbuffer_w),
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
@@ -152,19 +152,28 @@
 
     def test_readinto(self):
         import _io
-        a = bytearray(b'x' * 10)
-        raw = _io.FileIO(self.tmpfile)
-        f = _io.BufferedReader(raw)
-        assert f.readinto(a) == 5
-        f.seek(0)
-        m = memoryview(bytearray(b"hello"))
-        assert f.readinto(m) == 5
-        exc = raises(TypeError, f.readinto, u"hello")
-        assert str(exc.value) == "must be read-write buffer, not str"
-        exc = raises(TypeError, f.readinto, memoryview(b"hello"))
-        assert str(exc.value) == "must be read-write buffer, not memoryview"
-        f.close()
-        assert a == b'a\nb\ncxxxxx'
+        for methodname in ["readinto", "readinto1"]:
+            a = bytearray(b'x' * 10)
+            raw = _io.FileIO(self.tmpfile)
+            f = _io.BufferedReader(raw)
+            readinto = getattr(f, methodname)
+            assert readinto(a) == 5
+            f.seek(0)
+            m = memoryview(bytearray(b"hello"))
+            assert readinto(m) == 5
+            #
+            exc = raises(TypeError, readinto, u"hello")
+            msg = str(exc.value)
+            print(msg)
+            assert " read-write b" in msg and msg.endswith(", not str")
+            #
+            exc = raises(TypeError, readinto, memoryview(b"hello"))
+            msg = str(exc.value)
+            print(msg)
+            assert " read-write b" in msg and msg.endswith(", not memoryview")
+            #
+            f.close()
+            assert a == b'a\nb\ncxxxxx'
 
     def test_readinto_buffer_overflow(self):
         import _io
diff --git a/pypy/module/_io/test/test_bytesio.py b/pypy/module/_io/test/test_bytesio.py
--- a/pypy/module/_io/test/test_bytesio.py
+++ b/pypy/module/_io/test/test_bytesio.py
@@ -98,23 +98,31 @@
 
     def test_readinto(self):
         import _io
-
-        b = _io.BytesIO(b"hello")
-        a1 = bytearray(b't')
-        a2 = bytearray(b'testing')
-        assert b.readinto(a1) == 1
-        assert b.readinto(a2) == 4
-        b.seek(0)
-        m = memoryview(bytearray(b"world"))
-        assert b.readinto(m) == 5
-        exc = raises(TypeError, b.readinto, u"hello")
-        assert str(exc.value) == "must be read-write buffer, not str"
-        exc = raises(TypeError, b.readinto, memoryview(b"hello"))
-        assert str(exc.value) == "must be read-write buffer, not memoryview"
-        b.close()
-        assert a1 == b"h"
-        assert a2 == b"elloing"
-        raises(ValueError, b.readinto, bytearray(b"hello"))
+        for methodname in ["readinto", "readinto1"]:
+            b = _io.BytesIO(b"hello")
+            readinto = getattr(b, methodname)
+            a1 = bytearray(b't')
+            a2 = bytearray(b'testing')
+            assert readinto(a1) == 1
+            assert readinto(a2) == 4
+            b.seek(0)
+            m = memoryview(bytearray(b"world"))
+            assert readinto(m) == 5
+            #
+            exc = raises(TypeError, readinto, u"hello")
+            msg = str(exc.value)
+            print(msg)
+            assert " read-write b" in msg and msg.endswith(", not str")
+            #
+            exc = raises(TypeError, readinto, memoryview(b"hello"))
+            msg = str(exc.value)
+            print(msg)
+            assert " read-write b" in msg and msg.endswith(", not memoryview")
+            #
+            b.close()
+            assert a1 == b"h"
+            assert a2 == b"elloing"
+            raises(ValueError, readinto, bytearray(b"hello"))
 
     def test_getbuffer(self):
         import _io


More information about the pypy-commit mailing list