Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | / Author: Sam Rushing <rushing@nightmare.com> |
Andrew M. Kuchling | 10f9c07 | 2001-11-05 21:25:42 +0000 | [diff] [blame] | 3 | / Hacked for Unix by AMK |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 4 | / $Id$ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 6 | / Modified to support mmap with offset - to map a 'window' of a file |
| 7 | / Author: Yotam Medini yotamm@mellanox.co.il |
| 8 | / |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 9 | / mmapmodule.cpp -- map a view of a file into memory |
| 10 | / |
| 11 | / todo: need permission flags, perhaps a 'chsize' analog |
| 12 | / not all functions check range yet!!! |
| 13 | / |
| 14 | / |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 15 | / This version of mmapmodule.c has been changed significantly |
| 16 | / from the original mmapfile.c on which it was based. |
| 17 | / The original version of mmapfile is maintained by Sam at |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 18 | / ftp://squirl.nightmare.com/pub/python/python-ext. |
| 19 | */ |
| 20 | |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 21 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 22 | #include <Python.h> |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 23 | #include "structmember.h" |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 24 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 25 | #ifndef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 26 | #define UNIX |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 27 | # ifdef __APPLE__ |
| 28 | # include <fcntl.h> |
| 29 | # endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 32 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 33 | #include <windows.h> |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 34 | static int |
| 35 | my_getpagesize(void) |
| 36 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | SYSTEM_INFO si; |
| 38 | GetSystemInfo(&si); |
| 39 | return si.dwPageSize; |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 40 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 41 | |
| 42 | static int |
| 43 | my_getallocationgranularity (void) |
| 44 | { |
| 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | SYSTEM_INFO si; |
| 47 | GetSystemInfo(&si); |
| 48 | return si.dwAllocationGranularity; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 54 | #include <sys/mman.h> |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 55 | #include <sys/stat.h> |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 56 | |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 57 | #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 58 | static int |
| 59 | my_getpagesize(void) |
| 60 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | return sysconf(_SC_PAGESIZE); |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 62 | } |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 63 | |
| 64 | #define my_getallocationgranularity my_getpagesize |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 65 | #else |
| 66 | #define my_getpagesize getpagesize |
| 67 | #endif |
| 68 | |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 69 | #endif /* UNIX */ |
| 70 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 71 | #include <string.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 72 | |
| 73 | #ifdef HAVE_SYS_TYPES_H |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 74 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 75 | #endif /* HAVE_SYS_TYPES_H */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 76 | |
Neal Norwitz | 3eaf2b5 | 2006-02-16 08:08:54 +0000 | [diff] [blame] | 77 | /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 78 | #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) |
| 79 | # define MAP_ANONYMOUS MAP_ANON |
| 80 | #endif |
| 81 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 82 | typedef enum |
| 83 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | ACCESS_DEFAULT, |
| 85 | ACCESS_READ, |
| 86 | ACCESS_WRITE, |
| 87 | ACCESS_COPY |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 88 | } access_mode; |
| 89 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 90 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | PyObject_HEAD |
| 92 | char * data; |
| 93 | size_t size; |
| 94 | size_t pos; /* relative to offset */ |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 95 | #ifdef MS_WINDOWS |
| 96 | PY_LONG_LONG offset; |
| 97 | #else |
| 98 | off_t offset; |
| 99 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | int exports; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 101 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 102 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 103 | HANDLE map_handle; |
| 104 | HANDLE file_handle; |
| 105 | char * tagname; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
| 108 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | int fd; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 110 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 111 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 112 | PyObject *weakreflist; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | access_mode access; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 114 | } mmap_object; |
| 115 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 116 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 117 | static void |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 118 | mmap_object_dealloc(mmap_object *m_obj) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 119 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 120 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | if (m_obj->data != NULL) |
| 122 | UnmapViewOfFile (m_obj->data); |
| 123 | if (m_obj->map_handle != NULL) |
| 124 | CloseHandle (m_obj->map_handle); |
| 125 | if (m_obj->file_handle != INVALID_HANDLE_VALUE) |
| 126 | CloseHandle (m_obj->file_handle); |
| 127 | if (m_obj->tagname) |
| 128 | PyMem_Free(m_obj->tagname); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 129 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 130 | |
| 131 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | if (m_obj->fd >= 0) |
| 133 | (void) close(m_obj->fd); |
| 134 | if (m_obj->data!=NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | munmap(m_obj->data, m_obj->size); |
| 136 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 137 | #endif /* UNIX */ |
| 138 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 139 | if (m_obj->weakreflist != NULL) |
| 140 | PyObject_ClearWeakRefs((PyObject *) m_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | Py_TYPE(m_obj)->tp_free((PyObject*)m_obj); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 145 | mmap_close_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | if (self->exports > 0) { |
| 148 | PyErr_SetString(PyExc_BufferError, "cannot close "\ |
| 149 | "exported pointers exist"); |
| 150 | return NULL; |
| 151 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 152 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | /* For each resource we maintain, we need to check |
| 154 | the value is valid, and if so, free the resource |
| 155 | and set the member value to an invalid value so |
| 156 | the dealloc does not attempt to resource clearing |
| 157 | again. |
| 158 | TODO - should we check for errors in the close operations??? |
| 159 | */ |
| 160 | if (self->data != NULL) { |
| 161 | UnmapViewOfFile(self->data); |
| 162 | self->data = NULL; |
| 163 | } |
| 164 | if (self->map_handle != NULL) { |
| 165 | CloseHandle(self->map_handle); |
| 166 | self->map_handle = NULL; |
| 167 | } |
| 168 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
| 169 | CloseHandle(self->file_handle); |
| 170 | self->file_handle = INVALID_HANDLE_VALUE; |
| 171 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 172 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 173 | |
| 174 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | if (0 <= self->fd) |
| 176 | (void) close(self->fd); |
| 177 | self->fd = -1; |
| 178 | if (self->data != NULL) { |
| 179 | munmap(self->data, self->size); |
| 180 | self->data = NULL; |
| 181 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 182 | #endif |
| 183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | Py_INCREF(Py_None); |
| 185 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 188 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | #define CHECK_VALID(err) \ |
| 190 | do { \ |
| 191 | if (self->map_handle == NULL) { \ |
| 192 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
| 193 | return err; \ |
| 194 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 195 | } while (0) |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 196 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 197 | |
| 198 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | #define CHECK_VALID(err) \ |
| 200 | do { \ |
| 201 | if (self->data == NULL) { \ |
| 202 | PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \ |
| 203 | return err; \ |
| 204 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 205 | } while (0) |
| 206 | #endif /* UNIX */ |
| 207 | |
| 208 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 209 | mmap_read_byte_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 210 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 212 | CHECK_VALID(NULL); |
| 213 | if (self->pos < self->size) { |
| 214 | char value = self->data[self->pos]; |
| 215 | self->pos += 1; |
Hirokazu Yamamoto | 3cdd5cb | 2010-11-04 12:09:08 +0000 | [diff] [blame] | 216 | return Py_BuildValue("B", (unsigned char)value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | } else { |
| 218 | PyErr_SetString(PyExc_ValueError, "read byte out of range"); |
| 219 | return NULL; |
| 220 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 224 | mmap_read_line_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | char *start = self->data+self->pos; |
| 228 | char *eof = self->data+self->size; |
| 229 | char *eol; |
| 230 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | eol = memchr(start, '\n', self->size - self->pos); |
| 235 | if (!eol) |
| 236 | eol = eof; |
| 237 | else |
| 238 | ++eol; /* we're interested in the position after the |
| 239 | newline. */ |
| 240 | result = PyBytes_FromStringAndSize(start, (eol - start)); |
| 241 | self->pos += (eol - start); |
| 242 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 245 | /* Basically the "n" format code with the ability to turn None into -1. */ |
| 246 | static int |
| 247 | mmap_convert_ssize_t(PyObject *obj, void *result) { |
| 248 | Py_ssize_t limit; |
| 249 | if (obj == Py_None) { |
| 250 | limit = -1; |
| 251 | } |
| 252 | else if (PyNumber_Check(obj)) { |
| 253 | limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError); |
| 254 | if (limit == -1 && PyErr_Occurred()) |
| 255 | return 0; |
| 256 | } |
| 257 | else { |
| 258 | PyErr_Format(PyExc_TypeError, |
| 259 | "integer argument expected, got '%.200s'", |
| 260 | Py_TYPE(obj)->tp_name); |
| 261 | return 0; |
| 262 | } |
| 263 | *((Py_ssize_t *)result) = limit; |
| 264 | return 1; |
| 265 | } |
| 266 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 267 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 268 | mmap_read_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 270 | { |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 271 | Py_ssize_t num_bytes = -1, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 274 | CHECK_VALID(NULL); |
Charles-François Natali | 4dd453c | 2011-06-08 19:18:14 +0200 | [diff] [blame] | 275 | if (!PyArg_ParseTuple(args, "|O&:read", mmap_convert_ssize_t, &num_bytes)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | /* silently 'adjust' out-of-range requests */ |
| 279 | assert(self->size >= self->pos); |
| 280 | n = self->size - self->pos; |
| 281 | /* The difference can overflow, only if self->size is greater than |
| 282 | * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed, |
| 283 | * because the mapped area and the returned string each need more |
| 284 | * than half of the addressable memory. So we clip the size, and let |
| 285 | * the code below raise MemoryError. |
| 286 | */ |
| 287 | if (n < 0) |
| 288 | n = PY_SSIZE_T_MAX; |
| 289 | if (num_bytes < 0 || num_bytes > n) { |
| 290 | num_bytes = n; |
| 291 | } |
| 292 | result = PyBytes_FromStringAndSize(self->data+self->pos, num_bytes); |
| 293 | self->pos += num_bytes; |
| 294 | return result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static PyObject * |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 298 | mmap_gfind(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | PyObject *args, |
| 300 | int reverse) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | Py_ssize_t start = self->pos; |
| 303 | Py_ssize_t end = self->size; |
| 304 | const char *needle; |
| 305 | Py_ssize_t len; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | CHECK_VALID(NULL); |
| 308 | if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find", |
| 309 | &needle, &len, &start, &end)) { |
| 310 | return NULL; |
| 311 | } else { |
| 312 | const char *p, *start_p, *end_p; |
| 313 | int sign = reverse ? -1 : 1; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | if (start < 0) |
| 316 | start += self->size; |
| 317 | if (start < 0) |
| 318 | start = 0; |
| 319 | else if ((size_t)start > self->size) |
| 320 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | if (end < 0) |
| 323 | end += self->size; |
| 324 | if (end < 0) |
| 325 | end = 0; |
| 326 | else if ((size_t)end > self->size) |
| 327 | end = self->size; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | start_p = self->data + start; |
| 330 | end_p = self->data + end; |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | for (p = (reverse ? end_p - len : start_p); |
| 333 | (p >= start_p) && (p + len <= end_p); p += sign) { |
| 334 | Py_ssize_t i; |
| 335 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 336 | /* nothing */; |
| 337 | if (i == len) { |
| 338 | return PyLong_FromSsize_t(p - self->data); |
| 339 | } |
| 340 | } |
| 341 | return PyLong_FromLong(-1); |
| 342 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 345 | static PyObject * |
| 346 | mmap_find_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | PyObject *args) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return mmap_gfind(self, args, 0); |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static PyObject * |
| 353 | mmap_rfind_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | PyObject *args) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 355 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | return mmap_gfind(self, args, 1); |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 359 | static int |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 360 | is_writable(mmap_object *self) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 361 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (self->access != ACCESS_READ) |
| 363 | return 1; |
| 364 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 365 | return 0; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 368 | static int |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 369 | is_resizeable(mmap_object *self) |
| 370 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | if (self->exports > 0) { |
| 372 | PyErr_SetString(PyExc_BufferError, |
| 373 | "mmap can't resize with extant buffers exported."); |
| 374 | return 0; |
| 375 | } |
| 376 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
| 377 | return 1; |
| 378 | PyErr_Format(PyExc_TypeError, |
| 379 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 380 | return 0; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 384 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 385 | mmap_write_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | Py_ssize_t length; |
| 389 | char *data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 391 | CHECK_VALID(NULL); |
| 392 | if (!PyArg_ParseTuple(args, "y#:write", &data, &length)) |
| 393 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | if (!is_writable(self)) |
| 396 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | if ((self->pos + length) > self->size) { |
| 399 | PyErr_SetString(PyExc_ValueError, "data out of range"); |
| 400 | return NULL; |
| 401 | } |
| 402 | memcpy(self->data+self->pos, data, length); |
| 403 | self->pos = self->pos+length; |
| 404 | Py_INCREF(Py_None); |
| 405 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 409 | mmap_write_byte_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | CHECK_VALID(NULL); |
| 415 | if (!PyArg_ParseTuple(args, "b:write_byte", &value)) |
| 416 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (!is_writable(self)) |
| 419 | return NULL; |
Hirokazu Yamamoto | 39c6dea | 2009-02-28 10:56:50 +0000 | [diff] [blame] | 420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | if (self->pos < self->size) { |
| 422 | *(self->data+self->pos) = value; |
| 423 | self->pos += 1; |
| 424 | Py_INCREF(Py_None); |
| 425 | return Py_None; |
| 426 | } |
| 427 | else { |
| 428 | PyErr_SetString(PyExc_ValueError, "write byte out of range"); |
| 429 | return NULL; |
| 430 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 431 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 432 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 433 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 434 | mmap_size_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 438 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 439 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
| 441 | DWORD low,high; |
| 442 | PY_LONG_LONG size; |
| 443 | low = GetFileSize(self->file_handle, &high); |
| 444 | if (low == INVALID_FILE_SIZE) { |
| 445 | /* It might be that the function appears to have failed, |
| 446 | when indeed its size equals INVALID_FILE_SIZE */ |
| 447 | DWORD error = GetLastError(); |
| 448 | if (error != NO_ERROR) |
| 449 | return PyErr_SetFromWindowsErr(error); |
| 450 | } |
| 451 | if (!high && low < LONG_MAX) |
| 452 | return PyLong_FromLong((long)low); |
| 453 | size = (((PY_LONG_LONG)high)<<32) + low; |
| 454 | return PyLong_FromLongLong(size); |
| 455 | } else { |
| 456 | return PyLong_FromSsize_t(self->size); |
| 457 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 458 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 459 | |
| 460 | #ifdef UNIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | { |
| 462 | struct stat buf; |
| 463 | if (-1 == fstat(self->fd, &buf)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 464 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 467 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 468 | return PyLong_FromLongLong(buf.st_size); |
| 469 | #else |
| 470 | return PyLong_FromLong(buf.st_size); |
| 471 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 473 | #endif /* UNIX */ |
| 474 | } |
| 475 | |
| 476 | /* This assumes that you want the entire file mapped, |
| 477 | / and when recreating the map will make the new file |
| 478 | / have the new size |
| 479 | / |
| 480 | / Is this really necessary? This could easily be done |
| 481 | / from python by just closing and re-opening with the |
| 482 | / new size? |
| 483 | */ |
| 484 | |
| 485 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 486 | mmap_resize_method(mmap_object *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | Py_ssize_t new_size; |
| 490 | CHECK_VALID(NULL); |
| 491 | if (!PyArg_ParseTuple(args, "n:resize", &new_size) || |
| 492 | !is_resizeable(self)) { |
| 493 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 494 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 495 | } else { |
| 496 | DWORD dwErrCode = 0; |
| 497 | DWORD off_hi, off_lo, newSizeLow, newSizeHigh; |
| 498 | /* First, unmap the file view */ |
| 499 | UnmapViewOfFile(self->data); |
| 500 | self->data = NULL; |
| 501 | /* Close the mapping object */ |
| 502 | CloseHandle(self->map_handle); |
| 503 | self->map_handle = NULL; |
| 504 | /* Move to the desired EOF position */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | newSizeHigh = (DWORD)((self->offset + new_size) >> 32); |
| 506 | newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF); |
| 507 | off_hi = (DWORD)(self->offset >> 32); |
| 508 | off_lo = (DWORD)(self->offset & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | SetFilePointer(self->file_handle, |
| 510 | newSizeLow, &newSizeHigh, FILE_BEGIN); |
| 511 | /* Change the size of the file */ |
| 512 | SetEndOfFile(self->file_handle); |
| 513 | /* Create another mapping object and remap the file view */ |
| 514 | self->map_handle = CreateFileMapping( |
| 515 | self->file_handle, |
| 516 | NULL, |
| 517 | PAGE_READWRITE, |
| 518 | 0, |
| 519 | 0, |
| 520 | self->tagname); |
| 521 | if (self->map_handle != NULL) { |
| 522 | self->data = (char *) MapViewOfFile(self->map_handle, |
| 523 | FILE_MAP_WRITE, |
| 524 | off_hi, |
| 525 | off_lo, |
| 526 | new_size); |
| 527 | if (self->data != NULL) { |
| 528 | self->size = new_size; |
| 529 | Py_INCREF(Py_None); |
| 530 | return Py_None; |
| 531 | } else { |
| 532 | dwErrCode = GetLastError(); |
| 533 | CloseHandle(self->map_handle); |
| 534 | self->map_handle = NULL; |
| 535 | } |
| 536 | } else { |
| 537 | dwErrCode = GetLastError(); |
| 538 | } |
| 539 | PyErr_SetFromWindowsErr(dwErrCode); |
| 540 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 541 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 542 | |
| 543 | #ifdef UNIX |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 544 | #ifndef HAVE_MREMAP |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | } else { |
| 546 | PyErr_SetString(PyExc_SystemError, |
| 547 | "mmap: resizing not available--no mremap()"); |
| 548 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 549 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | } else { |
| 551 | void *newmap; |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | if (ftruncate(self->fd, self->offset + new_size) == -1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 554 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | return NULL; |
| 556 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 557 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 558 | #ifdef MREMAP_MAYMOVE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 560 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | #if defined(__NetBSD__) |
| 562 | newmap = mremap(self->data, self->size, self->data, new_size, 0); |
| 563 | #else |
| 564 | newmap = mremap(self->data, self->size, new_size, 0); |
| 565 | #endif /* __NetBSD__ */ |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 566 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 567 | if (newmap == (void *)-1) |
| 568 | { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 569 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 570 | return NULL; |
| 571 | } |
| 572 | self->data = newmap; |
| 573 | self->size = new_size; |
| 574 | Py_INCREF(Py_None); |
| 575 | return Py_None; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 576 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 577 | #endif /* UNIX */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 582 | mmap_tell_method(mmap_object *self, PyObject *unused) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | CHECK_VALID(NULL); |
| 585 | return PyLong_FromSize_t(self->pos); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 589 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 590 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | Py_ssize_t offset = 0; |
| 592 | Py_ssize_t size = self->size; |
| 593 | CHECK_VALID(NULL); |
| 594 | if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size)) |
| 595 | return NULL; |
| 596 | if ((size_t)(offset + size) > self->size) { |
| 597 | PyErr_SetString(PyExc_ValueError, "flush values out of range"); |
| 598 | return NULL; |
| 599 | } |
R. David Murray | e194dd6 | 2010-10-18 01:14:06 +0000 | [diff] [blame] | 600 | |
| 601 | if (self->access == ACCESS_READ || self->access == ACCESS_COPY) |
| 602 | return PyLong_FromLong(0); |
| 603 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 604 | #ifdef MS_WINDOWS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 605 | return PyLong_FromLong((long) FlushViewOfFile(self->data+offset, size)); |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 606 | #elif defined(UNIX) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | /* XXX semantics of return value? */ |
| 608 | /* XXX flags for msync? */ |
| 609 | if (-1 == msync(self->data + offset, size, MS_SYNC)) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 610 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | return NULL; |
| 612 | } |
| 613 | return PyLong_FromLong(0); |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 614 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | PyErr_SetString(PyExc_ValueError, "flush not supported on this system"); |
| 616 | return NULL; |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 617 | #endif |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 621 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 622 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | Py_ssize_t dist; |
| 624 | int how=0; |
| 625 | CHECK_VALID(NULL); |
| 626 | if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how)) |
| 627 | return NULL; |
| 628 | else { |
| 629 | size_t where; |
| 630 | switch (how) { |
| 631 | case 0: /* relative to start */ |
| 632 | if (dist < 0) |
| 633 | goto onoutofrange; |
| 634 | where = dist; |
| 635 | break; |
| 636 | case 1: /* relative to current position */ |
| 637 | if ((Py_ssize_t)self->pos + dist < 0) |
| 638 | goto onoutofrange; |
| 639 | where = self->pos + dist; |
| 640 | break; |
| 641 | case 2: /* relative to end */ |
| 642 | if ((Py_ssize_t)self->size + dist < 0) |
| 643 | goto onoutofrange; |
| 644 | where = self->size + dist; |
| 645 | break; |
| 646 | default: |
| 647 | PyErr_SetString(PyExc_ValueError, "unknown seek type"); |
| 648 | return NULL; |
| 649 | } |
| 650 | if (where > self->size) |
| 651 | goto onoutofrange; |
| 652 | self->pos = where; |
| 653 | Py_INCREF(Py_None); |
| 654 | return Py_None; |
| 655 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 656 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 657 | onoutofrange: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | PyErr_SetString(PyExc_ValueError, "seek out of range"); |
| 659 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 663 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 664 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | unsigned long dest, src, cnt; |
| 666 | CHECK_VALID(NULL); |
| 667 | if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &cnt) || |
| 668 | !is_writable(self)) { |
| 669 | return NULL; |
| 670 | } else { |
| 671 | /* bounds check the values */ |
Brett Cannon | 5fac8af | 2011-06-06 20:22:56 -0700 | [diff] [blame] | 672 | if ((cnt + dest) < cnt || (cnt + src) < cnt || |
| 673 | src > self->size || (src + cnt) > self->size || |
| 674 | dest > self->size || (dest + cnt) > self->size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | PyErr_SetString(PyExc_ValueError, |
| 676 | "source, destination, or count out of range"); |
| 677 | return NULL; |
| 678 | } |
| 679 | memmove(self->data+dest, self->data+src, cnt); |
| 680 | Py_INCREF(Py_None); |
| 681 | return Py_None; |
| 682 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 685 | static PyObject * |
| 686 | mmap_closed_get(mmap_object *self) |
| 687 | { |
| 688 | #ifdef MS_WINDOWS |
| 689 | return PyBool_FromLong(self->map_handle == NULL ? 1 : 0); |
| 690 | #elif defined(UNIX) |
| 691 | return PyBool_FromLong(self->data == NULL ? 1 : 0); |
| 692 | #endif |
| 693 | } |
| 694 | |
| 695 | static PyObject * |
| 696 | mmap__enter__method(mmap_object *self, PyObject *args) |
| 697 | { |
| 698 | CHECK_VALID(NULL); |
| 699 | |
| 700 | Py_INCREF(self); |
| 701 | return (PyObject *)self; |
| 702 | } |
| 703 | |
| 704 | static PyObject * |
| 705 | mmap__exit__method(PyObject *self, PyObject *args) |
| 706 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 707 | _Py_IDENTIFIER(close); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 708 | |
| 709 | return _PyObject_CallMethodId(self, &PyId_close, NULL); |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 712 | static struct PyMethodDef mmap_object_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | {"close", (PyCFunction) mmap_close_method, METH_NOARGS}, |
| 714 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
| 715 | {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS}, |
| 716 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
| 717 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 718 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
| 719 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS}, |
| 720 | {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS}, |
| 721 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 722 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
| 723 | {"size", (PyCFunction) mmap_size_method, METH_NOARGS}, |
| 724 | {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS}, |
| 725 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 726 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 727 | {"__enter__", (PyCFunction) mmap__enter__method, METH_NOARGS}, |
| 728 | {"__exit__", (PyCFunction) mmap__exit__method, METH_VARARGS}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 729 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 730 | }; |
| 731 | |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 732 | static PyGetSetDef mmap_object_getset[] = { |
| 733 | {"closed", (getter) mmap_closed_get, NULL, NULL}, |
| 734 | {NULL} |
| 735 | }; |
| 736 | |
| 737 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 738 | /* Functions for treating an mmap'ed file as a buffer */ |
| 739 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 740 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 741 | mmap_buffer_getbuf(mmap_object *self, Py_buffer *view, int flags) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | CHECK_VALID(-1); |
| 744 | if (PyBuffer_FillInfo(view, (PyObject*)self, self->data, self->size, |
| 745 | (self->access == ACCESS_READ), flags) < 0) |
| 746 | return -1; |
| 747 | self->exports++; |
| 748 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 751 | static void |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 752 | mmap_buffer_releasebuf(mmap_object *self, Py_buffer *view) |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 753 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | self->exports--; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 757 | static Py_ssize_t |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 758 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 760 | CHECK_VALID(-1); |
| 761 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 765 | mmap_item(mmap_object *self, Py_ssize_t i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 766 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 767 | CHECK_VALID(NULL); |
| 768 | if (i < 0 || (size_t)i >= self->size) { |
| 769 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 770 | return NULL; |
| 771 | } |
| 772 | return PyBytes_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static PyObject * |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 776 | mmap_subscript(mmap_object *self, PyObject *item) |
| 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | CHECK_VALID(NULL); |
| 779 | if (PyIndex_Check(item)) { |
| 780 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 781 | if (i == -1 && PyErr_Occurred()) |
| 782 | return NULL; |
| 783 | if (i < 0) |
| 784 | i += self->size; |
| 785 | if (i < 0 || (size_t)i >= self->size) { |
| 786 | PyErr_SetString(PyExc_IndexError, |
| 787 | "mmap index out of range"); |
| 788 | return NULL; |
| 789 | } |
| 790 | return PyLong_FromLong(Py_CHARMASK(self->data[i])); |
| 791 | } |
| 792 | else if (PySlice_Check(item)) { |
| 793 | Py_ssize_t start, stop, step, slicelen; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 794 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 795 | if (PySlice_GetIndicesEx(item, self->size, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 796 | &start, &stop, &step, &slicelen) < 0) { |
| 797 | return NULL; |
| 798 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 800 | if (slicelen <= 0) |
| 801 | return PyBytes_FromStringAndSize("", 0); |
| 802 | else if (step == 1) |
| 803 | return PyBytes_FromStringAndSize(self->data + start, |
| 804 | slicelen); |
| 805 | else { |
| 806 | char *result_buf = (char *)PyMem_Malloc(slicelen); |
| 807 | Py_ssize_t cur, i; |
| 808 | PyObject *result; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 810 | if (result_buf == NULL) |
| 811 | return PyErr_NoMemory(); |
| 812 | for (cur = start, i = 0; i < slicelen; |
| 813 | cur += step, i++) { |
| 814 | result_buf[i] = self->data[cur]; |
| 815 | } |
| 816 | result = PyBytes_FromStringAndSize(result_buf, |
| 817 | slicelen); |
| 818 | PyMem_Free(result_buf); |
| 819 | return result; |
| 820 | } |
| 821 | } |
| 822 | else { |
| 823 | PyErr_SetString(PyExc_TypeError, |
| 824 | "mmap indices must be integers"); |
| 825 | return NULL; |
| 826 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 830 | mmap_concat(mmap_object *self, PyObject *bb) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | CHECK_VALID(NULL); |
| 833 | PyErr_SetString(PyExc_SystemError, |
| 834 | "mmaps don't support concatenation"); |
| 835 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 839 | mmap_repeat(mmap_object *self, Py_ssize_t n) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 841 | CHECK_VALID(NULL); |
| 842 | PyErr_SetString(PyExc_SystemError, |
| 843 | "mmaps don't support repeat operation"); |
| 844 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 848 | mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 849 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | const char *buf; |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | CHECK_VALID(-1); |
| 853 | if (i < 0 || (size_t)i >= self->size) { |
| 854 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 855 | return -1; |
| 856 | } |
| 857 | if (v == NULL) { |
| 858 | PyErr_SetString(PyExc_TypeError, |
| 859 | "mmap object doesn't support item deletion"); |
| 860 | return -1; |
| 861 | } |
| 862 | if (! (PyBytes_Check(v) && PyBytes_Size(v)==1) ) { |
| 863 | PyErr_SetString(PyExc_IndexError, |
| 864 | "mmap assignment must be length-1 bytes()"); |
| 865 | return -1; |
| 866 | } |
| 867 | if (!is_writable(self)) |
| 868 | return -1; |
| 869 | buf = PyBytes_AsString(v); |
| 870 | self->data[i] = buf[0]; |
| 871 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 874 | static int |
| 875 | mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value) |
| 876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | CHECK_VALID(-1); |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | if (!is_writable(self)) |
| 880 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | if (PyIndex_Check(item)) { |
| 883 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 884 | Py_ssize_t v; |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | if (i == -1 && PyErr_Occurred()) |
| 887 | return -1; |
| 888 | if (i < 0) |
| 889 | i += self->size; |
| 890 | if (i < 0 || (size_t)i >= self->size) { |
| 891 | PyErr_SetString(PyExc_IndexError, |
| 892 | "mmap index out of range"); |
| 893 | return -1; |
| 894 | } |
| 895 | if (value == NULL) { |
| 896 | PyErr_SetString(PyExc_TypeError, |
| 897 | "mmap doesn't support item deletion"); |
| 898 | return -1; |
| 899 | } |
| 900 | if (!PyIndex_Check(value)) { |
| 901 | PyErr_SetString(PyExc_TypeError, |
| 902 | "mmap item value must be an int"); |
| 903 | return -1; |
| 904 | } |
| 905 | v = PyNumber_AsSsize_t(value, PyExc_TypeError); |
| 906 | if (v == -1 && PyErr_Occurred()) |
| 907 | return -1; |
| 908 | if (v < 0 || v > 255) { |
| 909 | PyErr_SetString(PyExc_ValueError, |
| 910 | "mmap item value must be " |
| 911 | "in range(0, 256)"); |
| 912 | return -1; |
| 913 | } |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 914 | self->data[i] = (char) v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | else if (PySlice_Check(item)) { |
| 918 | Py_ssize_t start, stop, step, slicelen; |
| 919 | Py_buffer vbuf; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 920 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 921 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | self->size, &start, &stop, |
| 923 | &step, &slicelen) < 0) { |
| 924 | return -1; |
| 925 | } |
| 926 | if (value == NULL) { |
| 927 | PyErr_SetString(PyExc_TypeError, |
| 928 | "mmap object doesn't support slice deletion"); |
| 929 | return -1; |
| 930 | } |
| 931 | if (PyObject_GetBuffer(value, &vbuf, PyBUF_SIMPLE) < 0) |
| 932 | return -1; |
| 933 | if (vbuf.len != slicelen) { |
| 934 | PyErr_SetString(PyExc_IndexError, |
| 935 | "mmap slice assignment is wrong size"); |
| 936 | PyBuffer_Release(&vbuf); |
| 937 | return -1; |
| 938 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | if (slicelen == 0) { |
| 941 | } |
| 942 | else if (step == 1) { |
| 943 | memcpy(self->data + start, vbuf.buf, slicelen); |
| 944 | } |
| 945 | else { |
| 946 | Py_ssize_t cur, i; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 948 | for (cur = start, i = 0; |
| 949 | i < slicelen; |
| 950 | cur += step, i++) |
| 951 | { |
| 952 | self->data[cur] = ((char *)vbuf.buf)[i]; |
| 953 | } |
| 954 | } |
| 955 | PyBuffer_Release(&vbuf); |
| 956 | return 0; |
| 957 | } |
| 958 | else { |
| 959 | PyErr_SetString(PyExc_TypeError, |
| 960 | "mmap indices must be integer"); |
| 961 | return -1; |
| 962 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 965 | static PySequenceMethods mmap_as_sequence = { |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 966 | (lenfunc)mmap_length, /*sq_length*/ |
| 967 | (binaryfunc)mmap_concat, /*sq_concat*/ |
| 968 | (ssizeargfunc)mmap_repeat, /*sq_repeat*/ |
| 969 | (ssizeargfunc)mmap_item, /*sq_item*/ |
| 970 | 0, /*sq_slice*/ |
| 971 | (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/ |
| 972 | 0, /*sq_ass_slice*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 973 | }; |
| 974 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 975 | static PyMappingMethods mmap_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | (lenfunc)mmap_length, |
| 977 | (binaryfunc)mmap_subscript, |
| 978 | (objobjargproc)mmap_ass_subscript, |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 979 | }; |
| 980 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 981 | static PyBufferProcs mmap_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | (getbufferproc)mmap_buffer_getbuf, |
| 983 | (releasebufferproc)mmap_buffer_releasebuf, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 984 | }; |
| 985 | |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 986 | static PyObject * |
| 987 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict); |
| 988 | |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 989 | PyDoc_STRVAR(mmap_doc, |
| 990 | "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\ |
| 991 | \n\ |
| 992 | Maps length bytes from the file specified by the file handle fileno,\n\ |
| 993 | and returns a mmap object. If length is larger than the current size\n\ |
| 994 | of the file, the file is extended to contain length bytes. If length\n\ |
| 995 | is 0, the maximum length of the map is the current size of the file,\n\ |
| 996 | except that if the file is empty Windows raises an exception (you cannot\n\ |
| 997 | create an empty mapping on Windows).\n\ |
| 998 | \n\ |
| 999 | Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\ |
| 1000 | \n\ |
| 1001 | Maps length bytes from the file specified by the file descriptor fileno,\n\ |
| 1002 | and returns a mmap object. If length is 0, the maximum length of the map\n\ |
| 1003 | will be the current size of the file when mmap is called.\n\ |
| 1004 | flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\ |
| 1005 | private copy-on-write mapping, so changes to the contents of the mmap\n\ |
Benjamin Peterson | c4fe6f3 | 2008-08-19 18:57:56 +0000 | [diff] [blame] | 1006 | object will be private to this process, and MAP_SHARED creates a mapping\n\ |
Christian Heimes | e1c9811 | 2008-01-21 11:20:28 +0000 | [diff] [blame] | 1007 | that's shared with all other processes mapping the same areas of the file.\n\ |
| 1008 | The default value is MAP_SHARED.\n\ |
| 1009 | \n\ |
| 1010 | To map anonymous memory, pass -1 as the fileno (both versions)."); |
| 1011 | |
| 1012 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1013 | static PyTypeObject mmap_object_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1015 | "mmap.mmap", /* tp_name */ |
| 1016 | sizeof(mmap_object), /* tp_size */ |
| 1017 | 0, /* tp_itemsize */ |
| 1018 | /* methods */ |
| 1019 | (destructor) mmap_object_dealloc, /* tp_dealloc */ |
| 1020 | 0, /* tp_print */ |
| 1021 | 0, /* tp_getattr */ |
| 1022 | 0, /* tp_setattr */ |
| 1023 | 0, /* tp_reserved */ |
| 1024 | 0, /* tp_repr */ |
| 1025 | 0, /* tp_as_number */ |
| 1026 | &mmap_as_sequence, /*tp_as_sequence*/ |
| 1027 | &mmap_as_mapping, /*tp_as_mapping*/ |
| 1028 | 0, /*tp_hash*/ |
| 1029 | 0, /*tp_call*/ |
| 1030 | 0, /*tp_str*/ |
| 1031 | PyObject_GenericGetAttr, /*tp_getattro*/ |
| 1032 | 0, /*tp_setattro*/ |
| 1033 | &mmap_as_buffer, /*tp_as_buffer*/ |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1034 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | mmap_doc, /*tp_doc*/ |
| 1036 | 0, /* tp_traverse */ |
| 1037 | 0, /* tp_clear */ |
| 1038 | 0, /* tp_richcompare */ |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 1039 | offsetof(mmap_object, weakreflist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | 0, /* tp_iter */ |
| 1041 | 0, /* tp_iternext */ |
| 1042 | mmap_object_methods, /* tp_methods */ |
| 1043 | 0, /* tp_members */ |
Georg Brandl | 0bccc18 | 2010-08-01 14:50:00 +0000 | [diff] [blame] | 1044 | mmap_object_getset, /* tp_getset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | 0, /* tp_base */ |
| 1046 | 0, /* tp_dict */ |
| 1047 | 0, /* tp_descr_get */ |
| 1048 | 0, /* tp_descr_set */ |
| 1049 | 0, /* tp_dictoffset */ |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1050 | 0, /* tp_init */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | PyType_GenericAlloc, /* tp_alloc */ |
| 1052 | new_mmap_object, /* tp_new */ |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1053 | PyObject_Del, /* tp_free */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1054 | }; |
| 1055 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* extract the map size from the given PyObject |
| 1058 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 1059 | Returns -1 on error, with an appropriate Python exception raised. On |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1060 | success, the map size is returned. */ |
Martin v. Löwis | cfe7e09 | 2006-02-17 06:59:14 +0000 | [diff] [blame] | 1061 | static Py_ssize_t |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1062 | _GetMapSize(PyObject *o, const char* param) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | if (o == NULL) |
| 1065 | return 0; |
| 1066 | if (PyIndex_Check(o)) { |
| 1067 | Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); |
| 1068 | if (i==-1 && PyErr_Occurred()) |
| 1069 | return -1; |
| 1070 | if (i < 0) { |
| 1071 | PyErr_Format(PyExc_OverflowError, |
| 1072 | "memory mapped %s must be positive", |
| 1073 | param); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | return i; |
| 1077 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | PyErr_SetString(PyExc_TypeError, "map size must be an integral value"); |
| 1080 | return -1; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1083 | #ifdef UNIX |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1084 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 1085 | #define _Py_PARSE_OFF_T "L" |
| 1086 | #else |
| 1087 | #define _Py_PARSE_OFF_T "l" |
| 1088 | #endif |
| 1089 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1090 | static PyObject * |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1091 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1092 | { |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1093 | #ifdef HAVE_FSTAT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | struct stat st; |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1095 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | mmap_object *m_obj; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1097 | PyObject *map_size_obj = NULL; |
| 1098 | Py_ssize_t map_size; |
| 1099 | off_t offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
| 1101 | int devzero = -1; |
| 1102 | int access = (int)ACCESS_DEFAULT; |
| 1103 | static char *keywords[] = {"fileno", "length", |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1104 | "flags", "prot", |
| 1105 | "access", "offset", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1107 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii" _Py_PARSE_OFF_T, keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1108 | &fd, &map_size_obj, &flags, &prot, |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1109 | &access, &offset)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | return NULL; |
| 1111 | map_size = _GetMapSize(map_size_obj, "size"); |
| 1112 | if (map_size < 0) |
| 1113 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1114 | if (offset < 0) { |
| 1115 | PyErr_SetString(PyExc_OverflowError, |
| 1116 | "memory mapped offset must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1117 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1118 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | if ((access != (int)ACCESS_DEFAULT) && |
| 1121 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
| 1122 | return PyErr_Format(PyExc_ValueError, |
| 1123 | "mmap can't specify both access and flags, prot."); |
| 1124 | switch ((access_mode)access) { |
| 1125 | case ACCESS_READ: |
| 1126 | flags = MAP_SHARED; |
| 1127 | prot = PROT_READ; |
| 1128 | break; |
| 1129 | case ACCESS_WRITE: |
| 1130 | flags = MAP_SHARED; |
| 1131 | prot = PROT_READ | PROT_WRITE; |
| 1132 | break; |
| 1133 | case ACCESS_COPY: |
| 1134 | flags = MAP_PRIVATE; |
| 1135 | prot = PROT_READ | PROT_WRITE; |
| 1136 | break; |
| 1137 | case ACCESS_DEFAULT: |
Antoine Pitrou | 16a0a0b | 2011-03-06 01:11:03 +0100 | [diff] [blame] | 1138 | /* map prot to access type */ |
| 1139 | if ((prot & PROT_READ) && (prot & PROT_WRITE)) { |
| 1140 | /* ACCESS_DEFAULT */ |
| 1141 | } |
| 1142 | else if (prot & PROT_WRITE) { |
| 1143 | access = ACCESS_WRITE; |
| 1144 | } |
| 1145 | else { |
| 1146 | access = ACCESS_READ; |
| 1147 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | break; |
| 1149 | default: |
| 1150 | return PyErr_Format(PyExc_ValueError, |
| 1151 | "mmap invalid access parameter."); |
| 1152 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1153 | |
Victor Stinner | a6cd0cf | 2011-05-02 01:05:37 +0200 | [diff] [blame] | 1154 | #ifdef __APPLE__ |
| 1155 | /* Issue #11277: fsync(2) is not enough on OS X - a special, OS X specific |
| 1156 | fcntl(2) is necessary to force DISKSYNC and get around mmap(2) bug */ |
| 1157 | if (fd != -1) |
| 1158 | (void)fcntl(fd, F_FULLFSYNC); |
| 1159 | #endif |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1160 | #ifdef HAVE_FSTAT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { |
| 1162 | if (map_size == 0) { |
Jesus Cea | 941bfcc | 2012-09-10 00:27:55 +0200 | [diff] [blame] | 1163 | if (st.st_size == 0) { |
| 1164 | PyErr_SetString(PyExc_ValueError, |
| 1165 | "cannot mmap an empty file"); |
| 1166 | return NULL; |
| 1167 | } |
Antoine Pitrou | 305bc9e | 2011-01-20 21:07:24 +0000 | [diff] [blame] | 1168 | if (offset >= st.st_size) { |
| 1169 | PyErr_SetString(PyExc_ValueError, |
| 1170 | "mmap offset is greater than file size"); |
| 1171 | return NULL; |
| 1172 | } |
Richard Oudkerk | 0d09ba8 | 2013-02-13 12:18:03 +0000 | [diff] [blame] | 1173 | if (st.st_size - offset > PY_SSIZE_T_MAX) { |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1174 | PyErr_SetString(PyExc_ValueError, |
| 1175 | "mmap length is too large"); |
Richard Oudkerk | 0d09ba8 | 2013-02-13 12:18:03 +0000 | [diff] [blame] | 1176 | return NULL; |
| 1177 | } |
| 1178 | map_size = (Py_ssize_t) (st.st_size - offset); |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1179 | } else if (offset + (size_t)map_size > st.st_size) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | PyErr_SetString(PyExc_ValueError, |
| 1181 | "mmap length is greater than file size"); |
| 1182 | return NULL; |
| 1183 | } |
| 1184 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 1185 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
| 1187 | if (m_obj == NULL) {return NULL;} |
| 1188 | m_obj->data = NULL; |
| 1189 | m_obj->size = (size_t) map_size; |
| 1190 | m_obj->pos = (size_t) 0; |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 1191 | m_obj->weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | m_obj->exports = 0; |
| 1193 | m_obj->offset = offset; |
| 1194 | if (fd == -1) { |
| 1195 | m_obj->fd = -1; |
| 1196 | /* Assume the caller wants to map anonymous memory. |
| 1197 | This is the same behaviour as Windows. mmap.mmap(-1, size) |
| 1198 | on both Windows and Unix map anonymous memory. |
| 1199 | */ |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1200 | #ifdef MAP_ANONYMOUS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1201 | /* BSD way to map anonymous memory */ |
| 1202 | flags |= MAP_ANONYMOUS; |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1203 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1204 | /* SVR4 method to map anonymous memory is to open /dev/zero */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1205 | fd = devzero = _Py_open("/dev/zero", O_RDWR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | if (devzero == -1) { |
| 1207 | Py_DECREF(m_obj); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1208 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | return NULL; |
| 1210 | } |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1211 | #endif |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 1212 | } |
| 1213 | else { |
| 1214 | m_obj->fd = _Py_dup(fd); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | if (m_obj->fd == -1) { |
| 1216 | Py_DECREF(m_obj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1217 | return NULL; |
| 1218 | } |
| 1219 | } |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | m_obj->data = mmap(NULL, map_size, |
| 1222 | prot, flags, |
| 1223 | fd, offset); |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1225 | if (devzero != -1) { |
| 1226 | close(devzero); |
| 1227 | } |
| 1228 | |
| 1229 | if (m_obj->data == (char *)-1) { |
| 1230 | m_obj->data = NULL; |
| 1231 | Py_DECREF(m_obj); |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1232 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | return NULL; |
| 1234 | } |
| 1235 | m_obj->access = (access_mode)access; |
| 1236 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1237 | } |
| 1238 | #endif /* UNIX */ |
| 1239 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1240 | #ifdef MS_WINDOWS |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1241 | |
| 1242 | /* A note on sizes and offsets: while the actual map size must hold in a |
| 1243 | Py_ssize_t, both the total file size and the start offset can be longer |
| 1244 | than a Py_ssize_t, so we use PY_LONG_LONG which is always 64-bit. |
| 1245 | */ |
| 1246 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1247 | static PyObject * |
Georg Brandl | 86def6c | 2008-01-21 20:36:10 +0000 | [diff] [blame] | 1248 | new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1249 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1250 | mmap_object *m_obj; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1251 | PyObject *map_size_obj = NULL; |
| 1252 | Py_ssize_t map_size; |
| 1253 | PY_LONG_LONG offset = 0, size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | DWORD off_hi; /* upper 32 bits of offset */ |
| 1255 | DWORD off_lo; /* lower 32 bits of offset */ |
| 1256 | DWORD size_hi; /* upper 32 bits of size */ |
| 1257 | DWORD size_lo; /* lower 32 bits of size */ |
| 1258 | char *tagname = ""; |
| 1259 | DWORD dwErr = 0; |
| 1260 | int fileno; |
| 1261 | HANDLE fh = 0; |
| 1262 | int access = (access_mode)ACCESS_DEFAULT; |
| 1263 | DWORD flProtect, dwDesiredAccess; |
| 1264 | static char *keywords[] = { "fileno", "length", |
Stefan Krah | 2318699 | 2012-03-06 15:37:36 +0100 | [diff] [blame] | 1265 | "tagname", |
| 1266 | "access", "offset", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1267 | |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1268 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziL", keywords, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1269 | &fileno, &map_size_obj, |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1270 | &tagname, &access, &offset)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1271 | return NULL; |
| 1272 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1274 | switch((access_mode)access) { |
| 1275 | case ACCESS_READ: |
| 1276 | flProtect = PAGE_READONLY; |
| 1277 | dwDesiredAccess = FILE_MAP_READ; |
| 1278 | break; |
| 1279 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 1280 | flProtect = PAGE_READWRITE; |
| 1281 | dwDesiredAccess = FILE_MAP_WRITE; |
| 1282 | break; |
| 1283 | case ACCESS_COPY: |
| 1284 | flProtect = PAGE_WRITECOPY; |
| 1285 | dwDesiredAccess = FILE_MAP_COPY; |
| 1286 | break; |
| 1287 | default: |
| 1288 | return PyErr_Format(PyExc_ValueError, |
| 1289 | "mmap invalid access parameter."); |
| 1290 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1292 | map_size = _GetMapSize(map_size_obj, "size"); |
| 1293 | if (map_size < 0) |
| 1294 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1295 | if (offset < 0) { |
| 1296 | PyErr_SetString(PyExc_OverflowError, |
| 1297 | "memory mapped offset must be positive"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1298 | return NULL; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1299 | } |
Tim Peters | ec0a5f0 | 2006-02-16 23:47:20 +0000 | [diff] [blame] | 1300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | /* assume -1 and 0 both mean invalid filedescriptor |
| 1302 | to 'anonymously' map memory. |
| 1303 | XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5. |
| 1304 | XXX: Should this code be added? |
| 1305 | if (fileno == 0) |
| 1306 | PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1307 | "don't use 0 for anonymous memory", |
| 1308 | 1); |
| 1309 | */ |
| 1310 | if (fileno != -1 && fileno != 0) { |
Brian Curtin | ea47eaa | 2010-08-01 15:26:26 +0000 | [diff] [blame] | 1311 | /* Ensure that fileno is within the CRT's valid range */ |
| 1312 | if (_PyVerify_fd(fileno) == 0) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1313 | PyErr_SetFromErrno(PyExc_OSError); |
Brian Curtin | ea47eaa | 2010-08-01 15:26:26 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | fh = (HANDLE)_get_osfhandle(fileno); |
| 1317 | if (fh==(HANDLE)-1) { |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1318 | PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1319 | return NULL; |
| 1320 | } |
| 1321 | /* Win9x appears to need us seeked to zero */ |
| 1322 | lseek(fileno, 0, SEEK_SET); |
| 1323 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | m_obj = (mmap_object *)type->tp_alloc(type, 0); |
| 1326 | if (m_obj == NULL) |
| 1327 | return NULL; |
| 1328 | /* Set every field to an invalid marker, so we can safely |
| 1329 | destruct the object in the face of failure */ |
| 1330 | m_obj->data = NULL; |
| 1331 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
| 1332 | m_obj->map_handle = NULL; |
| 1333 | m_obj->tagname = NULL; |
| 1334 | m_obj->offset = offset; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | if (fh) { |
| 1337 | /* It is necessary to duplicate the handle, so the |
| 1338 | Python code can close it on us */ |
| 1339 | if (!DuplicateHandle( |
| 1340 | GetCurrentProcess(), /* source process handle */ |
| 1341 | fh, /* handle to be duplicated */ |
| 1342 | GetCurrentProcess(), /* target proc handle */ |
| 1343 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1344 | 0, /* access - ignored due to options value */ |
| 1345 | FALSE, /* inherited by child processes? */ |
| 1346 | DUPLICATE_SAME_ACCESS)) { /* options */ |
| 1347 | dwErr = GetLastError(); |
| 1348 | Py_DECREF(m_obj); |
| 1349 | PyErr_SetFromWindowsErr(dwErr); |
| 1350 | return NULL; |
| 1351 | } |
| 1352 | if (!map_size) { |
| 1353 | DWORD low,high; |
| 1354 | low = GetFileSize(fh, &high); |
| 1355 | /* low might just happen to have the value INVALID_FILE_SIZE; |
| 1356 | so we need to check the last error also. */ |
| 1357 | if (low == INVALID_FILE_SIZE && |
| 1358 | (dwErr = GetLastError()) != NO_ERROR) { |
| 1359 | Py_DECREF(m_obj); |
| 1360 | return PyErr_SetFromWindowsErr(dwErr); |
| 1361 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1362 | |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1363 | size = (((PY_LONG_LONG) high) << 32) + low; |
Jesus Cea | 1f2799b | 2012-09-10 22:49:50 +0200 | [diff] [blame] | 1364 | if (size == 0) { |
| 1365 | PyErr_SetString(PyExc_ValueError, |
| 1366 | "cannot mmap an empty file"); |
Jesus Cea | e8db356 | 2012-09-10 22:58:07 +0200 | [diff] [blame] | 1367 | Py_DECREF(m_obj); |
Jesus Cea | 1f2799b | 2012-09-10 22:49:50 +0200 | [diff] [blame] | 1368 | return NULL; |
| 1369 | } |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1370 | if (offset >= size) { |
Antoine Pitrou | 305bc9e | 2011-01-20 21:07:24 +0000 | [diff] [blame] | 1371 | PyErr_SetString(PyExc_ValueError, |
| 1372 | "mmap offset is greater than file size"); |
| 1373 | Py_DECREF(m_obj); |
| 1374 | return NULL; |
| 1375 | } |
Richard Oudkerk | 0d09ba8 | 2013-02-13 12:18:03 +0000 | [diff] [blame] | 1376 | if (size - offset > PY_SSIZE_T_MAX) { |
| 1377 | PyErr_SetString(PyExc_ValueError, |
| 1378 | "mmap length is too large"); |
| 1379 | Py_DECREF(m_obj); |
| 1380 | return NULL; |
| 1381 | } |
| 1382 | m_obj->size = (Py_ssize_t) (size - offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | } else { |
| 1384 | m_obj->size = map_size; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1385 | size = offset + map_size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | } |
| 1387 | } |
| 1388 | else { |
| 1389 | m_obj->size = map_size; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1390 | size = offset + map_size; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | /* set the initial position */ |
| 1394 | m_obj->pos = (size_t) 0; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1395 | |
Antoine Pitrou | c53204b | 2013-08-05 23:17:30 +0200 | [diff] [blame] | 1396 | m_obj->weakreflist = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | m_obj->exports = 0; |
| 1398 | /* set the tag name */ |
| 1399 | if (tagname != NULL && *tagname != '\0') { |
| 1400 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1401 | if (m_obj->tagname == NULL) { |
| 1402 | PyErr_NoMemory(); |
| 1403 | Py_DECREF(m_obj); |
| 1404 | return NULL; |
| 1405 | } |
| 1406 | strcpy(m_obj->tagname, tagname); |
| 1407 | } |
| 1408 | else |
| 1409 | m_obj->tagname = NULL; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | m_obj->access = (access_mode)access; |
Antoine Pitrou | 97696cb | 2011-02-21 23:46:27 +0000 | [diff] [blame] | 1412 | size_hi = (DWORD)(size >> 32); |
| 1413 | size_lo = (DWORD)(size & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | off_hi = (DWORD)(offset >> 32); |
| 1415 | off_lo = (DWORD)(offset & 0xFFFFFFFF); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | /* For files, it would be sufficient to pass 0 as size. |
| 1417 | For anonymous maps, we have to pass the size explicitly. */ |
| 1418 | m_obj->map_handle = CreateFileMapping(m_obj->file_handle, |
| 1419 | NULL, |
| 1420 | flProtect, |
| 1421 | size_hi, |
| 1422 | size_lo, |
| 1423 | m_obj->tagname); |
| 1424 | if (m_obj->map_handle != NULL) { |
| 1425 | m_obj->data = (char *) MapViewOfFile(m_obj->map_handle, |
| 1426 | dwDesiredAccess, |
| 1427 | off_hi, |
| 1428 | off_lo, |
| 1429 | m_obj->size); |
| 1430 | if (m_obj->data != NULL) |
| 1431 | return (PyObject *)m_obj; |
| 1432 | else { |
| 1433 | dwErr = GetLastError(); |
| 1434 | CloseHandle(m_obj->map_handle); |
| 1435 | m_obj->map_handle = NULL; |
| 1436 | } |
| 1437 | } else |
| 1438 | dwErr = GetLastError(); |
| 1439 | Py_DECREF(m_obj); |
| 1440 | PyErr_SetFromWindowsErr(dwErr); |
| 1441 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1442 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1443 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1444 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1445 | static void |
| 1446 | setint(PyObject *d, const char *name, long value) |
| 1447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | PyObject *o = PyLong_FromLong(value); |
| 1449 | if (o && PyDict_SetItemString(d, name, o) == 0) { |
| 1450 | Py_DECREF(o); |
| 1451 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1454 | |
| 1455 | static struct PyModuleDef mmapmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | PyModuleDef_HEAD_INIT, |
| 1457 | "mmap", |
| 1458 | NULL, |
| 1459 | -1, |
| 1460 | NULL, |
| 1461 | NULL, |
| 1462 | NULL, |
| 1463 | NULL, |
| 1464 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1465 | }; |
| 1466 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1467 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 1468 | PyInit_mmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | PyObject *dict, *module; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | if (PyType_Ready(&mmap_object_type) < 0) |
| 1473 | return NULL; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | module = PyModule_Create(&mmapmodule); |
| 1476 | if (module == NULL) |
| 1477 | return NULL; |
| 1478 | dict = PyModule_GetDict(module); |
| 1479 | if (!dict) |
| 1480 | return NULL; |
Antoine Pitrou | 6b4883d | 2011-10-12 02:54:14 +0200 | [diff] [blame] | 1481 | PyDict_SetItemString(dict, "error", PyExc_OSError); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1483 | #ifdef PROT_EXEC |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | setint(dict, "PROT_EXEC", PROT_EXEC); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1485 | #endif |
| 1486 | #ifdef PROT_READ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | setint(dict, "PROT_READ", PROT_READ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1488 | #endif |
| 1489 | #ifdef PROT_WRITE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1490 | setint(dict, "PROT_WRITE", PROT_WRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1491 | #endif |
| 1492 | |
| 1493 | #ifdef MAP_SHARED |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | setint(dict, "MAP_SHARED", MAP_SHARED); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1495 | #endif |
| 1496 | #ifdef MAP_PRIVATE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | setint(dict, "MAP_PRIVATE", MAP_PRIVATE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | #ifdef MAP_DENYWRITE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1501 | #endif |
| 1502 | #ifdef MAP_EXECUTABLE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1504 | #endif |
Neal Norwitz | 0e6bc8c | 2006-02-05 05:45:43 +0000 | [diff] [blame] | 1505 | #ifdef MAP_ANONYMOUS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | setint(dict, "MAP_ANON", MAP_ANONYMOUS); |
| 1507 | setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1508 | #endif |
| 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | setint(dict, "PAGESIZE", (long)my_getpagesize()); |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()); |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1514 | setint(dict, "ACCESS_READ", ACCESS_READ); |
| 1515 | setint(dict, "ACCESS_WRITE", ACCESS_WRITE); |
| 1516 | setint(dict, "ACCESS_COPY", ACCESS_COPY); |
| 1517 | return module; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1518 | } |