Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1 | |
| 2 | /* Memoryview object implementation */ |
| 3 | |
| 4 | #include "Python.h" |
| 5 | |
| 6 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 7 | memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 8 | { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 9 | if (view != NULL) |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 10 | *view = self->view; |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 11 | if (self->base == NULL) |
| 12 | return 0; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 13 | return self->base->ob_type->tp_as_buffer->bf_getbuffer(self->base, NULL, |
| 14 | PyBUF_FULL); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | static void |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 18 | memory_releasebuf(PyMemoryViewObject *self, Py_buffer *view) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 19 | { |
Guido van Rossum | be801ac | 2007-10-08 03:32:34 +0000 | [diff] [blame] | 20 | if (self->base != NULL) |
| 21 | PyObject_ReleaseBuffer(self->base, NULL); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | PyDoc_STRVAR(memory_doc, |
| 25 | "memoryview(object)\n\ |
| 26 | \n\ |
| 27 | Create a new memoryview object which references the given object."); |
| 28 | |
| 29 | PyObject * |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 30 | PyMemoryView_FromMemory(Py_buffer *info) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 31 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 32 | PyMemoryViewObject *mview; |
| 33 | |
| 34 | mview = (PyMemoryViewObject *)PyObject_New(PyMemoryViewObject, |
| 35 | &PyMemoryView_Type); |
| 36 | if (mview == NULL) return NULL; |
| 37 | mview->base = NULL; |
| 38 | mview->view = *info; |
| 39 | return (PyObject *)mview; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | PyObject * |
| 43 | PyMemoryView_FromObject(PyObject *base) |
| 44 | { |
| 45 | PyMemoryViewObject *mview; |
| 46 | |
| 47 | if (!PyObject_CheckBuffer(base)) { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 48 | PyErr_SetString(PyExc_TypeError, |
| 49 | "cannot make memory view because object does " |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 50 | "not have the buffer interface"); |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 51 | return NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 52 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 53 | |
| 54 | mview = (PyMemoryViewObject *)PyObject_New(PyMemoryViewObject, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 55 | &PyMemoryView_Type); |
| 56 | if (mview == NULL) return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 57 | |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 58 | mview->base = NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 59 | if (PyObject_GetBuffer(base, &(mview->view), PyBUF_FULL) < 0) { |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 60 | Py_DECREF(mview); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | mview->base = base; |
| 65 | Py_INCREF(base); |
| 66 | return (PyObject *)mview; |
| 67 | } |
| 68 | |
| 69 | static PyObject * |
| 70 | memory_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
| 71 | { |
| 72 | PyObject *obj; |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 73 | if (!PyArg_UnpackTuple(args, "memoryview", 1, 1, &obj)) return NULL; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 75 | return PyMemoryView_FromObject(obj); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | static void |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 80 | _strided_copy_nd(char *dest, char *src, int nd, Py_ssize_t *shape, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 81 | Py_ssize_t *strides, int itemsize, char fort) |
| 82 | { |
| 83 | int k; |
| 84 | Py_ssize_t outstride; |
| 85 | |
| 86 | if (nd==0) { |
| 87 | memcpy(dest, src, itemsize); |
| 88 | } |
| 89 | else if (nd == 1) { |
| 90 | for (k = 0; k<shape[0]; k++) { |
| 91 | memcpy(dest, src, itemsize); |
| 92 | dest += itemsize; |
| 93 | src += strides[0]; |
| 94 | } |
| 95 | } |
| 96 | else { |
| 97 | if (fort == 'F') { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 98 | /* Copy first dimension first, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 99 | second dimension second, etc... |
| 100 | Set up the recursive loop backwards so that final |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 101 | dimension is actually copied last. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 102 | */ |
| 103 | outstride = itemsize; |
| 104 | for (k=1; k<nd-1;k++) { |
| 105 | outstride *= shape[k]; |
| 106 | } |
| 107 | for (k=0; k<shape[nd-1]; k++) { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 108 | _strided_copy_nd(dest, src, nd-1, shape, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 109 | strides, itemsize, fort); |
| 110 | dest += outstride; |
| 111 | src += strides[nd-1]; |
| 112 | } |
| 113 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 114 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 115 | else { |
| 116 | /* Copy last dimension first, |
| 117 | second-to-last dimension second, etc. |
| 118 | Set up the recursion so that the |
| 119 | first dimension is copied last |
| 120 | */ |
| 121 | outstride = itemsize; |
| 122 | for (k=1; k < nd; k++) { |
| 123 | outstride *= shape[k]; |
| 124 | } |
| 125 | for (k=0; k<shape[0]; k++) { |
| 126 | _strided_copy_nd(dest, src, nd-1, shape+1, |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 127 | strides+1, itemsize, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 128 | fort); |
| 129 | dest += outstride; |
| 130 | src += strides[0]; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | void _add_one_to_index_F(int nd, Py_ssize_t *index, Py_ssize_t *shape); |
| 138 | void _add_one_to_index_C(int nd, Py_ssize_t *index, Py_ssize_t *shape); |
| 139 | |
| 140 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 141 | _indirect_copy_nd(char *dest, Py_buffer *view, char fort) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 142 | { |
| 143 | Py_ssize_t *indices; |
| 144 | int k; |
| 145 | Py_ssize_t elements; |
| 146 | char *ptr; |
| 147 | void (*func)(int, Py_ssize_t *, Py_ssize_t *); |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 148 | |
| 149 | |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 150 | /* XXX(nnorwitz): need to check for overflow! */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 151 | indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view->ndim); |
| 152 | if (indices == NULL) { |
| 153 | PyErr_NoMemory(); |
| 154 | return -1; |
| 155 | } |
| 156 | for (k=0; k<view->ndim;k++) { |
| 157 | indices[k] = 0; |
| 158 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 159 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 160 | elements = 1; |
| 161 | for (k=0; k<view->ndim; k++) { |
| 162 | elements *= view->shape[k]; |
| 163 | } |
| 164 | if (fort == 'F') { |
| 165 | func = _add_one_to_index_F; |
| 166 | } |
| 167 | else { |
| 168 | func = _add_one_to_index_C; |
| 169 | } |
| 170 | while (elements--) { |
| 171 | func(view->ndim, indices, view->shape); |
| 172 | ptr = PyBuffer_GetPointer(view, indices); |
| 173 | memcpy(dest, ptr, view->itemsize); |
| 174 | dest += view->itemsize; |
| 175 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 176 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 177 | PyMem_Free(indices); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 181 | /* |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 182 | Get a the data from an object as a contiguous chunk of memory (in |
| 183 | either 'C' or 'F'ortran order) even if it means copying it into a |
| 184 | separate memory area. |
| 185 | |
| 186 | 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] | 187 | 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] | 188 | 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] | 189 | 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] | 190 | |
| 191 | buffertype |
| 192 | |
| 193 | PyBUF_READ buffer only needs to be read-only |
Sean Reifscheider | 54cf12b | 2007-09-17 17:55:36 +0000 | [diff] [blame] | 194 | 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] | 195 | PyBUF_SHADOW buffer needs to be writable so shadow it with |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 196 | a contiguous buffer if it is not. The view will point to |
| 197 | the shadow buffer which can be written to and then |
| 198 | will be copied back into the other buffer when the memory |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 199 | view is de-allocated. While the shadow buffer is |
| 200 | being used, it will have an exclusive write lock on |
| 201 | the original buffer. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 202 | */ |
| 203 | |
| 204 | PyObject * |
| 205 | PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort) |
| 206 | { |
| 207 | PyMemoryViewObject *mem; |
| 208 | PyObject *bytes; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 209 | Py_buffer *view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 210 | int flags; |
| 211 | char *dest; |
| 212 | |
| 213 | if (!PyObject_CheckBuffer(obj)) { |
| 214 | PyErr_SetString(PyExc_TypeError, |
| 215 | "object does not have the buffer interface"); |
| 216 | return NULL; |
| 217 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 218 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 219 | mem = PyObject_New(PyMemoryViewObject, &PyMemoryView_Type); |
| 220 | if (mem == NULL) return NULL; |
| 221 | |
| 222 | view = &PyMemoryView(mem); |
| 223 | flags = PyBUF_FULL_RO; |
| 224 | switch(buffertype) { |
| 225 | case PyBUF_WRITE: |
| 226 | flags = PyBUF_FULL; |
| 227 | break; |
| 228 | case PyBUF_SHADOW: |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 229 | flags = PyBUF_FULL_XLCK; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | |
| 233 | if (PyObject_GetBuffer(obj, view, flags) != 0) { |
| 234 | PyObject_DEL(mem); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | if (PyBuffer_IsContiguous(view, fort)) { |
| 239 | /* no copy needed */ |
| 240 | Py_INCREF(obj); |
| 241 | mem->base = obj; |
| 242 | return (PyObject *)mem; |
| 243 | } |
| 244 | /* otherwise a copy is needed */ |
| 245 | if (buffertype == PyBUF_WRITE) { |
| 246 | PyObject_DEL(mem); |
| 247 | PyErr_SetString(PyExc_BufferError, |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 248 | "writable contiguous buffer requested " |
| 249 | "for a non-contiguousobject."); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | bytes = PyBytes_FromStringAndSize(NULL, view->len); |
| 253 | if (bytes == NULL) { |
| 254 | PyObject_ReleaseBuffer(obj, view); |
| 255 | return NULL; |
| 256 | } |
| 257 | dest = PyBytes_AS_STRING(bytes); |
| 258 | /* different copying strategy depending on whether |
| 259 | or not any pointer de-referencing is needed |
| 260 | */ |
| 261 | /* strided or in-direct copy */ |
| 262 | if (view->suboffsets==NULL) { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 263 | _strided_copy_nd(dest, view->buf, view->ndim, view->shape, |
| 264 | view->strides, view->itemsize, fort); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 265 | } |
| 266 | else { |
| 267 | if (_indirect_copy_nd(dest, view, fort) < 0) { |
| 268 | Py_DECREF(bytes); |
| 269 | PyObject_ReleaseBuffer(obj, view); |
| 270 | return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 271 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 272 | } |
| 273 | if (buffertype == PyBUF_SHADOW) { |
| 274 | /* return a shadowed memory-view object */ |
| 275 | view->buf = dest; |
| 276 | mem->base = PyTuple_Pack(2, obj, bytes); |
| 277 | Py_DECREF(bytes); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 278 | if (mem->base == NULL) { |
| 279 | PyObject_ReleaseBuffer(obj, view); |
| 280 | return NULL; |
| 281 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 282 | } |
| 283 | else { |
| 284 | PyObject_ReleaseBuffer(obj, view); |
| 285 | /* steal the reference */ |
| 286 | mem->base = bytes; |
| 287 | } |
| 288 | return (PyObject *)mem; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | static PyObject * |
| 293 | memory_format_get(PyMemoryViewObject *self) |
| 294 | { |
| 295 | return PyUnicode_FromString(self->view.format); |
| 296 | } |
| 297 | |
| 298 | static PyObject * |
| 299 | memory_itemsize_get(PyMemoryViewObject *self) |
| 300 | { |
| 301 | return PyInt_FromLong(self->view.itemsize); |
| 302 | } |
| 303 | |
| 304 | static PyObject * |
| 305 | _IntTupleFromSsizet(int len, Py_ssize_t *vals) |
| 306 | { |
| 307 | int i; |
| 308 | PyObject *o; |
| 309 | PyObject *intTuple; |
| 310 | |
| 311 | if (vals == NULL) { |
| 312 | Py_INCREF(Py_None); |
| 313 | return Py_None; |
| 314 | } |
| 315 | intTuple = PyTuple_New(len); |
| 316 | if (!intTuple) return NULL; |
| 317 | for(i=0; i<len; i++) { |
| 318 | o = PyInt_FromSsize_t(vals[i]); |
| 319 | if (!o) { |
| 320 | Py_DECREF(intTuple); |
| 321 | return NULL; |
| 322 | } |
| 323 | PyTuple_SET_ITEM(intTuple, i, o); |
| 324 | } |
| 325 | return intTuple; |
| 326 | } |
| 327 | |
| 328 | static PyObject * |
| 329 | memory_shape_get(PyMemoryViewObject *self) |
| 330 | { |
| 331 | return _IntTupleFromSsizet(self->view.ndim, self->view.shape); |
| 332 | } |
| 333 | |
| 334 | static PyObject * |
| 335 | memory_strides_get(PyMemoryViewObject *self) |
| 336 | { |
| 337 | return _IntTupleFromSsizet(self->view.ndim, self->view.strides); |
| 338 | } |
| 339 | |
| 340 | static PyObject * |
| 341 | memory_suboffsets_get(PyMemoryViewObject *self) |
| 342 | { |
| 343 | return _IntTupleFromSsizet(self->view.ndim, self->view.suboffsets); |
| 344 | } |
| 345 | |
| 346 | static PyObject * |
| 347 | memory_size_get(PyMemoryViewObject *self) |
| 348 | { |
| 349 | return PyInt_FromSsize_t(self->view.len); |
| 350 | } |
| 351 | |
| 352 | static PyObject * |
| 353 | memory_readonly_get(PyMemoryViewObject *self) |
| 354 | { |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 355 | return PyBool_FromLong(self->view.readonly); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | static PyObject * |
| 359 | memory_ndim_get(PyMemoryViewObject *self) |
| 360 | { |
| 361 | return PyInt_FromLong(self->view.ndim); |
| 362 | } |
| 363 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 364 | static PyGetSetDef memory_getsetlist[] ={ |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 365 | {"format", (getter)memory_format_get, NULL, NULL}, |
| 366 | {"itemsize", (getter)memory_itemsize_get, NULL, NULL}, |
| 367 | {"shape", (getter)memory_shape_get, NULL, NULL}, |
| 368 | {"strides", (getter)memory_strides_get, NULL, NULL}, |
| 369 | {"suboffsets", (getter)memory_suboffsets_get, NULL, NULL}, |
| 370 | {"size", (getter)memory_size_get, NULL, NULL}, |
| 371 | {"readonly", (getter)memory_readonly_get, NULL, NULL}, |
| 372 | {"ndim", (getter)memory_ndim_get, NULL, NULL}, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 373 | {NULL, NULL, NULL, NULL}, |
| 374 | }; |
| 375 | |
| 376 | |
| 377 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 378 | memory_tobytes(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 379 | { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 380 | return PyBytes_FromObject((PyObject *)mem); |
| 381 | } |
| 382 | |
| 383 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 384 | memory_tolist(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 385 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 386 | /* This should construct a (nested) list of unpacked objects |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 387 | possibly using the struct module. |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 388 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 389 | Py_INCREF(Py_NotImplemented); |
| 390 | return Py_NotImplemented; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | |
| 395 | static PyMethodDef memory_methods[] = { |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 396 | {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, NULL}, |
| 397 | {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, NULL}, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 398 | {NULL, NULL} /* sentinel */ |
| 399 | }; |
| 400 | |
| 401 | |
| 402 | static void |
| 403 | memory_dealloc(PyMemoryViewObject *self) |
| 404 | { |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 405 | if (self->base != NULL) { |
| 406 | if (PyTuple_Check(self->base)) { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 407 | /* Special case when first element is generic object |
| 408 | with buffer interface and the second element is a |
| 409 | contiguous "shadow" that must be copied back into |
| 410 | the data areay of the first tuple element before |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 411 | releasing the buffer on the first element. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 412 | */ |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 413 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 414 | PyObject_CopyData(PyTuple_GET_ITEM(self->base,0), |
| 415 | PyTuple_GET_ITEM(self->base,1)); |
| 416 | |
| 417 | /* The view member should have readonly == -1 in |
| 418 | this instance indicating that the memory can |
| 419 | be "locked" and was locked and will be unlocked |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 420 | again after this call. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 421 | */ |
| 422 | PyObject_ReleaseBuffer(PyTuple_GET_ITEM(self->base,0), |
| 423 | &(self->view)); |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 424 | } |
| 425 | else { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 426 | PyObject_ReleaseBuffer(self->base, &(self->view)); |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 427 | } |
| 428 | Py_CLEAR(self->base); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 429 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 430 | PyObject_DEL(self); |
| 431 | } |
| 432 | |
| 433 | static PyObject * |
| 434 | memory_repr(PyMemoryViewObject *self) |
| 435 | { |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 436 | return PyUnicode_FromFormat("<memory at %p>", self); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | |
| 440 | static PyObject * |
| 441 | memory_str(PyMemoryViewObject *self) |
| 442 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 443 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 444 | PyObject *res; |
| 445 | |
| 446 | if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
| 447 | return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 448 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 449 | res = PyBytes_FromStringAndSize(NULL, view.len); |
| 450 | PyBuffer_ToContiguous(PyBytes_AS_STRING(res), &view, view.len, 'C'); |
| 451 | PyObject_ReleaseBuffer((PyObject *)self, &view); |
| 452 | return res; |
| 453 | } |
| 454 | |
| 455 | /* Sequence methods */ |
| 456 | |
| 457 | static Py_ssize_t |
| 458 | memory_length(PyMemoryViewObject *self) |
| 459 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 460 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 461 | |
| 462 | if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
| 463 | return -1; |
| 464 | PyObject_ReleaseBuffer((PyObject *)self, &view); |
| 465 | return view.len; |
| 466 | } |
| 467 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 468 | /* |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 469 | mem[obj] returns a bytes object holding the data for one element if |
| 470 | obj fully indexes the memory view or another memory-view object |
| 471 | if it does not. |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 472 | |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 473 | 0-d memory-view objects can be referenced using ... or () but |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 474 | not with anything else. |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 475 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 476 | static PyObject * |
| 477 | memory_subscript(PyMemoryViewObject *self, PyObject *key) |
| 478 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 479 | Py_buffer *view; |
| 480 | view = &(self->view); |
| 481 | |
| 482 | if (view->ndim == 0) { |
| 483 | if (key == Py_Ellipsis || |
| 484 | (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
| 485 | Py_INCREF(self); |
| 486 | return (PyObject *)self; |
| 487 | } |
| 488 | else { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 489 | PyErr_SetString(PyExc_IndexError, |
| 490 | "invalid indexing of 0-dim memory"); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
| 493 | } |
| 494 | if (PyIndex_Check(key)) { |
| 495 | Py_ssize_t result; |
| 496 | result = PyNumber_AsSsize_t(key, NULL); |
| 497 | if (result == -1 && PyErr_Occurred()) |
| 498 | return NULL; |
| 499 | if (view->ndim == 1) { |
| 500 | /* Return a bytes object */ |
| 501 | char *ptr; |
| 502 | ptr = (char *)view->buf; |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 503 | if (result < 0) { |
| 504 | result += view->shape[0]; |
| 505 | } |
| 506 | if ((result < 0) || (result > view->shape[0])) { |
| 507 | PyErr_SetString(PyExc_IndexError, |
| 508 | "index out of bounds"); |
| 509 | return NULL; |
| 510 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 511 | if (view->strides == NULL) |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 512 | ptr += view->itemsize * result; |
| 513 | else |
| 514 | ptr += view->strides[0] * result; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 515 | if (view->suboffsets != NULL && |
| 516 | view->suboffsets[0] >= 0) |
| 517 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 518 | ptr = *((char **)ptr) + view->suboffsets[0]; |
| 519 | } |
| 520 | return PyBytes_FromStringAndSize(ptr, view->itemsize); |
| 521 | } |
| 522 | else { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 523 | /* Return a new memory-view object */ |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 524 | Py_buffer newview; |
Neal Norwitz | b35f128 | 2007-10-07 19:26:50 +0000 | [diff] [blame] | 525 | memset(&newview, 0, sizeof(newview)); |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 526 | /* XXX: This needs to be fixed so it |
| 527 | actually returns a sub-view |
| 528 | */ |
Neal Norwitz | b35f128 | 2007-10-07 19:26:50 +0000 | [diff] [blame] | 529 | return PyMemoryView_FromMemory(&newview); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 532 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 533 | /* Need to support getting a sliced view */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 534 | Py_INCREF(Py_NotImplemented); |
| 535 | return Py_NotImplemented; |
| 536 | } |
| 537 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 538 | |
| 539 | /* Need to support assigning memory if we can */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 540 | static int |
| 541 | memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) |
| 542 | { |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* As mapping */ |
| 547 | static PyMappingMethods memory_as_mapping = { |
| 548 | (lenfunc)memory_length, /*mp_length*/ |
| 549 | (binaryfunc)memory_subscript, /*mp_subscript*/ |
| 550 | (objobjargproc)memory_ass_sub, /*mp_ass_subscript*/ |
| 551 | }; |
| 552 | |
| 553 | |
| 554 | /* Buffer methods */ |
| 555 | |
| 556 | static PyBufferProcs memory_as_buffer = { |
| 557 | (getbufferproc)memory_getbuf, /* bf_getbuffer */ |
| 558 | (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ |
| 559 | }; |
| 560 | |
| 561 | |
| 562 | PyTypeObject PyMemoryView_Type = { |
| 563 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 564 | "memoryview", |
| 565 | sizeof(PyMemoryViewObject), |
| 566 | 0, |
| 567 | (destructor)memory_dealloc, /* tp_dealloc */ |
| 568 | 0, /* tp_print */ |
| 569 | 0, /* tp_getattr */ |
| 570 | 0, /* tp_setattr */ |
| 571 | 0, /* tp_compare */ |
| 572 | (reprfunc)memory_repr, /* tp_repr */ |
| 573 | 0, /* tp_as_number */ |
| 574 | 0, /* tp_as_sequence */ |
| 575 | &memory_as_mapping, /* tp_as_mapping */ |
| 576 | 0, /* tp_hash */ |
| 577 | 0, /* tp_call */ |
| 578 | (reprfunc)memory_str, /* tp_str */ |
| 579 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 580 | 0, /* tp_setattro */ |
| 581 | &memory_as_buffer, /* tp_as_buffer */ |
| 582 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 583 | memory_doc, /* tp_doc */ |
| 584 | 0, /* tp_traverse */ |
| 585 | 0, /* tp_clear */ |
| 586 | 0, /* tp_richcompare */ |
| 587 | 0, /* tp_weaklistoffset */ |
| 588 | 0, /* tp_iter */ |
| 589 | 0, /* tp_iternext */ |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 590 | memory_methods, /* tp_methods */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 591 | 0, /* tp_members */ |
| 592 | memory_getsetlist, /* tp_getset */ |
| 593 | 0, /* tp_base */ |
| 594 | 0, /* tp_dict */ |
| 595 | 0, /* tp_descr_get */ |
| 596 | 0, /* tp_descr_set */ |
| 597 | 0, /* tp_dictoffset */ |
| 598 | 0, /* tp_init */ |
| 599 | 0, /* tp_alloc */ |
| 600 | memory_new, /* tp_new */ |
| 601 | }; |