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