[pypy-commit] pypy py3.3: Implement mmap.read(None), which is the same as mmap.read() or mmap.read(-1).

mjacob noreply at buildbot.pypy.org
Fri Feb 27 17:32:23 CET 2015


Author: Manuel Jacob <me at manueljacob.de>
Branch: py3.3
Changeset: r76164:31755e06a2e3
Date: 2015-02-27 10:48 +0100
http://bitbucket.org/pypy/pypy/changeset/31755e06a2e3/

Log:	Implement mmap.read(None), which is the same as mmap.read() or
	mmap.read(-1).

diff --git a/pypy/module/mmap/interp_mmap.py b/pypy/module/mmap/interp_mmap.py
--- a/pypy/module/mmap/interp_mmap.py
+++ b/pypy/module/mmap/interp_mmap.py
@@ -1,7 +1,7 @@
 from pypy.interpreter.error import OperationError, wrap_oserror
 from pypy.interpreter.baseobjspace import W_Root
 from pypy.interpreter.typedef import TypeDef, GetSetProperty
-from pypy.interpreter.gateway import interp2app, unwrap_spec
+from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
 from rpython.rlib import rmmap, rarithmetic
 from rpython.rlib.buffer import Buffer
 from rpython.rlib.rmmap import RValueError, RTypeError, RMMapError
@@ -37,9 +37,13 @@
         self.check_valid()
         return self.space.wrapbytes(self.mmap.readline())
 
-    @unwrap_spec(num=int)
-    def read(self, num=-1):
+    @unwrap_spec(w_num=WrappedDefault(None))
+    def read(self, w_num):
         self.check_valid()
+        if self.space.is_none(w_num):
+            num = -1
+        else:
+            num = self.space.int_w(w_num)
         return self.space.wrapbytes(self.mmap.read(num))
 
     def find(self, w_tofind, w_start=None, w_end=None):
diff --git a/pypy/module/mmap/test/test_mmap.py b/pypy/module/mmap/test/test_mmap.py
--- a/pypy/module/mmap/test/test_mmap.py
+++ b/pypy/module/mmap/test/test_mmap.py
@@ -823,3 +823,12 @@
                 assert str(e) == "cannot mmap an empty file"
             except BaseException as e:
                 assert False, "unexpected exception: " + str(e)
+
+    def test_read_all(self):
+        from mmap import mmap
+        f = open(self.tmpname + "f", "wb+")
+        f.write(b"foobar")
+        f.flush()
+
+        m = mmap(f.fileno(), 6)
+        assert m.read(None) == b"foobar"


More information about the pypy-commit mailing list