Skip to content

Commit 5e26a66

Browse files
author
loewis
committed
Patches #749830, #1144555: allow UNIX mmap size to default to current
file size. git-svn-id: http://svn.python.org/projects/python/trunk@38546 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 0ca260e commit 5e26a66

5 files changed

Lines changed: 53 additions & 7 deletions

File tree

Doc/lib/libmmap.tex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ \section{\module{mmap} ---
6262
prot\optional{, access}}}}
6363
\strong{(\UNIX{} version)} Maps \var{length} bytes from the file
6464
specified by the file descriptor \var{fileno}, and returns a mmap
65-
object.
66-
65+
object. If \var{length} is \code{0}, the maximum length of the map
66+
will be the current size of the file when \function{mmap(} is
67+
called.
68+
6769
\var{flags} specifies the nature of the mapping.
6870
\constant{MAP_PRIVATE} creates a private copy-on-write mapping, so
6971
changes to the contents of the mmap object will be private to this

Lib/test/output/test_mmap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ test_mmap
3131
Modifying copy-on-write memory map.
3232
Ensuring copy-on-write maps cannot be resized.
3333
Ensuring invalid access parameter raises exception.
34+
Ensuring that passing 0 as map length sets map size to current file size.
35+
Ensuring that passing 0 as map length sets map size to current file size.
3436
Test passed

Lib/test/test_mmap.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,43 @@ def test_both():
311311
finally:
312312
os.unlink(TESTFN)
313313

314+
# test mapping of entire file by passing 0 for map length
315+
if hasattr(os, "stat"):
316+
print " Ensuring that passing 0 as map length sets map size to current file size."
317+
f = open(TESTFN, "w+")
314318

319+
try:
320+
f.write(2**16 * 'm') # Arbitrary character
321+
f.close()
322+
323+
f = open(TESTFN, "rb+")
324+
mf = mmap.mmap(f.fileno(), 0)
325+
verify(len(mf) == 2**16, "Map size should equal file size.")
326+
vereq(mf.read(2**16), 2**16 * "m")
327+
mf.close()
328+
f.close()
329+
330+
finally:
331+
os.unlink(TESTFN)
332+
333+
# test mapping of entire file by passing 0 for map length
334+
if hasattr(os, "stat"):
335+
print " Ensuring that passing 0 as map length sets map size to current file size."
336+
f = open(TESTFN, "w+")
337+
try:
338+
f.write(2**16 * 'm') # Arbitrary character
339+
f.close()
340+
341+
f = open(TESTFN, "rb+")
342+
mf = mmap.mmap(f.fileno(), 0)
343+
verify(len(mf) == 2**16, "Map size should equal file size.")
344+
vereq(mf.read(2**16), 2**16 * "m")
345+
mf.close()
346+
f.close()
347+
348+
finally:
349+
os.unlink(TESTFN)
350+
315351
print ' Test passed'
316352

317353
test_both()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and builtins
3636
Extension Modules
3737
-----------------
3838

39+
- Patches #749830, #1144555: allow UNIX mmap size to default to current
40+
file size.
41+
3942
- Added functional.partial(). See PEP309.
4043

4144
- Patch #1093585: raise a ValueError for negative history items in readline.

Modules/mmapmodule.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,14 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
896896
/* on OpenVMS we must ensure that all bytes are written to the file */
897897
fsync(fd);
898898
# endif
899-
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
900-
(size_t)map_size > st.st_size) {
901-
PyErr_SetString(PyExc_ValueError,
902-
"mmap length is greater than file size");
903-
return NULL;
899+
if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
900+
if (map_size == 0) {
901+
map_size = (int)st.st_size;
902+
} else if ((size_t)map_size > st.st_size) {
903+
PyErr_SetString(PyExc_ValueError,
904+
"mmap length is greater than file size");
905+
return NULL;
906+
}
904907
}
905908
#endif
906909
m_obj = PyObject_New (mmap_object, &mmap_object_type);

0 commit comments

Comments
 (0)