[Python-checkins] CVS: python/dist/src/Modules mmapmodule.c,2.21,2.22
Mark Hammond
python-dev@python.org
2000年7月29日 19:46:29 -0700
Update of /cvsroot/python/python/dist/src/Modules
In directory slayer.i.sourceforge.net:/tmp/cvs-serv5859
Modified Files:
mmapmodule.c
Log Message:
More Windows changes.
* After discussion with Trent, all INT_PTR references have been removed in favour of the HANDLE it should always have been. Trent can see no 64bit issues here.
* In this process, I noticed that the close operation was dangerous, in that we could end up passing bogus results to the Win32 API. These result of the API functions passed the bogus values were never (and still are not) checked, but this is closer to "the right thing" (tm) than before.
Tested on Windows and Linux.
Index: mmapmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/mmapmodule.c,v
retrieving revision 2.21
retrieving revision 2.22
diff -C2 -r2.21 -r2.22
*** mmapmodule.c 2000年07月30日 02:22:43 2.21
--- mmapmodule.c 2000年07月30日 02:46:26 2.22
***************
*** 13,17 ****
/ sizes.
/
! / The latest version of mmapfile is maintained by Sam at
/ ftp://squirl.nightmare.com/pub/python/python-ext.
*/
--- 13,19 ----
/ sizes.
/
! / This version of mmapmodule.c has been changed significantly
! / from the original mmapfile.c on which it was based.
! / The original version of mmapfile is maintained by Sam at
/ ftp://squirl.nightmare.com/pub/python/python-ext.
*/
***************
*** 25,32 ****
#ifdef MS_WIN32
#include <windows.h>
- #if _MSC_VER < 1200
- #define INT_PTR unsigned long
#endif
- #endif
#ifdef UNIX
--- 27,31 ----
***************
*** 49,53 ****
#ifdef MS_WIN32
HANDLE map_handle;
! INT_PTR file_handle;
char * tagname;
#endif
--- 48,52 ----
#ifdef MS_WIN32
HANDLE map_handle;
! HANDLE file_handle;
char * tagname;
#endif
***************
*** 66,71 ****
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);
--- 65,70 ----
if (m_obj->map_handle != INVALID_HANDLE_VALUE)
CloseHandle (m_obj->map_handle);
! if (m_obj->file_handle != INVALID_HANDLE_VALUE)
! CloseHandle (m_obj->file_handle);
if (m_obj->tagname)
PyMem_Free(m_obj->tagname);
***************
*** 88,95 ****
return NULL;
#ifdef MS_WIN32
! UnmapViewOfFile (self->data);
! CloseHandle (self->map_handle);
! CloseHandle ((HANDLE)self->file_handle);
! self->map_handle = (HANDLE) NULL;
#endif /* MS_WIN32 */
--- 87,109 ----
return NULL;
#ifdef MS_WIN32
! /* For each resource we maintain, we need to check
! the value is valid, and if so, free the resource
! and set the member value to an invalid value so
! the dealloc does not attempt to resource clearing
! again.
! TODO - should we check for errors in the close operations???
! */
! if (self->data != NULL) {
! UnmapViewOfFile (self->data);
! self->data = NULL;
! }
! if (self->map_handle != INVALID_HANDLE_VALUE) {
! CloseHandle (self->map_handle);
! self->map_handle = INVALID_HANDLE_VALUE;
! }
! if (self->file_handle != INVALID_HANDLE_VALUE) {
! CloseHandle (self->file_handle);
! self->file_handle = INVALID_HANDLE_VALUE;
! }
#endif /* MS_WIN32 */
***************
*** 264,271 ****
#ifdef MS_WIN32
! if (self->file_handle != (INT_PTR) -1) {
return (Py_BuildValue (
"l", (long)
! GetFileSize ((HANDLE)self->file_handle, NULL)));
} else {
return (Py_BuildValue ("l", (long) self->size) );
--- 278,285 ----
#ifdef MS_WIN32
! if (self->file_handle != INVALID_HANDLE_VALUE) {
return (Py_BuildValue (
"l", (long)
! GetFileSize (self->file_handle, NULL)));
} else {
return (Py_BuildValue ("l", (long) self->size) );
***************
*** 308,320 ****
UnmapViewOfFile (self->data);
/* Close the mapping object */
! CloseHandle ((HANDLE)self->map_handle);
/* Move to the desired EOF position */
! SetFilePointer ((HANDLE)self->file_handle,
new_size, NULL, FILE_BEGIN);
/* Change the size of the file */
! SetEndOfFile ((HANDLE)self->file_handle);
/* Create another mapping object and remap the file view */
self->map_handle = CreateFileMapping (
! (HANDLE) self->file_handle,
NULL,
PAGE_READWRITE,
--- 322,334 ----
UnmapViewOfFile (self->data);
/* Close the mapping object */
! CloseHandle (self->map_handle);
/* Move to the desired EOF position */
! SetFilePointer (self->file_handle,
new_size, NULL, FILE_BEGIN);
/* Change the size of the file */
! SetEndOfFile (self->file_handle);
/* Create another mapping object and remap the file view */
self->map_handle = CreateFileMapping (
! self->file_handle,
NULL,
PAGE_READWRITE,
***************
*** 803,807 ****
DWORD dwErr = 0;
int fileno;
! INT_PTR fh = 0;
/* Patch the object type */
--- 817,821 ----
DWORD dwErr = 0;
int fileno;
! HANDLE fh = 0;
/* Patch the object type */
***************
*** 822,827 ****
/* if an actual filename has been specified */
if (fileno != 0) {
! fh = _get_osfhandle(fileno);
! if (fh==-1) {
PyErr_SetFromErrno(mmap_module_error);
return NULL;
--- 836,841 ----
/* if an actual filename has been specified */
if (fileno != 0) {
! fh = (HANDLE)_get_osfhandle(fileno);
! if (fh==(HANDLE)-1) {
PyErr_SetFromErrno(mmap_module_error);
return NULL;
***************
*** 837,841 ****
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;
--- 851,855 ----
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;
***************
*** 846,850 ****
if (!DuplicateHandle(
GetCurrentProcess(), /* source process handle */
! (HANDLE)fh, /* handle to be duplicated */
GetCurrentProcess(), /* target proc handle */
(LPHANDLE)&m_obj->file_handle, /* result */
--- 860,864 ----
if (!DuplicateHandle(
GetCurrentProcess(), /* source process handle */
! fh, /* handle to be duplicated */
GetCurrentProcess(), /* target proc handle */
(LPHANDLE)&m_obj->file_handle, /* result */
***************
*** 858,862 ****
}
if (!map_size) {
! m_obj->size = GetFileSize ((HANDLE)fh, NULL);
} else {
m_obj->size = map_size;
--- 872,876 ----
}
if (!map_size) {
! m_obj->size = GetFileSize (fh, NULL);
} else {
m_obj->size = map_size;
***************
*** 883,887 ****
m_obj->tagname = NULL;
! m_obj->map_handle = CreateFileMapping ((HANDLE) m_obj->file_handle,
NULL,
PAGE_READWRITE,
--- 897,901 ----
m_obj->tagname = NULL;
! m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
NULL,
PAGE_READWRITE,