[pypy-commit] pypy py3.3: CPython has a different error when read() returns too much data.

amauryfa noreply at buildbot.pypy.org
Sun Oct 26 19:32:08 CET 2014


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3.3
Changeset: r74251:01438d34d9b2
Date: 2014-10-25 19:33 +0200
http://bitbucket.org/pypy/pypy/changeset/01438d34d9b2/

Log:	CPython has a different error when read() returns too much data.

diff --git a/pypy/module/marshal/interp_marshal.py b/pypy/module/marshal/interp_marshal.py
--- a/pypy/module/marshal/interp_marshal.py
+++ b/pypy/module/marshal/interp_marshal.py
@@ -1,4 +1,4 @@
-from pypy.interpreter.error import OperationError
+from pypy.interpreter.error import OperationError, oefmt
 from pypy.interpreter.gateway import WrappedDefault, unwrap_spec
 from rpython.rlib.rarithmetic import intmask
 from rpython.rlib import rstackovf
@@ -102,8 +102,13 @@
         space = self.space
         w_ret = space.call_function(self.func, space.wrap(n))
         ret = space.str_w(w_ret)
-        if len(ret) != n:
+        if len(ret) < n:
             self.raise_eof()
+        if len(ret) > n:
+            raise oefmt(space.w_ValueError,
+                        "read() returned too much data: "
+                        "%d bytes requested, %d returned",
+                        n, len(ret))
         return ret
 
 
diff --git a/pypy/module/marshal/test/test_marshal.py b/pypy/module/marshal/test/test_marshal.py
--- a/pypy/module/marshal/test/test_marshal.py
+++ b/pypy/module/marshal/test/test_marshal.py
@@ -205,6 +205,18 @@
         raises(EOFError, marshal.loads, b'<test>')
         raises((MemoryError, ValueError), marshal.loads, b'(test)')
 
+    def test_bad_reader(self):
+        import marshal, io
+        class BadReader(io.BytesIO):
+            def read(self, n=-1):
+                b = super().read(n)
+                if n is not None and n > 4:
+                    b += b' ' * 10**6
+                return b
+        for value in (1.0, 1j, b'0123456789', '0123456789'):
+            raises(ValueError, marshal.load,
+                   BadReader(marshal.dumps(value)))
+
 
 class AppTestSmallLong(AppTestMarshal):
     spaceconfig = AppTestMarshal.spaceconfig.copy()


More information about the pypy-commit mailing list