Author: Brian Kearns <[email protected]>
Branch: stdlib-2.7.4
Changeset: r63173:6c338908391b
Date: 2013-04-09 16:52 -0400
http://bitbucket.org/pypy/pypy/changeset/6c338908391b/

Log:    test and fix for mmap of empty file (cpython issue15676)

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
@@ -812,3 +812,17 @@
         assert m.read(10) == "ABCDEABCDE"
         m.close()
         f.close()
+
+    def test_empty_file(self):
+        import mmap
+        f = open(self.tmpname, 'w+b')
+        f.close()
+        with open(self.tmpname, 'rb') as f:
+            try:
+                m = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ)
+                m.close()
+                assert False, "should not have been able to mmap empty file"
+            except ValueError as e:
+                assert e.message == "cannot mmap an empty file"
+            except BaseException as e:
+                assert False, "unexpected exception: " + str(e)
diff --git a/rpython/rlib/rmmap.py b/rpython/rlib/rmmap.py
--- a/rpython/rlib/rmmap.py
+++ b/rpython/rlib/rmmap.py
@@ -657,6 +657,8 @@
             size = st[stat.ST_SIZE]
             if stat.S_ISREG(mode):
                 if map_size == 0:
+                    if size == 0:
+                        raise RValueError("cannot mmap an empty file")
                     if offset > size:
                         raise RValueError(
                             "mmap offset is greater than file size")
@@ -778,6 +780,8 @@
                     size = (high << 32) + low
                     size = rffi.cast(lltype.Signed, size)
                 if map_size == 0:
+                    if size == 0:
+                        raise RValueError("cannot mmap an empty file")
                     if offset > size:
                         raise RValueError(
                             "mmap offset is greater than file size")
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to