summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2005-08-24 07:17:35 (GMT)
committerGeorg Brandl <georg@python.org>2005-08-24 07:17:35 (GMT)
commita6161e476dc862464f8c55a92bc101d8241af54f (patch)
treeb329934de126809265e649c3a3da992e63aeaef8
parent98fa1ca6cacde3af0bce3dfcf753a0048d742854 (diff)
downloadcpython-a6161e476dc862464f8c55a92bc101d8241af54f.zip
cpython-a6161e476dc862464f8c55a92bc101d8241af54f.tar.gz
cpython-a6161e476dc862464f8c55a92bc101d8241af54f.tar.bz2
backport bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
-rw-r--r--Doc/lib/libmmap.tex1
-rw-r--r--Lib/test/test_mmap.py8
-rw-r--r--Misc/NEWS3
-rw-r--r--Modules/mmapmodule.c12
4 files changed, 23 insertions, 1 deletions
diff --git a/Doc/lib/libmmap.tex b/Doc/lib/libmmap.tex
index d0fbf88..8e531b1 100644
--- a/Doc/lib/libmmap.tex
+++ b/Doc/lib/libmmap.tex
@@ -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}
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index a6796d5..77f717c 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -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:
diff --git a/Misc/NEWS b/Misc/NEWS
index 808d58b..1fad9f1 100644
--- 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.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index e1a2f42..a3a9857 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -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);