Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1 | |
| 2 | /* Memoryview object implementation */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 6 | #define IS_RELEASED(memobj) \ |
| 7 | (((PyMemoryViewObject *) memobj)->view.buf == NULL) |
| 8 | |
| 9 | #define CHECK_RELEASED(memobj) \ |
| 10 | if (IS_RELEASED(memobj)) { \ |
| 11 | PyErr_SetString(PyExc_ValueError, \ |
| 12 | "operation forbidden on released memoryview object"); \ |
| 13 | return NULL; \ |
| 14 | } |
| 15 | |
| 16 | #define CHECK_RELEASED_INT(memobj) \ |
| 17 | if (IS_RELEASED(memobj)) { \ |
| 18 | PyErr_SetString(PyExc_ValueError, \ |
| 19 | "operation forbidden on released memoryview object"); \ |
| 20 | return -1; \ |
| 21 | } |
| 22 | |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 23 | static Py_ssize_t |
| 24 | get_shape0(Py_buffer *buf) |
| 25 | { |
| 26 | if (buf->shape != NULL) |
| 27 | return buf->shape[0]; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 28 | if (buf->ndim == 0) |
| 29 | return 1; |
| 30 | PyErr_SetString(PyExc_TypeError, |
| 31 | "exported buffer does not have any shape information associated " |
| 32 | "to it"); |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | static void |
| 37 | dup_buffer(Py_buffer *dest, Py_buffer *src) |
| 38 | { |
| 39 | *dest = *src; |
| 40 | if (src->ndim == 1 && src->shape != NULL) { |
| 41 | dest->shape = &(dest->smalltable[0]); |
| 42 | dest->shape[0] = get_shape0(src); |
| 43 | } |
| 44 | if (src->ndim == 1 && src->strides != NULL) { |
| 45 | dest->strides = &(dest->smalltable[1]); |
| 46 | dest->strides[0] = src->strides[0]; |
| 47 | } |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 50 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 51 | memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 52 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 53 | int res = 0; |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 54 | CHECK_RELEASED_INT(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 55 | /* XXX for whatever reason fixing the flags seems necessary */ |
| 56 | if (self->view.readonly) |
| 57 | flags &= ~PyBUF_WRITABLE; |
| 58 | if (self->view.obj != NULL) |
| 59 | res = PyObject_GetBuffer(self->view.obj, view, flags); |
| 60 | if (view) |
| 61 | dup_buffer(view, &self->view); |
| 62 | return res; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 66 | memory_releasebuf(PyMemoryViewObject *self, Py_buffer *view) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 67 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 68 | PyBuffer_Release(view); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | PyDoc_STRVAR(memory_doc, |
| 72 | "memoryview(object)\n\ |
| 73 | \n\ |
| 74 | Create a new memoryview object which references the given object."); |
| 75 | |
| 76 | PyObject * |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 77 | PyMemoryView_FromBuffer(Py_buffer *info) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 78 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 79 | PyMemoryViewObject *mview; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 80 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 81 | mview = (PyMemoryViewObject *) |
| 82 | PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); |
| 83 | if (mview == NULL) |
| 84 | return NULL; |
| 85 | mview->base = NULL; |
| 86 | dup_buffer(&mview->view, info); |
| 87 | /* NOTE: mview->view.obj should already have been incref'ed as |
| 88 | part of PyBuffer_FillInfo(). */ |
| 89 | _PyObject_GC_TRACK(mview); |
| 90 | return (PyObject *)mview; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | PyObject * |
| 94 | PyMemoryView_FromObject(PyObject *base) |
| 95 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 96 | PyMemoryViewObject *mview; |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 97 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 99 | if (!PyObject_CheckBuffer(base)) { |
| 100 | PyErr_SetString(PyExc_TypeError, |
| 101 | "cannot make memory view because object does " |
| 102 | "not have the buffer interface"); |
| 103 | return NULL; |
| 104 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 106 | if (PyObject_GetBuffer(base, &view, PyBUF_FULL_RO) < 0) |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 107 | return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | 05b7c56 | 2010-02-02 22:47:00 +0000 | [diff] [blame] | 109 | mview = (PyMemoryViewObject *)PyMemoryView_FromBuffer(&view); |
| 110 | if (mview == NULL) { |
| 111 | PyBuffer_Release(&view); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 112 | return NULL; |
| 113 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 115 | mview->base = base; |
| 116 | Py_INCREF(base); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 117 | return (PyObject *)mview; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static PyObject * |
| 121 | memory_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 122 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 123 | PyObject *obj; |
| 124 | static char *kwlist[] = {"object", 0}; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 125 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 126 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:memoryview", kwlist, |
| 127 | &obj)) { |
| 128 | return NULL; |
| 129 | } |
Christian Heimes | 7b6fc8e | 2007-11-08 02:28:11 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 131 | return PyMemoryView_FromObject(obj); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | |
| 135 | static void |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 136 | _strided_copy_nd(char *dest, char *src, int nd, Py_ssize_t *shape, |
Neal Norwitz | 61ec0d3 | 2007-10-26 06:44:10 +0000 | [diff] [blame] | 137 | Py_ssize_t *strides, Py_ssize_t itemsize, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 138 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 139 | int k; |
| 140 | Py_ssize_t outstride; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 142 | if (nd==0) { |
| 143 | memcpy(dest, src, itemsize); |
| 144 | } |
| 145 | else if (nd == 1) { |
| 146 | for (k = 0; k<shape[0]; k++) { |
| 147 | memcpy(dest, src, itemsize); |
| 148 | dest += itemsize; |
| 149 | src += strides[0]; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 150 | } |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 151 | } |
| 152 | else { |
| 153 | if (fort == 'F') { |
| 154 | /* Copy first dimension first, |
| 155 | second dimension second, etc... |
| 156 | Set up the recursive loop backwards so that final |
| 157 | dimension is actually copied last. |
| 158 | */ |
| 159 | outstride = itemsize; |
| 160 | for (k=1; k<nd-1;k++) { |
| 161 | outstride *= shape[k]; |
| 162 | } |
| 163 | for (k=0; k<shape[nd-1]; k++) { |
| 164 | _strided_copy_nd(dest, src, nd-1, shape, |
| 165 | strides, itemsize, fort); |
| 166 | dest += outstride; |
| 167 | src += strides[nd-1]; |
| 168 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 169 | } |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 170 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 171 | else { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 172 | /* Copy last dimension first, |
| 173 | second-to-last dimension second, etc. |
| 174 | Set up the recursion so that the |
| 175 | first dimension is copied last |
| 176 | */ |
| 177 | outstride = itemsize; |
| 178 | for (k=1; k < nd; k++) { |
| 179 | outstride *= shape[k]; |
| 180 | } |
| 181 | for (k=0; k<shape[0]; k++) { |
| 182 | _strided_copy_nd(dest, src, nd-1, shape+1, |
| 183 | strides+1, itemsize, |
| 184 | fort); |
| 185 | dest += outstride; |
| 186 | src += strides[0]; |
| 187 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 188 | } |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 189 | } |
| 190 | return; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 193 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 194 | _indirect_copy_nd(char *dest, Py_buffer *view, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 195 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 196 | Py_ssize_t *indices; |
| 197 | int k; |
| 198 | Py_ssize_t elements; |
| 199 | char *ptr; |
Antoine Pitrou | c73b909 | 2010-09-01 21:14:46 +0000 | [diff] [blame] | 200 | void (*func)(int, Py_ssize_t *, const Py_ssize_t *); |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 202 | if (view->ndim > PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) { |
| 203 | PyErr_NoMemory(); |
| 204 | return -1; |
| 205 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 206 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 207 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view->ndim); |
| 208 | if (indices == NULL) { |
| 209 | PyErr_NoMemory(); |
| 210 | return -1; |
| 211 | } |
| 212 | for (k=0; k<view->ndim;k++) { |
| 213 | indices[k] = 0; |
| 214 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 215 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 216 | elements = 1; |
| 217 | for (k=0; k<view->ndim; k++) { |
| 218 | elements *= view->shape[k]; |
| 219 | } |
| 220 | if (fort == 'F') { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 221 | func = _Py_add_one_to_index_F; |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 222 | } |
| 223 | else { |
Antoine Pitrou | f68c2a7 | 2010-09-01 12:58:21 +0000 | [diff] [blame] | 224 | func = _Py_add_one_to_index_C; |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 225 | } |
| 226 | while (elements--) { |
| 227 | func(view->ndim, indices, view->shape); |
| 228 | ptr = PyBuffer_GetPointer(view, indices); |
| 229 | memcpy(dest, ptr, view->itemsize); |
| 230 | dest += view->itemsize; |
| 231 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 233 | PyMem_Free(indices); |
| 234 | return 0; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 237 | /* |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 238 | Get a the data from an object as a contiguous chunk of memory (in |
| 239 | either 'C' or 'F'ortran order) even if it means copying it into a |
| 240 | separate memory area. |
| 241 | |
| 242 | Returns a new reference to a Memory view object. If no copy is needed, |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 243 | the memory view object points to the original memory and holds a |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 244 | lock on the original. If a copy is needed, then the memory view object |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 245 | points to a brand-new Bytes object (and holds a memory lock on it). |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 246 | |
| 247 | buffertype |
| 248 | |
| 249 | PyBUF_READ buffer only needs to be read-only |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 250 | PyBUF_WRITE buffer needs to be writable (give error if not contiguous) |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 251 | PyBUF_SHADOW buffer needs to be writable so shadow it with |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 252 | a contiguous buffer if it is not. The view will point to |
| 253 | the shadow buffer which can be written to and then |
| 254 | will be copied back into the other buffer when the memory |
Antoine Pitrou | 2f89aa6 | 2008-08-02 21:02:48 +0000 | [diff] [blame] | 255 | view is de-allocated. While the shadow buffer is |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 256 | being used, it will have an exclusive write lock on |
| 257 | the original buffer. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 258 | */ |
| 259 | |
| 260 | PyObject * |
| 261 | PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort) |
| 262 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 263 | PyMemoryViewObject *mem; |
| 264 | PyObject *bytes; |
| 265 | Py_buffer *view; |
| 266 | int flags; |
| 267 | char *dest; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 268 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 269 | if (!PyObject_CheckBuffer(obj)) { |
| 270 | PyErr_SetString(PyExc_TypeError, |
Georg Brandl | 61b96dc | 2009-08-04 20:29:27 +0000 | [diff] [blame] | 271 | "object does not support the buffer interface"); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 272 | return NULL; |
| 273 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 274 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 275 | mem = PyObject_GC_New(PyMemoryViewObject, &PyMemoryView_Type); |
| 276 | if (mem == NULL) |
| 277 | return NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 278 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 279 | view = &mem->view; |
| 280 | flags = PyBUF_FULL_RO; |
| 281 | switch(buffertype) { |
| 282 | case PyBUF_WRITE: |
| 283 | flags = PyBUF_FULL; |
| 284 | break; |
| 285 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 287 | if (PyObject_GetBuffer(obj, view, flags) != 0) { |
| 288 | Py_DECREF(mem); |
| 289 | return NULL; |
| 290 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 292 | if (PyBuffer_IsContiguous(view, fort)) { |
| 293 | /* no copy needed */ |
| 294 | Py_INCREF(obj); |
| 295 | mem->base = obj; |
| 296 | _PyObject_GC_TRACK(mem); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 297 | return (PyObject *)mem; |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 298 | } |
| 299 | /* otherwise a copy is needed */ |
| 300 | if (buffertype == PyBUF_WRITE) { |
| 301 | Py_DECREF(mem); |
| 302 | PyErr_SetString(PyExc_BufferError, |
| 303 | "writable contiguous buffer requested " |
| 304 | "for a non-contiguousobject."); |
| 305 | return NULL; |
| 306 | } |
| 307 | bytes = PyBytes_FromStringAndSize(NULL, view->len); |
| 308 | if (bytes == NULL) { |
| 309 | Py_DECREF(mem); |
| 310 | return NULL; |
| 311 | } |
| 312 | dest = PyBytes_AS_STRING(bytes); |
| 313 | /* different copying strategy depending on whether |
| 314 | or not any pointer de-referencing is needed |
| 315 | */ |
| 316 | /* strided or in-direct copy */ |
| 317 | if (view->suboffsets==NULL) { |
| 318 | _strided_copy_nd(dest, view->buf, view->ndim, view->shape, |
| 319 | view->strides, view->itemsize, fort); |
| 320 | } |
| 321 | else { |
| 322 | if (_indirect_copy_nd(dest, view, fort) < 0) { |
| 323 | Py_DECREF(bytes); |
| 324 | Py_DECREF(mem); |
| 325 | return NULL; |
| 326 | } |
| 327 | } |
| 328 | if (buffertype == PyBUF_SHADOW) { |
| 329 | /* return a shadowed memory-view object */ |
| 330 | view->buf = dest; |
| 331 | mem->base = PyTuple_Pack(2, obj, bytes); |
| 332 | Py_DECREF(bytes); |
| 333 | if (mem->base == NULL) { |
| 334 | Py_DECREF(mem); |
| 335 | return NULL; |
| 336 | } |
| 337 | } |
| 338 | else { |
| 339 | PyBuffer_Release(view); /* XXX ? */ |
| 340 | /* steal the reference */ |
| 341 | mem->base = bytes; |
| 342 | } |
| 343 | _PyObject_GC_TRACK(mem); |
| 344 | return (PyObject *)mem; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
| 348 | static PyObject * |
| 349 | memory_format_get(PyMemoryViewObject *self) |
| 350 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 351 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 352 | return PyUnicode_FromString(self->view.format); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static PyObject * |
| 356 | memory_itemsize_get(PyMemoryViewObject *self) |
| 357 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 358 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 359 | return PyLong_FromSsize_t(self->view.itemsize); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static PyObject * |
| 363 | _IntTupleFromSsizet(int len, Py_ssize_t *vals) |
| 364 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 365 | int i; |
| 366 | PyObject *o; |
| 367 | PyObject *intTuple; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 368 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 369 | if (vals == NULL) { |
| 370 | Py_INCREF(Py_None); |
| 371 | return Py_None; |
| 372 | } |
| 373 | intTuple = PyTuple_New(len); |
| 374 | if (!intTuple) return NULL; |
| 375 | for(i=0; i<len; i++) { |
| 376 | o = PyLong_FromSsize_t(vals[i]); |
| 377 | if (!o) { |
| 378 | Py_DECREF(intTuple); |
| 379 | return NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 380 | } |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 381 | PyTuple_SET_ITEM(intTuple, i, o); |
| 382 | } |
| 383 | return intTuple; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static PyObject * |
| 387 | memory_shape_get(PyMemoryViewObject *self) |
| 388 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 389 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 390 | return _IntTupleFromSsizet(self->view.ndim, self->view.shape); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static PyObject * |
| 394 | memory_strides_get(PyMemoryViewObject *self) |
| 395 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 396 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 397 | return _IntTupleFromSsizet(self->view.ndim, self->view.strides); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static PyObject * |
| 401 | memory_suboffsets_get(PyMemoryViewObject *self) |
| 402 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 403 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 404 | return _IntTupleFromSsizet(self->view.ndim, self->view.suboffsets); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static PyObject * |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 408 | memory_readonly_get(PyMemoryViewObject *self) |
| 409 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 410 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 411 | return PyBool_FromLong(self->view.readonly); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static PyObject * |
| 415 | memory_ndim_get(PyMemoryViewObject *self) |
| 416 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 417 | CHECK_RELEASED(self); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 418 | return PyLong_FromLong(self->view.ndim); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 421 | static PyGetSetDef memory_getsetlist[] ={ |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 422 | {"format", (getter)memory_format_get, NULL, NULL}, |
| 423 | {"itemsize", (getter)memory_itemsize_get, NULL, NULL}, |
| 424 | {"shape", (getter)memory_shape_get, NULL, NULL}, |
| 425 | {"strides", (getter)memory_strides_get, NULL, NULL}, |
| 426 | {"suboffsets", (getter)memory_suboffsets_get, NULL, NULL}, |
| 427 | {"readonly", (getter)memory_readonly_get, NULL, NULL}, |
| 428 | {"ndim", (getter)memory_ndim_get, NULL, NULL}, |
| 429 | {NULL, NULL, NULL, NULL}, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | |
| 433 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 434 | memory_tobytes(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 436 | CHECK_RELEASED(mem); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 437 | return PyObject_CallFunctionObjArgs( |
| 438 | (PyObject *) &PyBytes_Type, mem, NULL); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 441 | /* TODO: rewrite this function using the struct module to unpack |
| 442 | each buffer item */ |
| 443 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 444 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 445 | memory_tolist(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 446 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 447 | Py_buffer *view = &(mem->view); |
| 448 | Py_ssize_t i; |
| 449 | PyObject *res, *item; |
| 450 | char *buf; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 452 | CHECK_RELEASED(mem); |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 453 | if (strcmp(view->format, "B") || view->itemsize != 1) { |
| 454 | PyErr_SetString(PyExc_NotImplementedError, |
| 455 | "tolist() only supports byte views"); |
| 456 | return NULL; |
| 457 | } |
| 458 | if (view->ndim != 1) { |
| 459 | PyErr_SetString(PyExc_NotImplementedError, |
| 460 | "tolist() only supports one-dimensional objects"); |
| 461 | return NULL; |
| 462 | } |
| 463 | res = PyList_New(view->len); |
| 464 | if (res == NULL) |
| 465 | return NULL; |
| 466 | buf = view->buf; |
| 467 | for (i = 0; i < view->len; i++) { |
| 468 | item = PyLong_FromUnsignedLong((unsigned char) *buf); |
| 469 | if (item == NULL) { |
| 470 | Py_DECREF(res); |
| 471 | return NULL; |
| 472 | } |
| 473 | PyList_SET_ITEM(res, i, item); |
| 474 | buf++; |
| 475 | } |
| 476 | return res; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 479 | static void |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 480 | do_release(PyMemoryViewObject *self) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 481 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 482 | if (self->view.obj != NULL) { |
| 483 | if (self->base && PyTuple_Check(self->base)) { |
| 484 | /* Special case when first element is generic object |
| 485 | with buffer interface and the second element is a |
| 486 | contiguous "shadow" that must be copied back into |
| 487 | the data areay of the first tuple element before |
| 488 | releasing the buffer on the first element. |
| 489 | */ |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 490 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 491 | PyObject_CopyData(PyTuple_GET_ITEM(self->base,0), |
| 492 | PyTuple_GET_ITEM(self->base,1)); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 493 | |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 494 | /* The view member should have readonly == -1 in |
| 495 | this instance indicating that the memory can |
| 496 | be "locked" and was locked and will be unlocked |
| 497 | again after this call. |
| 498 | */ |
| 499 | PyBuffer_Release(&(self->view)); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 500 | } |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 501 | else { |
| 502 | PyBuffer_Release(&(self->view)); |
| 503 | } |
| 504 | Py_CLEAR(self->base); |
| 505 | } |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 506 | self->view.obj = NULL; |
| 507 | self->view.buf = NULL; |
| 508 | } |
| 509 | |
| 510 | static PyObject * |
| 511 | memory_enter(PyObject *self, PyObject *args) |
| 512 | { |
| 513 | CHECK_RELEASED(self); |
| 514 | Py_INCREF(self); |
| 515 | return self; |
| 516 | } |
| 517 | |
| 518 | static PyObject * |
| 519 | memory_exit(PyObject *self, PyObject *args) |
| 520 | { |
| 521 | do_release((PyMemoryViewObject *) self); |
| 522 | Py_RETURN_NONE; |
| 523 | } |
| 524 | |
| 525 | static PyMethodDef memory_methods[] = { |
| 526 | {"release", memory_exit, METH_NOARGS}, |
| 527 | {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, NULL}, |
| 528 | {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, NULL}, |
| 529 | {"__enter__", memory_enter, METH_NOARGS}, |
| 530 | {"__exit__", memory_exit, METH_VARARGS}, |
| 531 | {NULL, NULL} /* sentinel */ |
| 532 | }; |
| 533 | |
| 534 | |
| 535 | static void |
| 536 | memory_dealloc(PyMemoryViewObject *self) |
| 537 | { |
| 538 | _PyObject_GC_UNTRACK(self); |
| 539 | do_release(self); |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 540 | PyObject_GC_Del(self); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static PyObject * |
| 544 | memory_repr(PyMemoryViewObject *self) |
| 545 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 546 | if (IS_RELEASED(self)) |
| 547 | return PyUnicode_FromFormat("<released memory at %p>", self); |
| 548 | else |
| 549 | return PyUnicode_FromFormat("<memory at %p>", self); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 552 | /* Sequence methods */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 553 | static Py_ssize_t |
| 554 | memory_length(PyMemoryViewObject *self) |
| 555 | { |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 556 | CHECK_RELEASED_INT(self); |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 557 | return get_shape0(&self->view); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 560 | /* Alternate version of memory_subcript that only accepts indices. |
| 561 | Used by PySeqIter_New(). |
| 562 | */ |
| 563 | static PyObject * |
| 564 | memory_item(PyMemoryViewObject *self, Py_ssize_t result) |
| 565 | { |
| 566 | Py_buffer *view = &(self->view); |
| 567 | |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 568 | CHECK_RELEASED(self); |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 569 | if (view->ndim == 0) { |
| 570 | PyErr_SetString(PyExc_IndexError, |
| 571 | "invalid indexing of 0-dim memory"); |
| 572 | return NULL; |
| 573 | } |
| 574 | if (view->ndim == 1) { |
| 575 | /* Return a bytes object */ |
| 576 | char *ptr; |
| 577 | ptr = (char *)view->buf; |
| 578 | if (result < 0) { |
| 579 | result += get_shape0(view); |
| 580 | } |
| 581 | if ((result < 0) || (result >= get_shape0(view))) { |
| 582 | PyErr_SetString(PyExc_IndexError, |
| 583 | "index out of bounds"); |
| 584 | return NULL; |
| 585 | } |
| 586 | if (view->strides == NULL) |
| 587 | ptr += view->itemsize * result; |
| 588 | else |
| 589 | ptr += view->strides[0] * result; |
| 590 | if (view->suboffsets != NULL && |
| 591 | view->suboffsets[0] >= 0) { |
| 592 | ptr = *((char **)ptr) + view->suboffsets[0]; |
| 593 | } |
| 594 | return PyBytes_FromStringAndSize(ptr, view->itemsize); |
| 595 | } else { |
| 596 | /* Return a new memory-view object */ |
| 597 | Py_buffer newview; |
| 598 | memset(&newview, 0, sizeof(newview)); |
| 599 | /* XXX: This needs to be fixed so it actually returns a sub-view */ |
| 600 | return PyMemoryView_FromBuffer(&newview); |
| 601 | } |
| 602 | } |
| 603 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 604 | /* |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 605 | mem[obj] returns a bytes object holding the data for one element if |
| 606 | obj fully indexes the memory view or another memory-view object |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 607 | if it does not. |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 609 | 0-d memory-view objects can be referenced using ... or () but |
| 610 | not with anything else. |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 611 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 612 | static PyObject * |
| 613 | memory_subscript(PyMemoryViewObject *self, PyObject *key) |
| 614 | { |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 615 | Py_buffer *view; |
| 616 | view = &(self->view); |
| 617 | |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 618 | CHECK_RELEASED(self); |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 619 | if (view->ndim == 0) { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 620 | if (key == Py_Ellipsis || |
| 621 | (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
| 622 | Py_INCREF(self); |
| 623 | return (PyObject *)self; |
| 624 | } |
| 625 | else { |
| 626 | PyErr_SetString(PyExc_IndexError, |
| 627 | "invalid indexing of 0-dim memory"); |
| 628 | return NULL; |
| 629 | } |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 630 | } |
| 631 | if (PyIndex_Check(key)) { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 632 | Py_ssize_t result; |
| 633 | result = PyNumber_AsSsize_t(key, NULL); |
| 634 | if (result == -1 && PyErr_Occurred()) |
| 635 | return NULL; |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 636 | return memory_item(self, result); |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 637 | } |
| 638 | else if (PySlice_Check(key)) { |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 639 | Py_ssize_t start, stop, step, slicelength; |
| 640 | |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 641 | if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 642 | &start, &stop, &step, &slicelength) < 0) { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 643 | return NULL; |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 644 | } |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 646 | if (step == 1 && view->ndim == 1) { |
| 647 | Py_buffer newview; |
| 648 | void *newbuf = (char *) view->buf |
| 649 | + start * view->itemsize; |
| 650 | int newflags = view->readonly |
| 651 | ? PyBUF_CONTIG_RO : PyBUF_CONTIG; |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | c3b3924 | 2009-01-03 16:59:18 +0000 | [diff] [blame] | 653 | /* XXX There should be an API to create a subbuffer */ |
| 654 | if (view->obj != NULL) { |
| 655 | if (PyObject_GetBuffer(view->obj, &newview, newflags) == -1) |
| 656 | return NULL; |
| 657 | } |
| 658 | else { |
| 659 | newview = *view; |
| 660 | } |
| 661 | newview.buf = newbuf; |
| 662 | newview.len = slicelength * newview.itemsize; |
| 663 | newview.format = view->format; |
| 664 | newview.shape = &(newview.smalltable[0]); |
| 665 | newview.shape[0] = slicelength; |
| 666 | newview.strides = &(newview.itemsize); |
| 667 | return PyMemoryView_FromBuffer(&newview); |
| 668 | } |
| 669 | PyErr_SetNone(PyExc_NotImplementedError); |
| 670 | return NULL; |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 671 | } |
| 672 | PyErr_Format(PyExc_TypeError, |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 673 | "cannot index memory using \"%.200s\"", |
| 674 | key->ob_type->tp_name); |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 675 | return NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 678 | |
| 679 | /* Need to support assigning memory if we can */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 680 | static int |
| 681 | memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) |
| 682 | { |
Antoine Pitrou | 1ac745b | 2010-07-11 12:12:00 +0000 | [diff] [blame] | 683 | Py_ssize_t start, len, bytelen; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 684 | Py_buffer srcview; |
| 685 | Py_buffer *view = &(self->view); |
| 686 | char *srcbuf, *destbuf; |
| 687 | |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 688 | CHECK_RELEASED_INT(self); |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 689 | if (view->readonly) { |
| 690 | PyErr_SetString(PyExc_TypeError, |
| 691 | "cannot modify read-only memory"); |
| 692 | return -1; |
| 693 | } |
Antoine Pitrou | e0793ba | 2010-09-01 21:14:16 +0000 | [diff] [blame] | 694 | if (value == NULL) { |
| 695 | PyErr_SetString(PyExc_TypeError, |
| 696 | "cannot delete memory"); |
| 697 | return -1; |
| 698 | } |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 699 | if (view->ndim != 1) { |
| 700 | PyErr_SetNone(PyExc_NotImplementedError); |
| 701 | return -1; |
| 702 | } |
| 703 | if (PyIndex_Check(key)) { |
| 704 | start = PyNumber_AsSsize_t(key, NULL); |
| 705 | if (start == -1 && PyErr_Occurred()) |
| 706 | return -1; |
| 707 | if (start < 0) { |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 708 | start += get_shape0(view); |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 709 | } |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 710 | if ((start < 0) || (start >= get_shape0(view))) { |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 711 | PyErr_SetString(PyExc_IndexError, |
| 712 | "index out of bounds"); |
| 713 | return -1; |
| 714 | } |
| 715 | len = 1; |
| 716 | } |
| 717 | else if (PySlice_Check(key)) { |
| 718 | Py_ssize_t stop, step; |
| 719 | |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 720 | if (PySlice_GetIndicesEx((PySliceObject*)key, get_shape0(view), |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 721 | &start, &stop, &step, &len) < 0) { |
| 722 | return -1; |
| 723 | } |
| 724 | if (step != 1) { |
| 725 | PyErr_SetNone(PyExc_NotImplementedError); |
| 726 | return -1; |
| 727 | } |
| 728 | } |
| 729 | else { |
| 730 | PyErr_Format(PyExc_TypeError, |
| 731 | "cannot index memory using \"%.200s\"", |
| 732 | key->ob_type->tp_name); |
| 733 | return -1; |
| 734 | } |
| 735 | if (PyObject_GetBuffer(value, &srcview, PyBUF_CONTIG_RO) == -1) { |
| 736 | return -1; |
| 737 | } |
| 738 | /* XXX should we allow assignment of different item sizes |
| 739 | as long as the byte length is the same? |
| 740 | (e.g. assign 2 shorts to a 4-byte slice) */ |
| 741 | if (srcview.itemsize != view->itemsize) { |
| 742 | PyErr_Format(PyExc_TypeError, |
| 743 | "mismatching item sizes for \"%.200s\" and \"%.200s\"", |
| 744 | view->obj->ob_type->tp_name, srcview.obj->ob_type->tp_name); |
| 745 | goto _error; |
| 746 | } |
Antoine Pitrou | bc42040 | 2008-12-07 20:14:49 +0000 | [diff] [blame] | 747 | bytelen = len * view->itemsize; |
| 748 | if (bytelen != srcview.len) { |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 749 | PyErr_SetString(PyExc_ValueError, |
| 750 | "cannot modify size of memoryview object"); |
| 751 | goto _error; |
| 752 | } |
| 753 | /* Do the actual copy */ |
| 754 | destbuf = (char *) view->buf + start * view->itemsize; |
| 755 | srcbuf = (char *) srcview.buf; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 756 | if (destbuf + bytelen < srcbuf || srcbuf + bytelen < destbuf) |
| 757 | /* No overlapping */ |
| 758 | memcpy(destbuf, srcbuf, bytelen); |
Antoine Pitrou | 1ac745b | 2010-07-11 12:12:00 +0000 | [diff] [blame] | 759 | else |
| 760 | memmove(destbuf, srcbuf, bytelen); |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 761 | |
| 762 | PyBuffer_Release(&srcview); |
| 763 | return 0; |
| 764 | |
| 765 | _error: |
| 766 | PyBuffer_Release(&srcview); |
| 767 | return -1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 770 | static PyObject * |
| 771 | memory_richcompare(PyObject *v, PyObject *w, int op) |
| 772 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 773 | Py_buffer vv, ww; |
| 774 | int equal = 0; |
| 775 | PyObject *res; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 776 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 777 | vv.obj = NULL; |
| 778 | ww.obj = NULL; |
| 779 | if (op != Py_EQ && op != Py_NE) |
| 780 | goto _notimpl; |
Antoine Pitrou | 6e6cc83 | 2010-09-09 12:59:39 +0000 | [diff] [blame] | 781 | if ((PyMemoryView_Check(v) && IS_RELEASED(v)) || |
| 782 | (PyMemoryView_Check(w) && IS_RELEASED(w))) { |
| 783 | equal = (v == w); |
| 784 | goto _end; |
| 785 | } |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 786 | if (PyObject_GetBuffer(v, &vv, PyBUF_CONTIG_RO) == -1) { |
| 787 | PyErr_Clear(); |
| 788 | goto _notimpl; |
| 789 | } |
| 790 | if (PyObject_GetBuffer(w, &ww, PyBUF_CONTIG_RO) == -1) { |
| 791 | PyErr_Clear(); |
| 792 | goto _notimpl; |
| 793 | } |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 794 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 795 | if (vv.itemsize != ww.itemsize || vv.len != ww.len) |
| 796 | goto _end; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 797 | |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 798 | equal = !memcmp(vv.buf, ww.buf, vv.len); |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 799 | |
| 800 | _end: |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 801 | PyBuffer_Release(&vv); |
| 802 | PyBuffer_Release(&ww); |
| 803 | if ((equal && op == Py_EQ) || (!equal && op == Py_NE)) |
| 804 | res = Py_True; |
| 805 | else |
| 806 | res = Py_False; |
| 807 | Py_INCREF(res); |
| 808 | return res; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 809 | |
| 810 | _notimpl: |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 811 | PyBuffer_Release(&vv); |
| 812 | PyBuffer_Release(&ww); |
| 813 | Py_INCREF(Py_NotImplemented); |
| 814 | return Py_NotImplemented; |
Antoine Pitrou | 616d285 | 2008-08-19 22:09:34 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | |
Antoine Pitrou | c6b09eb | 2008-09-01 15:10:14 +0000 | [diff] [blame] | 818 | static int |
| 819 | memory_traverse(PyMemoryViewObject *self, visitproc visit, void *arg) |
| 820 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 821 | if (self->base != NULL) |
| 822 | Py_VISIT(self->base); |
| 823 | if (self->view.obj != NULL) |
| 824 | Py_VISIT(self->view.obj); |
| 825 | return 0; |
Antoine Pitrou | c6b09eb | 2008-09-01 15:10:14 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static int |
| 829 | memory_clear(PyMemoryViewObject *self) |
| 830 | { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 831 | Py_CLEAR(self->base); |
| 832 | PyBuffer_Release(&self->view); |
| 833 | return 0; |
Antoine Pitrou | c6b09eb | 2008-09-01 15:10:14 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 837 | /* As mapping */ |
| 838 | static PyMappingMethods memory_as_mapping = { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 839 | (lenfunc)memory_length, /* mp_length */ |
| 840 | (binaryfunc)memory_subscript, /* mp_subscript */ |
| 841 | (objobjargproc)memory_ass_sub, /* mp_ass_subscript */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 842 | }; |
| 843 | |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 844 | static PySequenceMethods memory_as_sequence = { |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 845 | 0, /* sq_length */ |
| 846 | 0, /* sq_concat */ |
| 847 | 0, /* sq_repeat */ |
| 848 | (ssizeargfunc)memory_item, /* sq_item */ |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 849 | }; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 850 | |
| 851 | /* Buffer methods */ |
| 852 | |
| 853 | static PyBufferProcs memory_as_buffer = { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 854 | (getbufferproc)memory_getbuf, /* bf_getbuffer */ |
| 855 | (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 856 | }; |
| 857 | |
| 858 | |
| 859 | PyTypeObject PyMemoryView_Type = { |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 860 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 861 | "memoryview", |
| 862 | sizeof(PyMemoryViewObject), |
| 863 | 0, |
| 864 | (destructor)memory_dealloc, /* tp_dealloc */ |
| 865 | 0, /* tp_print */ |
| 866 | 0, /* tp_getattr */ |
| 867 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 868 | 0, /* tp_reserved */ |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 869 | (reprfunc)memory_repr, /* tp_repr */ |
| 870 | 0, /* tp_as_number */ |
Raymond Hettinger | 159eac9 | 2009-06-23 20:38:54 +0000 | [diff] [blame] | 871 | &memory_as_sequence, /* tp_as_sequence */ |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 872 | &memory_as_mapping, /* tp_as_mapping */ |
| 873 | 0, /* tp_hash */ |
| 874 | 0, /* tp_call */ |
Benjamin Peterson | 8761855 | 2009-02-08 15:00:52 +0000 | [diff] [blame] | 875 | 0, /* tp_str */ |
Antoine Pitrou | 35b7e83 | 2009-01-03 19:20:36 +0000 | [diff] [blame] | 876 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 877 | 0, /* tp_setattro */ |
| 878 | &memory_as_buffer, /* tp_as_buffer */ |
| 879 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 880 | memory_doc, /* tp_doc */ |
| 881 | (traverseproc)memory_traverse, /* tp_traverse */ |
| 882 | (inquiry)memory_clear, /* tp_clear */ |
| 883 | memory_richcompare, /* tp_richcompare */ |
| 884 | 0, /* tp_weaklistoffset */ |
| 885 | 0, /* tp_iter */ |
| 886 | 0, /* tp_iternext */ |
| 887 | memory_methods, /* tp_methods */ |
| 888 | 0, /* tp_members */ |
| 889 | memory_getsetlist, /* tp_getset */ |
| 890 | 0, /* tp_base */ |
| 891 | 0, /* tp_dict */ |
| 892 | 0, /* tp_descr_get */ |
| 893 | 0, /* tp_descr_set */ |
| 894 | 0, /* tp_dictoffset */ |
| 895 | 0, /* tp_init */ |
| 896 | 0, /* tp_alloc */ |
| 897 | memory_new, /* tp_new */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 898 | }; |