]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
authorGeorg Brandl <georg@python.org>
Wed, 24 Aug 2005 07:17:40 +0000 (07:17 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 24 Aug 2005 07:17:40 +0000 (07:17 +0000)
Doc/lib/libmmap.tex
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index 0d7baa1ed1203aa1334ea0314e56eabfb28160d6..c7ab348fdd0ff55330910940b37e705871390712 100644 (file)
@@ -132,6 +132,7 @@ Memory-mapped file objects support the following methods:
 \end{methoddesc}
 
 \begin{methoddesc}{resize}{\var{newsize}}
+  Resizes the map and the underlying file, if any.
   If the mmap was created with \constant{ACCESS_READ} or
   \constant{ACCESS_COPY}, resizing the map will throw a \exception{TypeError} exception.
 \end{methoddesc}
index efb7180f5f66f84bae375f6b3a91a7faac12e816..a0386ef005e15a879a7c52ba668178fafdf86c89 100644 (file)
@@ -120,6 +120,14 @@ def test_both():
             else:
                 verify(0, 'Could seek beyond the new size')
 
+            # Check that the underlying file is truncated too
+            # (bug #728515)
+            f = open(TESTFN)
+            f.seek(0, 2)
+            verify(f.tell() == 512, 'Underlying file not truncated')
+            f.close()
+            verify(m.size() == 512, 'New size not reflected in file') 
+
         m.close()
 
     finally:
index 01714929253c8bdb11a4a4385d279c463c735e46..5bd2dc2da49d4033a3266c3b2b32ac1d3360423a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -128,6 +128,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Bug #728515: mmap.resize() now resizes the file on Unix as it did
+  on Windows.
+
 - Patch #1180695: Add nanosecond stat resolution, and st_gen, 
   st_birthtime for FreeBSD.
 
index aaa4925203b8a97bd43f4285dbc888f7b867d21e..f58e0f184cb44112baad07cdf04bf597fb7bba86 100644 (file)
@@ -421,6 +421,11 @@ mmap_resize_method(mmap_object *self,
                return NULL;
 #else
        } else {
+               if (ftruncate(self->fd, new_size) == -1) {
+                       PyErr_SetFromErrno(mmap_module_error);
+                       return NULL;
+               }
+               
                void *newmap;
 
 #ifdef MREMAP_MAYMOVE
@@ -910,7 +915,12 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
        if (m_obj == NULL) {return NULL;}
        m_obj->size = (size_t) map_size;
        m_obj->pos = (size_t) 0;
-       m_obj->fd = fd;
+       m_obj->fd = dup(fd);
+       if (m_obj->fd == -1) {
+               Py_DECREF(m_obj);
+               PyErr_SetFromErrno(mmap_module_error);
+               return NULL;
+       }
        m_obj->data = mmap(NULL, map_size, 
                           prot, flags,
                           fd, 0);