annotate Modules/mmapmodule.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a73373ccccc2
children 0151029de1d9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1 /*
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
2 / Author: Sam Rushing <rushing@nightmare.com>
20888
2cd2ae4de30d Remove obsolete e-mail address
Andrew M. Kuchling <amk@amk.ca>
parents: 18626
diff changeset
3 / Hacked for Unix by AMK
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
4 / $Id$
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
5
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
6 / Modified to support mmap with offset - to map a 'window' of a file
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
7 / Author: Yotam Medini yotamm@mellanox.co.il
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
8 /
12120
ec58b503b670 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 / 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
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 / 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
12 / 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
13 /
ec58b503b670 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
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
21 #define PY_SSIZE_T_CLEAN
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
22 #include <Python.h>
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
23
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
24 #ifndef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
25 #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
26 #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
27
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
28 #ifdef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
29 #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
30 static int
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
31 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
32 {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
33 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
34 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
35 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
36 }
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
37
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
38 static int
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
39 my_getallocationgranularity (void)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
40 {
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
41
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
42 SYSTEM_INFO si;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
43 GetSystemInfo(&si);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
44 return si.dwAllocationGranularity;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
45 }
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
46
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
47 #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
48
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
49 #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
50 #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
51 #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
52
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
53 #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
54 static int
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
55 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
56 {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
57 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
58 }
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
59
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
60 #define my_getallocationgranularity my_getpagesize
14956
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
61 #else
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
62 #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
63 #endif
c78e76a571b6 my_getpagesize(): New function; returns the size of a page of memory.
Fred Drake <fdrake@acm.org>
parents: 14836
diff changeset
64
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
65 #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
66
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
67 #include <string.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
68
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
69 #ifdef HAVE_SYS_TYPES_H
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
70 #include <sys/types.h>
38716
42808526511c Patch #1495999: Part two of Windows CE changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
71 #endif /* HAVE_SYS_TYPES_H */
12120
ec58b503b670 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
36515
7c9cfd3b5d0f Update comment and make accurate.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36501
diff changeset
73 /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
74 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
75 # define MAP_ANONYMOUS MAP_ANON
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
76 #endif
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
77
12120
ec58b503b670 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 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
79
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
80 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
81 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
82 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
83 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
84 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
85 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
86 } 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
87
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
88 typedef struct {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
89 PyObject_HEAD
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
90 char * data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
91 size_t size;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
92 size_t pos; /* relative to offset */
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
93 size_t offset;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
94
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
95 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
96 HANDLE map_handle;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
97 HANDLE file_handle;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
98 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
99 #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
100
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
101 #ifdef UNIX
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
102 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
103 #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
104
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
105 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
106 } 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
107
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
108
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
109 static void
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
110 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
111 {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
112 #ifdef MS_WINDOWS
13757
67b9b05ce5a5 Fixes for Windows (but also tested on Linux). Test suite now completes, and this module should not leak in the face of errors.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13620
diff changeset
113 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
114 UnmapViewOfFile (m_obj->data);
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
115 if (m_obj->map_handle != NULL)
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
116 CloseHandle (m_obj->map_handle);
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
117 if (m_obj->file_handle != INVALID_HANDLE_VALUE)
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
118 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
119 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
120 PyMem_Free(m_obj->tagname);
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
121 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
122
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
123 #ifdef UNIX
35821
b652f535201d Cleanup the previous checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35820
diff changeset
124 if (m_obj->fd >= 0)
b652f535201d Cleanup the previous checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35820
diff changeset
125 (void) close(m_obj->fd);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
126 if (m_obj->data!=NULL) {
12944
b1ec49b2c5c2 Removed MS_INVALIDATE flags
Andrew M. Kuchling <amk@amk.ca>
parents: 12941
diff changeset
127 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
128 munmap(m_obj->data, m_obj->size);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
129 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
130 #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
131
44886
2e151376941f Use Py_TYPE() instead of ->ob_type
Christian Heimes <christian@cheimes.de>
parents: 44877
diff changeset
132 Py_TYPE(m_obj)->tp_free((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
133 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
134
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
135 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
136 mmap_close_method(mmap_object *self, PyObject *unused)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
137 {
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
138 #ifdef MS_WINDOWS
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
139 /* For each resource we maintain, we need to check
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
140 the value is valid, and if so, free the resource
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
141 and set the member value to an invalid value so
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
142 the dealloc does not attempt to resource clearing
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
143 again.
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
144 TODO - should we check for errors in the close operations???
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
145 */
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
146 if (self->data != NULL) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
147 UnmapViewOfFile(self->data);
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
148 self->data = NULL;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
149 }
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
150 if (self->map_handle != NULL) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
151 CloseHandle(self->map_handle);
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
152 self->map_handle = NULL;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
153 }
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
154 if (self->file_handle != INVALID_HANDLE_VALUE) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
155 CloseHandle(self->file_handle);
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
156 self->file_handle = INVALID_HANDLE_VALUE;
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
157 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
158 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
159
ec58b503b670 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 #ifdef UNIX
35821
b652f535201d Cleanup the previous checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35820
diff changeset
161 (void) close(self->fd);
b652f535201d Cleanup the previous checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35820
diff changeset
162 self->fd = -1;
27205
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
163 if (self->data != NULL) {
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
164 munmap(self->data, self->size);
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
165 self->data = NULL;
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
166 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
167 #endif
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
168
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
169 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
170 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
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
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
173 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
174 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
175 do { \
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
176 if (self->map_handle == NULL) { \
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
177 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
178 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
179 } \
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
180 } while (0)
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
181 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
182
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
183 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
184 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
185 do { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
186 if (self->data == NULL) { \
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
187 PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
188 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
189 } \
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
190 } 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
191 #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
192
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
193 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
194 mmap_read_byte_method(mmap_object *self,
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
195 PyObject *unused)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
196 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
197 CHECK_VALID(NULL);
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
198 if (self->pos < self->size) {
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
199 char value = self->data[self->pos];
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
200 self->pos += 1;
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
201 return Py_BuildValue("c", value);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
202 } else {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
203 PyErr_SetString(PyExc_ValueError, "read byte out of range");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
204 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
205 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
206 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
207
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
208 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
209 mmap_read_line_method(mmap_object *self,
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
210 PyObject *unused)
12120
ec58b503b670 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 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
212 char *start = self->data+self->pos;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
213 char *eof = self->data+self->size;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
214 char *eol;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
215 PyObject *result;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
216
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
217 CHECK_VALID(NULL);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
218
12231
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
219 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
220 if (!eol)
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
221 eol = eof;
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
222 else
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
223 ++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
224 newline. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
225 result = PyString_FromStringAndSize(start, (eol - start));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
226 self->pos += (eol - start);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
227 return result;
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
228 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
229
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
230 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
231 mmap_read_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
232 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
233 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
234 Py_ssize_t num_bytes;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
235 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
236
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
237 CHECK_VALID(NULL);
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
238 if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
239 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
240
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
241 /* silently 'adjust' out-of-range requests */
48114
c4d24fc8ba8a Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents: 47527
diff changeset
242 if (num_bytes > self->size - self->pos) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
243 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
244 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
245 result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
246 self->pos += num_bytes;
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
247 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
248 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
249
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
250 static PyObject *
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
251 mmap_gfind(mmap_object *self,
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
252 PyObject *args,
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
253 int reverse)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
254 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
255 Py_ssize_t start = self->pos;
44981
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
256 Py_ssize_t end = self->size;
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
257 const char *needle;
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
258 Py_ssize_t 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
259
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
260 CHECK_VALID(NULL);
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
261 if (!PyArg_ParseTuple(args, reverse ? "s#|nn:rfind" : "s#|nn:find",
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
262 &needle, &len, &start, &end)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
263 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
264 } else {
44981
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
265 const char *p, *start_p, *end_p;
44995
6bbf6fdc152b Use int for the sign rather than a char. char can be signed or unsigned.
Neal Norwitz <nnorwitz@gmail.com>
parents: 44994
diff changeset
266 int sign = reverse ? -1 : 1;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
267
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
268 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
269 start += self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
270 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
271 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
272 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
273 start = self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
274
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
275 if (end < 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
276 end += self->size;
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
277 if (end < 0)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
278 end = 0;
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
279 else if ((size_t)end > self->size)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
280 end = self->size;
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
281
44981
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
282 start_p = self->data + start;
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
283 end_p = self->data + end;
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
284
44981
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
285 for (p = (reverse ? end_p - len : start_p);
fbb9ae9a514f Cleanup the code a bit. test_rfind is failing on PPC and PPC64 buildbots,
Neal Norwitz <nnorwitz@gmail.com>
parents: 44886
diff changeset
286 (p >= start_p) && (p + len <= end_p); p += sign) {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
287 Py_ssize_t i;
22177
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
288 for (i = 0; i < len && needle[i] == p[i]; ++i)
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
289 /* nothing */;
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
290 if (i == len) {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
291 return PyInt_FromSsize_t(p - self->data);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
292 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
293 }
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
294 return PyInt_FromLong(-1);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
295 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
296 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
297
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
298 static PyObject *
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
299 mmap_find_method(mmap_object *self,
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
300 PyObject *args)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
301 {
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
302 return mmap_gfind(self, args, 0);
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
303 }
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
304
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
305 static PyObject *
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
306 mmap_rfind_method(mmap_object *self,
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
307 PyObject *args)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
308 {
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
309 return mmap_gfind(self, args, 1);
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
310 }
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
311
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
312 static int
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
313 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
314 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
315 if (self->access != ACCESS_READ)
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
316 return 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
317 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
318 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
319 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
320
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
321 static int
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
322 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
323 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
324 if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
325 return 1;
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
326 PyErr_Format(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
327 "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
328 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
329 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
330
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
331
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
332 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
333 mmap_write_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
334 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
335 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
336 Py_ssize_t length;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
337 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
338
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
339 CHECK_VALID(NULL);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
340 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
341 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
342
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
343 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
344 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
345
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
346 if ((self->pos + length) > self->size) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
347 PyErr_SetString(PyExc_ValueError, "data out of range");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
348 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
349 }
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
350 memcpy(self->data+self->pos, data, length);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
351 self->pos = self->pos+length;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
352 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
353 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
354 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
355
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
356 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
357 mmap_write_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
358 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
359 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
360 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
361
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
362 CHECK_VALID(NULL);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
363 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
364 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
365
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
366 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
367 return NULL;
51907
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
368
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
369 if (self->pos < self->size) {
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
370 *(self->data+self->pos) = value;
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
371 self->pos += 1;
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
372 Py_INCREF(Py_None);
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
373 return Py_None;
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
374 }
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
375 else {
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
376 PyErr_SetString(PyExc_ValueError, "write byte out of range");
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
377 return NULL;
9de5d62ad402 Issue #5386: mmap.write_byte didn't check map size, so it could cause buffer
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51692
diff changeset
378 }
12120
ec58b503b670 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 }
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
380
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
381 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
382 mmap_size_method(mmap_object *self,
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
383 PyObject *unused)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
384 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
385 CHECK_VALID(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
386
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
387 #ifdef MS_WINDOWS
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
388 if (self->file_handle != INVALID_HANDLE_VALUE) {
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
389 DWORD low,high;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
390 PY_LONG_LONG size;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
391 low = GetFileSize(self->file_handle, &high);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
392 if (low == INVALID_FILE_SIZE) {
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
393 /* It might be that the function appears to have failed,
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
394 when indeed its size equals INVALID_FILE_SIZE */
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
395 DWORD error = GetLastError();
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
396 if (error != NO_ERROR)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
397 return PyErr_SetFromWindowsErr(error);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
398 }
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
399 if (!high && low < LONG_MAX)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
400 return PyInt_FromLong((long)low);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
401 size = (((PY_LONG_LONG)high)<<32) + low;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
402 return PyLong_FromLongLong(size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
403 } else {
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
404 return PyInt_FromSsize_t(self->size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
405 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
406 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
407
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
408 #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
409 {
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
410 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
411 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
412 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
413 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
414 }
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
415 return PyInt_FromSsize_t(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
416 }
12120
ec58b503b670 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 /* 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
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
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
420 /* 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
421 / 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
422 / 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
423 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
424 / 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
425 / 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
426 / 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
427 */
ec58b503b670 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
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
429 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
430 mmap_resize_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
431 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
432 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
433 Py_ssize_t new_size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
434 CHECK_VALID(NULL);
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
435 if (!PyArg_ParseTuple(args, "n:resize", &new_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
436 !is_resizeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
437 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
438 #ifdef MS_WINDOWS
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
439 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
440 DWORD dwErrCode = 0;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
441 DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
442 /* First, unmap the file view */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
443 UnmapViewOfFile(self->data);
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
444 self->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
445 /* Close the mapping object */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
446 CloseHandle(self->map_handle);
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
447 self->map_handle = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
448 /* Move to the desired EOF position */
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
449 #if SIZEOF_SIZE_T > 4
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
450 newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
451 newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
452 off_hi = (DWORD)(self->offset >> 32);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
453 off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
454 #else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
455 newSizeHigh = 0;
51692
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
456 newSizeLow = (DWORD)(self->offset + new_size);
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
457 off_hi = 0;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
458 off_lo = (DWORD)self->offset;
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
459 #endif
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
460 SetFilePointer(self->file_handle,
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
461 newSizeLow, &newSizeHigh, FILE_BEGIN);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
462 /* Change the size of the file */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
463 SetEndOfFile(self->file_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
464 /* Create another mapping object and remap the file view */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
465 self->map_handle = CreateFileMapping(
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
466 self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
467 NULL,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
468 PAGE_READWRITE,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
469 0,
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
470 0,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
471 self->tagname);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
472 if (self->map_handle != NULL) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
473 self->data = (char *) MapViewOfFile(self->map_handle,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
474 FILE_MAP_WRITE,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
475 off_hi,
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
476 off_lo,
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
477 new_size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
478 if (self->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
479 self->size = new_size;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
480 Py_INCREF(Py_None);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
481 return Py_None;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
482 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
483 dwErrCode = GetLastError();
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
484 CloseHandle(self->map_handle);
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
485 self->map_handle = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
486 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
487 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
488 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
489 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
490 PyErr_SetFromWindowsErr(dwErrCode);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
491 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
492 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
493
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
494 #ifdef UNIX
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
495 #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
496 } else {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
497 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
498 "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
499 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
500 #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
501 } else {
35612
35a99eccaca6 C89 compliance.
Armin Rigo <arigo@tunes.org>
parents: 35408
diff changeset
502 void *newmap;
35a99eccaca6 C89 compliance.
Armin Rigo <arigo@tunes.org>
parents: 35408
diff changeset
503
51692
9ad662a43eb3 Issue #5282: Fixed mmap resize on 32bit windows and unix. When offset > 0,
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51688
diff changeset
504 if (ftruncate(self->fd, self->offset + new_size) == -1) {
35408
3bb932801b64 bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
Georg Brandl <georg@python.org>
parents: 34662
diff changeset
505 PyErr_SetFromErrno(mmap_module_error);
3bb932801b64 bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
Georg Brandl <georg@python.org>
parents: 34662
diff changeset
506 return NULL;
3bb932801b64 bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
Georg Brandl <georg@python.org>
parents: 34662
diff changeset
507 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
508
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
509 #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
510 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
511 #else
52549
87171283d02f Apply patch for netbsd multiprocessing support
Jesse Noller <jnoller@gmail.com>
parents: 52510
diff changeset
512 #if defined(__NetBSD__)
87171283d02f Apply patch for netbsd multiprocessing support
Jesse Noller <jnoller@gmail.com>
parents: 52510
diff changeset
513 newmap = mremap(self->data, self->size, self->data, new_size, 0);
87171283d02f Apply patch for netbsd multiprocessing support
Jesse Noller <jnoller@gmail.com>
parents: 52510
diff changeset
514 #else
87171283d02f Apply patch for netbsd multiprocessing support
Jesse Noller <jnoller@gmail.com>
parents: 52510
diff changeset
515 newmap = mremap(self->data, self->size, new_size, 0);
87171283d02f Apply patch for netbsd multiprocessing support
Jesse Noller <jnoller@gmail.com>
parents: 52510
diff changeset
516 #endif /* __NetBSD__ */
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
517 #endif
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
518 if (newmap == (void *)-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
519 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
520 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
521 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
522 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
523 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
524 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
525 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
526 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
527 #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
528 #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
529 }
12120
ec58b503b670 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
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
532 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
533 mmap_tell_method(mmap_object *self, PyObject *unused)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
534 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
535 CHECK_VALID(NULL);
39632
d65e6dcfdcb5 Alexander Belopolsky pointed out that pos is a size_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 39606
diff changeset
536 return PyInt_FromSize_t(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
537 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
538
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
539 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
540 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
541 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
542 Py_ssize_t offset = 0;
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
543 Py_ssize_t size = self->size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
544 CHECK_VALID(NULL);
36540
7db3cb9e839f mmap_flush_method(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36539
diff changeset
545 if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
546 return NULL;
36540
7db3cb9e839f mmap_flush_method(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36539
diff changeset
547 if ((size_t)(offset + size) > self->size) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
548 PyErr_SetString(PyExc_ValueError, "flush values out of range");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
549 return NULL;
44994
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
550 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
551 #ifdef MS_WINDOWS
44994
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
552 return PyInt_FromLong((long) FlushViewOfFile(self->data+offset, size));
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
553 #elif defined(UNIX)
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
554 /* XXX semantics of return value? */
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
555 /* XXX flags for msync? */
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
556 if (-1 == msync(self->data + offset, size, MS_SYNC)) {
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
557 PyErr_SetFromErrno(mmap_module_error);
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
558 return NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
559 }
44994
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
560 return PyInt_FromLong(0);
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
561 #else
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
562 PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
563 return NULL;
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
564 #endif
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
565 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
566
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
567 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
568 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
569 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
570 Py_ssize_t dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
571 int how=0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
572 CHECK_VALID(NULL);
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
573 if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
574 return NULL;
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
575 else {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
576 size_t where;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
577 switch (how) {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
578 case 0: /* relative to start */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
579 if (dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
580 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
581 where = dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
582 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
583 case 1: /* relative to current position */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
584 if ((Py_ssize_t)self->pos + dist < 0)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
585 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
586 where = self->pos + dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
587 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
588 case 2: /* relative to end */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
589 if ((Py_ssize_t)self->size + dist < 0)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
590 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
591 where = self->size + dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
592 break;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
593 default:
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
594 PyErr_SetString(PyExc_ValueError, "unknown seek type");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
595 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
596 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
597 if (where > self->size)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
598 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
599 self->pos = where;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
600 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
601 return Py_None;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
602 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
603
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
604 onoutofrange:
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
605 PyErr_SetString(PyExc_ValueError, "seek out of range");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
606 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
607 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
608
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
609 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
610 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
611 {
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
612 unsigned long dest, src, cnt;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
613 CHECK_VALID(NULL);
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
614 if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) ||
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
615 !is_writeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
616 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
617 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
618 /* bounds check the values */
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
619 if (cnt < 0 || (cnt + dest) < cnt || (cnt + src) < cnt ||
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
620 src < 0 || src > self->size || (src + cnt) > self->size ||
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
621 dest < 0 || dest > self->size || (dest + cnt) > self->size) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
622 PyErr_SetString(PyExc_ValueError,
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
623 "source, destination, or count out of range");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
624 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
625 }
52670
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
626 memmove(self->data+dest, self->data+src, cnt);
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
627 Py_INCREF(Py_None);
a73373ccccc2 bounds check arguments to mmap.move(). All of them. Really.
Jack Diederich <jackdied@gmail.com>
parents: 52577
diff changeset
628 return Py_None;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
629 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
630 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
631
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
632 static struct PyMethodDef mmap_object_methods[] = {
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
633 {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
21757
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
634 {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
635 {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
21757
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
636 {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
637 {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
638 {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
639 {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
640 {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
21757
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
641 {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
642 {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
643 {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
644 {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
21757
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
645 {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
40631ae74b17 Patch #477750: Use METH_ constants in Modules.
Martin v. Löwis <martin@v.loewis.de>
parents: 21712
diff changeset
646 {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
647 {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
648 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
649
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
650 /* 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
651
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
652 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
653 mmap_buffer_getreadbuf(mmap_object *self, Py_ssize_t 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
654 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
655 CHECK_VALID(-1);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
656 if (index != 0) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
657 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
658 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
659 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
660 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
661 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
662 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
663 }
ec58b503b670 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
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
665 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
666 mmap_buffer_getwritebuf(mmap_object *self, Py_ssize_t index, const void **ptr)
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
667 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
668 CHECK_VALID(-1);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
669 if (index != 0) {
12146
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 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
672 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
673 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
674 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
675 return -1;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
676 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
677 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
678 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
679
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
680 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
681 mmap_buffer_getsegcount(mmap_object *self, Py_ssize_t *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
682 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
683 CHECK_VALID(-1);
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
684 if (lenp)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
685 *lenp = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
686 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
687 }
ec58b503b670 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
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
689 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
690 mmap_buffer_getcharbuffer(mmap_object *self, Py_ssize_t 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
691 {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
692 if (index != 0) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
693 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
694 "accessing non-existent buffer segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
695 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
696 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
697 *ptr = (const char *)self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
698 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
699 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
700
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
701 static Py_ssize_t
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
702 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
703 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
704 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
705 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
706 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
707
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
708 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
709 mmap_item(mmap_object *self, Py_ssize_t 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
710 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
711 CHECK_VALID(NULL);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
712 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
713 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
714 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
715 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
716 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
717 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
718
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
719 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
720 mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t 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
721 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
722 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
723 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
724 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
725 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
726 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
727 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
728 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
729 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
730 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
731 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
732 ihigh = self->size;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
733
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
734 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
735 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
736
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
737 static PyObject *
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
738 mmap_subscript(mmap_object *self, PyObject *item)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
739 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
740 CHECK_VALID(NULL);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
741 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
742 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
743 if (i == -1 && PyErr_Occurred())
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
744 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
745 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
746 i += self->size;
51688
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
747 if (i < 0 || (size_t)i >= self->size) {
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
748 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
749 "mmap index out of range");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
750 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
751 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
752 return PyString_FromStringAndSize(self->data + i, 1);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
753 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
754 else if (PySlice_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
755 Py_ssize_t start, stop, step, slicelen;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
756
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
757 if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
758 &start, &stop, &step, &slicelen) < 0) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
759 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
760 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
761
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
762 if (slicelen <= 0)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
763 return PyString_FromStringAndSize("", 0);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
764 else if (step == 1)
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
765 return PyString_FromStringAndSize(self->data + start,
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
766 slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
767 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
768 char *result_buf = (char *)PyMem_Malloc(slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
769 Py_ssize_t cur, i;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
770 PyObject *result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
771
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
772 if (result_buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
773 return PyErr_NoMemory();
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
774 for (cur = start, i = 0; i < slicelen;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
775 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
776 result_buf[i] = self->data[cur];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
777 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
778 result = PyString_FromStringAndSize(result_buf,
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
779 slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
780 PyMem_Free(result_buf);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
781 return result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
782 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
783 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
784 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
785 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
786 "mmap indices must be integers");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
787 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
788 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
789 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
790
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
791 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
792 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
793 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
794 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
795 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
796 "mmaps don't support concatenation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
797 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
798 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
799
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
800 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
801 mmap_repeat(mmap_object *self, Py_ssize_t 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
802 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
803 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
804 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
805 "mmaps don't support repeat operation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
806 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
807 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
808
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
809 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
810 mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t 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
811 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
812 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
813
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
814 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
815 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
816 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
817 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
818 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
819 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
820 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
821 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
822 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
823 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
824 ihigh = self->size;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
825
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
826 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
827 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
828 "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
829 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
830 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
831 if (! (PyString_Check(v)) ) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
832 PyErr_SetString(PyExc_IndexError,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
833 "mmap slice assignment must be a string");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
834 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
835 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
836 if (PyString_Size(v) != (ihigh - ilow)) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
837 PyErr_SetString(PyExc_IndexError,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
838 "mmap slice assignment is wrong size");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
839 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
840 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
841 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
842 return -1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
843 buf = PyString_AsString(v);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
844 memcpy(self->data + ilow, buf, ihigh-ilow);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
845 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
846 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
847
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
848 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
849 mmap_ass_item(mmap_object *self, Py_ssize_t 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
850 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
851 const char *buf;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
852
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
853 CHECK_VALID(-1);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
854 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
855 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
856 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
857 }
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
858 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
859 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
860 "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
861 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
862 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
863 if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
864 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
865 "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
866 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
867 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
868 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
869 return -1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
870 buf = PyString_AsString(v);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
871 self->data[i] = buf[0];
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
872 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
873 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
874
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
875 static int
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
876 mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
877 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
878 CHECK_VALID(-1);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
879
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
880 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
881 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
882 const char *buf;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
883
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
884 if (i == -1 && PyErr_Occurred())
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
885 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
886 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
887 i += self->size;
51688
0c6c0572c247 Issue #5292: Fixed mmap crash on its boundary access m[len(m)].
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
888 if (i < 0 || (size_t)i >= self->size) {
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
889 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
890 "mmap index out of range");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
891 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
892 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
893 if (value == NULL) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
894 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
895 "mmap object doesn't support item deletion");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
896 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
897 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
898 if (!PyString_Check(value) || PyString_Size(value) != 1) {
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
899 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
900 "mmap assignment must be single-character string");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
901 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
902 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
903 if (!is_writeable(self))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
904 return -1;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
905 buf = PyString_AsString(value);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
906 self->data[i] = buf[0];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
907 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
908 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
909 else if (PySlice_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
910 Py_ssize_t start, stop, step, slicelen;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
911
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
912 if (PySlice_GetIndicesEx((PySliceObject *)item,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
913 self->size, &start, &stop,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
914 &step, &slicelen) < 0) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
915 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
916 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
917 if (value == NULL) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
918 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
919 "mmap object doesn't support slice deletion");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
920 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
921 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
922 if (!PyString_Check(value)) {
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
923 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
924 "mmap slice assignment must be a string");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
925 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
926 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
927 if (PyString_Size(value) != slicelen) {
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
928 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
929 "mmap slice assignment is wrong size");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
930 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
931 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
932 if (!is_writeable(self))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
933 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
934
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
935 if (slicelen == 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
936 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
937 else if (step == 1) {
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
938 const char *buf = PyString_AsString(value);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
939
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
940 if (buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
941 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
942 memcpy(self->data + start, buf, slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
943 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
944 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
945 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
946 Py_ssize_t cur, i;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
947 const char *buf = PyString_AsString(value);
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
948
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
949 if (buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
950 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
951 for (cur = start, i = 0; i < slicelen;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
952 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
953 self->data[cur] = buf[i];
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
954 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
955 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
956 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
957 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
958 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
959 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
960 "mmap indices must be integer");
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
961 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
962 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
963 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
964
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
965 static PySequenceMethods mmap_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
966 (lenfunc)mmap_length, /*sq_length*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
967 (binaryfunc)mmap_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
968 (ssizeargfunc)mmap_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
969 (ssizeargfunc)mmap_item, /*sq_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
970 (ssizessizeargfunc)mmap_slice, /*sq_slice*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
971 (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
972 (ssizessizeobjargproc)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
973 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
974
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
975 static PyMappingMethods mmap_as_mapping = {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
976 (lenfunc)mmap_length,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
977 (binaryfunc)mmap_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
978 (objobjargproc)mmap_ass_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
979 };
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
980
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
981 static PyBufferProcs mmap_as_buffer = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
982 (readbufferproc)mmap_buffer_getreadbuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
983 (writebufferproc)mmap_buffer_getwritebuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
984 (segcountproc)mmap_buffer_getsegcount,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
985 (charbufferproc)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
986 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
987
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
988 static PyObject *
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
989 new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict);
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
990
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
991 PyDoc_STRVAR(mmap_doc,
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
992 "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
993 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
994 Maps length bytes from the file specified by the file handle fileno,\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
995 and returns a mmap object. If length is larger than the current size\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
996 of the file, the file is extended to contain length bytes. If length\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
997 is 0, the maximum length of the map is the current size of the file,\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
998 except that if the file is empty Windows raises an exception (you cannot\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
999 create an empty mapping on Windows).\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1000 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1001 Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1002 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1003 Maps length bytes from the file specified by the file descriptor fileno,\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1004 and returns a mmap object. If length is 0, the maximum length of the map\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1005 will be the current size of the file when mmap is called.\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1006 flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1007 private copy-on-write mapping, so changes to the contents of the mmap\n\
48300
7f038a4700b1 Fix strange character in the docstring.
Thomas Heller <theller@ctypes.org>
parents: 48114
diff changeset
1008 object will be private to this process, and MAP_SHARED creates a mapping\n\
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1009 that's shared with all other processes mapping the same areas of the file.\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1010 The default value is MAP_SHARED.\n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1011 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1012 To map anonymous memory, pass -1 as the fileno (both versions).");
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1013
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1014
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1015 static PyTypeObject mmap_object_type = {
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1016 PyVarObject_HEAD_INIT(NULL, 0)
21313
91dd223829c6 Patch supplied by Burton Radons for his own SF bug #487390: Modifying
Guido van Rossum <guido@python.org>
parents: 20970
diff changeset
1017 "mmap.mmap", /* tp_name */
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1018 sizeof(mmap_object), /* tp_size */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1019 0, /* tp_itemsize */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1020 /* methods */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1021 (destructor) mmap_object_dealloc, /* tp_dealloc */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1022 0, /* tp_print */
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1023 0, /* tp_getattr */
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1024 0, /* tp_setattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1025 0, /* tp_compare */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1026 0, /* tp_repr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1027 0, /* tp_as_number */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1028 &mmap_as_sequence, /*tp_as_sequence*/
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
1029 &mmap_as_mapping, /*tp_as_mapping*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1030 0, /*tp_hash*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1031 0, /*tp_call*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1032 0, /*tp_str*/
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1033 PyObject_GenericGetAttr, /*tp_getattro*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1034 0, /*tp_setattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1035 &mmap_as_buffer, /*tp_as_buffer*/
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1036 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1037 mmap_doc, /*tp_doc*/
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1038 0, /* tp_traverse */
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1039 0, /* tp_clear */
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1040 0, /* tp_richcompare */
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1041 0, /* tp_weaklistoffset */
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1042 0, /* tp_iter */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1043 0, /* tp_iternext */
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1044 mmap_object_methods, /* tp_methods */
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1045 0, /* tp_members */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1046 0, /* tp_getset */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1047 0, /* tp_base */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1048 0, /* tp_dict */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1049 0, /* tp_descr_get */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1050 0, /* tp_descr_set */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1051 0, /* tp_dictoffset */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1052 0, /* tp_init */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1053 PyType_GenericAlloc, /* tp_alloc */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1054 new_mmap_object, /* tp_new */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1055 PyObject_Del, /* tp_free */
12120
ec58b503b670 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
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1058
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1059 /* extract the map size from the given PyObject
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1060
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
1061 Returns -1 on error, with an appropriate Python exception raised. On
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1062 success, the map size is returned. */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1063 static Py_ssize_t
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1064 _GetMapSize(PyObject *o, const char* param)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1065 {
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1066 if (o == NULL)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1067 return 0;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1068 if (PyIndex_Check(o)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1069 Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36684
diff changeset
1070 if (i==-1 && PyErr_Occurred())
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1071 return -1;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1072 if (i < 0) {
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1073 PyErr_Format(PyExc_OverflowError,
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1074 "memory mapped %s must be positive",
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1075 param);
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1076 return -1;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1077 }
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1078 return i;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1079 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1080
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1081 PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1082 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1083 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1084
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1085 #ifdef UNIX
12120
ec58b503b670 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 static PyObject *
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1087 new_mmap_object(PyTypeObject *type, 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
1088 {
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1089 #ifdef HAVE_FSTAT
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1090 struct stat st;
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1091 #endif
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1092 mmap_object *m_obj;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1093 PyObject *map_size_obj = NULL, *offset_obj = NULL;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1094 Py_ssize_t map_size, offset;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1095 int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1096 int devzero = -1;
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1097 int access = (int)ACCESS_DEFAULT;
36684
0bebeb393045 More unconsting.
Martin v. Löwis <martin@v.loewis.de>
parents: 36565
diff changeset
1098 static char *keywords[] = {"fileno", "length",
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1099 "flags", "prot",
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1100 "access", "offset", 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
1101
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1102 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
35999
9541369b0dca Add const to several API functions that take char *.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35821
diff changeset
1103 &fd, &map_size_obj, &flags, &prot,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1104 &access, &offset_obj))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1105 return NULL;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1106 map_size = _GetMapSize(map_size_obj, "size");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1107 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1108 return NULL;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1109 offset = _GetMapSize(offset_obj, "offset");
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1110 if (offset < 0)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1111 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
1112
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1113 if ((access != (int)ACCESS_DEFAULT) &&
36445
a3046d43eb52 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 36324
diff changeset
1114 ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1115 return PyErr_Format(PyExc_ValueError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1116 "mmap can't specify both access and flags, prot.");
36445
a3046d43eb52 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 36324
diff changeset
1117 switch ((access_mode)access) {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1118 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
1119 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
1120 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
1121 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1122 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
1123 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
1124 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
1125 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1126 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
1127 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
1128 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
1129 break;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1130 case ACCESS_DEFAULT:
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1131 /* 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
1132 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1133 default:
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1134 return PyErr_Format(PyExc_ValueError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1135 "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
1136 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1137
45358
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44995
diff changeset
1138 if (prot == PROT_READ) {
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44995
diff changeset
1139 access = ACCESS_READ;
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44995
diff changeset
1140 }
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44995
diff changeset
1141
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1142 #ifdef HAVE_FSTAT
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1143 # ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1144 /* on OpenVMS we must ensure that all bytes are written to the file */
44619
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1145 if (fd != -1) {
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1146 fsync(fd);
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1147 }
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1148 # endif
44619
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1149 if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
1150 if (map_size == 0) {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1151 map_size = st.st_size;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1152 } else if ((size_t)offset + (size_t)map_size > st.st_size) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1153 PyErr_SetString(PyExc_ValueError,
34662
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
1154 "mmap length is greater than file size");
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
1155 return NULL;
b40586129bb0 Patches #749830, #1144555: allow UNIX mmap size to default to current
Martin v. Löwis <martin@v.loewis.de>
parents: 32123
diff changeset
1156 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1157 }
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1158 #endif
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1159 m_obj = (mmap_object *)type->tp_alloc(type, 0);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1160 if (m_obj == NULL) {return NULL;}
36273
d0104364f6d1 Fix SF bug #1402308, segfault when using mmap(-1, ...)
Neal Norwitz <nnorwitz@gmail.com>
parents: 36073
diff changeset
1161 m_obj->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1162 m_obj->size = (size_t) map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1163 m_obj->pos = (size_t) 0;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1164 m_obj->offset = offset;
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1165 if (fd == -1) {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1166 m_obj->fd = -1;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1167 /* Assume the caller wants to map anonymous memory.
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1168 This is the same behaviour as Windows. mmap.mmap(-1, size)
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1169 on both Windows and Unix map anonymous memory.
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1170 */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1171 #ifdef MAP_ANONYMOUS
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1172 /* BSD way to map anonymous memory */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1173 flags |= MAP_ANONYMOUS;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1174 #else
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1175 /* SVR4 method to map anonymous memory is to open /dev/zero */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1176 fd = devzero = open("/dev/zero", O_RDWR);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1177 if (devzero == -1) {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1178 Py_DECREF(m_obj);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1179 PyErr_SetFromErrno(mmap_module_error);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1180 return NULL;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1181 }
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1182 #endif
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1183 } else {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1184 m_obj->fd = dup(fd);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1185 if (m_obj->fd == -1) {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1186 Py_DECREF(m_obj);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1187 PyErr_SetFromErrno(mmap_module_error);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1188 return NULL;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1189 }
35408
3bb932801b64 bug [ 728515 ] mmap's resize method resizes the file in win32 but not unix
Georg Brandl <georg@python.org>
parents: 34662
diff changeset
1190 }
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1191
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1192 m_obj->data = mmap(NULL, map_size,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1193 prot, flags,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1194 fd, offset);
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1195
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1196 if (devzero != -1) {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1197 close(devzero);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1198 }
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1199
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1200 if (m_obj->data == (char *)-1) {
32123
5f4f2f536f96 Set data pointer to NULL after an error; this keeps the mmap_dealloc() function from trying to do msync(-1);munmap(-1).
Andrew M. Kuchling <amk@amk.ca>
parents: 31384
diff changeset
1201 m_obj->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1202 Py_DECREF(m_obj);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1203 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1204 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1205 }
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1206 m_obj->access = (access_mode)access;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1207 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
1208 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1209 #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
1210
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
1211 #ifdef MS_WINDOWS
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1212 static PyObject *
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1213 new_mmap_object(PyTypeObject *type, 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
1214 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1215 mmap_object *m_obj;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1216 PyObject *map_size_obj = NULL, *offset_obj = NULL;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1217 Py_ssize_t map_size, offset;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1218 DWORD off_hi; /* upper 32 bits of offset */
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1219 DWORD off_lo; /* lower 32 bits of offset */
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1220 DWORD size_hi; /* upper 32 bits of size */
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1221 DWORD size_lo; /* lower 32 bits of size */
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1222 char *tagname = "";
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1223 DWORD dwErr = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1224 int fileno;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1225 HANDLE fh = 0;
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1226 int access = (access_mode)ACCESS_DEFAULT;
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1227 DWORD flProtect, dwDesiredAccess;
36684
0bebeb393045 More unconsting.
Martin v. Löwis <martin@v.loewis.de>
parents: 36565
diff changeset
1228 static char *keywords[] = { "fileno", "length",
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1229 "tagname",
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1230 "access", "offset", NULL };
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1231
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1232 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1233 &fileno, &map_size_obj,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1234 &tagname, &access, &offset_obj)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1235 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
1236 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1237
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1238 switch((access_mode)access) {
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1239 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
1240 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
1241 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
1242 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1243 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
1244 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
1245 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
1246 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1247 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
1248 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
1249 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
1250 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1251 default:
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1252 return PyErr_Format(PyExc_ValueError,
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1253 "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
1254 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1255
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1256 map_size = _GetMapSize(map_size_obj, "size");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1257 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1258 return NULL;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1259 offset = _GetMapSize(offset_obj, "offset");
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1260 if (offset < 0)
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1261 return NULL;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1262
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1263 /* assume -1 and 0 both mean invalid filedescriptor
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1264 to 'anonymously' map memory.
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1265 XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1266 XXX: Should this code be added?
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1267 if (fileno == 0)
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1268 PyErr_Warn(PyExc_DeprecationWarning,
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1269 "don't use 0 for anonymous memory");
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1270 */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1271 if (fileno != -1 && fileno != 0) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1272 fh = (HANDLE)_get_osfhandle(fileno);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1273 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
1274 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
1275 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
1276 }
12241
7e9a5cf55c76 Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents: 12231
diff changeset
1277 /* Win9x appears to need us seeked to zero */
28558
84acba227954 Fix two crashes on Windows:
Guido van Rossum <guido@python.org>
parents: 27756
diff changeset
1278 lseek(fileno, 0, SEEK_SET);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1279 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1280
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1281 m_obj = (mmap_object *)type->tp_alloc(type, 0);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1282 if (m_obj == NULL)
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
1283 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
1284 /* 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
1285 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
1286 m_obj->data = NULL;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1287 m_obj->file_handle = INVALID_HANDLE_VALUE;
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
1288 m_obj->map_handle = NULL;
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
1289 m_obj->tagname = NULL;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1290 m_obj->offset = offset;
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
1291
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1292 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
1293 /* 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
1294 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
1295 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
1296 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
1297 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
1298 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
1299 (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
1300 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
1301 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
1302 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
1303 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
1304 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
1305 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
1306 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
1307 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1308 if (!map_size) {
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1309 DWORD low,high;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1310 low = GetFileSize(fh, &high);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1311 /* low might just happen to have the value INVALID_FILE_SIZE;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1312 so we need to check the last error also. */
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1313 if (low == INVALID_FILE_SIZE &&
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1314 (dwErr = GetLastError()) != NO_ERROR) {
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1315 Py_DECREF(m_obj);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1316 return PyErr_SetFromWindowsErr(dwErr);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1317 }
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1318
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1319 #if SIZEOF_SIZE_T > 4
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1320 m_obj->size = (((size_t)high)<<32) + low;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1321 #else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1322 if (high)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1323 /* File is too large to map completely */
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1324 m_obj->size = (size_t)-1;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1325 else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1326 m_obj->size = low;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1327 #endif
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1328 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1329 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
1330 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1331 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1332 else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1333 m_obj->size = map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1334 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1335
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1336 /* set the initial position */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1337 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
1338
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
1339 /* 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
1340 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
1341 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
1342 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
1343 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
1344 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
1345 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
1346 }
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
1347 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
1348 }
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
1349 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
1350 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
1351
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1352 m_obj->access = (access_mode)access;
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1353 /* DWORD is a 4-byte int. If we're on a box where size_t consumes
36550
bce3f356cd7b Fix typo.
Thomas Wouters <thomas@python.org>
parents: 36541
diff changeset
1354 * more than 4 bytes, we need to break it apart. Else (size_t
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1355 * consumes 4 bytes), C doesn't define what happens if we shift
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1356 * right by 32, so we need different code.
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1357 */
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1358 #if SIZEOF_SIZE_T > 4
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1359 size_hi = (DWORD)((offset + m_obj->size) >> 32);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1360 size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1361 off_hi = (DWORD)(offset >> 32);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1362 off_lo = (DWORD)(offset & 0xFFFFFFFF);
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1363 #else
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1364 size_hi = 0;
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1365 size_lo = (DWORD)(offset + m_obj->size);
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1366 off_hi = 0;
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1367 off_lo = (DWORD)offset;
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1368 #endif
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1369 /* For files, it would be sufficient to pass 0 as size.
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1370 For anonymous maps, we have to pass the size explicitly. */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1371 m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1372 NULL,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1373 flProtect,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1374 size_hi,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1375 size_lo,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1376 m_obj->tagname);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1377 if (m_obj->map_handle != NULL) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1378 m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1379 dwDesiredAccess,
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1380 off_hi,
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1381 off_lo,
51911
29d652e3f2d3 Issue #1733986: Fixed mmap crash in accessing elements of second map object
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51907
diff changeset
1382 m_obj->size);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1383 if (m_obj->data != NULL)
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1384 return (PyObject *)m_obj;
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
1385 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
1386 dwErr = GetLastError();
52026
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
1387 CloseHandle(m_obj->map_handle);
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
1388 m_obj->map_handle = NULL;
79ea0667604b Issue #5385: Fixed mmap crash after resize failure on windows.
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51911
diff changeset
1389 }
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1390 } else
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1391 dwErr = GetLastError();
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
1392 Py_DECREF(m_obj);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1393 PyErr_SetFromWindowsErr(dwErr);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1394 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
1395 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
1396 #endif /* MS_WINDOWS */
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1397
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1398 static void
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1399 setint(PyObject *d, const char *name, long value)
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1400 {
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1401 PyObject *o = PyInt_FromLong(value);
39478
317605f2cf6f Get rid of compiler warning
Neal Norwitz <nnorwitz@gmail.com>
parents: 39470
diff changeset
1402 if (o && PyDict_SetItemString(d, name, o) == 0) {
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1403 Py_DECREF(o);
39478
317605f2cf6f Get rid of compiler warning
Neal Norwitz <nnorwitz@gmail.com>
parents: 39470
diff changeset
1404 }
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1405 }
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1406
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
1407 PyMODINIT_FUNC
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1408 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
1409 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1410 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
1411
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1412 if (PyType_Ready(&mmap_object_type) < 0)
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1413 return;
15851
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1414
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1415 module = Py_InitModule("mmap", NULL);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36273
diff changeset
1416 if (module == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36273
diff changeset
1417 return;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1418 dict = PyModule_GetDict(module);
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1419 if (!dict)
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1420 return;
45380
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1421 mmap_module_error = PyErr_NewException("mmap.error",
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1422 PyExc_EnvironmentError , NULL);
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1423 if (mmap_module_error == NULL)
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1424 return;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1425 PyDict_SetItemString(dict, "error", mmap_module_error);
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1426 PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1427 #ifdef PROT_EXEC
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1428 setint(dict, "PROT_EXEC", 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
1429 #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
1430 #ifdef PROT_READ
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1431 setint(dict, "PROT_READ", 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
1432 #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
1433 #ifdef PROT_WRITE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1434 setint(dict, "PROT_WRITE", 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
1435 #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
1436
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1437 #ifdef MAP_SHARED
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1438 setint(dict, "MAP_SHARED", 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
1439 #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
1440 #ifdef MAP_PRIVATE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1441 setint(dict, "MAP_PRIVATE", 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
1442 #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
1443 #ifdef MAP_DENYWRITE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1444 setint(dict, "MAP_DENYWRITE", 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
1445 #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
1446 #ifdef MAP_EXECUTABLE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1447 setint(dict, "MAP_EXECUTABLE", 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
1448 #endif
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1449 #ifdef MAP_ANONYMOUS
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1450 setint(dict, "MAP_ANON", MAP_ANONYMOUS);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1451 setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1452 #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
1453
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1454 setint(dict, "PAGESIZE", (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
1455
43640
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1456 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
04088abf4e4f Add phuang patch from Issue 708374 which adds offset parameter to mmap module.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42951
diff changeset
1457
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1458 setint(dict, "ACCESS_READ", ACCESS_READ);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1459 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1460 setint(dict, "ACCESS_COPY", 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
1461 }