[PATCH] add offset argument to mmap

Christopher Li python at chrisli.org
Thu May 26 10:29:18 EDT 2005


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 <python at 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);



More information about the Python-list mailing list