[pypy-commit] pypy py3.5: Fix: a memoryview on a read-only mmap must be read-only

rlamy pypy.commits at gmail.com
Tue Feb 21 14:17:32 EST 2017


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: py3.5
Changeset: r90278:56963c746067
Date: 2017-02-21 19:16 +0000
http://bitbucket.org/pypy/pypy/changeset/56963c746067/

Log:	Fix: a memoryview on a read-only mmap must be read-only

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
@@ -20,8 +20,11 @@
 
     def buffer_w(self, space, flags):
         self.check_valid()
-        return MMapBuffer(self.space, self.mmap,
-                          bool(flags & space.BUF_WRITABLE))
+        readonly = (self.mmap.access == ACCESS_READ)
+        write_required = bool(flags & space.BUF_WRITABLE)
+        if write_required and readonly:
+            raise oefmt(space.w_BufferError, "Object is not writable.")
+        return MMapBuffer(self.space, self.mmap, readonly)
 
     def writebuf_w(self, space):
         self.check_writeable()
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
@@ -536,8 +536,9 @@
         f.close()
 
     def test_memoryview(self):
-        from mmap import mmap
-        f = open(self.tmpname + "y", "bw+")
+        from mmap import mmap, PROT_READ
+        filename = self.tmpname + "y"
+        f = open(filename, "bw+")
         f.write(b"foobar")
         f.flush()
         m = mmap(f.fileno(), 6)
@@ -549,10 +550,18 @@
         del b  # For CPython: "exported pointers exist"
         m.close()
         f.close()
+        with open(filename, "rb") as f:
+            m = mmap(f.fileno(), 6, prot=PROT_READ)
+            b = memoryview(m)
+            assert b.readonly is True
+            assert b[:] == b"foobar"
+            del b
+            m.close()
 
     def test_offset(self):
         from mmap import mmap, ALLOCATIONGRANULARITY
-        f = open(self.tmpname + "y", "wb+")
+        filename = self.tmpname + "y"
+        f = open(filename, "wb+")
         f.write(b"foobar" * ALLOCATIONGRANULARITY)
         f.flush()
         size = ALLOCATIONGRANULARITY


More information about the pypy-commit mailing list