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 |
| 199 | view is de-allocated. |
| 200 | */ |
| 201 | |
| 202 | PyObject * |
| 203 | PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char fort) |
| 204 | { |
| 205 | PyMemoryViewObject *mem; |
| 206 | PyObject *bytes; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 207 | Py_buffer *view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 208 | int flags; |
| 209 | char *dest; |
| 210 | |
| 211 | if (!PyObject_CheckBuffer(obj)) { |
| 212 | PyErr_SetString(PyExc_TypeError, |
| 213 | "object does not have the buffer interface"); |
| 214 | return NULL; |
| 215 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 216 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 217 | mem = PyObject_New(PyMemoryViewObject, &PyMemoryView_Type); |
| 218 | if (mem == NULL) return NULL; |
| 219 | |
| 220 | view = &PyMemoryView(mem); |
| 221 | flags = PyBUF_FULL_RO; |
| 222 | switch(buffertype) { |
| 223 | case PyBUF_WRITE: |
| 224 | flags = PyBUF_FULL; |
| 225 | break; |
| 226 | case PyBUF_SHADOW: |
| 227 | flags = PyBUF_FULL_LCK; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | if (PyObject_GetBuffer(obj, view, flags) != 0) { |
| 232 | PyObject_DEL(mem); |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | if (PyBuffer_IsContiguous(view, fort)) { |
| 237 | /* no copy needed */ |
| 238 | Py_INCREF(obj); |
| 239 | mem->base = obj; |
| 240 | return (PyObject *)mem; |
| 241 | } |
| 242 | /* otherwise a copy is needed */ |
| 243 | if (buffertype == PyBUF_WRITE) { |
| 244 | PyObject_DEL(mem); |
| 245 | PyErr_SetString(PyExc_BufferError, |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 246 | "writable contiguous buffer requested " |
| 247 | "for a non-contiguousobject."); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 248 | return NULL; |
| 249 | } |
| 250 | bytes = PyBytes_FromStringAndSize(NULL, view->len); |
| 251 | if (bytes == NULL) { |
| 252 | PyObject_ReleaseBuffer(obj, view); |
| 253 | return NULL; |
| 254 | } |
| 255 | dest = PyBytes_AS_STRING(bytes); |
| 256 | /* different copying strategy depending on whether |
| 257 | or not any pointer de-referencing is needed |
| 258 | */ |
| 259 | /* strided or in-direct copy */ |
| 260 | if (view->suboffsets==NULL) { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 261 | _strided_copy_nd(dest, view->buf, view->ndim, view->shape, |
| 262 | view->strides, view->itemsize, fort); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 263 | } |
| 264 | else { |
| 265 | if (_indirect_copy_nd(dest, view, fort) < 0) { |
| 266 | Py_DECREF(bytes); |
| 267 | PyObject_ReleaseBuffer(obj, view); |
| 268 | return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 269 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 270 | } |
| 271 | if (buffertype == PyBUF_SHADOW) { |
| 272 | /* return a shadowed memory-view object */ |
| 273 | view->buf = dest; |
| 274 | mem->base = PyTuple_Pack(2, obj, bytes); |
| 275 | Py_DECREF(bytes); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 276 | if (mem->base == NULL) { |
| 277 | PyObject_ReleaseBuffer(obj, view); |
| 278 | return NULL; |
| 279 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 280 | } |
| 281 | else { |
| 282 | PyObject_ReleaseBuffer(obj, view); |
| 283 | /* steal the reference */ |
| 284 | mem->base = bytes; |
| 285 | } |
| 286 | return (PyObject *)mem; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | static PyObject * |
| 291 | memory_format_get(PyMemoryViewObject *self) |
| 292 | { |
| 293 | return PyUnicode_FromString(self->view.format); |
| 294 | } |
| 295 | |
| 296 | static PyObject * |
| 297 | memory_itemsize_get(PyMemoryViewObject *self) |
| 298 | { |
| 299 | return PyInt_FromLong(self->view.itemsize); |
| 300 | } |
| 301 | |
| 302 | static PyObject * |
| 303 | _IntTupleFromSsizet(int len, Py_ssize_t *vals) |
| 304 | { |
| 305 | int i; |
| 306 | PyObject *o; |
| 307 | PyObject *intTuple; |
| 308 | |
| 309 | if (vals == NULL) { |
| 310 | Py_INCREF(Py_None); |
| 311 | return Py_None; |
| 312 | } |
| 313 | intTuple = PyTuple_New(len); |
| 314 | if (!intTuple) return NULL; |
| 315 | for(i=0; i<len; i++) { |
| 316 | o = PyInt_FromSsize_t(vals[i]); |
| 317 | if (!o) { |
| 318 | Py_DECREF(intTuple); |
| 319 | return NULL; |
| 320 | } |
| 321 | PyTuple_SET_ITEM(intTuple, i, o); |
| 322 | } |
| 323 | return intTuple; |
| 324 | } |
| 325 | |
| 326 | static PyObject * |
| 327 | memory_shape_get(PyMemoryViewObject *self) |
| 328 | { |
| 329 | return _IntTupleFromSsizet(self->view.ndim, self->view.shape); |
| 330 | } |
| 331 | |
| 332 | static PyObject * |
| 333 | memory_strides_get(PyMemoryViewObject *self) |
| 334 | { |
| 335 | return _IntTupleFromSsizet(self->view.ndim, self->view.strides); |
| 336 | } |
| 337 | |
| 338 | static PyObject * |
| 339 | memory_suboffsets_get(PyMemoryViewObject *self) |
| 340 | { |
| 341 | return _IntTupleFromSsizet(self->view.ndim, self->view.suboffsets); |
| 342 | } |
| 343 | |
| 344 | static PyObject * |
| 345 | memory_size_get(PyMemoryViewObject *self) |
| 346 | { |
| 347 | return PyInt_FromSsize_t(self->view.len); |
| 348 | } |
| 349 | |
| 350 | static PyObject * |
| 351 | memory_readonly_get(PyMemoryViewObject *self) |
| 352 | { |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 353 | return PyBool_FromLong(self->view.readonly); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | static PyObject * |
| 357 | memory_ndim_get(PyMemoryViewObject *self) |
| 358 | { |
| 359 | return PyInt_FromLong(self->view.ndim); |
| 360 | } |
| 361 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 362 | static PyGetSetDef memory_getsetlist[] ={ |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 363 | {"format", (getter)memory_format_get, NULL, NULL}, |
| 364 | {"itemsize", (getter)memory_itemsize_get, NULL, NULL}, |
| 365 | {"shape", (getter)memory_shape_get, NULL, NULL}, |
| 366 | {"strides", (getter)memory_strides_get, NULL, NULL}, |
| 367 | {"suboffsets", (getter)memory_suboffsets_get, NULL, NULL}, |
| 368 | {"size", (getter)memory_size_get, NULL, NULL}, |
| 369 | {"readonly", (getter)memory_readonly_get, NULL, NULL}, |
| 370 | {"ndim", (getter)memory_ndim_get, NULL, NULL}, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 371 | {NULL, NULL, NULL, NULL}, |
| 372 | }; |
| 373 | |
| 374 | |
| 375 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 376 | memory_tobytes(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 377 | { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 378 | return PyBytes_FromObject((PyObject *)mem); |
| 379 | } |
| 380 | |
| 381 | static PyObject * |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 382 | memory_tolist(PyMemoryViewObject *mem, PyObject *noargs) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 383 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 384 | /* This should construct a (nested) list of unpacked objects |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 385 | possibly using the struct module. |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 386 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 387 | Py_INCREF(Py_NotImplemented); |
| 388 | return Py_NotImplemented; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | |
| 393 | static PyMethodDef memory_methods[] = { |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 394 | {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, NULL}, |
| 395 | {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, NULL}, |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 396 | {NULL, NULL} /* sentinel */ |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | static void |
| 401 | memory_dealloc(PyMemoryViewObject *self) |
| 402 | { |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 403 | if (self->base != NULL) { |
| 404 | if (PyTuple_Check(self->base)) { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 405 | /* Special case when first element is generic object |
| 406 | with buffer interface and the second element is a |
| 407 | contiguous "shadow" that must be copied back into |
| 408 | the data areay of the first tuple element before |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 409 | releasing the buffer on the first element. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 410 | */ |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 411 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 412 | PyObject_CopyData(PyTuple_GET_ITEM(self->base,0), |
| 413 | PyTuple_GET_ITEM(self->base,1)); |
| 414 | |
| 415 | /* The view member should have readonly == -1 in |
| 416 | this instance indicating that the memory can |
| 417 | be "locked" and was locked and will be unlocked |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 418 | again after this call. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 419 | */ |
| 420 | PyObject_ReleaseBuffer(PyTuple_GET_ITEM(self->base,0), |
| 421 | &(self->view)); |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 422 | } |
| 423 | else { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 424 | PyObject_ReleaseBuffer(self->base, &(self->view)); |
Neal Norwitz | 666bb41 | 2007-08-19 18:38:46 +0000 | [diff] [blame] | 425 | } |
| 426 | Py_CLEAR(self->base); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 427 | } |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 428 | PyObject_DEL(self); |
| 429 | } |
| 430 | |
| 431 | static PyObject * |
| 432 | memory_repr(PyMemoryViewObject *self) |
| 433 | { |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 434 | /* XXX(nnorwitz): the code should be different or remove condition. */ |
| 435 | if (self->base == NULL) |
| 436 | return PyUnicode_FromFormat("<memory at %p>", self); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 437 | else |
Neal Norwitz | faa54a3 | 2007-08-19 04:23:20 +0000 | [diff] [blame] | 438 | return PyUnicode_FromFormat("<memory at %p>", self); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | |
| 442 | static PyObject * |
| 443 | memory_str(PyMemoryViewObject *self) |
| 444 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 445 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 446 | PyObject *res; |
| 447 | |
| 448 | if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
| 449 | return NULL; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 450 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 451 | res = PyBytes_FromStringAndSize(NULL, view.len); |
| 452 | PyBuffer_ToContiguous(PyBytes_AS_STRING(res), &view, view.len, 'C'); |
| 453 | PyObject_ReleaseBuffer((PyObject *)self, &view); |
| 454 | return res; |
| 455 | } |
| 456 | |
| 457 | /* Sequence methods */ |
| 458 | |
| 459 | static Py_ssize_t |
| 460 | memory_length(PyMemoryViewObject *self) |
| 461 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 462 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 463 | |
| 464 | if (PyObject_GetBuffer((PyObject *)self, &view, PyBUF_FULL) < 0) |
| 465 | return -1; |
| 466 | PyObject_ReleaseBuffer((PyObject *)self, &view); |
| 467 | return view.len; |
| 468 | } |
| 469 | |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 470 | /* |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 471 | mem[obj] returns a bytes object holding the data for one element if |
| 472 | obj fully indexes the memory view or another memory-view object |
| 473 | if it does not. |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 474 | |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 475 | 0-d memory-view objects can be referenced using ... or () but |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 476 | not with anything else. |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 477 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 478 | static PyObject * |
| 479 | memory_subscript(PyMemoryViewObject *self, PyObject *key) |
| 480 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 481 | Py_buffer *view; |
| 482 | view = &(self->view); |
| 483 | |
| 484 | if (view->ndim == 0) { |
| 485 | if (key == Py_Ellipsis || |
| 486 | (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
| 487 | Py_INCREF(self); |
| 488 | return (PyObject *)self; |
| 489 | } |
| 490 | else { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 491 | PyErr_SetString(PyExc_IndexError, |
| 492 | "invalid indexing of 0-dim memory"); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 493 | return NULL; |
| 494 | } |
| 495 | } |
| 496 | if (PyIndex_Check(key)) { |
| 497 | Py_ssize_t result; |
| 498 | result = PyNumber_AsSsize_t(key, NULL); |
| 499 | if (result == -1 && PyErr_Occurred()) |
| 500 | return NULL; |
| 501 | if (view->ndim == 1) { |
| 502 | /* Return a bytes object */ |
| 503 | char *ptr; |
| 504 | ptr = (char *)view->buf; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 505 | if (view->strides == NULL) |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 506 | ptr += view->itemsize * result; |
| 507 | else |
| 508 | ptr += view->strides[0] * result; |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 509 | if (view->suboffsets != NULL && |
| 510 | view->suboffsets[0] >= 0) |
| 511 | { |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 512 | ptr = *((char **)ptr) + view->suboffsets[0]; |
| 513 | } |
| 514 | return PyBytes_FromStringAndSize(ptr, view->itemsize); |
| 515 | } |
| 516 | else { |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 517 | /* Return a new memory-view object */ |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 518 | Py_buffer newview; |
Neal Norwitz | b35f128 | 2007-10-07 19:26:50 +0000 | [diff] [blame] | 519 | memset(&newview, 0, sizeof(newview)); |
| 520 | return PyMemoryView_FromMemory(&newview); |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 523 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 524 | Py_INCREF(Py_NotImplemented); |
| 525 | return Py_NotImplemented; |
| 526 | } |
| 527 | |
| 528 | static int |
| 529 | memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) |
| 530 | { |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* As mapping */ |
| 535 | static PyMappingMethods memory_as_mapping = { |
| 536 | (lenfunc)memory_length, /*mp_length*/ |
| 537 | (binaryfunc)memory_subscript, /*mp_subscript*/ |
| 538 | (objobjargproc)memory_ass_sub, /*mp_ass_subscript*/ |
| 539 | }; |
| 540 | |
| 541 | |
| 542 | /* Buffer methods */ |
| 543 | |
| 544 | static PyBufferProcs memory_as_buffer = { |
| 545 | (getbufferproc)memory_getbuf, /* bf_getbuffer */ |
| 546 | (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ |
| 547 | }; |
| 548 | |
| 549 | |
| 550 | PyTypeObject PyMemoryView_Type = { |
| 551 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 552 | "memoryview", |
| 553 | sizeof(PyMemoryViewObject), |
| 554 | 0, |
| 555 | (destructor)memory_dealloc, /* tp_dealloc */ |
| 556 | 0, /* tp_print */ |
| 557 | 0, /* tp_getattr */ |
| 558 | 0, /* tp_setattr */ |
| 559 | 0, /* tp_compare */ |
| 560 | (reprfunc)memory_repr, /* tp_repr */ |
| 561 | 0, /* tp_as_number */ |
| 562 | 0, /* tp_as_sequence */ |
| 563 | &memory_as_mapping, /* tp_as_mapping */ |
| 564 | 0, /* tp_hash */ |
| 565 | 0, /* tp_call */ |
| 566 | (reprfunc)memory_str, /* tp_str */ |
| 567 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 568 | 0, /* tp_setattro */ |
| 569 | &memory_as_buffer, /* tp_as_buffer */ |
| 570 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 571 | memory_doc, /* tp_doc */ |
| 572 | 0, /* tp_traverse */ |
| 573 | 0, /* tp_clear */ |
| 574 | 0, /* tp_richcompare */ |
| 575 | 0, /* tp_weaklistoffset */ |
| 576 | 0, /* tp_iter */ |
| 577 | 0, /* tp_iternext */ |
Guido van Rossum | 5dde61d | 2007-09-25 22:10:05 +0000 | [diff] [blame] | 578 | memory_methods, /* tp_methods */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 579 | 0, /* tp_members */ |
| 580 | memory_getsetlist, /* tp_getset */ |
| 581 | 0, /* tp_base */ |
| 582 | 0, /* tp_dict */ |
| 583 | 0, /* tp_descr_get */ |
| 584 | 0, /* tp_descr_set */ |
| 585 | 0, /* tp_dictoffset */ |
| 586 | 0, /* tp_init */ |
| 587 | 0, /* tp_alloc */ |
| 588 | memory_new, /* tp_new */ |
| 589 | }; |