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