annotate Modules/mmapmodule.c @ 16798:258dcafe2d0b legacy-trunk

Gustavo Niemeyer <niemeyer@conectiva.com>: Fixed recno support (keys are integers rather than strings). Work around DB bug that cause stdin to be closed by rnopen() when the DB file needed to exist but did not (no longer segfaults). This closes SF tracker patch #403445. Also wrapped some long lines and added whitespace around operators -- FLD.
author Fred Drake <fdrake@acm.org>
date Tue, 27 Feb 2001 18:56:46 +0000
parents a3c1a234cee7
children c5b50552d56d 1fe8e50e6e1b
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>
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
3 / Hacked for Unix by A.M. Kuchling <amk1@bigfoot.com>
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
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
23 #ifndef MS_WIN32
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
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
27 #ifdef MS_WIN32
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 {
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
32 SYSTEM_INFO si;
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
33 GetSystemInfo(&si);
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
34 return si.dwPageSize;
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 <unistd.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
40 #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
41 #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
42
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
43 #ifndef MS_SYNC
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
44 /* This is missing e.g. on SunOS 4.1.4 */
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
45 #define MS_SYNC 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
46 #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
47
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
48 #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
49 static int
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
50 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
51 {
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
52 return sysconf(_SC_PAGESIZE);
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
53 }
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
54 #else
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
55 #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
56 #endif
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
57
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
58 #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
59
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
60 #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
61 #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
62
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
63 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
64
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
65 typedef struct {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
66 PyObject_HEAD
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
67 char * data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
68 size_t size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
69 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
70
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
71 #ifdef MS_WIN32
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
72 HANDLE map_handle;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
73 HANDLE file_handle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
74 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
75 #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
76
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 #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
78 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
79 #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
80 } 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
81
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
82 static void
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
83 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
84 {
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 #ifdef MS_WIN32
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
86 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
87 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
88 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
89 CloseHandle (m_obj->map_handle);
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
90 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
91 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
92 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
93 PyMem_Free(m_obj->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
94 #endif /* MS_WIN32 */
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
95
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
96 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
97 if (m_obj->data!=NULL) {
12944
b1ec49b2c5c2 Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents: 12941
diff changeset
98 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
99 munmap(m_obj->data, m_obj->size);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
100 }
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
101 #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
102
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
103 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
104 }
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
105
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 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
107 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
108 {
12846
b58b9033c65a Use PyArg_ParseTuple and specify the method names, following a suggestion
Andrew M. Kuchling <amk@amk.ca>
parents: 12845
diff changeset
109 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
110 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
111 #ifdef MS_WIN32
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
112 /* For each resource we maintain, we need to check
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
113 the value is valid, and if so, free the resource
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
114 and set the member value to an invalid value so
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
115 the dealloc does not attempt to resource clearing
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
116 again.
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
117 TODO - should we check for errors in the close operations???
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
118 */
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
119 if (self->data != NULL) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
120 UnmapViewOfFile (self->data);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
121 self->data = NULL;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
122 }
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
123 if (self->map_handle != INVALID_HANDLE_VALUE) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
124 CloseHandle (self->map_handle);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
125 self->map_handle = INVALID_HANDLE_VALUE;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
126 }
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
127 if (self->file_handle != INVALID_HANDLE_VALUE) {
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
128 CloseHandle (self->file_handle);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
129 self->file_handle = INVALID_HANDLE_VALUE;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
130 }
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
131 #endif /* MS_WIN32 */
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
132
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
133 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
134 munmap(self->data, self->size);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
135 self->data = 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
136 #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
137
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
138 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
139 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
140 }
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
141
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
142 #ifdef MS_WIN32
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
143 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
144 do { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
145 if (!self->map_handle) { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
146 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
147 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
148 } \
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
149 } 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
150 #endif /* MS_WIN32 */
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
151
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
152 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
153 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
154 do { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
155 if (self->data == NULL) { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
156 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
157 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
158 } \
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
159 } 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
160 #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
161
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
162 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
163 mmap_read_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
164 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
165 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
166 char value;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
167 char *where;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
168 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
169 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
170 return NULL;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
171 if (self->pos < self->size) {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
172 where = self->data + self->pos;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
173 value = (char) *(where);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
174 self->pos += 1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
175 return Py_BuildValue("c", (char) *(where));
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
176 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
177 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
178 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
179 }
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
180 }
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
181
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
182 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
183 mmap_read_line_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
184 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
185 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
186 char *start = self->data+self->pos;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
187 char *eof = self->data+self->size;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
188 char *eol;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
189 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
190
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
191 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
192 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
193 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
194
12231
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
195 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
196 if (!eol)
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
197 eol = eof;
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
198 else
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
199 ++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
200 newline. */
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
201 result = PyString_FromStringAndSize(start, (eol - start));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
202 self->pos += (eol - start);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
203 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
204 }
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
205
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
206 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
207 mmap_read_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
208 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
209 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
210 long num_bytes;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
211 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
212
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
213 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
214 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
215 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
216
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
217 /* silently 'adjust' out-of-range requests */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
218 if ((self->pos + num_bytes) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
219 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
220 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
221 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
222 self->pos += num_bytes;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
223 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
224 }
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
225
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
226 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
227 mmap_find_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
228 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
229 {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
230 int start = self->pos;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
231 char *needle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
232 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
233
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
234 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
235 if (!PyArg_ParseTuple (args, "s#|i:find", &needle, &len, &start)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
236 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
237 } else {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
238 char *p = self->data+self->pos;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
239 char *e = self->data+self->size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
240 while (p < e) {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
241 char *s = p;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
242 char *n = needle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
243 while ((s<e) && (*n) && !(*s-*n)) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
244 s++, n++;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
245 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
246 if (!*n) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
247 return Py_BuildValue (
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
248 "i",
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
249 (int) (p - (self->data + start)));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
250 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
251 p++;
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
252 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
253 return Py_BuildValue ("l", (long) -1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
254 }
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
255 }
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
256
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
257 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
258 mmap_write_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
259 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
260 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
261 long length;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
262 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
263
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
264 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
265 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
266 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
267
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
268 if ((self->pos + length) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
269 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
270 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
271 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
272 memcpy (self->data+self->pos, data, length);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
273 self->pos = self->pos+length;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
274 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
275 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
276 }
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
277
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
278 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
279 mmap_write_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
280 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
281 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
282 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
283
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
284 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
285 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
286 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
287
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
288 *(self->data+self->pos) = value;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
289 self->pos += 1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
290 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
291 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
292 }
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
293
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
294 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
295 mmap_size_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
296 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
297 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
298 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
299 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
300 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
301
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
302 #ifdef MS_WIN32
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
303 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
304 return (Py_BuildValue (
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
305 "l", (long)
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
306 GetFileSize (self->file_handle, NULL)));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
307 } else {
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
308 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
309 }
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
310 #endif /* MS_WIN32 */
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
311
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
312 #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
313 {
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
314 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
315 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
316 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
317 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
318 }
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
319 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
320 }
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
321 #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
322 }
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
323
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
324 /* 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
325 / 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
326 / 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
327 /
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 / 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
329 / 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
330 / 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
331 */
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
332
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
333 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
334 mmap_resize_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
335 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
336 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
337 unsigned long new_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
338 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
339 if (!PyArg_ParseTuple (args, "l:resize", &new_size)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
340 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
341 #ifdef MS_WIN32
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
342 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
343 DWORD dwErrCode = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
344 /* First, unmap the file view */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
345 UnmapViewOfFile (self->data);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
346 /* Close the mapping object */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
347 CloseHandle (self->map_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
348 /* Move to the desired EOF position */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
349 SetFilePointer (self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
350 new_size, NULL, FILE_BEGIN);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
351 /* Change the size of the file */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
352 SetEndOfFile (self->file_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
353 /* 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
354 self->map_handle = CreateFileMapping (
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
355 self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
356 NULL,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
357 PAGE_READWRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
358 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
359 new_size,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
360 self->tagname);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
361 if (self->map_handle != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
362 self->data = (char *) MapViewOfFile (self->map_handle,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
363 FILE_MAP_WRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
364 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
365 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
366 0);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
367 if (self->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
368 self->size = new_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
369 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
370 return Py_None;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
371 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
372 dwErrCode = GetLastError();
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
373 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
374 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
375 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
376 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
377 PyErr_SetFromWindowsErr(dwErrCode);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
378 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
379 #endif /* MS_WIN32 */
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
380
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
381 #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
382 #ifndef 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
383 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
384 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
385 "mmap: resizing not available--no mremap()");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
386 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
387 #else
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
388 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
389 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
390
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
391 #ifdef MREMAP_MAYMOVE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
392 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
393 #else
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
394 newmap = mremap(self->data, self->size, new_size, 0);
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
395 #endif
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
396 if (newmap == (void *)-1)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
397 {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
398 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
399 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
400 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
401 self->data = newmap;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
402 self->size = new_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
403 Py_INCREF(Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
404 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
405 #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
406 #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
407 }
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
408 }
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
409
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
410 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
411 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
412 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
413 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
414 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
415 return NULL;
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
416 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
417 }
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
418
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
419 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
420 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
421 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
422 size_t offset = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
423 size_t size = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
424 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
425 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
426 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
427 } else if ((offset + size) > self->size) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
428 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
429 "flush values out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
430 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
431 } else {
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
432 #ifdef MS_WIN32
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
433 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
434 FlushViewOfFile(self->data+offset, 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
435 #endif /* MS_WIN32 */
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
436 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
437 /* XXX semantics of return value? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
438 /* XXX flags for msync? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
439 if (-1 == msync(self->data + offset, size,
12944
b1ec49b2c5c2 Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents: 12941
diff changeset
440 MS_SYNC))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
441 {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
442 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
443 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
444 }
13463
b7066bb79581 - added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents: 13441
diff changeset
445 return Py_BuildValue ("l", (long) 0);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
446 #endif /* UNIX */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
447 }
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
448 }
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
449
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
450 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
451 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
452 {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
453 int dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
454 int how=0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
455 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
456 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
457 return(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
458 } else {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
459 size_t where;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
460 switch (how) {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
461 case 0: /* relative to start */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
462 if (dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
463 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
464 where = dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
465 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
466 case 1: /* relative to current position */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
467 if ((int)self->pos + dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
468 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
469 where = self->pos + dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
470 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
471 case 2: /* relative to end */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
472 if ((int)self->size + dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
473 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
474 where = self->size + dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
475 break;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
476 default:
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
477 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
478 "unknown seek type");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
479 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
480 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
481 if (where > self->size)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
482 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
483 self->pos = where;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
484 Py_INCREF (Py_None);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
485 return (Py_None);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
486 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
487
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
488 onoutofrange:
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
489 PyErr_SetString (PyExc_ValueError, "seek out of range");
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
490 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
491 }
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
492
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
493 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
494 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
495 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
496 unsigned long dest, src, count;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
497 CHECK_VALID(NULL);
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
498 if (!PyArg_ParseTuple (args, "iii:move", &dest, &src, &count)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
499 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
500 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
501 /* bounds check the values */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
502 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
503 ((src+count) > self->size)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
504 /* dest will fit? */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
505 || (dest+count > self->size)) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
506 PyErr_SetString (PyExc_ValueError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
507 "source or destination out of range");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
508 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
509 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
510 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
511 Py_INCREF (Py_None);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
512 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
513 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
514 }
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
515 }
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
516
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
517 static struct PyMethodDef mmap_object_methods[] = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
518 {"close", (PyCFunction) mmap_close_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
519 {"find", (PyCFunction) mmap_find_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
520 {"flush", (PyCFunction) mmap_flush_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
521 {"move", (PyCFunction) mmap_move_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
522 {"read", (PyCFunction) mmap_read_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
523 {"read_byte", (PyCFunction) mmap_read_byte_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
524 {"readline", (PyCFunction) mmap_read_line_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
525 {"resize", (PyCFunction) mmap_resize_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
526 {"seek", (PyCFunction) mmap_seek_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
527 {"size", (PyCFunction) mmap_size_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
528 {"tell", (PyCFunction) mmap_tell_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
529 {"write", (PyCFunction) mmap_write_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
530 {"write_byte", (PyCFunction) mmap_write_byte_method, 1},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
531 {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
532 };
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
533
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
534 /* 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
535
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
536 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
537 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
538 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
539 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
540 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
541 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
542 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
543 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
544 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
545 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
546 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
547 }
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
548
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 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
550 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
551 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
552 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
553 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
554 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
555 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
556 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
557 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
558 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
559 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
560 }
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
561
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
562 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
563 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
564 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
565 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
566 if (lenp)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
567 *lenp = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
568 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
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
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 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
572 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
573 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
574 if ( index != 0 ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
575 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
576 "accessing non-existent buffer segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
577 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
578 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
579 *ptr = (const char *)self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
580 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
581 }
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
582
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 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
584 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
585 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
586 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
587 }
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
588
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
589 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
590 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
591 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
592 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
593 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
594 }
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
595
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
596 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
597 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
598 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
599 CHECK_VALID(NULL);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
600 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
601 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
602 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
603 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
604 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
605 }
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
606
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 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
608 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
609 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
610 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
611 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
612 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
613 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
614 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
615 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
616 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
617 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
618 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
619 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
620 ihigh = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
621
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
622 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
623 }
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
624
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 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
626 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
627 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
628 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
629 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
630 "mmaps don't support concatenation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
631 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
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_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
636 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
637 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
638 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
639 "mmaps don't support repeat operation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
640 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
641 }
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
642
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 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
644 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
645 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
646 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
647
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
648 CHECK_VALID(-1);
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 if (! (PyString_Check(v)) ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
661 PyErr_SetString(PyExc_IndexError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
662 "mmap slice assignment must be a string");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
663 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
664 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
665 if ( PyString_Size(v) != (ihigh - ilow) ) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
666 PyErr_SetString(PyExc_IndexError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
667 "mmap slice assignment is wrong size");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
668 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
669 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
670 buf = PyString_AsString(v);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
671 memcpy(self->data + ilow, buf, ihigh-ilow);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
672 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
673 }
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
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
675 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
676 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
677 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
678 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
679
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
680 CHECK_VALID(-1);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
681 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
682 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
683 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
684 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
685 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
686 PyErr_SetString(PyExc_IndexError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
687 "mmap assignment must be single-character string");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
688 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
689 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
690 buf = PyString_AsString(v);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
691 self->data[i] = buf[0];
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
692 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
693 }
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
694
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
695 static PySequenceMethods mmap_as_sequence = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
696 (inquiry)mmap_length, /*sq_length*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
697 (binaryfunc)mmap_concat, /*sq_concat*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
698 (intargfunc)mmap_repeat, /*sq_repeat*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
699 (intargfunc)mmap_item, /*sq_item*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
700 (intintargfunc)mmap_slice, /*sq_slice*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
701 (intobjargproc)mmap_ass_item, /*sq_ass_item*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
702 (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
703 };
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
704
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
705 static PyBufferProcs mmap_as_buffer = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
706 (getreadbufferproc)mmap_buffer_getreadbuf,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
707 (getwritebufferproc)mmap_buffer_getwritebuf,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
708 (getsegcountproc)mmap_buffer_getsegcount,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
709 (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
710 };
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
711
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
712 static PyTypeObject mmap_object_type = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
713 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
714 0, /* ob_size */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
715 "mmap", /* tp_name */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
716 sizeof(mmap_object), /* tp_size */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
717 0, /* tp_itemsize */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
718 /* methods */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
719 (destructor) mmap_object_dealloc, /* tp_dealloc */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
720 0, /* tp_print */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
721 (getattrfunc) mmap_object_getattr, /* tp_getattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
722 0, /* tp_setattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
723 0, /* tp_compare */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
724 0, /* tp_repr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
725 0, /* tp_as_number */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
726 &mmap_as_sequence, /*tp_as_sequence*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
727 0, /*tp_as_mapping*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
728 0, /*tp_hash*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
729 0, /*tp_call*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
730 0, /*tp_str*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
731 0, /*tp_getattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
732 0, /*tp_setattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
733 &mmap_as_buffer, /*tp_as_buffer*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
734 Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
735 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
736 };
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
737
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
738
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
739 /* extract the map size from the given PyObject
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
740
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
741 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
742 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
743 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
744 (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
745
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
746 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
747 success, the map size is returned. */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
748 static int
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
749 _GetMapSize(PyObject *o)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
750 {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
751 if (PyInt_Check(o)) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
752 long i = PyInt_AsLong(o);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
753 if (PyErr_Occurred())
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
754 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
755 if (i < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
756 goto onnegoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
757 if (i > INT_MAX)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
758 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
759 return (int)i;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
760 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
761 else if (PyLong_Check(o)) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
762 long i = PyLong_AsLong(o);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
763 if (PyErr_Occurred()) {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
764 /* yes negative overflow is mistaken for positive overflow
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
765 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
766 if (PyErr_ExceptionMatches(PyExc_OverflowError))
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
767 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
768 else
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
769 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
770 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
771 if (i < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
772 goto onnegoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
773 if (i > INT_MAX)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
774 goto onposoverflow;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
775 return (int)i;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
776 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
777 else {
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
778 PyErr_SetString(PyExc_TypeError,
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
779 "map size must be an integral value");
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
780 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
781 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
782
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
783 onnegoverflow:
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
784 PyErr_SetString(PyExc_OverflowError,
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
785 "memory mapped size must be positive");
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
786 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
787
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
788 onposoverflow:
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
789 PyErr_SetString(PyExc_OverflowError,
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
790 "memory mapped size is too large (limited by C int)");
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
791 return -1;
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
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
794 #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
795 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
796 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
797 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
798 mmap_object *m_obj;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
799 PyObject *map_size_obj = NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
800 int map_size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
801 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
802 char *keywords[] = {"file", "size", "flags", "prot", 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
803
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
804 if (!PyArg_ParseTupleAndKeywords(args, kwdict,
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
805 "iO|ii", keywords,
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
806 &fd, &map_size_obj, &flags, &prot)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
807 )
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
808 return NULL;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
809 map_size = _GetMapSize(map_size_obj);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
810 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
811 return NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
812
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
813 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
814 if (m_obj == NULL) {return NULL;}
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
815 m_obj->size = (size_t) map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
816 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
817 m_obj->fd = fd;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
818 m_obj->data = mmap(NULL, map_size,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
819 prot, flags,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
820 fd, 0);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
821 if (m_obj->data == (void *)-1)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
822 {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
823 Py_DECREF(m_obj);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
824 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
825 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
826 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
827 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
828 }
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
829 #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
830
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
831 #ifdef MS_WIN32
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
832 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
833 new_mmap_object(PyObject *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
834 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
835 mmap_object *m_obj;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
836 PyObject *map_size_obj = NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
837 int map_size;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
838 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
839
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
840 DWORD dwErr = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
841 int fileno;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
842 HANDLE fh = 0;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
843
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
844 if (!PyArg_ParseTuple(args,
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
845 "iO|z",
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
846 &fileno,
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
847 &map_size_obj,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
848 &tagname)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
849 )
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
850 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
851
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
852 map_size = _GetMapSize(map_size_obj);
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
853 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
854 return NULL;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
855
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
856 /* if an actual filename has been specified */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
857 if (fileno != 0) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
858 fh = (HANDLE)_get_osfhandle(fileno);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
859 if (fh==(HANDLE)-1) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
860 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
861 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
862 }
12241
7e9a5cf55c76 Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents: 12231
diff changeset
863 /* Win9x appears to need us seeked to zero */
7e9a5cf55c76 Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents: 12231
diff changeset
864 fseek(&_iob[fileno], 0, SEEK_SET);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
865 }
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
866
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12361
diff changeset
867 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
868 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
869 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
870 /* 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
871 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
872 m_obj->data = NULL;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
873 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
874 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
875 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
876
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
877 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
878 /* 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
879 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
880 if (!DuplicateHandle(
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
881 GetCurrentProcess(), /* source process handle */
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
882 fh, /* handle to be duplicated */
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
883 GetCurrentProcess(), /* target proc handle */
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
884 (LPHANDLE)&m_obj->file_handle, /* result */
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
885 0, /* access - ignored due to options 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
886 FALSE, /* inherited by child processes? */
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
887 DUPLICATE_SAME_ACCESS)) { /* options */
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
888 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
889 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
890 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
891 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
892 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
893 if (!map_size) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
894 m_obj->size = GetFileSize (fh, NULL);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
895 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
896 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
897 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
898 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
899 else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
900 m_obj->size = map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
901 }
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
902
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
903 /* set the initial position */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
904 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
905
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
906 /* 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
907 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
908 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
909 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
910 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
911 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
912 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
913 }
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
914 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
915 }
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
916 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
917 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
918
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
919 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
920 NULL,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
921 PAGE_READWRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
922 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
923 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
924 m_obj->tagname);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
925 if (m_obj->map_handle != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
926 m_obj->data = (char *) MapViewOfFile (m_obj->map_handle,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
927 FILE_MAP_WRITE,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
928 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
929 0,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
930 0);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
931 if (m_obj->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
932 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
933 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
934 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
935 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
936 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
937 dwErr = GetLastError();
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
938 }
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
939 Py_DECREF(m_obj);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
940 PyErr_SetFromWindowsErr(dwErr);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
941 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
942 }
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
943 #endif /* MS_WIN32 */
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
944
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
945 /* 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
946 static struct PyMethodDef mmap_functions[] = {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
947 {"mmap", (PyCFunction) new_mmap_object,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
948 METH_VARARGS|METH_KEYWORDS},
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
949 {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
950 };
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
951
15803
9b6814bfb2c0 Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents: 15794
diff changeset
952 DL_EXPORT(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
953 initmmap(void)
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
954 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
955 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
956
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
957 /* 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
958 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
959
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
960 module = Py_InitModule ("mmap", mmap_functions);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
961 dict = PyModule_GetDict (module);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
962 mmap_module_error = PyExc_EnvironmentError;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
963 Py_INCREF(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
964 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
965 #ifdef PROT_EXEC
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
966 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
967 #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
968 #ifdef PROT_READ
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
969 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
970 #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
971 #ifdef PROT_WRITE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
972 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
973 #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
974
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
975 #ifdef MAP_SHARED
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
976 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
977 #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
978 #ifdef MAP_PRIVATE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
979 PyDict_SetItemString (dict, "MAP_PRIVATE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
980 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
981 #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
982 #ifdef MAP_DENYWRITE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
983 PyDict_SetItemString (dict, "MAP_DENYWRITE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
984 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
985 #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
986 #ifdef MAP_EXECUTABLE
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
987 PyDict_SetItemString (dict, "MAP_EXECUTABLE",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
988 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
989 #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
990 #ifdef MAP_ANON
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
991 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
992 PyDict_SetItemString (dict, "MAP_ANONYMOUS",
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
993 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
994 #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
995
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
996 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
997 PyInt_FromLong( (long)my_getpagesize() ) );
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
998 }
12845
229fd3a5afa0 Add missing PyArg_NoArgs() calls to methods that didn't take arguments
Andrew M. Kuchling <amk@amk.ca>
parents: 12576
diff changeset
999