annotate Modules/mmapmodule.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents a27583c836b2
children e38335d4df91
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);
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
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 }
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
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);
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
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
54748
3c8f4f7e4a62 Merged revisions 73425 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52578
diff changeset
161 if (0 <= self->fd)
3c8f4f7e4a62 Merged revisions 73425 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52578
diff changeset
162 (void) close(self->fd);
35821
b652f535201d Cleanup the previous checkin.
Neal Norwitz <nnorwitz@gmail.com>
parents: 35820
diff changeset
163 self->fd = -1;
27205
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
164 if (self->data != NULL) {
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
165 munmap(self->data, self->size);
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
166 self->data = NULL;
3c8d91f3f4c6 SF #665913, Fix mmap module core dump with unix
Neal Norwitz <nnorwitz@gmail.com>
parents: 25130
diff changeset
167 }
12120
ec58b503b670 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 #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
169
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
170 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
171 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
172 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
173
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
174 #ifdef MS_WINDOWS
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
175 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
176 do { \
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
177 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
178 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
179 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
180 } \
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
181 } while (0)
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
182 #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
183
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
184 #ifdef UNIX
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
185 #define CHECK_VALID(err) \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
186 do { \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
187 if (self->data == NULL) { \
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
188 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
189 return err; \
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
190 } \
12120
ec58b503b670 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 } 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
192 #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
193
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
194 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
195 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
196 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
197 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
198 CHECK_VALID(NULL);
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
199 if (self->pos < self->size) {
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
200 char value = self->data[self->pos];
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
201 self->pos += 1;
17735
147632ad1bcc Minor fiddling related to
Tim Peters <tim.peters@gmail.com>
parents: 17600
diff changeset
202 return Py_BuildValue("c", value);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
203 } else {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
204 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
205 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
206 }
12120
ec58b503b670 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
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
209 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
210 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
211 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
212 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
213 char *start = self->data+self->pos;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
214 char *eof = self->data+self->size;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
215 char *eol;
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
216 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
217
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
218 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
219
12231
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
220 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
221 if (!eol)
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
222 eol = eof;
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
223 else
074c30ff3206 Patch from Hrvoje Niksic <hniksic@iskon.hr>:
Fred Drake <fdrake@acm.org>
parents: 12178
diff changeset
224 ++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
225 newline. */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
226 result = PyString_FromStringAndSize(start, (eol - start));
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
227 self->pos += (eol - start);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
228 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
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
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
231 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
232 mmap_read_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
233 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
234 {
54971
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
235 Py_ssize_t num_bytes, n;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
236 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
237
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
238 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
239 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
240 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
241
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
242 /* silently 'adjust' out-of-range requests */
54971
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
243 assert(self->size >= self->pos);
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
244 n = self->size - self->pos;
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
245 /* The difference can overflow, only if self->size is greater than
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
246 * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
247 * because the mapped area and the returned string each need more
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
248 * than half of the addressable memory. So we clip the size, and let
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
249 * the code below raise MemoryError.
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
250 */
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
251 if (n < 0)
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
252 n = PY_SSIZE_T_MAX;
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
253 if (num_bytes < 0 || num_bytes > n) {
a27583c836b2 Merged revisions 73677,73681 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 54748
diff changeset
254 num_bytes = n;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
255 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
256 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
257 self->pos += num_bytes;
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
258 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
259 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
260
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
261 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
262 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
263 PyObject *args,
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
264 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
265 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
266 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
267 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
268 const char *needle;
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
269 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
270
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
271 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
272 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
273 &needle, &len, &start, &end)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
274 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
275 } 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
276 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
277 int sign = reverse ? -1 : 1;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
278
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
279 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
280 start += self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
281 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
282 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
283 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
284 start = self->size;
17821
780248cd1d9a Fix the .find() method for memory maps.
Greg Stein <gstein@lyra.org>
parents: 17735
diff changeset
285
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
286 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
287 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
288 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
289 end = 0;
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
290 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
291 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
292
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
293 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
294 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
295
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
296 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
297 (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
298 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
299 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
300 /* nothing */;
ee33bbaf3381 SF bug 515943: searching for data with \0 in mmap.
Tim Peters <tim.peters@gmail.com>
parents: 21757
diff changeset
301 if (i == len) {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
302 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
303 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
304 }
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
305 return PyInt_FromLong(-1);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
306 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
307 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
308
44774
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
309 static PyObject *
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
310 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
311 PyObject *args)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
312 {
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
313 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
314 }
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
315
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
316 static PyObject *
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
317 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
318 PyObject *args)
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
319 {
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
320 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
321 }
80366621c502 Patch #976880: add mmap .rfind() method, and 'end' paramter to .find().
Andrew M. Kuchling <amk@amk.ca>
parents: 44619
diff changeset
322
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
323 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
324 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
325 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
326 if (self->access != ACCESS_READ)
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
327 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
328 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
329 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
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
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
332 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
333 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
334 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
335 if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
336 return 1;
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
337 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
338 "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
339 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
340 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
341
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
342
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
343 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
344 mmap_write_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
345 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
346 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
347 Py_ssize_t length;
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
348 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
349
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
350 CHECK_VALID(NULL);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
351 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
352 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
353
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
354 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
355 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
356
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
357 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
358 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
359 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
360 }
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
361 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
362 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
363 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
364 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
365 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
366
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
367 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
368 mmap_write_byte_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
369 PyObject *args)
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
370 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
371 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
372
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
373 CHECK_VALID(NULL);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
374 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
375 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
376
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
377 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
378 return NULL;
51909
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
379
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
380 if (self->pos < self->size) {
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
381 *(self->data+self->pos) = value;
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
382 self->pos += 1;
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
383 Py_INCREF(Py_None);
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
384 return Py_None;
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
385 }
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
386 else {
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
387 PyErr_SetString(PyExc_ValueError, "write byte out of range");
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
388 return NULL;
90037f97d870 Merged revisions 70052 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51704
diff changeset
389 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
390 }
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
391
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
392 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
393 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
394 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
395 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
396 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
397
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
398 #ifdef MS_WINDOWS
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
399 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
400 DWORD low,high;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
401 PY_LONG_LONG size;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
402 low = GetFileSize(self->file_handle, &high);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
403 if (low == INVALID_FILE_SIZE) {
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
404 /* 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
405 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
406 DWORD error = GetLastError();
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
407 if (error != NO_ERROR)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
408 return PyErr_SetFromWindowsErr(error);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
409 }
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
410 if (!high && low < LONG_MAX)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
411 return PyInt_FromLong((long)low);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
412 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
413 return PyLong_FromLongLong(size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
414 } else {
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
415 return PyInt_FromSsize_t(self->size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
416 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
417 #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
418
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
419 #ifdef UNIX
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
420 {
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
421 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
422 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
423 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
424 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
425 }
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
426 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
427 }
12120
ec58b503b670 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 #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
429 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
430
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
431 /* 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
432 / 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
433 / 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
434 /
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
435 / 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
436 / 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
437 / 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
438 */
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
439
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
440 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
441 mmap_resize_method(mmap_object *self,
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
442 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
443 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
444 Py_ssize_t new_size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
445 CHECK_VALID(NULL);
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
446 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
447 !is_resizeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
448 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
449 #ifdef MS_WINDOWS
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
450 } else {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
451 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
452 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
453 /* 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
454 UnmapViewOfFile(self->data);
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
455 self->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
456 /* Close the mapping object */
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
457 CloseHandle(self->map_handle);
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
458 self->map_handle = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
459 /* 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
460 #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
461 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
462 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
463 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
464 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
465 #else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
466 newSizeHigh = 0;
51704
39f09c9ed5a5 Merged revisions 69714,69718 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
467 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
468 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
469 off_lo = (DWORD)self->offset;
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
470 #endif
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
471 SetFilePointer(self->file_handle,
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
472 newSizeLow, &newSizeHigh, FILE_BEGIN);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
473 /* 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
474 SetEndOfFile(self->file_handle);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
475 /* 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
476 self->map_handle = CreateFileMapping(
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
477 self->file_handle,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
478 NULL,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
479 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
480 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
481 0,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
482 self->tagname);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
483 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
484 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
485 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
486 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
487 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
488 new_size);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
489 if (self->data != NULL) {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
490 self->size = new_size;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
491 Py_INCREF(Py_None);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
492 return Py_None;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
493 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
494 dwErrCode = GetLastError();
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
495 CloseHandle(self->map_handle);
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
496 self->map_handle = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
497 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
498 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
499 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
500 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
501 PyErr_SetFromWindowsErr(dwErrCode);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
502 return NULL;
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
503 #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
504
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
505 #ifdef UNIX
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
506 #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
507 } else {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
508 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
509 "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
510 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
511 #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
512 } else {
35612
35a99eccaca6 C89 compliance.
Armin Rigo <arigo@tunes.org>
parents: 35408
diff changeset
513 void *newmap;
35a99eccaca6 C89 compliance.
Armin Rigo <arigo@tunes.org>
parents: 35408
diff changeset
514
51704
39f09c9ed5a5 Merged revisions 69714,69718 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
515 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
516 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
517 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
518 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
519
12948
acb0261b6942 Support resizing the mapping depending on whether HAVE_MREMAP is defined
Andrew M. Kuchling <amk@amk.ca>
parents: 12945
diff changeset
520 #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
521 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
522 #else
52550
6ddbdfdb7a86 Merged revisions 70849 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents: 52513
diff changeset
523 #if defined(__NetBSD__)
6ddbdfdb7a86 Merged revisions 70849 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents: 52513
diff changeset
524 newmap = mremap(self->data, self->size, self->data, new_size, 0);
6ddbdfdb7a86 Merged revisions 70849 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents: 52513
diff changeset
525 #else
6ddbdfdb7a86 Merged revisions 70849 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents: 52513
diff changeset
526 newmap = mremap(self->data, self->size, new_size, 0);
6ddbdfdb7a86 Merged revisions 70849 via svnmerge from
Jesse Noller <jnoller@gmail.com>
parents: 52513
diff changeset
527 #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
528 #endif
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
529 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
530 {
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
531 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
532 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
533 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
534 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
535 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
536 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
537 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
538 #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
539 #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
540 }
12120
ec58b503b670 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 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
542
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
543 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 38485
diff changeset
544 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
545 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
546 CHECK_VALID(NULL);
39632
d65e6dcfdcb5 Alexander Belopolsky pointed out that pos is a size_t
Neal Norwitz <nnorwitz@gmail.com>
parents: 39606
diff changeset
547 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
548 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
549
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
550 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
551 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
552 {
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36515
diff changeset
553 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
554 Py_ssize_t size = self->size;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
555 CHECK_VALID(NULL);
36540
7db3cb9e839f mmap_flush_method(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36539
diff changeset
556 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
557 return NULL;
36540
7db3cb9e839f mmap_flush_method(): Squash compiler warning about
Tim Peters <tim.peters@gmail.com>
parents: 36539
diff changeset
558 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
559 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
560 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
561 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
562 #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
563 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
564 #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
565 /* 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
566 /* 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
567 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
568 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
569 return NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
570 }
44994
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
571 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
572 #else
ff58b30a8d52 Mostly reformat. Also set an error and return NULL if neither MS_WINDOWS
Neal Norwitz <nnorwitz@gmail.com>
parents: 44981
diff changeset
573 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
574 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
575 #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
576 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
577
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
578 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
579 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
580 {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
581 Py_ssize_t dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
582 int how=0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
583 CHECK_VALID(NULL);
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
584 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
585 return NULL;
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
586 else {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
587 size_t where;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
588 switch (how) {
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
589 case 0: /* relative to start */
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
590 if (dist < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
591 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
592 where = dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
593 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
594 case 1: /* relative to current position */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
595 if ((Py_ssize_t)self->pos + dist < 0)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
596 goto onoutofrange;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
597 where = self->pos + dist;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
598 break;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
599 case 2: /* relative to end */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
600 if ((Py_ssize_t)self->size + dist < 0)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
601 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
602 where = self->size + dist;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
603 break;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
604 default:
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, "unknown seek type");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
606 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
607 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
608 if (where > self->size)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
609 goto onoutofrange;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
610 self->pos = where;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
611 Py_INCREF(Py_None);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
612 return Py_None;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
613 }
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
614
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 onoutofrange:
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
616 PyErr_SetString(PyExc_ValueError, "seek out of range");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
617 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
618 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
619
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
620 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
621 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
622 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
623 unsigned long dest, src, count;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
624 CHECK_VALID(NULL);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
625 if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &count) ||
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
626 !is_writeable(self)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
627 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
628 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
629 /* bounds check the values */
52513
235622617497 Merged revisions 70800 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52027
diff changeset
630 unsigned long pos = src > dest ? src : dest;
52578
d9c8d1a5ddee Merged revisions 70879 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 52550
diff changeset
631 if (self->size < pos || count > self->size - pos) {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
632 PyErr_SetString(PyExc_ValueError,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
633 "source or destination out of range");
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
634 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
635 } else {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
636 memmove(self->data+dest, self->data+src, count);
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
637 Py_INCREF(Py_None);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
638 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
639 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
640 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
641 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
642
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
643 static 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
644 {"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
645 {"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
646 {"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
647 {"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
648 {"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
649 {"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
650 {"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
651 {"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
652 {"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
653 {"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
654 {"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
655 {"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
656 {"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
657 {"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
658 {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
659 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
660
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
661 /* 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
662
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
663 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
664 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
665 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
666 CHECK_VALID(-1);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
667 if (index != 0) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
668 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
669 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
670 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
671 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
672 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
673 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
674 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
675
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
676 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
677 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
678 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
679 CHECK_VALID(-1);
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
680 if (index != 0) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
681 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
682 "Accessing non-existent mmap segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
683 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
684 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
685 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
686 return -1;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
687 *ptr = self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
688 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
689 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
690
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
691 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
692 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
693 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
694 CHECK_VALID(-1);
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
695 if (lenp)
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
696 *lenp = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
697 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
698 }
ec58b503b670 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
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
700 static Py_ssize_t
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
701 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
702 {
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
703 if (index != 0) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
704 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
705 "accessing non-existent buffer segment");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
706 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
707 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
708 *ptr = (const char *)self->data;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
709 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
710 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
711
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
712 static Py_ssize_t
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
713 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
714 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
715 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
716 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
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_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
721 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
722 CHECK_VALID(NULL);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
723 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
724 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
725 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
726 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
727 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
728 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
729
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
730 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
731 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
732 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
733 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
734 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
735 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
736 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
737 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
738 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
739 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
740 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
741 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
742 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
743 ihigh = self->size;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
744
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
745 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
746 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
747
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
748 static PyObject *
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
749 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
750 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
751 CHECK_VALID(NULL);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
752 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
753 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
754 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
755 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
756 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
757 i += self->size;
51704
39f09c9ed5a5 Merged revisions 69714,69718 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
758 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
759 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
760 "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
761 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
762 }
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(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
764 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
765 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
766 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
767
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
768 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
769 &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
770 return NULL;
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
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
773 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
774 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
775 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
776 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
777 slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
778 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
779 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
780 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
781 PyObject *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 if (result_buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
784 return PyErr_NoMemory();
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
785 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
786 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
787 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
788 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
789 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
790 slicelen);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
791 PyMem_Free(result_buf);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
792 return result;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
793 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
794 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
795 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
796 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
797 "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
798 return NULL;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
799 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
800 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
801
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
802 static PyObject *
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
803 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
804 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
805 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
806 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
807 "mmaps don't support concatenation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
808 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
809 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
810
ec58b503b670 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 static PyObject *
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
812 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
813 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
814 CHECK_VALID(NULL);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
815 PyErr_SetString(PyExc_SystemError,
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
816 "mmaps don't support repeat operation");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
817 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
818 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
819
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
820 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
821 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
822 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
823 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
824
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
825 CHECK_VALID(-1);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
826 if (ilow < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
827 ilow = 0;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
828 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
829 ilow = self->size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
830 if (ihigh < 0)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
831 ihigh = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
832 if (ihigh < ilow)
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
833 ihigh = ilow;
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
834 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
835 ihigh = self->size;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
836
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
837 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
838 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
839 "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
840 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
841 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
842 if (! (PyString_Check(v)) ) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
843 PyErr_SetString(PyExc_IndexError,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
844 "mmap slice assignment must be a string");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
845 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
846 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
847 if (PyString_Size(v) != (ihigh - ilow)) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
848 PyErr_SetString(PyExc_IndexError,
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
849 "mmap slice assignment is wrong size");
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
850 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
851 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
852 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
853 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
854 buf = PyString_AsString(v);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
855 memcpy(self->data + ilow, buf, ihigh-ilow);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
856 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
857 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
858
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
859 static int
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
860 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
861 {
12361
791a112be3bc Correct fix by Mark Favas for the cast problems.
Guido van Rossum <guido@python.org>
parents: 12360
diff changeset
862 const char *buf;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
863
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
864 CHECK_VALID(-1);
13264
b9adb79cdeb6 clean up warnings in Win32 build of mmapmodule.c
Guido van Rossum <guido@python.org>
parents: 12954
diff changeset
865 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
866 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
867 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
868 }
18626
2b5e7383c77a Fix SF #441664: Python crash on del of a slice of a mmap
Thomas Wouters <thomas@python.org>
parents: 17832
diff changeset
869 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
870 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
871 "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
872 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
873 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
874 if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
875 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
876 "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
877 return -1;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
878 }
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
879 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
880 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
881 buf = PyString_AsString(v);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
882 self->data[i] = buf[0];
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
883 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
884 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
885
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
886 static int
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
887 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
888 {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
889 CHECK_VALID(-1);
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
890
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
891 if (PyIndex_Check(item)) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
892 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
893 const char *buf;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
894
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
895 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
896 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
897 if (i < 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
898 i += self->size;
51704
39f09c9ed5a5 Merged revisions 69714,69718 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 48300
diff changeset
899 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
900 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
901 "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
902 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
903 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
904 if (value == NULL) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
905 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
906 "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
907 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
908 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
909 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
910 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
911 "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
912 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
913 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
914 if (!is_writeable(self))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
915 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
916 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
917 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
918 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
919 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
920 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
921 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
922
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
923 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
924 self->size, &start, &stop,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
925 &step, &slicelen) < 0) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
926 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
927 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
928 if (value == NULL) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
929 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
930 "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
931 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
932 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
933 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
934 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
935 "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
936 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
937 }
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
938 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
939 PyErr_SetString(PyExc_IndexError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
940 "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
941 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
942 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
943 if (!is_writeable(self))
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
944 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
945
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
946 if (slicelen == 0)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
947 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
948 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
949 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
950
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
951 if (buf == NULL)
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
952 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
953 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
954 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
955 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
956 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
957 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
958 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
959
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
960 if (buf == NULL)
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 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
963 cur += step, i++) {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
964 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
965 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
966 return 0;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
967 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
968 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
969 else {
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
970 PyErr_SetString(PyExc_TypeError,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
971 "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
972 return -1;
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
973 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
974 }
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
975
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
976 static PySequenceMethods mmap_as_sequence = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
977 (lenfunc)mmap_length, /*sq_length*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
978 (binaryfunc)mmap_concat, /*sq_concat*/
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
979 (ssizeargfunc)mmap_repeat, /*sq_repeat*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
980 (ssizeargfunc)mmap_item, /*sq_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
981 (ssizessizeargfunc)mmap_slice, /*sq_slice*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
982 (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
983 (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
984 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
985
42951
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
986 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
987 (lenfunc)mmap_length,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
988 (binaryfunc)mmap_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
989 (objobjargproc)mmap_ass_subscript,
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
990 };
9620134010a5 Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents: 42333
diff changeset
991
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
992 static PyBufferProcs mmap_as_buffer = {
36501
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
993 (readbufferproc)mmap_buffer_getreadbuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
994 (writebufferproc)mmap_buffer_getwritebuf,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
995 (segcountproc)mmap_buffer_getsegcount,
7fdc639bc5b4 Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents: 36446
diff changeset
996 (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
997 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
998
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
999 static PyObject *
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1000 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
1001
44827
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1002 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
1003 "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
1004 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1005 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
1006 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
1007 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
1008 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
1009 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
1010 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
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 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
1013 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1014 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
1015 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
1016 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
1017 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
1018 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
1019 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
1020 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
1021 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
1022 \n\
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1023 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
1024
eab00db80c27 Switch mmap from old Py_FindMethod to new PyObject_GenericGetAttr attribute access.
Georg Brandl <georg@python.org>
parents: 44774
diff changeset
1025
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1026 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
1027 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
1028 "mmap.mmap", /* tp_name */
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1029 sizeof(mmap_object), /* tp_size */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1030 0, /* tp_itemsize */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1031 /* methods */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1032 (destructor) mmap_object_dealloc, /* tp_dealloc */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1033 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
1034 0, /* tp_getattr */
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1035 0, /* tp_setattr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1036 0, /* tp_compare */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1037 0, /* tp_repr */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1038 0, /* tp_as_number */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1039 &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
1040 &mmap_as_mapping, /*tp_as_mapping*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1041 0, /*tp_hash*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1042 0, /*tp_call*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1043 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
1044 PyObject_GenericGetAttr, /*tp_getattro*/
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1045 0, /*tp_setattro*/
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1046 &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
1047 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
1048 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
1049 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
1050 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
1051 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
1052 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
1053 0, /* tp_iter */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1054 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
1055 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
1056 0, /* tp_members */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1057 0, /* tp_getset */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1058 0, /* tp_base */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1059 0, /* tp_dict */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1060 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
1061 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
1062 0, /* tp_dictoffset */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1063 0, /* tp_init */
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1064 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
1065 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
1066 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
1067 };
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1068
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1069
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1070 /* extract the map size from the given PyObject
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1071
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
1072 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
1073 success, the map size is returned. */
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1074 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
1075 _GetMapSize(PyObject *o, const char* param)
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1076 {
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
1077 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
1078 return 0;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1079 if (PyIndex_Check(o)) {
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1080 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
1081 if (i==-1 && PyErr_Occurred())
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1082 return -1;
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1083 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
1084 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
1085 "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
1086 param);
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1087 return -1;
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1088 }
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1089 return i;
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1090 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1091
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 38716
diff changeset
1092 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
1093 return -1;
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1094 }
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1095
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1096 #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
1097 static PyObject *
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1098 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
1099 {
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1100 #ifdef HAVE_FSTAT
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1101 struct stat st;
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1102 #endif
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1103 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
1104 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
1105 Py_ssize_t map_size, offset;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1106 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
1107 int devzero = -1;
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1108 int access = (int)ACCESS_DEFAULT;
36684
0bebeb393045 More unconsting.
Martin v. Löwis <martin@v.loewis.de>
parents: 36565
diff changeset
1109 static char *keywords[] = {"fileno", "length",
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1110 "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
1111 "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
1112
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
1113 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
1114 &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
1115 &access, &offset_obj))
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1116 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
1117 map_size = _GetMapSize(map_size_obj, "size");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1118 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1119 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
1120 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
1121 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
1122 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
1123
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1124 if ((access != (int)ACCESS_DEFAULT) &&
36445
a3046d43eb52 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 36324
diff changeset
1125 ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1126 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
1127 "mmap can't specify both access and flags, prot.");
36445
a3046d43eb52 Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents: 36324
diff changeset
1128 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
1129 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
1130 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
1131 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
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 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
1134 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
1135 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
1136 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1137 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
1138 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
1139 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
1140 break;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1141 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
1142 /* 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
1143 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1144 default:
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1145 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
1146 "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
1147 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1148
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
1149 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
1150 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
1151 }
a97689b3e742 Bug #2111: mmap segfaults when trying to write a block opened with PROT_READ
Christian Heimes <christian@cheimes.de>
parents: 44995
diff changeset
1152
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1153 #ifdef HAVE_FSTAT
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1154 # ifdef __VMS
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1155 /* 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
1156 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
1157 fsync(fd);
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1158 }
28949
ac487a7f7f2c Patch #708495: Port more stuff to OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents: 28558
diff changeset
1159 # endif
44619
42676a4840b5 Check for fd of -1 to save fsync() and fstat() call
Andrew M. Kuchling <amk@amk.ca>
parents: 44350
diff changeset
1160 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
1161 if (map_size == 0) {
36541
f7f0d2a5b20b Remove size restrictions.
Martin v. Löwis <martin@v.loewis.de>
parents: 36540
diff changeset
1162 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
1163 } 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
1164 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
1165 "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
1166 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
1167 }
25130
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1168 }
368bc3a4919e SF bug # 585792, Invalid mmap crashes Python interpreter
Neal Norwitz <nnorwitz@gmail.com>
parents: 24363
diff changeset
1169 #endif
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1170 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
1171 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
1172 m_obj->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1173 m_obj->size = (size_t) map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1174 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
1175 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
1176 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
1177 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
1178 /* 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
1179 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
1180 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
1181 */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1182 #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
1183 /* 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
1184 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
1185 #else
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1186 /* 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
1187 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
1188 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
1189 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
1190 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
1191 return NULL;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1192 }
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1193 #endif
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1194 } else {
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1195 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
1196 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
1197 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
1198 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
1199 return NULL;
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1200 }
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
1201 }
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
1202
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1203 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
1204 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
1205 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
1206
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1207 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
1208 close(devzero);
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1209 }
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1210
20970
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1211 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
1212 m_obj->data = NULL;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1213 Py_DECREF(m_obj);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1214 PyErr_SetFromErrno(mmap_module_error);
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1215 return NULL;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1216 }
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1217 m_obj->access = (access_mode)access;
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1218 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
1219 }
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1220 #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
1221
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
1222 #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
1223 static PyObject *
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1224 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
1225 {
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1226 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
1227 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
1228 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
1229 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
1230 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
1231 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
1232 DWORD size_lo; /* lower 32 bits of size */
13441
5edee6878943 -- ANSI-fying, names
Fredrik Lundh <fredrik@pythonware.com>
parents: 13359
diff changeset
1233 char *tagname = "";
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1234 DWORD dwErr = 0;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1235 int fileno;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1236 HANDLE fh = 0;
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1237 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
1238 DWORD flProtect, dwDesiredAccess;
36684
0bebeb393045 More unconsting.
Martin v. Löwis <martin@v.loewis.de>
parents: 36565
diff changeset
1239 static char *keywords[] = { "fileno", "length",
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1240 "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
1241 "access", "offset", NULL };
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1242
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
1243 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1244 &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
1245 &tagname, &access, &offset_obj)) {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1246 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
1247 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1248
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1249 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
1250 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
1251 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
1252 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
1253 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1254 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
1255 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
1256 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
1257 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1258 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
1259 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
1260 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
1261 break;
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1262 default:
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1263 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
1264 "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
1265 }
e167b82c6444 CVS patch #477161: New "access" keyword for mmap, from Jay T Miller.
Tim Peters <tim.peters@gmail.com>
parents: 20888
diff changeset
1266
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
1267 map_size = _GetMapSize(map_size_obj, "size");
12945
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1268 if (map_size < 0)
5d2fb67408a7 Patch from Trent Mick:
Andrew M. Kuchling <amk@amk.ca>
parents: 12944
diff changeset
1269 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
1270 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
1271 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
1272 return NULL;
36537
fdabdf148336 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 36536
diff changeset
1273
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1274 /* 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
1275 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
1276 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
1277 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
1278 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
1279 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
1280 "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
1281 */
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1282 if (fileno != -1 && fileno != 0) {
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1283 fh = (HANDLE)_get_osfhandle(fileno);
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1284 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
1285 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
1286 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
1287 }
12241
7e9a5cf55c76 Mark Hammond <mhammond@skippinet.com.au>:
Fred Drake <fdrake@acm.org>
parents: 12231
diff changeset
1288 /* 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
1289 lseek(fileno, 0, SEEK_SET);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1290 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1291
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1292 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
1293 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
1294 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
1295 /* 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
1296 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
1297 m_obj->data = NULL;
13758
bc37e6a335f2 More Windows changes.
Mark Hammond <mhammond@skippinet.com.au>
parents: 13757
diff changeset
1298 m_obj->file_handle = INVALID_HANDLE_VALUE;
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
1299 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
1300 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
1301 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
1302
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1303 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
1304 /* 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
1305 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
1306 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
1307 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
1308 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
1309 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
1310 (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
1311 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
1312 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
1313 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
1314 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
1315 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
1316 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
1317 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
1318 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1319 if (!map_size) {
36564
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1320 DWORD low,high;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1321 low = GetFileSize(fh, &high);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1322 /* 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
1323 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
1324 if (low == INVALID_FILE_SIZE &&
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1325 (dwErr = GetLastError()) != NO_ERROR) {
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1326 Py_DECREF(m_obj);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1327 return PyErr_SetFromWindowsErr(dwErr);
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1328 }
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1329
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1330 #if SIZEOF_SIZE_T > 4
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1331 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
1332 #else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1333 if (high)
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1334 /* 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
1335 m_obj->size = (size_t)-1;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1336 else
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1337 m_obj->size = low;
8de58246fef8 Fix size computation on Win64.
Martin v. Löwis <martin@v.loewis.de>
parents: 36550
diff changeset
1338 #endif
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1339 } else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1340 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
1341 }
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1342 }
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1343 else {
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1344 m_obj->size = map_size;
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1345 }
12120
ec58b503b670 Added mmap module -- map a view of a file into memory on Win32 and Unix.
Andrew M. Kuchling <amk@amk.ca>
parents:
diff changeset
1346
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1347 /* set the initial position */
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1348 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
1349
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
1350 /* 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
1351 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
1352 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
1353 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
1354 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
1355 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
1356 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
1357 }
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
1358 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
1359 }
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
1360 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
1361 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
1362
36073
15b58e45f1a1 SF Patch #1365916, mmap fails on AMD64
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
1363 m_obj->access = (access_mode)access;
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1364 /* 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
1365 * 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
1366 * 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
1367 * right by 32, so we need different code.
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1368 */
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1369 #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
1370 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
1371 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
1372 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
1373 off_lo = (DWORD)(offset & 0xFFFFFFFF);
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1374 #else
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1375 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
1376 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
1377 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
1378 off_lo = (DWORD)offset;
36536
c06ee0c91209 new_mmap_object(), Windows flavor.
Tim Peters <tim.peters@gmail.com>
parents: 36535
diff changeset
1379 #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
1380 /* 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
1381 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
1382 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
1383 NULL,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1384 flProtect,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1385 size_hi,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1386 size_lo,
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1387 m_obj->tagname);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1388 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
1389 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
1390 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
1391 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
1392 off_lo,
51914
2a0f84ac49f0 Merged revisions 70056 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51909
diff changeset
1393 m_obj->size);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1394 if (m_obj->data != NULL)
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1395 return (PyObject *)m_obj;
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
1396 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
1397 dwErr = GetLastError();
52027
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
1398 CloseHandle(m_obj->map_handle);
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
1399 m_obj->map_handle = NULL;
cdd3f60d9974 Merged revisions 70189 via svnmerge from
Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
parents: 51914
diff changeset
1400 }
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1401 } else
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1402 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
1403 Py_DECREF(m_obj);
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1404 PyErr_SetFromWindowsErr(dwErr);
36538
316938b4f8d0 Removed pointless parens around `return` expressions;
Tim Peters <tim.peters@gmail.com>
parents: 36537
diff changeset
1405 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
1406 }
24071
8142f8d668a0 Patch #569753: Remove support for WIN16.
Martin v. Löwis <martin@v.loewis.de>
parents: 22177
diff changeset
1407 #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
1408
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1409 static void
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1410 setint(PyObject *d, const char *name, long value)
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1411 {
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1412 PyObject *o = PyInt_FromLong(value);
39478
317605f2cf6f Get rid of compiler warning
Neal Norwitz <nnorwitz@gmail.com>
parents: 39470
diff changeset
1413 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
1414 Py_DECREF(o);
39478
317605f2cf6f Get rid of compiler warning
Neal Norwitz <nnorwitz@gmail.com>
parents: 39470
diff changeset
1415 }
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1416 }
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1417
24363
979ff5d0ae07 Replace DL_IMPORT with PyMODINIT_FUNC and remove "/export:init..." link
Mark Hammond <mhammond@skippinet.com.au>
parents: 24071
diff changeset
1418 PyMODINIT_FUNC
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1419 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
1420 {
12146
e8c30e69666b Hacked for Win32 by Mark Hammond.
Guido van Rossum <guido@python.org>
parents: 12120
diff changeset
1421 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
1422
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1423 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
1424 return;
15851
a3c1a234cee7 SF bug 128713: type(mmap_object) blew up on Linux.
Tim Peters <tim.peters@gmail.com>
parents: 15803
diff changeset
1425
44836
fabc688a08c6 #1087741: make mmap.mmap the type of mmap objects, not a
Georg Brandl <georg@python.org>
parents: 44827
diff changeset
1426 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
1427 if (module == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36273
diff changeset
1428 return;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1429 dict = PyModule_GetDict(module);
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1430 if (!dict)
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1431 return;
45380
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1432 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
1433 PyExc_EnvironmentError , NULL);
cc0232de066c Issue 2112. mmap does not raises EnvironmentError no more, but
Facundo Batista <facundobatista@gmail.com>
parents: 45358
diff changeset
1434 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
1435 return;
36539
0d489128441a Remove space between function name and left paren
Tim Peters <tim.peters@gmail.com>
parents: 36538
diff changeset
1436 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
1437 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
1438 #ifdef PROT_EXEC
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1439 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
1440 #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
1441 #ifdef PROT_READ
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1442 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
1443 #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
1444 #ifdef PROT_WRITE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1445 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
1446 #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
1447
ec58b503b670 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 #ifdef MAP_SHARED
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1449 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
1450 #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
1451 #ifdef MAP_PRIVATE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1452 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
1453 #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
1454 #ifdef MAP_DENYWRITE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1455 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
1456 #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
1457 #ifdef MAP_EXECUTABLE
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1458 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
1459 #endif
36446
02f0744a9cd0 Patch #1407135, bug #1424041, make mmap.mmap(-1, length) work the same
Neal Norwitz <nnorwitz@gmail.com>
parents: 36445
diff changeset
1460 #ifdef MAP_ANONYMOUS
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1461 setint(dict, "MAP_ANON", MAP_ANONYMOUS);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1462 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
1463 #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
1464
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1465 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
1466
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
1467 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
1468
39470
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1469 setint(dict, "ACCESS_READ", ACCESS_READ);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1470 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
daedaacf7e11 Check return result of PyModule_GetDict().
Neal Norwitz <nnorwitz@gmail.com>
parents: 39463
diff changeset
1471 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
1472 }