From 38387b8b910beeea9c6c2ed6c7b1bd26724bd813 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 24 Aug 2005 07:17:40 +0000 Subject: [PATCH] bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix --- Doc/lib/libmmap.tex | 1 + Lib/test/test_mmap.py | 8 ++++++++ Misc/NEWS | 3 +++ Modules/mmapmodule.c | 12 +++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Doc/lib/libmmap.tex b/Doc/lib/libmmap.tex index 0d7baa1ed12..c7ab348fdd0 100644 --- a/Doc/lib/libmmap.tex +++ b/Doc/lib/libmmap.tex @@ -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} diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py index efb7180f5f6..a0386ef005e 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 01714929253..5bd2dc2da49 100644 --- 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. diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index aaa4925203b..f58e0f184cb 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 @@ -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);