]> granicus.if.org Git - python/commitdiff
Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
authorHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Tue, 17 Feb 2009 13:17:26 +0000 (13:17 +0000)
committerHirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
Tue, 17 Feb 2009 13:17:26 +0000 (13:17 +0000)
The file was resized to wrong size.

Lib/test/test_mmap.py
Misc/NEWS
Modules/mmapmodule.c

index e1e59670c6c9cbee85cc4bb9e8e33c5f6c8c24ff..c06b7579ccd764b2ea73343a364765e21abc7e24 100644 (file)
@@ -417,6 +417,27 @@ class MmapTests(unittest.TestCase):
             m = mmap.mmap(f.fileno(), mapsize - halfsize, offset=halfsize)
             self.assertEqual(m[0:3], 'foo')
             f.close()
+
+            # Try resizing map
+            try:
+                m.resize(512)
+            except SystemError:
+                pass
+            else:
+                # resize() is supported
+                self.assertEqual(len(m), 512)
+                # Check that we can no longer seek beyond the new size.
+                self.assertRaises(ValueError, m.seek, 513, 0)
+                # Check that the content is not changed
+                self.assertEqual(m[0:3], 'foo')
+
+                # Check that the underlying file is truncated too
+                f = open(TESTFN)
+                f.seek(0, 2)
+                self.assertEqual(f.tell(), halfsize + 512)
+                f.close()
+                self.assertEqual(m.size(), halfsize + 512)
+
             m.close()
 
         finally:
index 3b3f9d83e5c202d5e2c69714e692ed934cfda549..74c881c78a102753fd06c0e4dca21474c373640b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -159,6 +159,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
+  The file was resized to wrong size.
+
 - Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
 
 - Issue #2279: distutils.sdist.add_defaults now add files 
index df2c058ec3fa2c11f69bc54bb8741240b59cb081..fad60d109f8c8c304ab614e2dd1e33f1c02b95d3 100644 (file)
@@ -444,7 +444,7 @@ mmap_resize_method(mmap_object *self,
                off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
 #else
                newSizeHigh = 0;
-               newSizeLow = (DWORD)new_size;
+               newSizeLow = (DWORD)(self->offset + new_size);
                off_hi = 0;
                off_lo = (DWORD)self->offset;
 #endif
@@ -490,7 +490,7 @@ mmap_resize_method(mmap_object *self,
        } else {
                void *newmap;
 
-               if (ftruncate(self->fd, new_size) == -1) {
+               if (ftruncate(self->fd, self->offset + new_size) == -1) {
                        PyErr_SetFromErrno(mmap_module_error);
                        return NULL;
                }