[pypy-commit] pypy stdlib-2.7.13: support resizing anonymous mmaps (2.7.12 and pypy previously get a EBADF

arigo pypy.commits at gmail.com
Sun Dec 18 12:56:00 EST 2016


Author: Armin Rigo <arigo at tunes.org>
Branch: stdlib-2.7.13
Changeset: r89157:8e961f254b34
Date: 2016-12-18 18:55 +0100
http://bitbucket.org/pypy/pypy/changeset/8e961f254b34/

Log:	support resizing anonymous mmaps (2.7.12 and pypy previously get a
	EBADF in this case)

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
@@ -872,3 +872,25 @@
                 assert str(e) == "cannot mmap an empty file"
             except BaseException as e:
                 assert False, "unexpected exception: " + str(e)
+
+    def test_resize_past_pos(self):
+        import os, mmap, sys
+        if os.name == "nt":
+            skip("cannot resize anonymous mmaps on Windows")
+        if sys.version_info < (2, 7, 13):
+            skip("cannot resize anonymous mmaps before 2.7.13")
+        m = mmap.mmap(-1, 8192)
+        m.read(5000)
+        try:
+            m.resize(4096)
+        except SystemError:
+            skip("resizing not supported")
+        assert m.tell() == 5000
+        assert m.read(14) == ''
+        assert m.read(-1) == ''
+        raises(ValueError, m.read_byte)
+        assert m.readline() == ''
+        raises(ValueError, m.write_byte, 'b')
+        raises(ValueError, m.write, 'abc')
+        assert m.tell() == 5000
+        m.close()
diff --git a/rpython/rlib/rmmap.py b/rpython/rlib/rmmap.py
--- a/rpython/rlib/rmmap.py
+++ b/rpython/rlib/rmmap.py
@@ -507,6 +507,8 @@
         return rffi.ptradd(self.data, offset)
 
     def getslice(self, start, length):
+        if length < 0:
+            return ''
         return rffi.charpsize2str(self.getptr(start), length)
 
     def setslice(self, start, newdata):
@@ -549,8 +551,9 @@
             if not has_mremap:
                 raise RValueError("mmap: resizing not available--no mremap()")
 
-            # resize the underlying file first
-            os.ftruncate(self.fd, self.offset + newsize)
+            # resize the underlying file first, if there is one
+            if self.fd >= 0:
+                os.ftruncate(self.fd, self.offset + newsize)
 
             # now resize the mmap
             newdata = c_mremap(self.getptr(0), self.size, newsize,


More information about the pypy-commit mailing list