Commit 5ebfd36a authored by Tim Peters's avatar Tim Peters

CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.

This gives mmap() on Windows the ability to create read-only, write-
through and copy-on-write mmaps.  A new keyword argument is introduced
because the mmap() signatures diverged between Windows and Unix, so
while they (now) both support this functionality, there wasn't a way to
spell it in a common way without introducing a new spelling gimmick.
The old spellings are still accepted, so there isn't a backward-
compatibility issue here.
parent afeb2a4d
This diff is collapsed.
......@@ -17,4 +17,17 @@ test_mmap
Try to seek beyond end of mmap...
Try to seek to negative position...
Attempting resize()
Creating 10 byte test data file.
Opening mmap with access=ACCESS_READ
Ensuring that readonly mmap can't be slice assigned.
Ensuring that readonly mmap can't be item assigned.
Ensuring that readonly mmap can't be write() to.
Ensuring that readonly mmap can't be write_byte() to.
Ensuring that readonly mmap can't be resized.
Opening mmap with access=ACCESS_WRITE
Modifying write-through memory map.
Opening mmap with access=ACCESS_COPY
Modifying copy-on-write memory map.
Ensuring copy-on-write maps cannot be resized.
Ensuring invalid access parameter raises exception.
Test passed
from test_support import verify, TESTFN
from test_support import verify, vereq, TESTFN
import mmap
import os, re
......@@ -25,15 +25,15 @@ def test_both():
print type(m) # SF bug 128713: segfaulted on Linux
print ' Position of foo:', m.find('foo') / float(PAGESIZE), 'pages'
verify(m.find('foo') == PAGESIZE)
vereq(m.find('foo'), PAGESIZE)
print ' Length of file:', len(m) / float(PAGESIZE), 'pages'
verify(len(m) == 2*PAGESIZE)
vereq(len(m), 2*PAGESIZE)
print ' Contents of byte 0:', repr(m[0])
verify(m[0] == '\0')
vereq(m[0], '\0')
print ' Contents of first 3 bytes:', repr(m[0:3])
verify(m[0:3] == '\0\0\0')
vereq(m[0:3], '\0\0\0')
# Modify the file's content
print "\n Modifying file's content..."
......@@ -42,11 +42,11 @@ def test_both():
# Check that the modification worked
print ' Contents of byte 0:', repr(m[0])
verify(m[0] == '3')
vereq(m[0], '3')
print ' Contents of first 3 bytes:', repr(m[0:3])
verify(m[0:3] == '3\0\0')
vereq(m[0:3], '3\0\0')
print ' Contents of second page:', repr(m[PAGESIZE-1 : PAGESIZE + 7])
verify(m[PAGESIZE-1 : PAGESIZE + 7] == '\0foobar\0')
vereq(m[PAGESIZE-1 : PAGESIZE + 7], '\0foobar\0')
m.flush()
......@@ -61,19 +61,19 @@ def test_both():
print ' Regex match on mmap (page start, length of match):',
print start / float(PAGESIZE), length
verify(start == PAGESIZE)
verify(end == PAGESIZE + 6)
vereq(start, PAGESIZE)
vereq(end, PAGESIZE + 6)
# test seeking around (try to overflow the seek implementation)
m.seek(0,0)
print ' Seek to zeroth byte'
verify(m.tell() == 0)
vereq(m.tell(), 0)
m.seek(42,1)
print ' Seek to 42nd byte'
verify(m.tell() == 42)
vereq(m.tell(), 42)
m.seek(0,2)
print ' Seek to last byte'
verify(m.tell() == len(m))
vereq(m.tell(), len(m))
print ' Try to seek to negative position...'
try:
......@@ -132,6 +132,118 @@ def test_both():
except OSError:
pass
# Test for "access" keyword parameter
try:
mapsize = 10
print " Creating", mapsize, "byte test data file."
open(TESTFN, "wb").write("a"*mapsize)
print " Opening mmap with access=ACCESS_READ"
f = open(TESTFN, "rb")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_READ)
verify(m[:] == 'a'*mapsize, "Readonly memory map data incorrect.")
print " Ensuring that readonly mmap can't be slice assigned."
try:
m[:] = 'b'*mapsize
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be item assigned."
try:
m[0] = 'b'
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be write() to."
try:
m.seek(0,0)
m.write('abc')
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be write_byte() to."
try:
m.seek(0,0)
m.write_byte('d')
except TypeError:
pass
else:
verify(0, "Able to write to readonly memory map")
print " Ensuring that readonly mmap can't be resized."
try:
m.resize(2*mapsize)
except SystemError: # resize is not universally supported
pass
except TypeError:
pass
else:
verify(0, "Able to resize readonly memory map")
del m, f
verify(open(TESTFN, "rb").read() == 'a'*mapsize,
"Readonly memory map data file was modified")
print " Opening mmap with access=ACCESS_WRITE"
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_WRITE)
print " Modifying write-through memory map."
m[:] = 'c'*mapsize
verify(m[:] == 'c'*mapsize,
"Write-through memory map memory not updated properly.")
m.flush()
del m, f
verify(open(TESTFN).read() == 'c'*mapsize,
"Write-through memory map data file not updated properly.")
print " Opening mmap with access=ACCESS_COPY"
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=mmap.ACCESS_COPY)
print " Modifying copy-on-write memory map."
m[:] = 'd'*mapsize
verify(m[:] == 'd' * mapsize,
"Copy-on-write memory map data not written correctly.")
m.flush()
verify(open(TESTFN, "rb").read() == 'c'*mapsize,
"Copy-on-write test data file should not be modified.")
try:
print " Ensuring copy-on-write maps cannot be resized."
m.resize(2*mapsize)
except TypeError:
pass
else:
verify(0, "Copy-on-write mmap resize did not raise exception.")
del m, f
try:
print " Ensuring invalid access parameter raises exception."
f = open(TESTFN, "r+b")
m = mmap.mmap(f.fileno(), mapsize, access=4)
except ValueError:
pass
else:
verify(0, "Invalid access code should have raised exception.")
if os.name == "posix":
print " Trying incompatible flags, prot and access parameters."
f=open(TESTFN, "r+b")
try:
m = mmap.mmap(f.fileno(), mapsize, flags=mmap.MAP_PRIVATE,
prot=mmap.PROT_READ, access=mmap.ACCESS_WRITE)
except ValueError:
pass
else:
verify(0, "Incompatible parameters should raise ValueError.")
finally:
try:
os.unlink(TESTFN)
except OSError:
pass
print ' Test passed'
test_both()
......@@ -36,6 +36,13 @@ Core and builtins
Extension modules
- mmap has a new keyword argument, "access", allowing a uniform way for
both Windows and Unix users to create read-only, write-through and
copy-on-write memory mappings. This was previously possible only on
Unix. A new keyword argument was required to support this in a
uniform way because the mmap() signuatures had diverged across
platforms. Thanks to Jay T Miller for repairing this!
- By default, the gc.garbage list now contains only those instances in
unreachable cycles that have __del__ methods; in 2.1 it contained all
instances in unreachable cycles. "Instances" here has been generalized
......
......@@ -62,6 +62,14 @@ my_getpagesize(void)
static PyObject *mmap_module_error;
typedef enum
{
ACCESS_DEFAULT,
ACCESS_READ,
ACCESS_WRITE,
ACCESS_COPY
} access_mode;
typedef struct {
PyObject_HEAD
char * data;
......@@ -77,8 +85,11 @@ typedef struct {
#ifdef UNIX
int fd;
#endif
access_mode access;
} mmap_object;
static void
mmap_object_dealloc(mmap_object *m_obj)
{
......@@ -260,6 +271,26 @@ mmap_find_method(mmap_object *self,
}
}
static int
is_writeable(mmap_object *self)
{
if (self->access != ACCESS_READ)
return 1;
PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
return 0;
}
static int
is_resizeable(mmap_object *self)
{
if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
return 1;
PyErr_Format(PyExc_TypeError,
"mmap can't resize a readonly or copy-on-write memory map.");
return 0;
}
static PyObject *
mmap_write_method(mmap_object *self,
PyObject *args)
......@@ -271,6 +302,9 @@ mmap_write_method(mmap_object *self,
if (!PyArg_ParseTuple (args, "s#:write", &data, &length))
return(NULL);
if (!is_writeable(self))
return NULL;
if ((self->pos + length) > self->size) {
PyErr_SetString (PyExc_ValueError, "data out of range");
return NULL;
......@@ -291,6 +325,8 @@ mmap_write_byte_method(mmap_object *self,
if (!PyArg_ParseTuple (args, "c:write_byte", &value))
return(NULL);
if (!is_writeable(self))
return NULL;
*(self->data+self->pos) = value;
self->pos += 1;
Py_INCREF (Py_None);
......@@ -342,7 +378,8 @@ mmap_resize_method(mmap_object *self,
{
unsigned long new_size;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple (args, "l:resize", &new_size)) {
if (!PyArg_ParseTuple (args, "l:resize", &new_size) ||
!is_resizeable(self)) {
return NULL;
#ifdef MS_WIN32
} else {
......@@ -386,12 +423,12 @@ mmap_resize_method(mmap_object *self,
#ifdef UNIX
#ifndef HAVE_MREMAP
} else {
} else {
PyErr_SetString(PyExc_SystemError,
"mmap: resizing not available--no mremap()");
return NULL;
#else
} else {
} else {
void *newmap;
#ifdef MREMAP_MAYMOVE
......@@ -410,7 +447,7 @@ mmap_resize_method(mmap_object *self,
return Py_None;
#endif /* HAVE_MREMAP */
#endif /* UNIX */
}
}
}
static PyObject *
......@@ -491,7 +528,7 @@ mmap_seek_method(mmap_object *self, PyObject *args)
return (Py_None);
}
onoutofrange:
onoutofrange:
PyErr_SetString (PyExc_ValueError, "seek out of range");
return NULL;
}
......@@ -501,7 +538,8 @@ mmap_move_method(mmap_object *self, PyObject *args)
{
unsigned long dest, src, count;
CHECK_VALID(NULL);
if (!PyArg_ParseTuple (args, "iii:move", &dest, &src, &count)) {
if (!PyArg_ParseTuple (args, "iii:move", &dest, &src, &count) ||
!is_writeable(self)) {
return NULL;
} else {
/* bounds check the values */
......@@ -561,6 +599,8 @@ mmap_buffer_getwritebuf(mmap_object *self, int index, const void **ptr)
"Accessing non-existent mmap segment");
return -1;
}
if (!is_writeable(self))
return -1;
*ptr = self->data;
return self->size;
}
......@@ -678,6 +718,8 @@ mmap_ass_slice(mmap_object *self, int ilow, int ihigh, PyObject *v)
"mmap slice assignment is wrong size");
return -1;
}
if (!is_writeable(self))
return -1;
buf = PyString_AsString(v);
memcpy(self->data + ilow, buf, ihigh-ilow);
return 0;
......@@ -703,6 +745,8 @@ mmap_ass_item(mmap_object *self, int i, PyObject *v)
"mmap assignment must be single-character string");
return -1;
}
if (!is_writeable(self))
return -1;
buf = PyString_AsString(v);
self->data[i] = buf[0];
return 0;
......@@ -796,12 +840,12 @@ _GetMapSize(PyObject *o)
return -1;
}
onnegoverflow:
onnegoverflow:
PyErr_SetString(PyExc_OverflowError,
"memory mapped size must be positive");
return -1;
onposoverflow:
onposoverflow:
PyErr_SetString(PyExc_OverflowError,
"memory mapped size is too large (limited by C int)");
return -1;
......@@ -815,17 +859,43 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
PyObject *map_size_obj = NULL;
int map_size;
int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
char *keywords[] = {"file", "size", "flags", "prot", NULL};
access_mode access = ACCESS_DEFAULT;
char *keywords[] = {"fileno", "length",
"flags", "prot",
"access", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwdict,
"iO|ii", keywords,
&fd, &map_size_obj, &flags, &prot)
)
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii", keywords,
&fd, &map_size_obj, &flags, &prot, &access))
return NULL;
map_size = _GetMapSize(map_size_obj);
if (map_size < 0)
return NULL;
if ((access != ACCESS_DEFAULT) &&
((flags != MAP_SHARED) || ( prot != (PROT_WRITE | PROT_READ))))
return PyErr_Format(PyExc_ValueError,
"mmap can't specify both access and flags, prot.");
switch(access) {
case ACCESS_READ:
flags = MAP_SHARED;
prot = PROT_READ;
break;
case ACCESS_WRITE:
flags = MAP_SHARED;
prot = PROT_READ | PROT_WRITE;
break;
case ACCESS_COPY:
flags = MAP_PRIVATE;
prot = PROT_READ | PROT_WRITE;
break;
case ACCESS_DEFAULT:
/* use the specified or default values of flags and prot */
break;
default:
return PyErr_Format(PyExc_ValueError,
"mmap invalid access parameter.");
}
m_obj = PyObject_New (mmap_object, &mmap_object_type);
if (m_obj == NULL) {return NULL;}
m_obj->size = (size_t) map_size;
......@@ -834,36 +904,56 @@ new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
m_obj->data = mmap(NULL, map_size,
prot, flags,
fd, 0);
if (m_obj->data == (char *)-1)
{
if (m_obj->data == (char *)-1) {
Py_DECREF(m_obj);
PyErr_SetFromErrno(mmap_module_error);
return NULL;
}
m_obj->access = access;
return (PyObject *)m_obj;
}
#endif /* UNIX */
#ifdef MS_WIN32
static PyObject *
new_mmap_object(PyObject *self, PyObject *args)
new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
{
mmap_object *m_obj;
PyObject *map_size_obj = NULL;
int map_size;
char *tagname = "";
DWORD dwErr = 0;
int fileno;
HANDLE fh = 0;
if (!PyArg_ParseTuple(args,
"iO|z",
&fileno,
&map_size_obj,
&tagname)
)
access_mode access = ACCESS_DEFAULT;
DWORD flProtect, dwDesiredAccess;
char *keywords[] = { "fileno", "length",
"tagname",
"access", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zi", keywords,
&fileno, &map_size_obj,
&tagname, &access)) {
return NULL;
}
switch(access) {
case ACCESS_READ:
flProtect = PAGE_READONLY;
dwDesiredAccess = FILE_MAP_READ;
break;
case ACCESS_DEFAULT: case ACCESS_WRITE:
flProtect = PAGE_READWRITE;
dwDesiredAccess = FILE_MAP_WRITE;
break;
case ACCESS_COPY:
flProtect = PAGE_WRITECOPY;
dwDesiredAccess = FILE_MAP_COPY;
break;
default:
return PyErr_Format(PyExc_ValueError,
"mmap invalid access parameter.");
}
map_size = _GetMapSize(map_size_obj);
if (map_size < 0)
......@@ -932,15 +1022,16 @@ new_mmap_object(PyObject *self, PyObject *args)
else
m_obj->tagname = NULL;
m_obj->access = access;
m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
NULL,
PAGE_READWRITE,
flProtect,
0,
m_obj->size,
m_obj->tagname);
if (m_obj->map_handle != NULL) {
m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,
FILE_MAP_WRITE,
dwDesiredAccess,
0,
0,
0);
......@@ -966,7 +1057,7 @@ static struct PyMethodDef mmap_functions[] = {
};
DL_EXPORT(void)
initmmap(void)
initmmap(void)
{
PyObject *dict, *module;
......@@ -1011,5 +1102,11 @@ initmmap(void)
PyDict_SetItemString (dict, "PAGESIZE",
PyInt_FromLong( (long)my_getpagesize() ) );
}
PyDict_SetItemString (dict, "ACCESS_READ",
PyInt_FromLong(ACCESS_READ));
PyDict_SetItemString (dict, "ACCESS_WRITE",
PyInt_FromLong(ACCESS_WRITE));
PyDict_SetItemString (dict, "ACCESS_COPY",
PyInt_FromLong(ACCESS_COPY));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment