annotate Modules/mmapmodule.c @ 34662:b40586129bb0 legacy-trunk

Patches #749830, #1144555: allow UNIX mmap size to default to current file size.
author Martin v. Löwis <martin@v.loewis.de>
date Thu, 03 Mar 2005 11:22:44 +0000
parents 5f4f2f536f96
children 3bb932801b64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1 /*
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
2 / Author: Sam Rushing <rushing@nightmare.com>
20888
2cd2ae4de30d Remove obsolete e-mail address
Andrew M. Kuchling <amk@amk.ca>
parents: 18626
diff changeset
3 / Hacked for Unix by AMK
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
4 / $Id$
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
5
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
6 / mmapmodule.cpp -- map a view of a file into memory
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
7 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
8 / todo: need permission flags, perhaps a 'chsize' analog
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
9 / not all functions check range yet!!!
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
10 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
11 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
12 / Note: This module currently only deals with 32-bit file
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
13 / sizes.
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
14 /
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
15 / This version of mmapmodule.c has been changed significantly
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
16 / from the original mmapfile.c on which it was based.
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
17 / The original version of mmapfile is maintained by Sam at
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
18 / ftp://squirl.nightmare.com/pub/python/python-ext.
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
19 */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
20
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
21 #include <Python.h>
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
22
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
23 #ifndef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
24 #define UNIX
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
25 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
26
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
27 #ifdef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
28 #include <windows.h>
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
29 static int
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
30 my_getpagesize(void)
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
31 {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
32 SYSTEM_INFO si;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
33 GetSystemInfo(&si);
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
34 return si.dwPageSize;
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
35 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
36 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
37
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
38 #ifdef UNIX
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
39 #include <sys/mman.h>
12941
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
40 #include <sys/stat.h>
14836
67e0ff4aa7b9 For the benefit of SunOS 4.1.4, define MS_SYNC as 0 when it's
Guido van Rossum <guido@python.org>
parents: 13758
diff changeset
41
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
42 #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
43 static int
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
44 my_getpagesize(void)
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
45 {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
46 return sysconf(_SC_PAGESIZE);
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
47 }
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
48 #else
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
49 #define my_getpagesize getpagesize
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
50 #endif
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
51
14836
67e0ff4aa7b9 For the benefit of SunOS 4.1.4, define MS_SYNC as 0 when it's
Guido van Rossum <guido@python.org>
parents: 13758
diff changeset
52 #endif /* UNIX */
67e0ff4aa7b9 For the benefit of SunOS 4.1.4, define MS_SYNC as 0 when it's
Guido van Rossum <guido@python.org>
parents: 13758
diff changeset
53
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
54 #include <string.h>
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
55 #include <sys/types.h>
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
56
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
57 static PyObject *mmap_module_error;
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
58
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
59 typedef enum
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
60 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
61 ACCESS_DEFAULT,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
62 ACCESS_READ,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
63 ACCESS_WRITE,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
64 ACCESS_COPY
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
65 } access_mode;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
66
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
67 typedef struct {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
68 PyObject_HEAD
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
69 char * data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
70 size_t size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
71 size_t pos;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
72
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
73 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
74 HANDLE map_handle;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
75 HANDLE file_handle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
76 char * tagname;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
77 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
78
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
79 #ifdef UNIX
12941
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
80 int fd;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
81 #endif
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
82
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
83 access_mode access;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
84 } mmap_object;
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
85
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
86
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
87 static void
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
88 mmap_object_dealloc(mmap_object *m_obj)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
89 {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
90 #ifdef MS_WINDOWS
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
91 if (m_obj->data != NULL)
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
92 UnmapViewOfFile (m_obj->data);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
93 if (m_obj->map_handle != INVALID_HANDLE_VALUE)
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
94 CloseHandle (m_obj->map_handle);
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
95 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
96 CloseHandle (m_obj->file_handle);
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
97 if (m_obj->tagname)
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
98 PyMem_Free(m_obj->tagname);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
99 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
100
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
101 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
102 if (m_obj->data!=NULL) {
12944
b1ec49b2c5c2 Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents: 12941
diff changeset
103 msync(m_obj->data, m_obj->size, MS_SYNC);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
104 munmap(m_obj->data, m_obj->size);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
105 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
106 #endif /* UNIX */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
107
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
108 PyObject_Del(m_obj);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
109 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
110
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
111 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
112 mmap_close_method(mmap_object *self, PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
113 {
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
114 if (!PyArg_ParseTuple(args, ":close"))
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
115 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
116 #ifdef MS_WINDOWS
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
117 /* For each resource we maintain, we need to check
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
118 the value is valid, and if so, free the resource
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
119 and set the member value to an invalid value so
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
120 the dealloc does not attempt to resource clearing
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
121 again.
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
122 TODO - should we check for errors in the close operations???
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
123 */
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
124 if (self->data != NULL) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
125 UnmapViewOfFile (self->data);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
126 self->data = NULL;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
127 }
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
128 if (self->map_handle != INVALID_HANDLE_VALUE) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
129 CloseHandle (self->map_handle);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
130 self->map_handle = INVALID_HANDLE_VALUE;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
131 }
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
132 if (self->file_handle != INVALID_HANDLE_VALUE) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
133 CloseHandle (self->file_handle);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
134 self->file_handle = INVALID_HANDLE_VALUE;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
135 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
136 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
137
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
138 #ifdef UNIX
27205
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
139 if (self->data != NULL) {
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
140 munmap(self->data, self->size);
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
141 self->data = NULL;
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
142 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
143 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
144
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
145 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
146 return (Py_None);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
147 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
148
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
149 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
150 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
151 do { \
28558
84acba227954 Fix two crashes on Windows:
Guido van Rossum <guido@python.org>
parents: 27756
diff changeset
152 if (self->map_handle == INVALID_HANDLE_VALUE) { \
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
153 PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
154 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
155 } \
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
156 } while (0)
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
157 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
158
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
159 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
160 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
161 do { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
162 if (self->data == NULL) { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
163 PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
164 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
165 } \
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
166 } while (0)
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
167 #endif /* UNIX */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
168
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
169 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
170 mmap_read_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
171 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
172 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
173 CHECK_VALID(NULL);
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
174 if (!PyArg_ParseTuple(args, ":read_byte"))
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
175 return NULL;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
176 if (self->pos < self->size) {
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
177 char value = self->data[self->pos];
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
178 self->pos += 1;
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
179 return Py_BuildValue("c", value);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
180 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
181 PyErr_SetString (PyExc_ValueError, "read byte out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
182 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
183 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
184 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
185
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
186 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
187 mmap_read_line_method(mmap_object *self,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
188 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
189 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
190 char *start = self->data+self->pos;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
191 char *eof = self->data+self->size;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
192 char *eol;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
193 PyObject *result;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
194
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
195 CHECK_VALID(NULL);
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
196 if (!PyArg_ParseTuple(args, ":readline"))
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
197 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
198
12231
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
199 eol = memchr(start, '\n', self->size - self->pos);
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
200 if (!eol)
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
201 eol = eof;
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
202 else
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
203 ++eol; /* we're interested in the position after the
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
204 newline. */
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
205 result = PyString_FromStringAndSize(start, (eol - start));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
206 self->pos += (eol - start);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
207 return (result);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
208 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
209
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
210 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
211 mmap_read_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
212 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
213 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
214 long num_bytes;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
215 PyObject *result;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
216
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
217 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
218 if (!PyArg_ParseTuple(args, "l:read", &num_bytes))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
219 return(NULL);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
220
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
221 /* silently 'adjust' out-of-range requests */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
222 if ((self->pos + num_bytes) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
223 num_bytes -= (self->pos+num_bytes) - self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
224 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
225 result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
226 self->pos += num_bytes;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
227 return (result);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
228 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
229
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
230 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
231 mmap_find_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
232 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
233 {
17832
1becf442d735 Fix new compiler warnings. Also boost "start" from (C) int to long and
Tim Peters <tim.peters@gmail.com>
parents: 17821
diff changeset
234 long start = self->pos;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
235 char *needle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
236 int len;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
237
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
238 CHECK_VALID(NULL);
17832
1becf442d735 Fix new compiler warnings. Also boost "start" from (C) int to long and
Tim Peters <tim.peters@gmail.com>
parents: 17821
diff changeset
239 if (!PyArg_ParseTuple (args, "s#|l:find", &needle, &len, &start)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
240 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
241 } else {
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
242 char *p;
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
243 char *e = self->data + self->size;
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
244
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
245 if (start < 0)
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
246 start += self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
247 if (start < 0)
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
248 start = 0;
17832
1becf442d735 Fix new compiler warnings. Also boost "start" from (C) int to long and
Tim Peters <tim.peters@gmail.com>
parents: 17821
diff changeset
249 else if ((size_t)start > self->size)
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
250 start = self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
251
22177
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
252 for (p = self->data + start; p + len <= e; ++p) {
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
253 int i;
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
254 for (i = 0; i < len && needle[i] == p[i]; ++i)
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
255 /* nothing */;
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
256 if (i == len) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
257 return Py_BuildValue (
17832
1becf442d735 Fix new compiler warnings. Also boost "start" from (C) int to long and
Tim Peters <tim.peters@gmail.com>
parents: 17821
diff changeset
258 "l",
1becf442d735 Fix new compiler warnings. Also boost "start" from (C) int to long and
Tim Peters <tim.peters@gmail.com>
parents: 17821
diff changeset
259 (long) (p - self->data));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
260 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
261 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
262 return Py_BuildValue ("l", (long) -1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
263 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
264 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
265
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
266 static int
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
267 is_writeable(mmap_object *self)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
268 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
269 if (self->access != ACCESS_READ)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
270 return 1;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
271 PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
272 return 0;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
273 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
274
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
275 static int
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
276 is_resizeable(mmap_object *self)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
277 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
278 if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
279 return 1;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
280 PyErr_Format(PyExc_TypeError,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
281 "mmap can't resize a readonly or copy-on-write memory map.");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
282 return 0;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
283 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
284
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
285
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
286 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
287 mmap_write_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
288 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
289 {
27756
0395512ce1b5 SF patch #682514, mmapmodule.c write fix for LP64 executables
Neal Norwitz <nnorwitz@gmail.com>
parents: 27205
diff changeset
290 int length;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
291 char *data;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
292
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
293 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
294 if (!PyArg_ParseTuple (args, "s#:write", &data, &length))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
295 return(NULL);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
296
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
297 if (!is_writeable(self))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
298 return NULL;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
299
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
300 if ((self->pos + length) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
301 PyErr_SetString (PyExc_ValueError, "data out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
302 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
303 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
304 memcpy (self->data+self->pos, data, length);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
305 self->pos = self->pos+length;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
306 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
307 return (Py_None);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
308 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
309
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
310 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
311 mmap_write_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
312 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
313 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
314 char value;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
315
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
316 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
317 if (!PyArg_ParseTuple (args, "c:write_byte", &value))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
318 return(NULL);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
319
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
320 if (!is_writeable(self))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
321 return NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
322 *(self->data+self->pos) = value;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
323 self->pos += 1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
324 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
325 return (Py_None);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
326 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
327
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
328 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
329 mmap_size_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
330 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
331 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
332 CHECK_VALID(NULL);
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
333 if (!PyArg_ParseTuple(args, ":size"))
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
334 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
335
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
336 #ifdef MS_WINDOWS
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
337 if (self->file_handle != INVALID_HANDLE_VALUE) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
338 return (Py_BuildValue (
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
339 "l", (long)
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
340 GetFileSize (self->file_handle, NULL)));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
341 } else {
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
342 return (Py_BuildValue ("l", (long) self->size) );
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
343 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
344 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
345
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
346 #ifdef UNIX
12941
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
347 {
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
348 struct stat buf;
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
349 if (-1 == fstat(self->fd, &buf)) {
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
350 PyErr_SetFromErrno(mmap_module_error);
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
351 return NULL;
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
352 }
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
353 return (Py_BuildValue ("l", (long) buf.st_size) );
12941
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
354 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
355 #endif /* UNIX */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
356 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
357
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
358 /* This assumes that you want the entire file mapped,
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
359 / and when recreating the map will make the new file
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
360 / have the new size
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
361 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
362 / Is this really necessary? This could easily be done
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
363 / from python by just closing and re-opening with the
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
364 / new size?
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
365 */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
366
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
367 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
368 mmap_resize_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
369 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
370 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
371 unsigned long new_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
372 CHECK_VALID(NULL);
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
373 if (!PyArg_ParseTuple (args, "l:resize", &new_size) ||
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
374 !is_resizeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
375 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
376 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
377 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
378 DWORD dwErrCode = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
379 /* First, unmap the file view */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
380 UnmapViewOfFile (self->data);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
381 /* Close the mapping object */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
382 CloseHandle (self->map_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
383 /* Move to the desired EOF position */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
384 SetFilePointer (self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
385 new_size, NULL, FILE_BEGIN);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
386 /* Change the size of the file */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
387 SetEndOfFile (self->file_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
388 /* Create another mapping object and remap the file view */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
389 self->map_handle = CreateFileMapping (
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
390 self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
391 NULL,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
392 PAGE_READWRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
393 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
394 new_size,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
395 self->tagname);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
396 if (self->map_handle != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
397 self->data = (char *) MapViewOfFile (self->map_handle,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
398 FILE_MAP_WRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
399 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
400 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
401 0);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
402 if (self->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
403 self->size = new_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
404 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
405 return Py_None;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
406 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
407 dwErrCode = GetLastError();
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
408 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
409 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
410 dwErrCode = GetLastError();
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
411 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
412 PyErr_SetFromWindowsErr(dwErrCode);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
413 return (NULL);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
414 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
415
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
416 #ifdef UNIX
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
417 #ifndef HAVE_MREMAP
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
418 } else {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
419 PyErr_SetString(PyExc_SystemError,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
420 "mmap: resizing not available--no mremap()");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
421 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
422 #else
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
423 } else {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
424 void *newmap;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
425
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
426 #ifdef MREMAP_MAYMOVE
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
427 newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
428 #else
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
429 newmap = mremap(self->data, self->size, new_size, 0);
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
430 #endif
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
431 if (newmap == (void *)-1)
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
432 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
433 PyErr_SetFromErrno(mmap_module_error);
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
434 return NULL;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
435 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
436 self->data = newmap;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
437 self->size = new_size;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
438 Py_INCREF(Py_None);
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
439 return Py_None;
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
440 #endif /* HAVE_MREMAP */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
441 #endif /* UNIX */
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
442 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
443 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
444
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
445 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
446 mmap_tell_method(mmap_object *self, PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
447 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
448 CHECK_VALID(NULL);
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
449 if (!PyArg_ParseTuple(args, ":tell"))
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
450 return NULL;
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
451 return (Py_BuildValue ("l", (long) self->pos) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
452 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
453
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
454 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
455 mmap_flush_method(mmap_object *self, PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
456 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
457 size_t offset = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
458 size_t size = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
459 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
460 if (!PyArg_ParseTuple (args, "|ll:flush", &offset, &size)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
461 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
462 } else if ((offset + size) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
463 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
464 "flush values out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
465 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
466 } else {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
467 #ifdef MS_WINDOWS
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
468 return (Py_BuildValue("l", (long)
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
469 FlushViewOfFile(self->data+offset, size)));
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
470 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
471 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
472 /* XXX semantics of return value? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
473 /* XXX flags for msync? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
474 if (-1 == msync(self->data + offset, size,
12944
b1ec49b2c5c2 Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents: 12941
diff changeset
475 MS_SYNC))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
476 {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
477 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
478 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
479 }
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
480 return Py_BuildValue ("l", (long) 0);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
481 #endif /* UNIX */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
482 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
483 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
484
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
485 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
486 mmap_seek_method(mmap_object *self, PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
487 {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
488 int dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
489 int how=0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
490 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
491 if (!PyArg_ParseTuple (args, "i|i:seek", &dist, &how)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
492 return(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
493 } else {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
494 size_t where;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
495 switch (how) {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
496 case 0: /* relative to start */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
497 if (dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
498 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
499 where = dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
500 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
501 case 1: /* relative to current position */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
502 if ((int)self->pos + dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
503 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
504 where = self->pos + dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
505 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
506 case 2: /* relative to end */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
507 if ((int)self->size + dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
508 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
509 where = self->size + dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
510 break;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
511 default:
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
512 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
513 "unknown seek type");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
514 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
515 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
516 if (where > self->size)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
517 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
518 self->pos = where;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
519 Py_INCREF (Py_None);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
520 return (Py_None);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
521 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
522
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
523 onoutofrange:
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
524 PyErr_SetString (PyExc_ValueError, "seek out of range");
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
525 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
526 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
527
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
528 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
529 mmap_move_method(mmap_object *self, PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
530 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
531 unsigned long dest, src, count;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
532 CHECK_VALID(NULL);
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
533 if (!PyArg_ParseTuple (args, "iii:move", &dest, &src, &count) ||
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
534 !is_writeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
535 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
536 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
537 /* bounds check the values */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
538 if (/* end of source after end of data?? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
539 ((src+count) > self->size)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
540 /* dest will fit? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
541 || (dest+count > self->size)) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
542 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
543 "source or destination out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
544 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
545 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
546 memmove (self->data+dest, self->data+src, count);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
547 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
548 return Py_None;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
549 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
550 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
551 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
552
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
553 static struct PyMethodDef mmap_object_methods[] = {
21757
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
554 {"close", (PyCFunction) mmap_close_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
555 {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
556 {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
557 {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
558 {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
559 {"read_byte", (PyCFunction) mmap_read_byte_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
560 {"readline", (PyCFunction) mmap_read_line_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
561 {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
562 {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
563 {"size", (PyCFunction) mmap_size_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
564 {"tell", (PyCFunction) mmap_tell_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
565 {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
566 {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
567 {NULL, NULL} /* sentinel */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
568 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
569
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
570 /* Functions for treating an mmap'ed file as a buffer */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
571
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
572 static int
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
573 mmap_buffer_getreadbuf(mmap_object *self, int index, const void **ptr)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
574 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
575 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
576 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
577 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
578 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
579 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
580 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
581 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
582 return self->size;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
583 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
584
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
585 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
586 mmap_buffer_getwritebuf(mmap_object *self, int index, const void **ptr)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
587 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
588 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
589 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
590 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
591 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
592 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
593 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
594 if (!is_writeable(self))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
595 return -1;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
596 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
597 return self->size;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
598 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
599
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
600 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
601 mmap_buffer_getsegcount(mmap_object *self, int *lenp)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
602 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
603 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
604 if (lenp)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
605 *lenp = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
606 return 1;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
607 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
608
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
609 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
610 mmap_buffer_getcharbuffer(mmap_object *self, int index, const void **ptr)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
611 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
612 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
613 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
614 "accessing non-existent buffer segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
615 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
616 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
617 *ptr = (const char *)self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
618 return self->size;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
619 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
620
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
621 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
622 mmap_object_getattr(mmap_object *self, char *name)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
623 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
624 return Py_FindMethod (mmap_object_methods, (PyObject *)self, name);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
625 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
626
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
627 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
628 mmap_length(mmap_object *self)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
629 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
630 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
631 return self->size;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
632 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
633
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
634 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
635 mmap_item(mmap_object *self, int i)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
636 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
637 CHECK_VALID(NULL);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
638 if (i < 0 || (size_t)i >= self->size) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
639 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
640 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
641 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
642 return PyString_FromStringAndSize(self->data + i, 1);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
643 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
644
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
645 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
646 mmap_slice(mmap_object *self, int ilow, int ihigh)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
647 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
648 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
649 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
650 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
651 else if ((size_t)ilow > self->size)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
652 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
653 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
654 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
655 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
656 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
657 else if ((size_t)ihigh > self->size)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
658 ihigh = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
659
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
660 return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
661 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
662
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
663 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
664 mmap_concat(mmap_object *self, PyObject *bb)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
665 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
666 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
667 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
668 "mmaps don't support concatenation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
669 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
670 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
671
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
672 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
673 mmap_repeat(mmap_object *self, int n)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
674 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
675 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
676 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
677 "mmaps don't support repeat operation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
678 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
679 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
680
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
681 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
682 mmap_ass_slice(mmap_object *self, int ilow, int ihigh, PyObject *v)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
683 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
684 const char *buf;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
685
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
686 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
687 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
688 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
689 else if ((size_t)ilow > self->size)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
690 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
691 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
692 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
693 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
694 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
695 else if ((size_t)ihigh > self->size)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
696 ihigh = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
697
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
698 if (v == NULL) {
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
699 PyErr_SetString(PyExc_TypeError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
700 "mmap object doesn't support slice deletion");
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
701 return -1;
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
702 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
703 if (! (PyString_Check(v)) ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
704 PyErr_SetString(PyExc_IndexError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
705 "mmap slice assignment must be a string");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
706 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
707 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
708 if ( PyString_Size(v) != (ihigh - ilow) ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
709 PyErr_SetString(PyExc_IndexError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
710 "mmap slice assignment is wrong size");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
711 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
712 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
713 if (!is_writeable(self))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
714 return -1;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
715 buf = PyString_AsString(v);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
716 memcpy(self->data + ilow, buf, ihigh-ilow);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
717 return 0;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
718 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
719
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
720 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
721 mmap_ass_item(mmap_object *self, int i, PyObject *v)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
722 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
723 const char *buf;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
724
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
725 CHECK_VALID(-1);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
726 if (i < 0 || (size_t)i >= self->size) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
727 PyErr_SetString(PyExc_IndexError, "mmap index out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
728 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
729 }
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
730 if (v == NULL) {
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
731 PyErr_SetString(PyExc_TypeError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
732 "mmap object doesn't support item deletion");
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
733 return -1;
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
734 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
735 if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
736 PyErr_SetString(PyExc_IndexError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
737 "mmap assignment must be single-character string");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
738 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
739 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
740 if (!is_writeable(self))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
741 return -1;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
742 buf = PyString_AsString(v);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
743 self->data[i] = buf[0];
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
744 return 0;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
745 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
746
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
747 static PySequenceMethods mmap_as_sequence = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
748 (inquiry)mmap_length, /*sq_length*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
749 (binaryfunc)mmap_concat, /*sq_concat*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
750 (intargfunc)mmap_repeat, /*sq_repeat*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
751 (intargfunc)mmap_item, /*sq_item*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
752 (intintargfunc)mmap_slice, /*sq_slice*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
753 (intobjargproc)mmap_ass_item, /*sq_ass_item*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
754 (intintobjargproc)mmap_ass_slice, /*sq_ass_slice*/
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
755 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
756
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
757 static PyBufferProcs mmap_as_buffer = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
758 (getreadbufferproc)mmap_buffer_getreadbuf,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
759 (getwritebufferproc)mmap_buffer_getwritebuf,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
760 (getsegcountproc)mmap_buffer_getsegcount,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
761 (getcharbufferproc)mmap_buffer_getcharbuffer,
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
762 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
763
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
764 static PyTypeObject mmap_object_type = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
765 PyObject_HEAD_INIT(0) /* patched in module init */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
766 0, /* ob_size */
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 20970
diff changeset
767 "mmap.mmap", /* tp_name */
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
768 sizeof(mmap_object), /* tp_size */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
769 0, /* tp_itemsize */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
770 /* methods */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
771 (destructor) mmap_object_dealloc, /* tp_dealloc */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
772 0, /* tp_print */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
773 (getattrfunc) mmap_object_getattr, /* tp_getattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
774 0, /* tp_setattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
775 0, /* tp_compare */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
776 0, /* tp_repr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
777 0, /* tp_as_number */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
778 &mmap_as_sequence, /*tp_as_sequence*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
779 0, /*tp_as_mapping*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
780 0, /*tp_hash*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
781 0, /*tp_call*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
782 0, /*tp_str*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
783 0, /*tp_getattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
784 0, /*tp_setattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
785 &mmap_as_buffer, /*tp_as_buffer*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
786 Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
787 0, /*tp_doc*/
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
788 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
789
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
790
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
791 /* extract the map size from the given PyObject
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
792
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
793 The map size is restricted to [0, INT_MAX] because this is the current
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
794 Python limitation on object sizes. Although the mmap object *could* handle
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
795 a larger map size, there is no point because all the useful operations
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
796 (len(), slicing(), sequence indexing) are limited by a C int.
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
797
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13463
diff changeset
798 Returns -1 on error, with an appropriate Python exception raised. On
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
799 success, the map size is returned. */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
800 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
801 _GetMapSize(PyObject *o)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
802 {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
803 if (PyInt_Check(o)) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
804 long i = PyInt_AsLong(o);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
805 if (PyErr_Occurred())
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
806 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
807 if (i < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
808 goto onnegoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
809 if (i > INT_MAX)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
810 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
811 return (int)i;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
812 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
813 else if (PyLong_Check(o)) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
814 long i = PyLong_AsLong(o);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
815 if (PyErr_Occurred()) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
816 /* yes negative overflow is mistaken for positive overflow
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
817 but not worth the trouble to check sign of 'i' */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
818 if (PyErr_ExceptionMatches(PyExc_OverflowError))
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
819 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
820 else
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
821 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
822 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
823 if (i < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
824 goto onnegoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
825 if (i > INT_MAX)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
826 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
827 return (int)i;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
828 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
829 else {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
830 PyErr_SetString(PyExc_TypeError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
831 "map size must be an integral value");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
832 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
833 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
834
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
835 onnegoverflow:
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
836 PyErr_SetString(PyExc_OverflowError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
837 "memory mapped size must be positive");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
838 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
839
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
840 onposoverflow:
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
841 PyErr_SetString(PyExc_OverflowError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
842 "memory mapped size is too large (limited by C int)");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
843 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
844 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
845
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
846 #ifdef UNIX
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
847 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
848 new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
849 {
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
850 #ifdef HAVE_FSTAT
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
851 struct stat st;
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
852 #endif
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
853 mmap_object *m_obj;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
854 PyObject *map_size_obj = NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
855 int map_size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
856 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
857 access_mode access = ACCESS_DEFAULT;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
858 char *keywords[] = {"fileno", "length",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
859 "flags", "prot",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
860 "access", NULL};
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
861
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
862 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii", keywords,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
863 &fd, &map_size_obj, &flags, &prot, &access))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
864 return NULL;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
865 map_size = _GetMapSize(map_size_obj);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
866 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
867 return NULL;
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
868
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
869 if ((access != ACCESS_DEFAULT) &&
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
870 ((flags != MAP_SHARED) || ( prot != (PROT_WRITE | PROT_READ))))
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
871 return PyErr_Format(PyExc_ValueError,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
872 "mmap can't specify both access and flags, prot.");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
873 switch(access) {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
874 case ACCESS_READ:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
875 flags = MAP_SHARED;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
876 prot = PROT_READ;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
877 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
878 case ACCESS_WRITE:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
879 flags = MAP_SHARED;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
880 prot = PROT_READ | PROT_WRITE;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
881 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
882 case ACCESS_COPY:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
883 flags = MAP_PRIVATE;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
884 prot = PROT_READ | PROT_WRITE;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
885 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
886 case ACCESS_DEFAULT:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
887 /* use the specified or default values of flags and prot */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
888 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
889 default:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
890 return PyErr_Format(PyExc_ValueError,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
891 "mmap invalid access parameter.");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
892 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
893
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
894 #ifdef HAVE_FSTAT
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
895 # ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
896 /* on OpenVMS we must ensure that all bytes are written to the file */
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
897 fsync(fd);
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
898 # endif
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
899 if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
900 if (map_size == 0) {
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
901 map_size = (int)st.st_size;
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
902 } else if ((size_t)map_size > st.st_size) {
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
903 PyErr_SetString(PyExc_ValueError,
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
904 "mmap length is greater than file size");
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
905 return NULL;
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
906 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
907 }
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
908 #endif
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
909 m_obj = PyObject_New (mmap_object, &mmap_object_type);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
910 if (m_obj == NULL) {return NULL;}
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
911 m_obj->size = (size_t) map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
912 m_obj->pos = (size_t) 0;
12941
8c9554edbebb Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents: 12846
diff changeset
913 m_obj->fd = fd;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
914 m_obj->data = mmap(NULL, map_size,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
915 prot, flags,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
916 fd, 0);
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
917 if (m_obj->data == (char *)-1) {
32123
5f4f2f536f96 Set data pointer to NULL after an error; this keeps the mmap_dealloc() function from trying to do msync(-1);munmap(-1).
Andrew M. Kuchling <amk@amk.ca>
parents: 31384
diff changeset
918 m_obj->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
919 Py_DECREF(m_obj);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
920 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
921 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
922 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
923 m_obj->access = access;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
924 return (PyObject *)m_obj;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
925 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
926 #endif /* UNIX */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
927
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
928 #ifdef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
929 static PyObject *
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
930 new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
931 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
932 mmap_object *m_obj;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
933 PyObject *map_size_obj = NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
934 int map_size;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
935 char *tagname = "";
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
936 DWORD dwErr = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
937 int fileno;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
938 HANDLE fh = 0;
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
939 access_mode access = ACCESS_DEFAULT;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
940 DWORD flProtect, dwDesiredAccess;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
941 char *keywords[] = { "fileno", "length",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
942 "tagname",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
943 "access", NULL };
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
944
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
945 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zi", keywords,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
946 &fileno, &map_size_obj,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
947 &tagname, &access)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
948 return NULL;
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
949 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
950
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
951 switch(access) {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
952 case ACCESS_READ:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
953 flProtect = PAGE_READONLY;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
954 dwDesiredAccess = FILE_MAP_READ;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
955 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
956 case ACCESS_DEFAULT: case ACCESS_WRITE:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
957 flProtect = PAGE_READWRITE;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
958 dwDesiredAccess = FILE_MAP_WRITE;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
959 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
960 case ACCESS_COPY:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
961 flProtect = PAGE_WRITECOPY;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
962 dwDesiredAccess = FILE_MAP_COPY;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
963 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
964 default:
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
965 return PyErr_Format(PyExc_ValueError,
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
966 "mmap invalid access parameter.");
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
967 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
968
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
969 map_size = _GetMapSize(map_size_obj);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
970 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
971 return NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
972
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
973 /* if an actual filename has been specified */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
974 if (fileno != 0) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
975 fh = (HANDLE)_get_osfhandle(fileno);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
976 if (fh==(HANDLE)-1) {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
977 PyErr_SetFromErrno(mmap_module_error);
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
978 return NULL;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
979 }
12241
7e9a5cf55c76 Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents: 12231
diff changeset
980 /* Win9x appears to need us seeked to zero */
28558
84acba227954 Fix two crashes on Windows:
Guido van Rossum <guido@python.org>
parents: 27756
diff changeset
981 lseek(fileno, 0, SEEK_SET);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
982 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
983
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
984 m_obj = PyObject_New (mmap_object, &mmap_object_type);
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
985 if (m_obj==NULL)
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
986 return NULL;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
987 /* Set every field to an invalid marker, so we can safely
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
988 destruct the object in the face of failure */
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
989 m_obj->data = NULL;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
990 m_obj->file_handle = INVALID_HANDLE_VALUE;
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
991 m_obj->map_handle = INVALID_HANDLE_VALUE;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
992 m_obj->tagname = NULL;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
993
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
994 if (fh) {
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
995 /* It is necessary to duplicate the handle, so the
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
996 Python code can close it on us */
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
997 if (!DuplicateHandle(
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
998 GetCurrentProcess(), /* source process handle */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
999 fh, /* handle to be duplicated */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1000 GetCurrentProcess(), /* target proc handle */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1001 (LPHANDLE)&m_obj->file_handle, /* result */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1002 0, /* access - ignored due to options value */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1003 FALSE, /* inherited by child processes? */
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1004 DUPLICATE_SAME_ACCESS)) { /* options */
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1005 dwErr = GetLastError();
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1006 Py_DECREF(m_obj);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1007 PyErr_SetFromWindowsErr(dwErr);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1008 return NULL;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1009 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1010 if (!map_size) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1011 m_obj->size = GetFileSize (fh, NULL);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1012 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1013 m_obj->size = map_size;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1014 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1015 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1016 else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1017 m_obj->size = map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1018 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1019
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1020 /* set the initial position */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1021 m_obj->pos = (size_t) 0;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1022
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1023 /* set the tag name */
15794
b7d5f68be017 Windows mmap should (as the docs probably <wink> say) create a mapping
Tim Peters <tim.peters@gmail.com>
parents: 14956
diff changeset
1024 if (tagname != NULL && *tagname != '\0') {
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1025 m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1026 if (m_obj->tagname == NULL) {
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1027 PyErr_NoMemory();
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1028 Py_DECREF(m_obj);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1029 return NULL;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1030 }
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1031 strcpy(m_obj->tagname, tagname);
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1032 }
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1033 else
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1034 m_obj->tagname = NULL;
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1035
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1036 m_obj->access = access;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1037 m_obj->map_handle = CreateFileMapping (m_obj->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1038 NULL,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1039 flProtect,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1040 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1041 m_obj->size,
15794
b7d5f68be017 Windows mmap should (as the docs probably <wink> say) create a mapping
Tim Peters <tim.peters@gmail.com>
parents: 14956
diff changeset
1042 m_obj->tagname);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1043 if (m_obj->map_handle != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1044 m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1045 dwDesiredAccess,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1046 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1047 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1048 0);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1049 if (m_obj->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1050 return ((PyObject *) m_obj);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1051 } else {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1052 dwErr = GetLastError();
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1053 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1054 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1055 dwErr = GetLastError();
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1056 }
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
1057 Py_DECREF(m_obj);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1058 PyErr_SetFromWindowsErr(dwErr);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1059 return (NULL);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1060 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
1061 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1062
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1063 /* List of functions exported by this module */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1064 static struct PyMethodDef mmap_functions[] = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1065 {"mmap", (PyCFunction) new_mmap_object,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1066 METH_VARARGS|METH_KEYWORDS},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1067 {NULL, NULL} /* Sentinel */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1068 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1069
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
1070 PyMODINIT_FUNC
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1071 initmmap(void)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1072 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1073 PyObject *dict, *module;
15851
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1074
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1075 /* Patch the object type */
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1076 mmap_object_type.ob_type = &PyType_Type;
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1077
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1078 module = Py_InitModule ("mmap", mmap_functions);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1079 dict = PyModule_GetDict (module);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1080 mmap_module_error = PyExc_EnvironmentError;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1081 Py_INCREF(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1082 PyDict_SetItemString (dict, "error", mmap_module_error);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1083 #ifdef PROT_EXEC
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1084 PyDict_SetItemString (dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1085 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1086 #ifdef PROT_READ
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1087 PyDict_SetItemString (dict, "PROT_READ", PyInt_FromLong(PROT_READ) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1088 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1089 #ifdef PROT_WRITE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1090 PyDict_SetItemString (dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1091 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1092
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1093 #ifdef MAP_SHARED
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1094 PyDict_SetItemString (dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1095 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1096 #ifdef MAP_PRIVATE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1097 PyDict_SetItemString (dict, "MAP_PRIVATE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1098 PyInt_FromLong(MAP_PRIVATE) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1099 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1100 #ifdef MAP_DENYWRITE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1101 PyDict_SetItemString (dict, "MAP_DENYWRITE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1102 PyInt_FromLong(MAP_DENYWRITE) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1103 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1104 #ifdef MAP_EXECUTABLE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1105 PyDict_SetItemString (dict, "MAP_EXECUTABLE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1106 PyInt_FromLong(MAP_EXECUTABLE) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1107 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1108 #ifdef MAP_ANON
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1109 PyDict_SetItemString (dict, "MAP_ANON", PyInt_FromLong(MAP_ANON) );
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1110 PyDict_SetItemString (dict, "MAP_ANONYMOUS",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1111 PyInt_FromLong(MAP_ANON) );
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1112 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1113
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1114 PyDict_SetItemString (dict, "PAGESIZE",
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
1115 PyInt_FromLong( (long)my_getpagesize() ) );
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1116
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1117 PyDict_SetItemString (dict, "ACCESS_READ",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1118 PyInt_FromLong(ACCESS_READ));
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1119 PyDict_SetItemString (dict, "ACCESS_WRITE",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1120 PyInt_FromLong(ACCESS_WRITE));
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1121 PyDict_SetItemString (dict, "ACCESS_COPY",
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1122 PyInt_FromLong(ACCESS_COPY));
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1123 }