]> granicus.if.org Git - python/commitdiff
Fixes for Windows (but also tested on Linux). Test suite now completes, and this...
authorMark Hammond <mhammond@skippinet.com.au>
Sun, 30 Jul 2000 02:22:43 +0000 (02:22 +0000)
committerMark Hammond <mhammond@skippinet.com.au>
Sun, 30 Jul 2000 02:22:43 +0000 (02:22 +0000)
Checkin that replaces the INT_PTR types with HANDLEs still TBD (but as that is a "spelling" patch, rather than a functional one, I will commit it seperately.

Modules/mmapmodule.c

index 772e2afa3ac636012794bcd9febb744ba75e50af..051a71572cef8c7dcbd076a9b269d8ab6c8d3413 100644 (file)
@@ -61,9 +61,14 @@ static void
 mmap_object_dealloc(mmap_object *m_obj)
 {
 #ifdef MS_WIN32
-       UnmapViewOfFile (m_obj->data);
-       CloseHandle (m_obj->map_handle);
-       CloseHandle ((HANDLE)m_obj->file_handle);
+       if (m_obj->data != NULL)
+               UnmapViewOfFile (m_obj->data);
+       if (m_obj->map_handle != INVALID_HANDLE_VALUE)
+               CloseHandle (m_obj->map_handle);
+       if ((HANDLE)m_obj->file_handle != INVALID_HANDLE_VALUE)
+               CloseHandle ((HANDLE)m_obj->file_handle);
+       if (m_obj->tagname)
+               PyMem_Free(m_obj->tagname);
 #endif /* MS_WIN32 */
 
 #ifdef UNIX
@@ -826,9 +831,31 @@ new_mmap_object(PyObject *self, PyObject *args)
        }
 
        m_obj = PyObject_New (mmap_object, &mmap_object_type);
-    
+       if (m_obj==NULL)
+               return NULL;
+       /* Set every field to an invalid marker, so we can safely
+          destruct the object in the face of failure */
+       m_obj->data = NULL;
+       m_obj->file_handle = (INT_PTR)INVALID_HANDLE_VALUE;
+       m_obj->map_handle = INVALID_HANDLE_VALUE;
+       m_obj->tagname = NULL;
+
        if (fh) {
-               m_obj->file_handle = fh;
+               /* It is necessary to duplicate the handle, so the
+                  Python code can close it on us */
+               if (!DuplicateHandle(
+                           GetCurrentProcess(), /* source process handle */
+                           (HANDLE)fh, /* handle to be duplicated */
+                           GetCurrentProcess(), /* target proc handle */
+                           (LPHANDLE)&m_obj->file_handle, /* result */
+                           0, /* access - ignored due to options value */
+                           FALSE, /* inherited by child processes? */
+                           DUPLICATE_SAME_ACCESS)) { /* options */
+                       dwErr = GetLastError();
+                       Py_DECREF(m_obj);
+                       PyErr_SetFromWindowsErr(dwErr);
+                       return NULL;
+               }
                if (!map_size) {
                        m_obj->size = GetFileSize ((HANDLE)fh, NULL);
                } else {
@@ -836,13 +863,25 @@ new_mmap_object(PyObject *self, PyObject *args)
                }
        }
        else {
-               m_obj->file_handle = (INT_PTR) -1;
                m_obj->size = map_size;
        }
 
        /* set the initial position */
        m_obj->pos = (size_t) 0;
 
+       /* set the tag name */
+       if (tagname != NULL) {
+               m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
+               if (m_obj->tagname == NULL) {
+                       PyErr_NoMemory();
+                       Py_DECREF(m_obj);
+                       return NULL;
+               }
+               strcpy(m_obj->tagname, tagname);
+       }
+       else
+               m_obj->tagname = NULL;
+
        m_obj->map_handle = CreateFileMapping ((HANDLE) m_obj->file_handle,
                                               NULL,
                                               PAGE_READWRITE,
@@ -863,6 +902,7 @@ new_mmap_object(PyObject *self, PyObject *args)
        } else {
                dwErr = GetLastError();
        }
+       Py_DECREF(m_obj);
        PyErr_SetFromWindowsErr(dwErr);
        return (NULL);
 }