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