if (fileno == 0) PyErr_Warn(PyExc_DeprecationWarning, "don't use 0 for anonymous memory"); */ if (fileno != -1 && fileno != 0) { fh = (HANDLE)_get_osfhandle(fileno); if (fh==(HANDLE)-1) { PyErr_SetFromErrno(mmap_module_error); return NULL; } /* Win9x appears to need us seeked to zero */ lseek(fileno, 0, SEEK_SET); } 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 = INVALID_HANDLE_VALUE; m_obj->map_handle = INVALID_HANDLE_VALUE; m_obj->tagname = NULL; if (fh) { /* It is necessary to duplicate the handle, so the Python code can close it on us */ if (!DuplicateHandle( GetCurrentProcess(), /* source process 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) { DWORD low,high; low = GetFileSize(fh, &high); /* low might just happen to have the value INVALID_FILE_SIZE; so we need to check the last error also. */ if (low == INVALID_FILE_SIZE && (dwErr = GetLastError()) != NO_ERROR) { Py_DECREF(m_obj); return PyErr_SetFromWindowsErr(dwErr); } #if SIZEOF_SIZE_T > 4 m_obj->size = (((size_t)high)<<32) + low;#else if (high) /* File is too large to map completely */ m_obj->size = (size_t)-1; else m_obj->size = low;#endif } else { m_obj->size = map_size; } } else { m_obj->size = map_size; } /* set the initial position */ m_obj->pos = (size_t) 0; /* set the tag name */ if (tagname != NULL && *tagname != '\0') { 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->access = (access_mode)access; /* DWORD is a 4-byte int. If we're on a box where size_t consumes * more than 4 bytes, we need to break it apart. Else (size_t * consumes 4 bytes), C doesn't define what happens if we shift * right by 32, so we need different code. */#if SIZEOF_SIZE_T > 4 size_hi = (DWORD)(m_obj->size >> 32); size_lo = (DWORD)(m_obj->size & 0xFFFFFFFF);#else size_hi = 0; size_lo = (DWORD)m_obj->size;#endif m_obj->map_handle = CreateFileMapping(m_obj->file_handle, NULL, flProtect, size_hi, size_lo, m_obj->tagname); if (m_obj->map_handle != NULL) { m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, dwDesiredAccess, 0, 0, 0); if (m_obj->data != NULL) return (PyObject *)m_obj; else dwErr = GetLastError(); } else dwErr = GetLastError(); Py_DECREF(m_obj); PyErr_SetFromWindowsErr(dwErr); return NULL;}#endif /* MS_WINDOWS */ /* List of functions exported by this module */static struct PyMethodDef mmap_functions[] = { {"mmap", (PyCFunction) new_mmap_object, METH_VARARGS|METH_KEYWORDS}, {NULL, NULL} /* Sentinel */}; static voidsetint(PyObject *d, const char *name, long value){ PyObject *o = PyInt_FromLong(value); if (o && PyDict_SetItemString(d, name, o) == 0) { Py_DECREF(o); }} PyMODINIT_FUNC initmmap(void){ PyObject *dict, *module; /* Patch the object type */ mmap_object_type.ob_type = &PyType_Type; module = Py_InitModule("mmap", mmap_functions); if (module == NULL) return; dict = PyModule_GetDict(module); if (!dict) return; mmap_module_error = PyExc_EnvironmentError; PyDict_SetItemString(dict, "error", mmap_module_error);#ifdef PROT_EXEC setint(dict, "PROT_EXEC", PROT_EXEC);#endif#ifdef PROT_READ setint(dict, "PROT_READ", PROT_READ);#endif#ifdef PROT_WRITE setint(dict, "PROT_WRITE", PROT_WRITE);#endif #ifdef MAP_SHARED setint(dict, "MAP_SHARED", MAP_SHARED);#endif#ifdef MAP_PRIVATE setint(dict, "MAP_PRIVATE", MAP_PRIVATE);#endif#ifdef MAP_DENYWRITE setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);#endif#ifdef MAP_EXECUTABLE setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);#endif#ifdef MAP_ANONYMOUS setint(dict, "MAP_ANON", MAP_ANONYMOUS); setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);#endif setint(dict, "PAGESIZE", (long)my_getpagesize()); setint(dict, "ACCESS_READ", ACCESS_READ); setint(dict, "ACCESS_WRITE", ACCESS_WRITE); setint(dict, "ACCESS_COPY", ACCESS_COPY);}Lines 1001–1186 of 1186. Previous lines
if (fileno == 0) PyErr_Warn(PyExc_DeprecationWarning, "don't use 0 for anonymous memory"); */ if (fileno != -1 && fileno != 0) { fh = (HANDLE)_get_osfhandle(fileno); if (fh==(HANDLE)-1) { PyErr_SetFromErrno(mmap_module_error); return NULL; } /* Win9x appears to need us seeked to zero */ lseek(fileno, 0, SEEK_SET); } 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 = INVALID_HANDLE_VALUE; m_obj->map_handle = INVALID_HANDLE_VALUE; m_obj->tagname = NULL; if (fh) { /* It is necessary to duplicate the handle, so the Python code can close it on us */ if (!DuplicateHandle( GetCurrentProcess(), /* source process 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) { DWORD low,high; low = GetFileSize(fh, &high); /* low might just happen to have the value INVALID_FILE_SIZE; so we need to check the last error also. */ if (low == INVALID_FILE_SIZE && (dwErr = GetLastError()) != NO_ERROR) { Py_DECREF(m_obj); return PyErr_SetFromWindowsErr(dwErr); } #if SIZEOF_SIZE_T > 4 m_obj->size = (((size_t)high)<<32) + low; #else if (high) /* File is too large to map completely */ m_obj->size = (size_t)-1; else m_obj->size = low; #endif } else { m_obj->size = map_size; } } else { m_obj->size = map_size; } /* set the initial position */ m_obj->pos = (size_t) 0; /* set the tag name */ if (tagname != NULL && *tagname != '\0') { 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->access = (access_mode)access; /* DWORD is a 4-byte int. If we're on a box where size_t consumes * more than 4 bytes, we need to break it apart. Else (size_t * consumes 4 bytes), C doesn't define what happens if we shift * right by 32, so we need different code. */ #if SIZEOF_SIZE_T > 4 size_hi = (DWORD)(m_obj->size >> 32); size_lo = (DWORD)(m_obj->size & 0xFFFFFFFF); #else size_hi = 0; size_lo = (DWORD)m_obj->size; #endif m_obj->map_handle = CreateFileMapping(m_obj->file_handle, NULL, flProtect, size_hi, size_lo, m_obj->tagname); if (m_obj->map_handle != NULL) { m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, dwDesiredAccess, 0, 0, 0); if (m_obj->data != NULL) return (PyObject *)m_obj; else dwErr = GetLastError(); } else dwErr = GetLastError(); Py_DECREF(m_obj); PyErr_SetFromWindowsErr(dwErr); return NULL; } #endif /* MS_WINDOWS */ /* List of functions exported by this module */ static struct PyMethodDef mmap_functions[] = { {"mmap", (PyCFunction) new_mmap_object, METH_VARARGS|METH_KEYWORDS}, {NULL, NULL} /* Sentinel */ }; static void setint(PyObject *d, const char *name, long value) { PyObject *o = PyInt_FromLong(value); if (o && PyDict_SetItemString(d, name, o) == 0) { Py_DECREF(o); } } PyMODINIT_FUNC initmmap(void) { PyObject *dict, *module; /* Patch the object type */ mmap_object_type.ob_type = &PyType_Type; module = Py_InitModule("mmap", mmap_functions); if (module == NULL) return; dict = PyModule_GetDict(module); if (!dict) return; mmap_module_error = PyExc_EnvironmentError; PyDict_SetItemString(dict, "error", mmap_module_error); #ifdef PROT_EXEC setint(dict, "PROT_EXEC", PROT_EXEC); #endif #ifdef PROT_READ setint(dict, "PROT_READ", PROT_READ); #endif #ifdef PROT_WRITE setint(dict, "PROT_WRITE", PROT_WRITE); #endif #ifdef MAP_SHARED setint(dict, "MAP_SHARED", MAP_SHARED); #endif #ifdef MAP_PRIVATE setint(dict, "MAP_PRIVATE", MAP_PRIVATE); #endif #ifdef MAP_DENYWRITE setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE); #endif #ifdef MAP_EXECUTABLE setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE); #endif #ifdef MAP_ANONYMOUS setint(dict, "MAP_ANON", MAP_ANONYMOUS); setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS); #endif setint(dict, "PAGESIZE", (long)my_getpagesize()); setint(dict, "ACCESS_READ", ACCESS_READ); setint(dict, "ACCESS_WRITE", ACCESS_WRITE); setint(dict, "ACCESS_COPY", ACCESS_COPY); }