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 | |
| 6 | / mmapmodule.cpp -- map a view of a file into memory |
| 7 | / |
| 8 | / todo: need permission flags, perhaps a 'chsize' analog |
| 9 | / not all functions check range yet!!! |
| 10 | / |
| 11 | / |
| 12 | / Note: This module currently only deals with 32-bit file |
| 13 | / sizes. |
| 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 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 21 | #include <Python.h> |
| 22 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 23 | #ifndef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 24 | #define UNIX |
| 25 | #endif |
| 26 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 27 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 28 | #include <windows.h> |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 29 | static int |
| 30 | my_getpagesize(void) |
| 31 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 32 | SYSTEM_INFO si; |
| 33 | GetSystemInfo(&si); |
| 34 | return si.dwPageSize; |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 35 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | #ifdef UNIX |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 39 | #include <sys/mman.h> |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 40 | #include <sys/stat.h> |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 41 | |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 42 | #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE) |
| 43 | static int |
| 44 | my_getpagesize(void) |
| 45 | { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 46 | return sysconf(_SC_PAGESIZE); |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 47 | } |
| 48 | #else |
| 49 | #define my_getpagesize getpagesize |
| 50 | #endif |
| 51 | |
Guido van Rossum | 4b36e6b | 2000-09-25 13:16:15 +0000 | [diff] [blame] | 52 | #endif /* UNIX */ |
| 53 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 54 | #include <string.h> |
| 55 | #include <sys/types.h> |
| 56 | |
| 57 | static PyObject *mmap_module_error; |
| 58 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 59 | typedef enum |
| 60 | { |
| 61 | ACCESS_DEFAULT, |
| 62 | ACCESS_READ, |
| 63 | ACCESS_WRITE, |
| 64 | ACCESS_COPY |
| 65 | } access_mode; |
| 66 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 67 | typedef struct { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 68 | PyObject_HEAD |
| 69 | char * data; |
| 70 | size_t size; |
| 71 | size_t pos; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 72 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 73 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 74 | HANDLE map_handle; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 75 | HANDLE file_handle; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 76 | char * tagname; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 80 | int fd; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 81 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 82 | |
| 83 | access_mode access; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 84 | } mmap_object; |
| 85 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 86 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 87 | static void |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 88 | mmap_object_dealloc(mmap_object *m_obj) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 89 | { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 90 | #ifdef MS_WINDOWS |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 91 | if (m_obj->data != NULL) |
| 92 | UnmapViewOfFile (m_obj->data); |
| 93 | if (m_obj->map_handle != INVALID_HANDLE_VALUE) |
| 94 | CloseHandle (m_obj->map_handle); |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 95 | if (m_obj->file_handle != INVALID_HANDLE_VALUE) |
| 96 | CloseHandle (m_obj->file_handle); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 97 | if (m_obj->tagname) |
| 98 | PyMem_Free(m_obj->tagname); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 99 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 100 | |
| 101 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 102 | if (m_obj->fd >= 0) |
| 103 | (void) close(m_obj->fd); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 104 | if (m_obj->data!=NULL) { |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 105 | msync(m_obj->data, m_obj->size, MS_SYNC); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 106 | munmap(m_obj->data, m_obj->size); |
| 107 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 108 | #endif /* UNIX */ |
| 109 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 110 | PyObject_Del(m_obj); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 114 | mmap_close_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 115 | { |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 116 | if (!PyArg_ParseTuple(args, ":close")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 117 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 118 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 119 | /* For each resource we maintain, we need to check |
| 120 | the value is valid, and if so, free the resource |
| 121 | and set the member value to an invalid value so |
| 122 | the dealloc does not attempt to resource clearing |
| 123 | again. |
| 124 | TODO - should we check for errors in the close operations??? |
| 125 | */ |
| 126 | if (self->data != NULL) { |
| 127 | UnmapViewOfFile (self->data); |
| 128 | self->data = NULL; |
| 129 | } |
| 130 | if (self->map_handle != INVALID_HANDLE_VALUE) { |
| 131 | CloseHandle (self->map_handle); |
| 132 | self->map_handle = INVALID_HANDLE_VALUE; |
| 133 | } |
| 134 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
| 135 | CloseHandle (self->file_handle); |
| 136 | self->file_handle = INVALID_HANDLE_VALUE; |
| 137 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 138 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 139 | |
| 140 | #ifdef UNIX |
Neal Norwitz | 6eac200 | 2005-11-02 05:36:37 +0000 | [diff] [blame] | 141 | (void) close(self->fd); |
| 142 | self->fd = -1; |
Neal Norwitz | e604c02 | 2003-01-10 20:52:16 +0000 | [diff] [blame] | 143 | if (self->data != NULL) { |
| 144 | munmap(self->data, self->size); |
| 145 | self->data = NULL; |
| 146 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 147 | #endif |
| 148 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 149 | Py_INCREF (Py_None); |
| 150 | return (Py_None); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 153 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 154 | #define CHECK_VALID(err) \ |
| 155 | do { \ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 156 | if (self->map_handle == INVALID_HANDLE_VALUE) { \ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 157 | PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \ |
| 158 | return err; \ |
| 159 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 160 | } while (0) |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 161 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 162 | |
| 163 | #ifdef UNIX |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 164 | #define CHECK_VALID(err) \ |
| 165 | do { \ |
| 166 | if (self->data == NULL) { \ |
| 167 | PyErr_SetString (PyExc_ValueError, "mmap closed or invalid"); \ |
| 168 | return err; \ |
| 169 | } \ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 170 | } while (0) |
| 171 | #endif /* UNIX */ |
| 172 | |
| 173 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 174 | mmap_read_byte_method(mmap_object *self, |
| 175 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 176 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 177 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 178 | if (!PyArg_ParseTuple(args, ":read_byte")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 179 | return NULL; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 180 | if (self->pos < self->size) { |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 181 | char value = self->data[self->pos]; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 182 | self->pos += 1; |
Tim Peters | d6283b8 | 2001-05-09 18:48:26 +0000 | [diff] [blame] | 183 | return Py_BuildValue("c", value); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 184 | } else { |
| 185 | PyErr_SetString (PyExc_ValueError, "read byte out of range"); |
| 186 | return NULL; |
| 187 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 191 | mmap_read_line_method(mmap_object *self, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 192 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 193 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 194 | char *start = self->data+self->pos; |
| 195 | char *eof = self->data+self->size; |
| 196 | char *eol; |
| 197 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 199 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 200 | if (!PyArg_ParseTuple(args, ":readline")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 201 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 202 | |
Fred Drake | 56a87a0 | 2000-04-04 18:17:35 +0000 | [diff] [blame] | 203 | eol = memchr(start, '\n', self->size - self->pos); |
| 204 | if (!eol) |
| 205 | eol = eof; |
| 206 | else |
| 207 | ++eol; /* we're interested in the position after the |
| 208 | newline. */ |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 209 | result = PyString_FromStringAndSize(start, (eol - start)); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 210 | self->pos += (eol - start); |
| 211 | return (result); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 215 | mmap_read_method(mmap_object *self, |
| 216 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 217 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 218 | long num_bytes; |
| 219 | PyObject *result; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 220 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 221 | CHECK_VALID(NULL); |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 222 | if (!PyArg_ParseTuple(args, "l:read", &num_bytes)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 223 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 224 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 225 | /* silently 'adjust' out-of-range requests */ |
| 226 | if ((self->pos + num_bytes) > self->size) { |
| 227 | num_bytes -= (self->pos+num_bytes) - self->size; |
| 228 | } |
| 229 | result = Py_BuildValue("s#", self->data+self->pos, num_bytes); |
| 230 | self->pos += num_bytes; |
| 231 | return (result); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 235 | mmap_find_method(mmap_object *self, |
| 236 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 237 | { |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 238 | long start = self->pos; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 239 | char *needle; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 240 | int len; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 242 | CHECK_VALID(NULL); |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 243 | if (!PyArg_ParseTuple (args, "s#|l:find", &needle, &len, &start)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 244 | return NULL; |
| 245 | } else { |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 246 | char *p; |
| 247 | char *e = self->data + self->size; |
| 248 | |
| 249 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 250 | start += self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 251 | if (start < 0) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 252 | start = 0; |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 253 | else if ((size_t)start > self->size) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 254 | start = self->size; |
Greg Stein | 834f4dd | 2001-05-14 09:32:26 +0000 | [diff] [blame] | 255 | |
Tim Peters | c9ffa06 | 2002-03-08 05:43:32 +0000 | [diff] [blame] | 256 | for (p = self->data + start; p + len <= e; ++p) { |
| 257 | int i; |
| 258 | for (i = 0; i < len && needle[i] == p[i]; ++i) |
| 259 | /* nothing */; |
| 260 | if (i == len) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 261 | return Py_BuildValue ( |
Tim Peters | d401edd | 2001-05-14 23:19:12 +0000 | [diff] [blame] | 262 | "l", |
| 263 | (long) (p - self->data)); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 264 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 265 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 266 | return Py_BuildValue ("l", (long) -1); |
| 267 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 270 | static int |
| 271 | is_writeable(mmap_object *self) |
| 272 | { |
| 273 | if (self->access != ACCESS_READ) |
| 274 | return 1; |
| 275 | PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map."); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int |
| 280 | is_resizeable(mmap_object *self) |
| 281 | { |
| 282 | if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT)) |
| 283 | return 1; |
| 284 | PyErr_Format(PyExc_TypeError, |
| 285 | "mmap can't resize a readonly or copy-on-write memory map."); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 290 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 291 | mmap_write_method(mmap_object *self, |
| 292 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 293 | { |
Neal Norwitz | 4d933fe | 2003-02-07 19:44:56 +0000 | [diff] [blame] | 294 | int length; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 295 | char *data; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 297 | CHECK_VALID(NULL); |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 298 | if (!PyArg_ParseTuple (args, "s#:write", &data, &length)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 299 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 300 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 301 | if (!is_writeable(self)) |
| 302 | return NULL; |
| 303 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 304 | if ((self->pos + length) > self->size) { |
| 305 | PyErr_SetString (PyExc_ValueError, "data out of range"); |
| 306 | return NULL; |
| 307 | } |
| 308 | memcpy (self->data+self->pos, data, length); |
| 309 | self->pos = self->pos+length; |
| 310 | Py_INCREF (Py_None); |
| 311 | return (Py_None); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 315 | mmap_write_byte_method(mmap_object *self, |
| 316 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 317 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 318 | char value; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 320 | CHECK_VALID(NULL); |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 321 | if (!PyArg_ParseTuple (args, "c:write_byte", &value)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 322 | return(NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 323 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 324 | if (!is_writeable(self)) |
| 325 | return NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 326 | *(self->data+self->pos) = value; |
| 327 | self->pos += 1; |
| 328 | Py_INCREF (Py_None); |
| 329 | return (Py_None); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 330 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 331 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 332 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 333 | mmap_size_method(mmap_object *self, |
| 334 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 335 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 336 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 337 | if (!PyArg_ParseTuple(args, ":size")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 338 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 339 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 340 | #ifdef MS_WINDOWS |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 341 | if (self->file_handle != INVALID_HANDLE_VALUE) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 342 | return (Py_BuildValue ( |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 343 | "l", (long) |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 344 | GetFileSize (self->file_handle, NULL))); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 345 | } else { |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 346 | return (Py_BuildValue ("l", (long) self->size) ); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 347 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 348 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 349 | |
| 350 | #ifdef UNIX |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 351 | { |
| 352 | struct stat buf; |
| 353 | if (-1 == fstat(self->fd, &buf)) { |
| 354 | PyErr_SetFromErrno(mmap_module_error); |
| 355 | return NULL; |
| 356 | } |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 357 | return (Py_BuildValue ("l", (long) buf.st_size) ); |
Andrew M. Kuchling | 7b9fb92 | 2000-06-17 22:41:22 +0000 | [diff] [blame] | 358 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 359 | #endif /* UNIX */ |
| 360 | } |
| 361 | |
| 362 | /* This assumes that you want the entire file mapped, |
| 363 | / and when recreating the map will make the new file |
| 364 | / have the new size |
| 365 | / |
| 366 | / Is this really necessary? This could easily be done |
| 367 | / from python by just closing and re-opening with the |
| 368 | / new size? |
| 369 | */ |
| 370 | |
| 371 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 372 | mmap_resize_method(mmap_object *self, |
| 373 | PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 374 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 375 | unsigned long new_size; |
| 376 | CHECK_VALID(NULL); |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 377 | if (!PyArg_ParseTuple (args, "k:resize", &new_size) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 378 | !is_resizeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 379 | return NULL; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 380 | #ifdef MS_WINDOWS |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 381 | } else { |
| 382 | DWORD dwErrCode = 0; |
| 383 | /* First, unmap the file view */ |
| 384 | UnmapViewOfFile (self->data); |
| 385 | /* Close the mapping object */ |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 386 | CloseHandle (self->map_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 387 | /* Move to the desired EOF position */ |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 388 | SetFilePointer (self->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 389 | new_size, NULL, FILE_BEGIN); |
| 390 | /* Change the size of the file */ |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 391 | SetEndOfFile (self->file_handle); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 392 | /* Create another mapping object and remap the file view */ |
| 393 | self->map_handle = CreateFileMapping ( |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 394 | self->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 395 | NULL, |
| 396 | PAGE_READWRITE, |
| 397 | 0, |
| 398 | new_size, |
| 399 | self->tagname); |
| 400 | if (self->map_handle != NULL) { |
| 401 | self->data = (char *) MapViewOfFile (self->map_handle, |
| 402 | FILE_MAP_WRITE, |
| 403 | 0, |
| 404 | 0, |
| 405 | 0); |
| 406 | if (self->data != NULL) { |
| 407 | self->size = new_size; |
| 408 | Py_INCREF (Py_None); |
| 409 | return Py_None; |
| 410 | } else { |
| 411 | dwErrCode = GetLastError(); |
| 412 | } |
| 413 | } else { |
| 414 | dwErrCode = GetLastError(); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 415 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 416 | PyErr_SetFromWindowsErr(dwErrCode); |
| 417 | return (NULL); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 418 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 419 | |
| 420 | #ifdef UNIX |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 421 | #ifndef HAVE_MREMAP |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 422 | } else { |
| 423 | PyErr_SetString(PyExc_SystemError, |
| 424 | "mmap: resizing not available--no mremap()"); |
| 425 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 426 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 427 | } else { |
Armin Rigo | 335ffe8 | 2005-09-20 19:04:02 +0000 | [diff] [blame] | 428 | void *newmap; |
| 429 | |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 430 | if (ftruncate(self->fd, new_size) == -1) { |
| 431 | PyErr_SetFromErrno(mmap_module_error); |
| 432 | return NULL; |
| 433 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 434 | |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 435 | #ifdef MREMAP_MAYMOVE |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 436 | newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 437 | #else |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 438 | newmap = mremap(self->data, self->size, new_size, 0); |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 439 | #endif |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 440 | if (newmap == (void *)-1) |
| 441 | { |
| 442 | PyErr_SetFromErrno(mmap_module_error); |
| 443 | return NULL; |
| 444 | } |
| 445 | self->data = newmap; |
| 446 | self->size = new_size; |
| 447 | Py_INCREF(Py_None); |
| 448 | return Py_None; |
Andrew M. Kuchling | 6fef30e | 2000-06-18 14:51:21 +0000 | [diff] [blame] | 449 | #endif /* HAVE_MREMAP */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 450 | #endif /* UNIX */ |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 451 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 455 | mmap_tell_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 456 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 457 | CHECK_VALID(NULL); |
Andrew M. Kuchling | 841b9fb | 2000-06-03 20:43:43 +0000 | [diff] [blame] | 458 | if (!PyArg_ParseTuple(args, ":tell")) |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 459 | return NULL; |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 460 | return (Py_BuildValue ("l", (long) self->pos) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 464 | mmap_flush_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 465 | { |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 466 | unsigned long offset = 0; |
| 467 | unsigned long size = self->size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 468 | CHECK_VALID(NULL); |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 469 | if (!PyArg_ParseTuple (args, "|kk:flush", &offset, &size)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 470 | return NULL; |
| 471 | } else if ((offset + size) > self->size) { |
| 472 | PyErr_SetString (PyExc_ValueError, |
| 473 | "flush values out of range"); |
| 474 | return NULL; |
| 475 | } else { |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 476 | #ifdef MS_WINDOWS |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 477 | return (Py_BuildValue("l", (long) |
| 478 | FlushViewOfFile(self->data+offset, size))); |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 479 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 480 | #ifdef UNIX |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 481 | /* XXX semantics of return value? */ |
| 482 | /* XXX flags for msync? */ |
| 483 | if (-1 == msync(self->data + offset, size, |
Andrew M. Kuchling | 9bc5f33 | 2000-06-18 04:25:08 +0000 | [diff] [blame] | 484 | MS_SYNC)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 485 | { |
| 486 | PyErr_SetFromErrno(mmap_module_error); |
| 487 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 488 | } |
Fredrik Lundh | 5cccf50 | 2000-07-09 13:16:13 +0000 | [diff] [blame] | 489 | return Py_BuildValue ("l", (long) 0); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 490 | #endif /* UNIX */ |
| 491 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 495 | mmap_seek_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 496 | { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 497 | int dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 498 | int how=0; |
| 499 | CHECK_VALID(NULL); |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 500 | if (!PyArg_ParseTuple (args, "i|i:seek", &dist, &how)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 501 | return(NULL); |
| 502 | } else { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 503 | size_t where; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 504 | switch (how) { |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 505 | case 0: /* relative to start */ |
| 506 | if (dist < 0) |
| 507 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 508 | where = dist; |
| 509 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 510 | case 1: /* relative to current position */ |
| 511 | if ((int)self->pos + dist < 0) |
| 512 | goto onoutofrange; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 513 | where = self->pos + dist; |
| 514 | break; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 515 | case 2: /* relative to end */ |
| 516 | if ((int)self->size + dist < 0) |
| 517 | goto onoutofrange; |
| 518 | where = self->size + dist; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 519 | break; |
| 520 | default: |
| 521 | PyErr_SetString (PyExc_ValueError, |
| 522 | "unknown seek type"); |
| 523 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 524 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 525 | if (where > self->size) |
| 526 | goto onoutofrange; |
| 527 | self->pos = where; |
| 528 | Py_INCREF (Py_None); |
| 529 | return (Py_None); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 530 | } |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 531 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 532 | onoutofrange: |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 533 | PyErr_SetString (PyExc_ValueError, "seek out of range"); |
| 534 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 538 | mmap_move_method(mmap_object *self, PyObject *args) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 539 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 540 | unsigned long dest, src, count; |
| 541 | CHECK_VALID(NULL); |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 542 | if (!PyArg_ParseTuple (args, "kkk:move", &dest, &src, &count) || |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 543 | !is_writeable(self)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 544 | return NULL; |
| 545 | } else { |
| 546 | /* bounds check the values */ |
| 547 | if (/* end of source after end of data?? */ |
| 548 | ((src+count) > self->size) |
| 549 | /* dest will fit? */ |
| 550 | || (dest+count > self->size)) { |
| 551 | PyErr_SetString (PyExc_ValueError, |
| 552 | "source or destination out of range"); |
| 553 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 554 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 555 | memmove (self->data+dest, self->data+src, count); |
| 556 | Py_INCREF (Py_None); |
| 557 | return Py_None; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 558 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 559 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | static struct PyMethodDef mmap_object_methods[] = { |
Martin v. Löwis | 43b936d | 2002-01-17 23:15:58 +0000 | [diff] [blame] | 563 | {"close", (PyCFunction) mmap_close_method, METH_VARARGS}, |
| 564 | {"find", (PyCFunction) mmap_find_method, METH_VARARGS}, |
| 565 | {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS}, |
| 566 | {"move", (PyCFunction) mmap_move_method, METH_VARARGS}, |
| 567 | {"read", (PyCFunction) mmap_read_method, METH_VARARGS}, |
| 568 | {"read_byte", (PyCFunction) mmap_read_byte_method, METH_VARARGS}, |
| 569 | {"readline", (PyCFunction) mmap_read_line_method, METH_VARARGS}, |
| 570 | {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS}, |
| 571 | {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS}, |
| 572 | {"size", (PyCFunction) mmap_size_method, METH_VARARGS}, |
| 573 | {"tell", (PyCFunction) mmap_tell_method, METH_VARARGS}, |
| 574 | {"write", (PyCFunction) mmap_write_method, METH_VARARGS}, |
| 575 | {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS}, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 576 | {NULL, NULL} /* sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | /* Functions for treating an mmap'ed file as a buffer */ |
| 580 | |
| 581 | static int |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 582 | mmap_buffer_getreadbuf(mmap_object *self, int index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 583 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 584 | CHECK_VALID(-1); |
| 585 | if ( index != 0 ) { |
| 586 | PyErr_SetString(PyExc_SystemError, |
| 587 | "Accessing non-existent mmap segment"); |
| 588 | return -1; |
| 589 | } |
| 590 | *ptr = self->data; |
| 591 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 595 | mmap_buffer_getwritebuf(mmap_object *self, int index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 596 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 597 | CHECK_VALID(-1); |
| 598 | if ( index != 0 ) { |
| 599 | PyErr_SetString(PyExc_SystemError, |
| 600 | "Accessing non-existent mmap segment"); |
| 601 | return -1; |
| 602 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 603 | if (!is_writeable(self)) |
| 604 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 605 | *ptr = self->data; |
| 606 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 610 | mmap_buffer_getsegcount(mmap_object *self, int *lenp) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 611 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 612 | CHECK_VALID(-1); |
| 613 | if (lenp) |
| 614 | *lenp = self->size; |
| 615 | return 1; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 619 | mmap_buffer_getcharbuffer(mmap_object *self, int index, const void **ptr) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 620 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 621 | if ( index != 0 ) { |
| 622 | PyErr_SetString(PyExc_SystemError, |
| 623 | "accessing non-existent buffer segment"); |
| 624 | return -1; |
| 625 | } |
| 626 | *ptr = (const char *)self->data; |
| 627 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 631 | mmap_object_getattr(mmap_object *self, char *name) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 632 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 633 | return Py_FindMethod (mmap_object_methods, (PyObject *)self, name); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 637 | mmap_length(mmap_object *self) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 638 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 639 | CHECK_VALID(-1); |
| 640 | return self->size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 644 | mmap_item(mmap_object *self, int i) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 645 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 646 | CHECK_VALID(NULL); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 647 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 648 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 649 | return NULL; |
| 650 | } |
| 651 | return PyString_FromStringAndSize(self->data + i, 1); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 655 | mmap_slice(mmap_object *self, int ilow, int ihigh) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 656 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 657 | CHECK_VALID(NULL); |
| 658 | if (ilow < 0) |
| 659 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 660 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 661 | ilow = self->size; |
| 662 | if (ihigh < 0) |
| 663 | ihigh = 0; |
| 664 | if (ihigh < ilow) |
| 665 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 666 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 667 | ihigh = self->size; |
| 668 | |
| 669 | return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 673 | mmap_concat(mmap_object *self, PyObject *bb) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 674 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 675 | CHECK_VALID(NULL); |
| 676 | PyErr_SetString(PyExc_SystemError, |
| 677 | "mmaps don't support concatenation"); |
| 678 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 682 | mmap_repeat(mmap_object *self, int n) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 683 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 684 | CHECK_VALID(NULL); |
| 685 | PyErr_SetString(PyExc_SystemError, |
| 686 | "mmaps don't support repeat operation"); |
| 687 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 691 | mmap_ass_slice(mmap_object *self, int ilow, int ihigh, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 692 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 693 | const char *buf; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 694 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 695 | CHECK_VALID(-1); |
| 696 | if (ilow < 0) |
| 697 | ilow = 0; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 698 | else if ((size_t)ilow > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 699 | ilow = self->size; |
| 700 | if (ihigh < 0) |
| 701 | ihigh = 0; |
| 702 | if (ihigh < ilow) |
| 703 | ihigh = ilow; |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 704 | else if ((size_t)ihigh > self->size) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 705 | ihigh = self->size; |
| 706 | |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 707 | if (v == NULL) { |
| 708 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 709 | "mmap object doesn't support slice deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 710 | return -1; |
| 711 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 712 | if (! (PyString_Check(v)) ) { |
| 713 | PyErr_SetString(PyExc_IndexError, |
| 714 | "mmap slice assignment must be a string"); |
| 715 | return -1; |
| 716 | } |
| 717 | if ( PyString_Size(v) != (ihigh - ilow) ) { |
| 718 | PyErr_SetString(PyExc_IndexError, |
| 719 | "mmap slice assignment is wrong size"); |
| 720 | return -1; |
| 721 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 722 | if (!is_writeable(self)) |
| 723 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 724 | buf = PyString_AsString(v); |
| 725 | memcpy(self->data + ilow, buf, ihigh-ilow); |
| 726 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 730 | mmap_ass_item(mmap_object *self, int i, PyObject *v) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 731 | { |
Guido van Rossum | 36d4f8b | 2000-04-10 21:34:37 +0000 | [diff] [blame] | 732 | const char *buf; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 733 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 734 | CHECK_VALID(-1); |
Guido van Rossum | ce8e1dc | 2000-07-01 00:51:51 +0000 | [diff] [blame] | 735 | if (i < 0 || (size_t)i >= self->size) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 736 | PyErr_SetString(PyExc_IndexError, "mmap index out of range"); |
| 737 | return -1; |
| 738 | } |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 739 | if (v == NULL) { |
| 740 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 741 | "mmap object doesn't support item deletion"); |
Thomas Wouters | 1baac72 | 2001-07-16 15:47:36 +0000 | [diff] [blame] | 742 | return -1; |
| 743 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 744 | if (! (PyString_Check(v) && PyString_Size(v)==1) ) { |
| 745 | PyErr_SetString(PyExc_IndexError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 746 | "mmap assignment must be single-character string"); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 747 | return -1; |
| 748 | } |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 749 | if (!is_writeable(self)) |
| 750 | return -1; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 751 | buf = PyString_AsString(v); |
| 752 | self->data[i] = buf[0]; |
| 753 | return 0; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | static PySequenceMethods mmap_as_sequence = { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 757 | (inquiry)mmap_length, /*sq_length*/ |
| 758 | (binaryfunc)mmap_concat, /*sq_concat*/ |
| 759 | (intargfunc)mmap_repeat, /*sq_repeat*/ |
| 760 | (intargfunc)mmap_item, /*sq_item*/ |
| 761 | (intintargfunc)mmap_slice, /*sq_slice*/ |
| 762 | (intobjargproc)mmap_ass_item, /*sq_ass_item*/ |
| 763 | (intintobjargproc)mmap_ass_slice, /*sq_ass_slice*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | static PyBufferProcs mmap_as_buffer = { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 767 | (getreadbufferproc)mmap_buffer_getreadbuf, |
| 768 | (getwritebufferproc)mmap_buffer_getwritebuf, |
| 769 | (getsegcountproc)mmap_buffer_getsegcount, |
| 770 | (getcharbufferproc)mmap_buffer_getcharbuffer, |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | static PyTypeObject mmap_object_type = { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 774 | PyObject_HEAD_INIT(0) /* patched in module init */ |
| 775 | 0, /* ob_size */ |
Guido van Rossum | 1464839 | 2001-12-08 18:02:58 +0000 | [diff] [blame] | 776 | "mmap.mmap", /* tp_name */ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 777 | sizeof(mmap_object), /* tp_size */ |
| 778 | 0, /* tp_itemsize */ |
| 779 | /* methods */ |
| 780 | (destructor) mmap_object_dealloc, /* tp_dealloc */ |
| 781 | 0, /* tp_print */ |
| 782 | (getattrfunc) mmap_object_getattr, /* tp_getattr */ |
| 783 | 0, /* tp_setattr */ |
| 784 | 0, /* tp_compare */ |
| 785 | 0, /* tp_repr */ |
| 786 | 0, /* tp_as_number */ |
| 787 | &mmap_as_sequence, /*tp_as_sequence*/ |
| 788 | 0, /*tp_as_mapping*/ |
| 789 | 0, /*tp_hash*/ |
| 790 | 0, /*tp_call*/ |
| 791 | 0, /*tp_str*/ |
| 792 | 0, /*tp_getattro*/ |
| 793 | 0, /*tp_setattro*/ |
| 794 | &mmap_as_buffer, /*tp_as_buffer*/ |
| 795 | Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/ |
| 796 | 0, /*tp_doc*/ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 797 | }; |
| 798 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 799 | |
| 800 | /* extract the map size from the given PyObject |
| 801 | |
| 802 | The map size is restricted to [0, INT_MAX] because this is the current |
| 803 | Python limitation on object sizes. Although the mmap object *could* handle |
| 804 | a larger map size, there is no point because all the useful operations |
| 805 | (len(), slicing(), sequence indexing) are limited by a C int. |
| 806 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 807 | Returns -1 on error, with an appropriate Python exception raised. On |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 808 | success, the map size is returned. */ |
| 809 | static int |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 810 | _GetMapSize(PyObject *o) |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 811 | { |
| 812 | if (PyInt_Check(o)) { |
| 813 | long i = PyInt_AsLong(o); |
| 814 | if (PyErr_Occurred()) |
| 815 | return -1; |
| 816 | if (i < 0) |
| 817 | goto onnegoverflow; |
| 818 | if (i > INT_MAX) |
| 819 | goto onposoverflow; |
| 820 | return (int)i; |
| 821 | } |
| 822 | else if (PyLong_Check(o)) { |
| 823 | long i = PyLong_AsLong(o); |
| 824 | if (PyErr_Occurred()) { |
| 825 | /* yes negative overflow is mistaken for positive overflow |
| 826 | but not worth the trouble to check sign of 'i' */ |
| 827 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 828 | goto onposoverflow; |
| 829 | else |
| 830 | return -1; |
| 831 | } |
| 832 | if (i < 0) |
| 833 | goto onnegoverflow; |
| 834 | if (i > INT_MAX) |
| 835 | goto onposoverflow; |
| 836 | return (int)i; |
| 837 | } |
| 838 | else { |
| 839 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 840 | "map size must be an integral value"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 841 | return -1; |
| 842 | } |
| 843 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 844 | onnegoverflow: |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 845 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 846 | "memory mapped size must be positive"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 847 | return -1; |
| 848 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 849 | onposoverflow: |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 850 | PyErr_SetString(PyExc_OverflowError, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 851 | "memory mapped size is too large (limited by C int)"); |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 852 | return -1; |
| 853 | } |
| 854 | |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 855 | #ifdef UNIX |
| 856 | static PyObject * |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 857 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 858 | { |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 859 | #ifdef HAVE_FSTAT |
| 860 | struct stat st; |
| 861 | #endif |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 862 | mmap_object *m_obj; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 863 | PyObject *map_size_obj = NULL; |
| 864 | int map_size; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 865 | int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 866 | int access = (int)ACCESS_DEFAULT; |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 867 | static const char *keywords[] = {"fileno", "length", |
| 868 | "flags", "prot", |
| 869 | "access", NULL}; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 870 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 871 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iii", keywords, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 872 | &fd, &map_size_obj, &flags, &prot, |
| 873 | &access)) |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 874 | return NULL; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 875 | map_size = _GetMapSize(map_size_obj); |
| 876 | if (map_size < 0) |
| 877 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 878 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 879 | if ((access != (int)ACCESS_DEFAULT) && |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame^] | 880 | ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ)))) |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 881 | return PyErr_Format(PyExc_ValueError, |
| 882 | "mmap can't specify both access and flags, prot."); |
Neal Norwitz | d1cfc8a | 2006-02-05 03:36:57 +0000 | [diff] [blame^] | 883 | switch ((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 884 | case ACCESS_READ: |
| 885 | flags = MAP_SHARED; |
| 886 | prot = PROT_READ; |
| 887 | break; |
| 888 | case ACCESS_WRITE: |
| 889 | flags = MAP_SHARED; |
| 890 | prot = PROT_READ | PROT_WRITE; |
| 891 | break; |
| 892 | case ACCESS_COPY: |
| 893 | flags = MAP_PRIVATE; |
| 894 | prot = PROT_READ | PROT_WRITE; |
| 895 | break; |
| 896 | case ACCESS_DEFAULT: |
| 897 | /* use the specified or default values of flags and prot */ |
| 898 | break; |
| 899 | default: |
| 900 | return PyErr_Format(PyExc_ValueError, |
| 901 | "mmap invalid access parameter."); |
| 902 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 903 | |
| 904 | #ifdef HAVE_FSTAT |
Martin v. Löwis | c16f3bd | 2003-05-03 09:14:54 +0000 | [diff] [blame] | 905 | # ifdef __VMS |
| 906 | /* on OpenVMS we must ensure that all bytes are written to the file */ |
| 907 | fsync(fd); |
| 908 | # endif |
Martin v. Löwis | 7fe60c0 | 2005-03-03 11:22:44 +0000 | [diff] [blame] | 909 | if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) { |
| 910 | if (map_size == 0) { |
| 911 | map_size = (int)st.st_size; |
| 912 | } else if ((size_t)map_size > st.st_size) { |
| 913 | PyErr_SetString(PyExc_ValueError, |
| 914 | "mmap length is greater than file size"); |
| 915 | return NULL; |
| 916 | } |
Neal Norwitz | b567392 | 2002-09-05 21:48:07 +0000 | [diff] [blame] | 917 | } |
| 918 | #endif |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 919 | m_obj = PyObject_New (mmap_object, &mmap_object_type); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 920 | if (m_obj == NULL) {return NULL;} |
Neal Norwitz | 3b4fff8 | 2006-01-11 08:54:45 +0000 | [diff] [blame] | 921 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 922 | m_obj->size = (size_t) map_size; |
| 923 | m_obj->pos = (size_t) 0; |
Georg Brandl | 38387b8 | 2005-08-24 07:17:40 +0000 | [diff] [blame] | 924 | m_obj->fd = dup(fd); |
| 925 | if (m_obj->fd == -1) { |
| 926 | Py_DECREF(m_obj); |
| 927 | PyErr_SetFromErrno(mmap_module_error); |
| 928 | return NULL; |
| 929 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 930 | m_obj->data = mmap(NULL, map_size, |
| 931 | prot, flags, |
| 932 | fd, 0); |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 933 | if (m_obj->data == (char *)-1) { |
Andrew M. Kuchling | 16581c8 | 2004-05-19 14:39:08 +0000 | [diff] [blame] | 934 | m_obj->data = NULL; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 935 | Py_DECREF(m_obj); |
| 936 | PyErr_SetFromErrno(mmap_module_error); |
| 937 | return NULL; |
| 938 | } |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 939 | m_obj->access = (access_mode)access; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 940 | return (PyObject *)m_obj; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 941 | } |
| 942 | #endif /* UNIX */ |
| 943 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 944 | #ifdef MS_WINDOWS |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 945 | static PyObject * |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 946 | new_mmap_object(PyObject *self, PyObject *args, PyObject *kwdict) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 947 | { |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 948 | mmap_object *m_obj; |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 949 | PyObject *map_size_obj = NULL; |
| 950 | int map_size; |
Fredrik Lundh | 54cf3dc | 2000-07-08 22:05:01 +0000 | [diff] [blame] | 951 | char *tagname = ""; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 952 | DWORD dwErr = 0; |
| 953 | int fileno; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 954 | HANDLE fh = 0; |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 955 | int access = (access_mode)ACCESS_DEFAULT; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 956 | DWORD flProtect, dwDesiredAccess; |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 957 | static const char *keywords[] = { "fileno", "length", |
| 958 | "tagname", |
| 959 | "access", NULL }; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 960 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 961 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|zi", keywords, |
| 962 | &fileno, &map_size_obj, |
| 963 | &tagname, &access)) { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 964 | return NULL; |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 967 | switch((access_mode)access) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 968 | case ACCESS_READ: |
| 969 | flProtect = PAGE_READONLY; |
| 970 | dwDesiredAccess = FILE_MAP_READ; |
| 971 | break; |
| 972 | case ACCESS_DEFAULT: case ACCESS_WRITE: |
| 973 | flProtect = PAGE_READWRITE; |
| 974 | dwDesiredAccess = FILE_MAP_WRITE; |
| 975 | break; |
| 976 | case ACCESS_COPY: |
| 977 | flProtect = PAGE_WRITECOPY; |
| 978 | dwDesiredAccess = FILE_MAP_COPY; |
| 979 | break; |
| 980 | default: |
| 981 | return PyErr_Format(PyExc_ValueError, |
| 982 | "mmap invalid access parameter."); |
| 983 | } |
| 984 | |
Andrew M. Kuchling | 70d2742 | 2000-06-18 04:45:14 +0000 | [diff] [blame] | 985 | map_size = _GetMapSize(map_size_obj); |
| 986 | if (map_size < 0) |
| 987 | return NULL; |
| 988 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 989 | /* if an actual filename has been specified */ |
| 990 | if (fileno != 0) { |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 991 | fh = (HANDLE)_get_osfhandle(fileno); |
| 992 | if (fh==(HANDLE)-1) { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 993 | PyErr_SetFromErrno(mmap_module_error); |
| 994 | return NULL; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 995 | } |
Fred Drake | 1ef4e2d | 2000-04-05 14:15:31 +0000 | [diff] [blame] | 996 | /* Win9x appears to need us seeked to zero */ |
Guido van Rossum | 69c2b88 | 2003-04-09 19:31:02 +0000 | [diff] [blame] | 997 | lseek(fileno, 0, SEEK_SET); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 998 | } |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 999 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1000 | m_obj = PyObject_New (mmap_object, &mmap_object_type); |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1001 | if (m_obj==NULL) |
| 1002 | return NULL; |
| 1003 | /* Set every field to an invalid marker, so we can safely |
| 1004 | destruct the object in the face of failure */ |
| 1005 | m_obj->data = NULL; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1006 | m_obj->file_handle = INVALID_HANDLE_VALUE; |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1007 | m_obj->map_handle = INVALID_HANDLE_VALUE; |
| 1008 | m_obj->tagname = NULL; |
| 1009 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1010 | if (fh) { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1011 | /* It is necessary to duplicate the handle, so the |
| 1012 | Python code can close it on us */ |
| 1013 | if (!DuplicateHandle( |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1014 | GetCurrentProcess(), /* source process handle */ |
| 1015 | fh, /* handle to be duplicated */ |
| 1016 | GetCurrentProcess(), /* target proc handle */ |
| 1017 | (LPHANDLE)&m_obj->file_handle, /* result */ |
| 1018 | 0, /* access - ignored due to options value */ |
| 1019 | FALSE, /* inherited by child processes? */ |
| 1020 | DUPLICATE_SAME_ACCESS)) { /* options */ |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1021 | dwErr = GetLastError(); |
| 1022 | Py_DECREF(m_obj); |
| 1023 | PyErr_SetFromWindowsErr(dwErr); |
| 1024 | return NULL; |
| 1025 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1026 | if (!map_size) { |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1027 | m_obj->size = GetFileSize (fh, NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1028 | } else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1029 | m_obj->size = map_size; |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1030 | } |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1031 | } |
| 1032 | else { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1033 | m_obj->size = map_size; |
| 1034 | } |
| 1035 | |
| 1036 | /* set the initial position */ |
| 1037 | m_obj->pos = (size_t) 0; |
| 1038 | |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1039 | /* set the tag name */ |
Tim Peters | 0d9f9dc | 2001-01-10 05:42:18 +0000 | [diff] [blame] | 1040 | if (tagname != NULL && *tagname != '\0') { |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1041 | m_obj->tagname = PyMem_Malloc(strlen(tagname)+1); |
| 1042 | if (m_obj->tagname == NULL) { |
| 1043 | PyErr_NoMemory(); |
| 1044 | Py_DECREF(m_obj); |
| 1045 | return NULL; |
| 1046 | } |
| 1047 | strcpy(m_obj->tagname, tagname); |
| 1048 | } |
| 1049 | else |
| 1050 | m_obj->tagname = NULL; |
| 1051 | |
Neal Norwitz | 8856fb7 | 2005-12-18 03:34:22 +0000 | [diff] [blame] | 1052 | m_obj->access = (access_mode)access; |
Mark Hammond | 071864a | 2000-07-30 02:46:26 +0000 | [diff] [blame] | 1053 | m_obj->map_handle = CreateFileMapping (m_obj->file_handle, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1054 | NULL, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1055 | flProtect, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1056 | 0, |
| 1057 | m_obj->size, |
Tim Peters | 0d9f9dc | 2001-01-10 05:42:18 +0000 | [diff] [blame] | 1058 | m_obj->tagname); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1059 | if (m_obj->map_handle != NULL) { |
| 1060 | m_obj->data = (char *) MapViewOfFile (m_obj->map_handle, |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1061 | dwDesiredAccess, |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1062 | 0, |
| 1063 | 0, |
| 1064 | 0); |
| 1065 | if (m_obj->data != NULL) { |
| 1066 | return ((PyObject *) m_obj); |
| 1067 | } else { |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1068 | dwErr = GetLastError(); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } else { |
| 1071 | dwErr = GetLastError(); |
| 1072 | } |
Mark Hammond | 2cbed00 | 2000-07-30 02:22:43 +0000 | [diff] [blame] | 1073 | Py_DECREF(m_obj); |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1074 | PyErr_SetFromWindowsErr(dwErr); |
| 1075 | return (NULL); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1076 | } |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1077 | #endif /* MS_WINDOWS */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1078 | |
| 1079 | /* List of functions exported by this module */ |
| 1080 | static struct PyMethodDef mmap_functions[] = { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1081 | {"mmap", (PyCFunction) new_mmap_object, |
| 1082 | METH_VARARGS|METH_KEYWORDS}, |
| 1083 | {NULL, NULL} /* Sentinel */ |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1084 | }; |
| 1085 | |
Mark Hammond | 62b1ab1 | 2002-07-23 06:31:15 +0000 | [diff] [blame] | 1086 | PyMODINIT_FUNC |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1087 | initmmap(void) |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1088 | { |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1089 | PyObject *dict, *module; |
Tim Peters | 2caf8df | 2001-01-14 05:05:51 +0000 | [diff] [blame] | 1090 | |
| 1091 | /* Patch the object type */ |
| 1092 | mmap_object_type.ob_type = &PyType_Type; |
| 1093 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1094 | module = Py_InitModule ("mmap", mmap_functions); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 1095 | if (module == NULL) |
| 1096 | return; |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1097 | dict = PyModule_GetDict (module); |
| 1098 | mmap_module_error = PyExc_EnvironmentError; |
| 1099 | Py_INCREF(mmap_module_error); |
| 1100 | PyDict_SetItemString (dict, "error", mmap_module_error); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1101 | #ifdef PROT_EXEC |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1102 | PyDict_SetItemString (dict, "PROT_EXEC", PyInt_FromLong(PROT_EXEC) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1103 | #endif |
| 1104 | #ifdef PROT_READ |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1105 | PyDict_SetItemString (dict, "PROT_READ", PyInt_FromLong(PROT_READ) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1106 | #endif |
| 1107 | #ifdef PROT_WRITE |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1108 | PyDict_SetItemString (dict, "PROT_WRITE", PyInt_FromLong(PROT_WRITE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1109 | #endif |
| 1110 | |
| 1111 | #ifdef MAP_SHARED |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1112 | PyDict_SetItemString (dict, "MAP_SHARED", PyInt_FromLong(MAP_SHARED) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1113 | #endif |
| 1114 | #ifdef MAP_PRIVATE |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1115 | PyDict_SetItemString (dict, "MAP_PRIVATE", |
| 1116 | PyInt_FromLong(MAP_PRIVATE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1117 | #endif |
| 1118 | #ifdef MAP_DENYWRITE |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1119 | PyDict_SetItemString (dict, "MAP_DENYWRITE", |
| 1120 | PyInt_FromLong(MAP_DENYWRITE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1121 | #endif |
| 1122 | #ifdef MAP_EXECUTABLE |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1123 | PyDict_SetItemString (dict, "MAP_EXECUTABLE", |
| 1124 | PyInt_FromLong(MAP_EXECUTABLE) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1125 | #endif |
| 1126 | #ifdef MAP_ANON |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1127 | PyDict_SetItemString (dict, "MAP_ANON", PyInt_FromLong(MAP_ANON) ); |
| 1128 | PyDict_SetItemString (dict, "MAP_ANONYMOUS", |
| 1129 | PyInt_FromLong(MAP_ANON) ); |
Andrew M. Kuchling | 1ed7d2d | 2000-03-30 21:14:30 +0000 | [diff] [blame] | 1130 | #endif |
| 1131 | |
Guido van Rossum | 09fdf07 | 2000-03-31 01:17:07 +0000 | [diff] [blame] | 1132 | PyDict_SetItemString (dict, "PAGESIZE", |
Fred Drake | 145f96e | 2000-10-01 17:50:46 +0000 | [diff] [blame] | 1133 | PyInt_FromLong( (long)my_getpagesize() ) ); |
Andrew M. Kuchling | 961fe17 | 2000-06-03 19:41:42 +0000 | [diff] [blame] | 1134 | |
Tim Peters | 5ebfd36 | 2001-11-13 23:11:19 +0000 | [diff] [blame] | 1135 | PyDict_SetItemString (dict, "ACCESS_READ", |
| 1136 | PyInt_FromLong(ACCESS_READ)); |
| 1137 | PyDict_SetItemString (dict, "ACCESS_WRITE", |
| 1138 | PyInt_FromLong(ACCESS_WRITE)); |
| 1139 | PyDict_SetItemString (dict, "ACCESS_COPY", |
| 1140 | PyInt_FromLong(ACCESS_COPY)); |
| 1141 | } |