Mercurial > cpython-lru_cache-weakref
annotate Modules/mmapmodule.c @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 8529e558f71d |
| children | 6ae361df870e |
| 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 |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
23 #ifndef MS_WIN32 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
24 #define UNIX |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
25 #endif |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
26 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
27 #ifdef MS_WIN32 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
28 #include <windows.h> |
|
14956
c78e76a571b6
my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents:
14836
diff
changeset
|
29 static int |
|
c78e76a571b6
my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents:
14836
diff
changeset
|
30 my_getpagesize(void) |
|
c78e76a571b6
my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents:
14836
diff
changeset
|
31 { |
|
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 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
78 #ifdef MS_WIN32 |
|
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 { |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
95 #ifdef MS_WIN32 |
|
13757
67b9b05ce5a5
Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13620
diff
changeset
|
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); |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
104 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
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; |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
121 #ifdef MS_WIN32 |
|
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 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
141 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
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 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
144 munmap(self->data, self->size); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
145 self->data = NULL; |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
146 #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
|
147 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
148 Py_INCREF (Py_None); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
149 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
|
150 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
151 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
152 #ifdef MS_WIN32 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
153 #define CHECK_VALID(err) \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
154 do { \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
155 if (!self->map_handle) { \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
156 PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
157 return err; \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
158 } \ |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
159 } while (0) |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
160 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
161 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
162 #ifdef UNIX |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
163 #define CHECK_VALID(err) \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
164 do { \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
165 if (self->data == NULL) { \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
166 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
|
167 return err; \ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
168 } \ |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
169 } 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
|
170 #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
|
171 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
173 mmap_read_byte_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
174 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
|
175 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
176 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
|
177 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
|
178 return NULL; |
| 12945 | 179 if (self->pos < self->size) { |
|
17735
147632ad1bcc
Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents:
17600
diff
changeset
|
180 char value = self->data[self->pos]; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
181 self->pos += 1; |
|
17735
147632ad1bcc
Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents:
17600
diff
changeset
|
182 return Py_BuildValue("c", value); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
183 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
184 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
|
185 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
186 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
187 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
188 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
190 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
|
191 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
|
192 { |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
193 char *start = self->data+self->pos; |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
194 char *eof = self->data+self->size; |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
195 char *eol; |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
196 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
|
197 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
198 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
|
199 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
|
200 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
|
201 |
|
12231
074c30ff3206
Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents:
12178
diff
changeset
|
202 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
|
203 if (!eol) |
|
074c30ff3206
Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents:
12178
diff
changeset
|
204 eol = eof; |
|
074c30ff3206
Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents:
12178
diff
changeset
|
205 else |
|
074c30ff3206
Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents:
12178
diff
changeset
|
206 ++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
|
207 newline. */ |
| 12945 | 208 result = PyString_FromStringAndSize(start, (eol - start)); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
209 self->pos += (eol - start); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
210 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
|
211 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
212 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
214 mmap_read_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
215 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
|
216 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
217 long num_bytes; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
218 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
|
219 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
220 CHECK_VALID(NULL); |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
221 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
|
222 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
|
223 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
224 /* silently 'adjust' out-of-range requests */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
225 if ((self->pos + num_bytes) > self->size) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
226 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
|
227 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
228 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
|
229 self->pos += num_bytes; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
230 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
|
231 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
232 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
234 mmap_find_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
235 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
|
236 { |
|
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
|
237 long start = self->pos; |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
238 char *needle; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
239 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
|
240 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
241 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
|
242 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
|
243 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
244 } else { |
|
17821
780248cd1d9a
Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents:
17735
diff
changeset
|
245 char *p; |
|
780248cd1d9a
Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents:
17735
diff
changeset
|
246 char *e = self->data + self->size; |
|
780248cd1d9a
Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents:
17735
diff
changeset
|
247 |
|
780248cd1d9a
Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents:
17735
diff
changeset
|
248 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
|
249 start += self->size; |
|
17821
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 = 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
|
252 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
|
253 start = self->size; |
|
17821
780248cd1d9a
Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents:
17735
diff
changeset
|
254 |
|
22180
266334a5c4ca
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21713
diff
changeset
|
255 for (p = self->data + start; p + len <= e; ++p) { |
|
266334a5c4ca
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21713
diff
changeset
|
256 int i; |
|
266334a5c4ca
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21713
diff
changeset
|
257 for (i = 0; i < len && needle[i] == p[i]; ++i) |
|
266334a5c4ca
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21713
diff
changeset
|
258 /* nothing */; |
|
266334a5c4ca
backport tim_one's checkin of
Michael W. Hudson <mwh@python.net>
parents:
21713
diff
changeset
|
259 if (i == len) { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
260 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
|
261 "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
|
262 (long) (p - self->data)); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
263 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
264 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
265 return Py_BuildValue ("l", (long) -1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
266 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
267 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
268 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
269 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
|
270 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
|
271 { |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 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
|
276 } |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
277 |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
278 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
|
279 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
|
280 { |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 "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
|
285 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
|
286 } |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
287 |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
288 |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
289 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
290 mmap_write_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
291 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
|
292 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
293 long length; |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
294 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
|
295 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
296 CHECK_VALID(NULL); |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
297 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
|
298 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
|
299 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
300 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
|
301 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
|
302 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
303 if ((self->pos + length) > self->size) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
304 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
|
305 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
306 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
307 memcpy (self->data+self->pos, data, length); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
308 self->pos = self->pos+length; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
309 Py_INCREF (Py_None); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
310 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
|
311 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
312 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
314 mmap_write_byte_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
315 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
|
316 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
317 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
|
318 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
319 CHECK_VALID(NULL); |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
320 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
|
321 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
|
322 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
323 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
|
324 return NULL; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
325 *(self->data+self->pos) = value; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
326 self->pos += 1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
327 Py_INCREF (Py_None); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
328 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
|
329 } |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
330 |
|
12120
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
332 mmap_size_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
333 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
|
334 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
335 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
|
336 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
|
337 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
|
338 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
339 #ifdef MS_WIN32 |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
340 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
|
341 return (Py_BuildValue ( |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
342 "l", (long) |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
343 GetFileSize (self->file_handle, NULL))); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
344 } else { |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
345 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
|
346 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
347 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
348 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
349 #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
|
350 { |
|
8c9554edbebb
Fix the size() method to return the size of the file on Unix, not the
Andrew M. Kuchling <amk@amk.ca>
parents:
12846
diff
changeset
|
351 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
|
352 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
|
353 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
|
354 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
|
355 } |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
356 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
|
357 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
358 #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
|
359 } |
|
ec58b503b670
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 |
|
ec58b503b670
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 /* 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
|
362 / 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
|
363 / 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
|
364 / |
|
ec58b503b670
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 / 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
|
366 / 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
|
367 / 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
|
368 */ |
|
ec58b503b670
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 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
371 mmap_resize_method(mmap_object *self, |
|
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
372 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
|
373 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
374 unsigned long new_size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
375 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
|
376 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
|
377 !is_resizeable(self)) { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
378 return NULL; |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
379 #ifdef MS_WIN32 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
380 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
381 DWORD dwErrCode = 0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
382 /* First, unmap the file view */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
383 UnmapViewOfFile (self->data); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
384 /* Close the mapping object */ |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
385 CloseHandle (self->map_handle); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
386 /* Move to the desired EOF position */ |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
387 SetFilePointer (self->file_handle, |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
388 new_size, NULL, FILE_BEGIN); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
389 /* Change the size of the file */ |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
390 SetEndOfFile (self->file_handle); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
391 /* 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
|
392 self->map_handle = CreateFileMapping ( |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
393 self->file_handle, |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
394 NULL, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
395 PAGE_READWRITE, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
396 0, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
397 new_size, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
398 self->tagname); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
399 if (self->map_handle != NULL) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
400 self->data = (char *) MapViewOfFile (self->map_handle, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
401 FILE_MAP_WRITE, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
402 0, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
403 0, |
|
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 if (self->data != NULL) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
406 self->size = new_size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
407 Py_INCREF (Py_None); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
408 return Py_None; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
409 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
410 dwErrCode = GetLastError(); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
411 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
412 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
413 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
|
414 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
415 PyErr_SetFromWindowsErr(dwErrCode); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
416 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
|
417 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
418 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
419 #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
|
420 #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
|
421 } else { |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
422 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
|
423 "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
|
424 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
|
425 #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
|
426 } else { |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
427 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
|
428 |
|
12948
acb0261b6942
Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents:
12945
diff
changeset
|
429 #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
|
430 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
|
431 #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
|
432 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
|
433 #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
|
434 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
|
435 { |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
436 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
|
437 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
|
438 } |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
439 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
|
440 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
|
441 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
|
442 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
|
443 #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
|
444 #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
|
445 } |
|
12120
ec58b503b670
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 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
447 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
449 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
|
450 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
451 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
|
452 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
|
453 return NULL; |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
454 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
|
455 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
456 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
458 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
|
459 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
460 size_t offset = 0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
461 size_t size = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
462 CHECK_VALID(NULL); |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
463 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
|
464 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
465 } else if ((offset + size) > self->size) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
466 PyErr_SetString (PyExc_ValueError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
467 "flush values out of range"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
468 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
469 } else { |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
470 #ifdef MS_WIN32 |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
471 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
|
472 FlushViewOfFile(self->data+offset, size))); |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
473 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
474 #ifdef UNIX |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
475 /* XXX semantics of return value? */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
476 /* XXX flags for msync? */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
477 if (-1 == msync(self->data + offset, size, |
|
12944
b1ec49b2c5c2
Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents:
12941
diff
changeset
|
478 MS_SYNC)) |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
479 { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
480 PyErr_SetFromErrno(mmap_module_error); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
481 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
|
482 } |
|
13463
b7066bb79581
- added (long) casts to a couple of Py_BuildValue calls,
Fredrik Lundh <fredrik@pythonware.com>
parents:
13441
diff
changeset
|
483 return Py_BuildValue ("l", (long) 0); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
484 #endif /* UNIX */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
485 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
486 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
487 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
489 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
|
490 { |
| 12945 | 491 int dist; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
492 int how=0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
493 CHECK_VALID(NULL); |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
494 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
|
495 return(NULL); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
496 } else { |
| 12945 | 497 size_t where; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
498 switch (how) { |
| 12945 | 499 case 0: /* relative to start */ |
| 500 if (dist < 0) | |
| 501 goto onoutofrange; | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
502 where = dist; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
503 break; |
| 12945 | 504 case 1: /* relative to current position */ |
| 505 if ((int)self->pos + dist < 0) | |
| 506 goto onoutofrange; | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
507 where = self->pos + dist; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
508 break; |
| 12945 | 509 case 2: /* relative to end */ |
| 510 if ((int)self->size + dist < 0) | |
| 511 goto onoutofrange; | |
| 512 where = self->size + dist; | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
513 break; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
514 default: |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
515 PyErr_SetString (PyExc_ValueError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
516 "unknown seek type"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
517 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
518 } |
| 12945 | 519 if (where > self->size) |
| 520 goto onoutofrange; | |
| 521 self->pos = where; | |
| 522 Py_INCREF (Py_None); | |
| 523 return (Py_None); | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
524 } |
| 12945 | 525 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
526 onoutofrange: |
| 12945 | 527 PyErr_SetString (PyExc_ValueError, "seek out of range"); |
| 528 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
|
529 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
530 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
532 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
|
533 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
534 unsigned long dest, src, count; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
535 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
|
536 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
|
537 !is_writeable(self)) { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
538 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
539 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
540 /* bounds check the values */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
541 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
|
542 ((src+count) > self->size) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
543 /* dest will fit? */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
544 || (dest+count > self->size)) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
545 PyErr_SetString (PyExc_ValueError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
546 "source or destination out of range"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
547 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
|
548 } else { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
549 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
|
550 Py_INCREF (Py_None); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
551 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
|
552 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
553 } |
|
12120
ec58b503b670
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 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
555 |
|
ec58b503b670
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 static struct PyMethodDef mmap_object_methods[] = { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
557 {"close", (PyCFunction) mmap_close_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
558 {"find", (PyCFunction) mmap_find_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
559 {"flush", (PyCFunction) mmap_flush_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
560 {"move", (PyCFunction) mmap_move_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
561 {"read", (PyCFunction) mmap_read_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
562 {"read_byte", (PyCFunction) mmap_read_byte_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
563 {"readline", (PyCFunction) mmap_read_line_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
564 {"resize", (PyCFunction) mmap_resize_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
565 {"seek", (PyCFunction) mmap_seek_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
566 {"size", (PyCFunction) mmap_size_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
567 {"tell", (PyCFunction) mmap_tell_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
568 {"write", (PyCFunction) mmap_write_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
569 {"write_byte", (PyCFunction) mmap_write_byte_method, 1}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
570 {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
|
571 }; |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
572 |
|
ec58b503b670
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 /* 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
|
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 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
|
576 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
|
577 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
578 CHECK_VALID(-1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
579 if ( index != 0 ) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
580 PyErr_SetString(PyExc_SystemError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
581 "Accessing non-existent mmap segment"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
582 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
583 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
584 *ptr = self->data; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
585 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
|
586 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
587 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
588 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
589 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
|
590 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
591 CHECK_VALID(-1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
592 if ( index != 0 ) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
593 PyErr_SetString(PyExc_SystemError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
594 "Accessing non-existent mmap segment"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
595 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
596 } |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
597 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
|
598 return -1; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
599 *ptr = self->data; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
600 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
|
601 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
602 |
|
ec58b503b670
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 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
604 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
|
605 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
606 CHECK_VALID(-1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
607 if (lenp) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
608 *lenp = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
609 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
|
610 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
611 |
|
ec58b503b670
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 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
613 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
|
614 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
615 if ( index != 0 ) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
616 PyErr_SetString(PyExc_SystemError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
617 "accessing non-existent buffer segment"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
618 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
619 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
620 *ptr = (const char *)self->data; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
621 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
|
622 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
623 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
624 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
625 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
|
626 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
627 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
|
628 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
629 |
|
ec58b503b670
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 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
631 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
|
632 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
633 CHECK_VALID(-1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
634 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
|
635 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
636 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
638 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
|
639 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
640 CHECK_VALID(NULL); |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
641 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
|
642 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
|
643 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
644 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
645 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
|
646 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
647 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
649 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
|
650 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
651 CHECK_VALID(NULL); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
652 if (ilow < 0) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
653 ilow = 0; |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
654 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
|
655 ilow = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
656 if (ihigh < 0) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
657 ihigh = 0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
658 if (ihigh < ilow) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
659 ihigh = ilow; |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
660 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
|
661 ihigh = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
662 |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
663 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
|
664 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
665 |
|
ec58b503b670
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 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
667 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
|
668 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
669 CHECK_VALID(NULL); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
670 PyErr_SetString(PyExc_SystemError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
671 "mmaps don't support concatenation"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
672 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
|
673 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
674 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
675 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
676 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
|
677 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
678 CHECK_VALID(NULL); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
679 PyErr_SetString(PyExc_SystemError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
680 "mmaps don't support repeat operation"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
681 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
|
682 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
683 |
|
ec58b503b670
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 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
685 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
|
686 { |
|
12361
791a112be3bc
Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents:
12360
diff
changeset
|
687 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
|
688 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
689 CHECK_VALID(-1); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
690 if (ilow < 0) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
691 ilow = 0; |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
692 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
|
693 ilow = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
694 if (ihigh < 0) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
695 ihigh = 0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
696 if (ihigh < ilow) |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
697 ihigh = ilow; |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
698 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
|
699 ihigh = self->size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
700 |
|
18626
2b5e7383c77a
Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents:
17832
diff
changeset
|
701 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
|
702 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
|
703 "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
|
704 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
|
705 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
706 if (! (PyString_Check(v)) ) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
707 PyErr_SetString(PyExc_IndexError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
708 "mmap slice assignment must be a string"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
709 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
710 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
711 if ( PyString_Size(v) != (ihigh - ilow) ) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
712 PyErr_SetString(PyExc_IndexError, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
713 "mmap slice assignment is wrong size"); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
714 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
715 } |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
716 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
|
717 return -1; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
718 buf = PyString_AsString(v); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
719 memcpy(self->data + ilow, buf, ihigh-ilow); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
720 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
|
721 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
722 |
|
ec58b503b670
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 static int |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
724 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
|
725 { |
|
12361
791a112be3bc
Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents:
12360
diff
changeset
|
726 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
|
727 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
728 CHECK_VALID(-1); |
|
13264
b9adb79cdeb6
clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents:
12954
diff
changeset
|
729 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
|
730 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
|
731 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
732 } |
|
18626
2b5e7383c77a
Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents:
17832
diff
changeset
|
733 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
|
734 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
|
735 "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
|
736 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
|
737 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
738 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
|
739 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
|
740 "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
|
741 return -1; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
742 } |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
743 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
|
744 return -1; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
745 buf = PyString_AsString(v); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
746 self->data[i] = buf[0]; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
747 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
|
748 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
749 |
|
ec58b503b670
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 static PySequenceMethods mmap_as_sequence = { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
751 (inquiry)mmap_length, /*sq_length*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
752 (binaryfunc)mmap_concat, /*sq_concat*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
753 (intargfunc)mmap_repeat, /*sq_repeat*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
754 (intargfunc)mmap_item, /*sq_item*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
755 (intintargfunc)mmap_slice, /*sq_slice*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
756 (intobjargproc)mmap_ass_item, /*sq_ass_item*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
757 (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
|
758 }; |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
759 |
|
ec58b503b670
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 static PyBufferProcs mmap_as_buffer = { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
761 (getreadbufferproc)mmap_buffer_getreadbuf, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
762 (getwritebufferproc)mmap_buffer_getwritebuf, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
763 (getsegcountproc)mmap_buffer_getsegcount, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
764 (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
|
765 }; |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
766 |
|
ec58b503b670
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 static PyTypeObject mmap_object_type = { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
768 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
|
769 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
|
770 "mmap.mmap", /* tp_name */ |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
771 sizeof(mmap_object), /* tp_size */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
772 0, /* tp_itemsize */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
773 /* methods */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
774 (destructor) mmap_object_dealloc, /* tp_dealloc */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
775 0, /* tp_print */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
776 (getattrfunc) mmap_object_getattr, /* tp_getattr */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
777 0, /* tp_setattr */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
778 0, /* tp_compare */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
779 0, /* tp_repr */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
780 0, /* tp_as_number */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
781 &mmap_as_sequence, /*tp_as_sequence*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
782 0, /*tp_as_mapping*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
783 0, /*tp_hash*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
784 0, /*tp_call*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
785 0, /*tp_str*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
786 0, /*tp_getattro*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
787 0, /*tp_setattro*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
788 &mmap_as_buffer, /*tp_as_buffer*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
789 Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
790 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
|
791 }; |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
792 |
| 12945 | 793 |
| 794 /* extract the map size from the given PyObject | |
| 795 | |
| 796 The map size is restricted to [0, INT_MAX] because this is the current | |
| 797 Python limitation on object sizes. Although the mmap object *could* handle | |
| 798 a larger map size, there is no point because all the useful operations | |
| 799 (len(), slicing(), sequence indexing) are limited by a C int. | |
| 800 | |
|
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
|
801 Returns -1 on error, with an appropriate Python exception raised. On |
| 12945 | 802 success, the map size is returned. */ |
| 803 static int | |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
804 _GetMapSize(PyObject *o) |
| 12945 | 805 { |
| 806 if (PyInt_Check(o)) { | |
| 807 long i = PyInt_AsLong(o); | |
| 808 if (PyErr_Occurred()) | |
| 809 return -1; | |
| 810 if (i < 0) | |
| 811 goto onnegoverflow; | |
| 812 if (i > INT_MAX) | |
| 813 goto onposoverflow; | |
| 814 return (int)i; | |
| 815 } | |
| 816 else if (PyLong_Check(o)) { | |
| 817 long i = PyLong_AsLong(o); | |
| 818 if (PyErr_Occurred()) { | |
| 819 /* yes negative overflow is mistaken for positive overflow | |
| 820 but not worth the trouble to check sign of 'i' */ | |
| 821 if (PyErr_ExceptionMatches(PyExc_OverflowError)) | |
| 822 goto onposoverflow; | |
| 823 else | |
| 824 return -1; | |
| 825 } | |
| 826 if (i < 0) | |
| 827 goto onnegoverflow; | |
| 828 if (i > INT_MAX) | |
| 829 goto onposoverflow; | |
| 830 return (int)i; | |
| 831 } | |
| 832 else { | |
| 833 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
|
834 "map size must be an integral value"); |
| 12945 | 835 return -1; |
| 836 } | |
| 837 | |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
838 onnegoverflow: |
| 12945 | 839 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
|
840 "memory mapped size must be positive"); |
| 12945 | 841 return -1; |
| 842 | |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
843 onposoverflow: |
| 12945 | 844 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
|
845 "memory mapped size is too large (limited by C int)"); |
| 12945 | 846 return -1; |
| 847 } | |
| 848 | |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
849 #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
|
850 static PyObject * |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
851 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
|
852 { |
|
25131
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
853 #ifdef HAVE_FSTAT |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
854 struct stat st; |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
855 #endif |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
856 mmap_object *m_obj; |
| 12945 | 857 PyObject *map_size_obj = NULL; |
| 858 int map_size; | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
859 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
|
860 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
|
861 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
|
862 "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
|
863 "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
|
864 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
865 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
|
866 &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
|
867 return NULL; |
| 12945 | 868 map_size = _GetMapSize(map_size_obj); |
| 869 if (map_size < 0) | |
| 870 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
|
871 |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
872 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
|
873 ((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
|
874 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
|
875 "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
|
876 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
|
877 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
|
878 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
|
879 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
|
880 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
881 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
|
882 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
|
883 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
|
884 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
885 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
|
886 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
|
887 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
|
888 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
889 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
|
890 /* 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
|
891 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
892 default: |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
893 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
|
894 "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
|
895 } |
|
25131
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
896 |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
897 #ifdef HAVE_FSTAT |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
898 if (fstat(fd, &st) == 0 && (size_t)map_size > st.st_size) { |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
899 PyErr_SetString(PyExc_ValueError, |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
900 "mmap length is greater than file size"); |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
901 return NULL; |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
902 } |
|
8529e558f71d
Backport of SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents:
22180
diff
changeset
|
903 #endif |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12361
diff
changeset
|
904 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
|
905 if (m_obj == NULL) {return NULL;} |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
906 m_obj->size = (size_t) map_size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
907 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
|
908 m_obj->fd = fd; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
909 m_obj->data = mmap(NULL, map_size, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
910 prot, flags, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
911 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
|
912 if (m_obj->data == (char *)-1) { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
913 Py_DECREF(m_obj); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
914 PyErr_SetFromErrno(mmap_module_error); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
915 return NULL; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
916 } |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
917 m_obj->access = access; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
918 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
|
919 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
920 #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
|
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 #ifdef MS_WIN32 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
923 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
|
924 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
|
925 { |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
926 mmap_object *m_obj; |
| 12945 | 927 PyObject *map_size_obj = NULL; |
| 928 int map_size; | |
|
13441
5edee6878943
-- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents:
13359
diff
changeset
|
929 char *tagname = ""; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
930 DWORD dwErr = 0; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
931 int fileno; |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
932 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
|
933 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
|
934 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
|
935 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
|
936 "tagname", |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
937 "access", NULL }; |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
938 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
939 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
|
940 &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
|
941 &tagname, &access)) { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
942 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
|
943 } |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
944 |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
945 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
|
946 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
|
947 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
|
948 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
|
949 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
950 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
|
951 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
|
952 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
|
953 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
954 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
|
955 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
|
956 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
|
957 break; |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
958 default: |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
959 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
|
960 "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
|
961 } |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
962 |
| 12945 | 963 map_size = _GetMapSize(map_size_obj); |
| 964 if (map_size < 0) | |
| 965 return NULL; | |
| 966 | |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
967 /* if an actual filename has been specified */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
968 if (fileno != 0) { |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
969 fh = (HANDLE)_get_osfhandle(fileno); |
|
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
970 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
|
971 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
|
972 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
|
973 } |
|
12241
7e9a5cf55c76
Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents:
12231
diff
changeset
|
974 /* Win9x appears to need us seeked to zero */ |
|
7e9a5cf55c76
Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents:
12231
diff
changeset
|
975 fseek(&_iob[fileno], 0, SEEK_SET); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
976 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
977 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12361
diff
changeset
|
978 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
|
979 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
|
980 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
|
981 /* 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
|
982 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
|
983 m_obj->data = NULL; |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
984 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
|
985 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
|
986 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
|
987 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
988 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
|
989 /* 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
|
990 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
|
991 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
|
992 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
|
993 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
|
994 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
|
995 (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
|
996 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
|
997 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
|
998 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
|
999 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
|
1000 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
|
1001 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
|
1002 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
|
1003 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1004 if (!map_size) { |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
1005 m_obj->size = GetFileSize (fh, NULL); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1006 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1007 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
|
1008 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1009 } |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1010 else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1011 m_obj->size = map_size; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1012 } |
|
12120
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1013 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1014 /* set the initial position */ |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1015 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
|
1016 |
|
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
|
1017 /* 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
|
1018 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
|
1019 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
|
1020 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
|
1021 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
|
1022 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
|
1023 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
|
1024 } |
|
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 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
|
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 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
|
1028 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
|
1029 |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
1030 m_obj->access = access; |
|
13758
bc37e6a335f2
More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents:
13757
diff
changeset
|
1031 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
|
1032 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
|
1033 flProtect, |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1034 0, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1035 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
|
1036 m_obj->tagname); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1037 if (m_obj->map_handle != NULL) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1038 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
|
1039 dwDesiredAccess, |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1040 0, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1041 0, |
|
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 if (m_obj->data != NULL) { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1044 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
|
1045 } 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
|
1046 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
|
1047 } |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1048 } else { |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1049 dwErr = GetLastError(); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1050 } |
|
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
|
1051 Py_DECREF(m_obj); |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1052 PyErr_SetFromWindowsErr(dwErr); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1053 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
|
1054 } |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1055 #endif /* MS_WIN32 */ |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1056 |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1057 /* 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
|
1058 static struct PyMethodDef mmap_functions[] = { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1059 {"mmap", (PyCFunction) new_mmap_object, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1060 METH_VARARGS|METH_KEYWORDS}, |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1061 {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
|
1062 }; |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1063 |
|
15803
9b6814bfb2c0
Part of SF patch #102409 by jlt63 to support building these modules
Guido van Rossum <guido@python.org>
parents:
15794
diff
changeset
|
1064 DL_EXPORT(void) |
|
20970
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
1065 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
|
1066 { |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1067 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
|
1068 |
|
a3c1a234cee7
SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents:
15803
diff
changeset
|
1069 /* 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
|
1070 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
|
1071 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1072 module = Py_InitModule ("mmap", mmap_functions); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1073 dict = PyModule_GetDict (module); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1074 mmap_module_error = PyExc_EnvironmentError; |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1075 Py_INCREF(mmap_module_error); |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1076 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
|
1077 #ifdef PROT_EXEC |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1078 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
|
1079 #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
|
1080 #ifdef PROT_READ |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1081 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
|
1082 #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
|
1083 #ifdef PROT_WRITE |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1084 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
|
1085 #endif |
|
ec58b503b670
Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
1086 |
|
ec58b503b670
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 #ifdef MAP_SHARED |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1088 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
|
1089 #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
|
1090 #ifdef MAP_PRIVATE |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1091 PyDict_SetItemString (dict, "MAP_PRIVATE", |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1092 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
|
1093 #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
|
1094 #ifdef MAP_DENYWRITE |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1095 PyDict_SetItemString (dict, "MAP_DENYWRITE", |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1096 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
|
1097 #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
|
1098 #ifdef MAP_EXECUTABLE |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1099 PyDict_SetItemString (dict, "MAP_EXECUTABLE", |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1100 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
|
1101 #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
|
1102 #ifdef MAP_ANON |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1103 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
|
1104 PyDict_SetItemString (dict, "MAP_ANONYMOUS", |
|
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1105 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
|
1106 #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
|
1107 |
|
12146
e8c30e69666b
Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents:
12120
diff
changeset
|
1108 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
|
1109 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
|
1110 |
|
e167b82c6444
CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents:
20888
diff
changeset
|
1111 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
|
1112 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
|
1113 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
|
1114 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
|
1115 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
|
1116 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
|
1117 } |
