]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
backport 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:35 +0000 (07:17 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 24 Aug 2005 07:17:35 +0000 (07:17 +0000)
Doc/lib/libmmap.tex
Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index d0fbf884b9a69723e4915f45c9d78606de51e1e4..8e531b150cb56a9e90c44e52453e7e49532b3b2d 100644 (file)
@@ -130,6 +130,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 a6796d5279adbf898e96cdfd1a1040a64cbf9e6d..77f717c336c2223472fa5a2c5103bddce0e000f2 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 808d58bb351f13ef94a513f3122c5bbd864d8622..1fad9f10899ba718771df4c73f9a6fda2a6fd4e0 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -50,6 +50,9 @@ Core and builtins
 Extension Modules
 -----------------
 
+- Bug #728515: mmap.resize() now resizes the file on Unix as it did
+  on Windows.
+
 - Bug #1234979: For the argument of thread.Lock.acquire, the Windows 
   implemented treated all integer values except 1 as false.
 
index e1a2f4238b930b9c2709d5e55478d99c32972524..a3a9857eb2718efee7e0a0ec6a575b7f5d62ec78 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
@@ -907,7 +912,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);