]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Patches #749830, #1144555: allow UNIX mmap size to default to current
authorMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 11:22:44 +0000 (11:22 +0000)
committerMartin v. Löwis <martin@v.loewis.de>
Thu, 3 Mar 2005 11:22:44 +0000 (11:22 +0000)
file size.

Doc/lib/libmmap.tex
Lib/test/output/test_mmap
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index d0fbf884b9a69723e4915f45c9d78606de51e1e4..0d7baa1ed1203aa1334ea0314e56eabfb28160d6 100644 (file)
@@ -62,8 +62,10 @@ the underlying file.
                          prot\optional{, access}}}}
   \strong{(\UNIX{} version)} Maps \var{length} bytes from the file
   specified by the file descriptor \var{fileno}, and returns a mmap
-  object.
-
+  object.  If \var{length} is \code{0}, the maximum length of the map
+  will be the current size of the file when \function{mmap(} is
+  called.
+  
   \var{flags} specifies the nature of the mapping.
   \constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
   changes to the contents of the mmap object will be private to this
index 1706ad58680585e1d760d3ee3616c0c31984127e..02b24bc358fec88167ac31207ff2246415c9af9c 100644 (file)
@@ -31,4 +31,6 @@ test_mmap
   Modifying copy-on-write memory map.
   Ensuring copy-on-write maps cannot be resized.
   Ensuring invalid access parameter raises exception.
+  Ensuring that passing 0 as map length sets map size to current file size.
+  Ensuring that passing 0 as map length sets map size to current file size.
  Test passed
index a6796d5279adbf898e96cdfd1a1040a64cbf9e6d..d2251736088bffd7aa3dbffb6355f14311c2474d 100644 (file)
@@ -311,7 +311,43 @@ def test_both():
     finally:
         os.unlink(TESTFN)
 
+    # test mapping of entire file by passing 0 for map length
+    if hasattr(os, "stat"):
+        print "  Ensuring that passing 0 as map length sets map size to current file size."
+        f = open(TESTFN, "w+")
 
+        try:
+            f.write(2**16 * 'm') # Arbitrary character
+            f.close()
+
+            f = open(TESTFN, "rb+")
+            mf = mmap.mmap(f.fileno(), 0) 
+            verify(len(mf) == 2**16, "Map size should equal file size.")
+            vereq(mf.read(2**16), 2**16 * "m")
+            mf.close()
+            f.close()
+
+        finally:
+            os.unlink(TESTFN)
+    
+    # test mapping of entire file by passing 0 for map length
+    if hasattr(os, "stat"):
+        print "  Ensuring that passing 0 as map length sets map size to current file size."
+        f = open(TESTFN, "w+")
+        try:
+            f.write(2**16 * 'm') # Arbitrary character
+            f.close()
+    
+            f = open(TESTFN, "rb+")
+            mf = mmap.mmap(f.fileno(), 0) 
+            verify(len(mf) == 2**16, "Map size should equal file size.")
+            vereq(mf.read(2**16), 2**16 * "m")
+            mf.close()
+            f.close()
+    
+        finally:
+            os.unlink(TESTFN)
+    
     print ' Test passed'
 
 test_both()
index ea463f917bbb8edcfab261f9a3aca8eee2565bc5..708404d2b912586a3528dd71066ff0e25e50be8f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Patches #749830, #1144555: allow UNIX mmap size to default to current 
+  file size.
+
 - Added functional.partial().  See PEP309.
 
 - Patch #1093585: raise a ValueError for negative history items in readline.
index e1a2f4238b930b9c2709d5e55478d99c32972524..aaa4925203b8a97bd43f4285dbc888f7b867d21e 100644 (file)
@@ -896,11 +896,14 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
        /* on OpenVMS we must ensure that all bytes are written to the file */
        fsync(fd);
 #  endif
-       if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
-           (size_t)map_size > st.st_size) {
-               PyErr_SetString(PyExc_ValueError, 
-                               "mmap length is greater than file size");
-               return NULL;
+       if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
+               if (map_size == 0) {
+                       map_size = (int)st.st_size;
+               } else if ((size_t)map_size > st.st_size) {
+                       PyErr_SetString(PyExc_ValueError, 
+                                       "mmap length is greater than file size");
+                       return NULL;
+               }
        }
 #endif
        m_obj = PyObject_New (mmap_object, &mmap_object_type);