By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,142 Members | 910 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,142 IT Pros & Developers. It's quick & easy.

[PATCH] add offset argument to mmap

P: n/a
I am surprised to find out the mmap module in python always
mmap from offset 0. So I just hack up some patch to allow it
accept offset arguments.

So here it is.

Chris
Add optional offset argument to mmap module.

Test on linux with Python 2.4.
The windows code is totally untested.

Signed-Off-by: Christopher Li <py****@chrisli.org>

Index: Modules/mmapmodule.c
================================================== =================
--- Modules.orig/mmapmodule.c 2004-05-19 10:39:08.000000000 -0400
+++ Modules/mmapmodule.c 2005-05-26 10:01:51.000000000 -0400
@@ -853,14 +853,17 @@
mmap_object *m_obj;
PyObject *map_size_obj = NULL;
int map_size;
+ unsigned long offset = 0;
int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
access_mode access = ACCESS_DEFAULT;
char *keywords[] = {"fileno", "length",
"flags", "prot",
- "access", NULL};
+ "access", "offset",
+ NULL};

- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii", keywords,
- &fd, &map_size_obj, &flags, &prot, &access))
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiil", keywords,
+ &fd, &map_size_obj, &flags, &prot,
+ &access, &offset))
return NULL;
map_size = _GetMapSize(map_size_obj);
if (map_size < 0)
@@ -910,7 +913,7 @@
m_obj->fd = fd;
m_obj->data = mmap(NULL, map_size,
prot, flags,
- fd, 0);
+ fd, offset);
if (m_obj->data == (char *)-1) {
m_obj->data = NULL;
Py_DECREF(m_obj);
@@ -929,6 +932,7 @@
mmap_object *m_obj;
PyObject *map_size_obj = NULL;
int map_size;
+ unsigned long offset = 0;
char *tagname = "";
DWORD dwErr = 0;
int fileno;
@@ -939,9 +943,9 @@
"tagname",
"access", NULL };

- if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zi", keywords,
+ if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zil", keywords,
&fileno, &map_size_obj,
- &tagname, &access)) {
+ &tagname, &access, &offset)) {
return NULL;
}

@@ -1040,8 +1044,8 @@
if (m_obj->map_handle != NULL) {
m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,
dwDesiredAccess,
- 0,
- 0,
+ offset>>32,
+ (unsigned int)offset,
0);
if (m_obj->data != NULL) {
return ((PyObject *) m_obj);
Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.