annotate Modules/mmapmodule.c @ 28646:193b8aec00ce legacy-trunk

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