| 1 | n/a | /* Memoryview object implementation */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | #include "pystrhex.h" |
|---|
| 5 | n/a | #include <stddef.h> |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | /****************************************************************************/ |
|---|
| 9 | n/a | /* ManagedBuffer Object */ |
|---|
| 10 | n/a | /****************************************************************************/ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | /* |
|---|
| 13 | n/a | ManagedBuffer Object: |
|---|
| 14 | n/a | --------------------- |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | The purpose of this object is to facilitate the handling of chained |
|---|
| 17 | n/a | memoryviews that have the same underlying exporting object. PEP-3118 |
|---|
| 18 | n/a | allows the underlying object to change while a view is exported. This |
|---|
| 19 | n/a | could lead to unexpected results when constructing a new memoryview |
|---|
| 20 | n/a | from an existing memoryview. |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | Rather than repeatedly redirecting buffer requests to the original base |
|---|
| 23 | n/a | object, all chained memoryviews use a single buffer snapshot. This |
|---|
| 24 | n/a | snapshot is generated by the constructor _PyManagedBuffer_FromObject(). |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | Ownership rules: |
|---|
| 27 | n/a | ---------------- |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | The master buffer inside a managed buffer is filled in by the original |
|---|
| 30 | n/a | base object. shape, strides, suboffsets and format are read-only for |
|---|
| 31 | n/a | all consumers. |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | A memoryview's buffer is a private copy of the exporter's buffer. shape, |
|---|
| 34 | n/a | strides and suboffsets belong to the memoryview and are thus writable. |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | If a memoryview itself exports several buffers via memory_getbuf(), all |
|---|
| 37 | n/a | buffer copies share shape, strides and suboffsets. In this case, the |
|---|
| 38 | n/a | arrays are NOT writable. |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | Reference count assumptions: |
|---|
| 41 | n/a | ---------------------------- |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | The 'obj' member of a Py_buffer must either be NULL or refer to the |
|---|
| 44 | n/a | exporting base object. In the Python codebase, all getbufferprocs |
|---|
| 45 | n/a | return a new reference to view.obj (example: bytes_buffer_getbuffer()). |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | PyBuffer_Release() decrements view.obj (if non-NULL), so the |
|---|
| 48 | n/a | releasebufferprocs must NOT decrement view.obj. |
|---|
| 49 | n/a | */ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | #define CHECK_MBUF_RELEASED(mbuf) \ |
|---|
| 53 | n/a | if (((_PyManagedBufferObject *)mbuf)->flags&_Py_MANAGED_BUFFER_RELEASED) { \ |
|---|
| 54 | n/a | PyErr_SetString(PyExc_ValueError, \ |
|---|
| 55 | n/a | "operation forbidden on released memoryview object"); \ |
|---|
| 56 | n/a | return NULL; \ |
|---|
| 57 | n/a | } |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | static inline _PyManagedBufferObject * |
|---|
| 61 | n/a | mbuf_alloc(void) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | mbuf = (_PyManagedBufferObject *) |
|---|
| 66 | n/a | PyObject_GC_New(_PyManagedBufferObject, &_PyManagedBuffer_Type); |
|---|
| 67 | n/a | if (mbuf == NULL) |
|---|
| 68 | n/a | return NULL; |
|---|
| 69 | n/a | mbuf->flags = 0; |
|---|
| 70 | n/a | mbuf->exports = 0; |
|---|
| 71 | n/a | mbuf->master.obj = NULL; |
|---|
| 72 | n/a | _PyObject_GC_TRACK(mbuf); |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | return mbuf; |
|---|
| 75 | n/a | } |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | static PyObject * |
|---|
| 78 | n/a | _PyManagedBuffer_FromObject(PyObject *base) |
|---|
| 79 | n/a | { |
|---|
| 80 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | mbuf = mbuf_alloc(); |
|---|
| 83 | n/a | if (mbuf == NULL) |
|---|
| 84 | n/a | return NULL; |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | if (PyObject_GetBuffer(base, &mbuf->master, PyBUF_FULL_RO) < 0) { |
|---|
| 87 | n/a | mbuf->master.obj = NULL; |
|---|
| 88 | n/a | Py_DECREF(mbuf); |
|---|
| 89 | n/a | return NULL; |
|---|
| 90 | n/a | } |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | return (PyObject *)mbuf; |
|---|
| 93 | n/a | } |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | static void |
|---|
| 96 | n/a | mbuf_release(_PyManagedBufferObject *self) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | if (self->flags&_Py_MANAGED_BUFFER_RELEASED) |
|---|
| 99 | n/a | return; |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | /* NOTE: at this point self->exports can still be > 0 if this function |
|---|
| 102 | n/a | is called from mbuf_clear() to break up a reference cycle. */ |
|---|
| 103 | n/a | self->flags |= _Py_MANAGED_BUFFER_RELEASED; |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | /* PyBuffer_Release() decrements master->obj and sets it to NULL. */ |
|---|
| 106 | n/a | _PyObject_GC_UNTRACK(self); |
|---|
| 107 | n/a | PyBuffer_Release(&self->master); |
|---|
| 108 | n/a | } |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | static void |
|---|
| 111 | n/a | mbuf_dealloc(_PyManagedBufferObject *self) |
|---|
| 112 | n/a | { |
|---|
| 113 | n/a | assert(self->exports == 0); |
|---|
| 114 | n/a | mbuf_release(self); |
|---|
| 115 | n/a | if (self->flags&_Py_MANAGED_BUFFER_FREE_FORMAT) |
|---|
| 116 | n/a | PyMem_Free(self->master.format); |
|---|
| 117 | n/a | PyObject_GC_Del(self); |
|---|
| 118 | n/a | } |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | static int |
|---|
| 121 | n/a | mbuf_traverse(_PyManagedBufferObject *self, visitproc visit, void *arg) |
|---|
| 122 | n/a | { |
|---|
| 123 | n/a | Py_VISIT(self->master.obj); |
|---|
| 124 | n/a | return 0; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | static int |
|---|
| 128 | n/a | mbuf_clear(_PyManagedBufferObject *self) |
|---|
| 129 | n/a | { |
|---|
| 130 | n/a | assert(self->exports >= 0); |
|---|
| 131 | n/a | mbuf_release(self); |
|---|
| 132 | n/a | return 0; |
|---|
| 133 | n/a | } |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | PyTypeObject _PyManagedBuffer_Type = { |
|---|
| 136 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 137 | n/a | "managedbuffer", |
|---|
| 138 | n/a | sizeof(_PyManagedBufferObject), |
|---|
| 139 | n/a | 0, |
|---|
| 140 | n/a | (destructor)mbuf_dealloc, /* tp_dealloc */ |
|---|
| 141 | n/a | 0, /* tp_print */ |
|---|
| 142 | n/a | 0, /* tp_getattr */ |
|---|
| 143 | n/a | 0, /* tp_setattr */ |
|---|
| 144 | n/a | 0, /* tp_reserved */ |
|---|
| 145 | n/a | 0, /* tp_repr */ |
|---|
| 146 | n/a | 0, /* tp_as_number */ |
|---|
| 147 | n/a | 0, /* tp_as_sequence */ |
|---|
| 148 | n/a | 0, /* tp_as_mapping */ |
|---|
| 149 | n/a | 0, /* tp_hash */ |
|---|
| 150 | n/a | 0, /* tp_call */ |
|---|
| 151 | n/a | 0, /* tp_str */ |
|---|
| 152 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 153 | n/a | 0, /* tp_setattro */ |
|---|
| 154 | n/a | 0, /* tp_as_buffer */ |
|---|
| 155 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 156 | n/a | 0, /* tp_doc */ |
|---|
| 157 | n/a | (traverseproc)mbuf_traverse, /* tp_traverse */ |
|---|
| 158 | n/a | (inquiry)mbuf_clear /* tp_clear */ |
|---|
| 159 | n/a | }; |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | /****************************************************************************/ |
|---|
| 163 | n/a | /* MemoryView Object */ |
|---|
| 164 | n/a | /****************************************************************************/ |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | /* In the process of breaking reference cycles mbuf_release() can be |
|---|
| 167 | n/a | called before memory_release(). */ |
|---|
| 168 | n/a | #define BASE_INACCESSIBLE(mv) \ |
|---|
| 169 | n/a | (((PyMemoryViewObject *)mv)->flags&_Py_MEMORYVIEW_RELEASED || \ |
|---|
| 170 | n/a | ((PyMemoryViewObject *)mv)->mbuf->flags&_Py_MANAGED_BUFFER_RELEASED) |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | #define CHECK_RELEASED(mv) \ |
|---|
| 173 | n/a | if (BASE_INACCESSIBLE(mv)) { \ |
|---|
| 174 | n/a | PyErr_SetString(PyExc_ValueError, \ |
|---|
| 175 | n/a | "operation forbidden on released memoryview object"); \ |
|---|
| 176 | n/a | return NULL; \ |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | #define CHECK_RELEASED_INT(mv) \ |
|---|
| 180 | n/a | if (BASE_INACCESSIBLE(mv)) { \ |
|---|
| 181 | n/a | PyErr_SetString(PyExc_ValueError, \ |
|---|
| 182 | n/a | "operation forbidden on released memoryview object"); \ |
|---|
| 183 | n/a | return -1; \ |
|---|
| 184 | n/a | } |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | #define CHECK_LIST_OR_TUPLE(v) \ |
|---|
| 187 | n/a | if (!PyList_Check(v) && !PyTuple_Check(v)) { \ |
|---|
| 188 | n/a | PyErr_SetString(PyExc_TypeError, \ |
|---|
| 189 | n/a | #v " must be a list or a tuple"); \ |
|---|
| 190 | n/a | return NULL; \ |
|---|
| 191 | n/a | } |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | #define VIEW_ADDR(mv) (&((PyMemoryViewObject *)mv)->view) |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | /* Check for the presence of suboffsets in the first dimension. */ |
|---|
| 196 | n/a | #define HAVE_PTR(suboffsets, dim) (suboffsets && suboffsets[dim] >= 0) |
|---|
| 197 | n/a | /* Adjust ptr if suboffsets are present. */ |
|---|
| 198 | n/a | #define ADJUST_PTR(ptr, suboffsets, dim) \ |
|---|
| 199 | n/a | (HAVE_PTR(suboffsets, dim) ? *((char**)ptr) + suboffsets[dim] : ptr) |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | /* Memoryview buffer properties */ |
|---|
| 202 | n/a | #define MV_C_CONTIGUOUS(flags) (flags&(_Py_MEMORYVIEW_SCALAR|_Py_MEMORYVIEW_C)) |
|---|
| 203 | n/a | #define MV_F_CONTIGUOUS(flags) \ |
|---|
| 204 | n/a | (flags&(_Py_MEMORYVIEW_SCALAR|_Py_MEMORYVIEW_FORTRAN)) |
|---|
| 205 | n/a | #define MV_ANY_CONTIGUOUS(flags) \ |
|---|
| 206 | n/a | (flags&(_Py_MEMORYVIEW_SCALAR|_Py_MEMORYVIEW_C|_Py_MEMORYVIEW_FORTRAN)) |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | /* Fast contiguity test. Caller must ensure suboffsets==NULL and ndim==1. */ |
|---|
| 209 | n/a | #define MV_CONTIGUOUS_NDIM1(view) \ |
|---|
| 210 | n/a | ((view)->shape[0] == 1 || (view)->strides[0] == (view)->itemsize) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | /* getbuffer() requests */ |
|---|
| 213 | n/a | #define REQ_INDIRECT(flags) ((flags&PyBUF_INDIRECT) == PyBUF_INDIRECT) |
|---|
| 214 | n/a | #define REQ_C_CONTIGUOUS(flags) ((flags&PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) |
|---|
| 215 | n/a | #define REQ_F_CONTIGUOUS(flags) ((flags&PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) |
|---|
| 216 | n/a | #define REQ_ANY_CONTIGUOUS(flags) ((flags&PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS) |
|---|
| 217 | n/a | #define REQ_STRIDES(flags) ((flags&PyBUF_STRIDES) == PyBUF_STRIDES) |
|---|
| 218 | n/a | #define REQ_SHAPE(flags) ((flags&PyBUF_ND) == PyBUF_ND) |
|---|
| 219 | n/a | #define REQ_WRITABLE(flags) (flags&PyBUF_WRITABLE) |
|---|
| 220 | n/a | #define REQ_FORMAT(flags) (flags&PyBUF_FORMAT) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | PyDoc_STRVAR(memory_doc, |
|---|
| 224 | n/a | "memoryview(object)\n--\n\ |
|---|
| 225 | n/a | \n\ |
|---|
| 226 | n/a | Create a new memoryview object which references the given object."); |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | /**************************************************************************/ |
|---|
| 230 | n/a | /* Copy memoryview buffers */ |
|---|
| 231 | n/a | /**************************************************************************/ |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | /* The functions in this section take a source and a destination buffer |
|---|
| 234 | n/a | with the same logical structure: format, itemsize, ndim and shape |
|---|
| 235 | n/a | are identical, with ndim > 0. |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | NOTE: All buffers are assumed to have PyBUF_FULL information, which |
|---|
| 238 | n/a | is the case for memoryviews! */ |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | /* Assumptions: ndim >= 1. The macro tests for a corner case that should |
|---|
| 242 | n/a | perhaps be explicitly forbidden in the PEP. */ |
|---|
| 243 | n/a | #define HAVE_SUBOFFSETS_IN_LAST_DIM(view) \ |
|---|
| 244 | n/a | (view->suboffsets && view->suboffsets[dest->ndim-1] >= 0) |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | static inline int |
|---|
| 247 | n/a | last_dim_is_contiguous(const Py_buffer *dest, const Py_buffer *src) |
|---|
| 248 | n/a | { |
|---|
| 249 | n/a | assert(dest->ndim > 0 && src->ndim > 0); |
|---|
| 250 | n/a | return (!HAVE_SUBOFFSETS_IN_LAST_DIM(dest) && |
|---|
| 251 | n/a | !HAVE_SUBOFFSETS_IN_LAST_DIM(src) && |
|---|
| 252 | n/a | dest->strides[dest->ndim-1] == dest->itemsize && |
|---|
| 253 | n/a | src->strides[src->ndim-1] == src->itemsize); |
|---|
| 254 | n/a | } |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | /* This is not a general function for determining format equivalence. |
|---|
| 257 | n/a | It is used in copy_single() and copy_buffer() to weed out non-matching |
|---|
| 258 | n/a | formats. Skipping the '@' character is specifically used in slice |
|---|
| 259 | n/a | assignments, where the lvalue is already known to have a single character |
|---|
| 260 | n/a | format. This is a performance hack that could be rewritten (if properly |
|---|
| 261 | n/a | benchmarked). */ |
|---|
| 262 | n/a | static inline int |
|---|
| 263 | n/a | equiv_format(const Py_buffer *dest, const Py_buffer *src) |
|---|
| 264 | n/a | { |
|---|
| 265 | n/a | const char *dfmt, *sfmt; |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | assert(dest->format && src->format); |
|---|
| 268 | n/a | dfmt = dest->format[0] == '@' ? dest->format+1 : dest->format; |
|---|
| 269 | n/a | sfmt = src->format[0] == '@' ? src->format+1 : src->format; |
|---|
| 270 | n/a | |
|---|
| 271 | n/a | if (strcmp(dfmt, sfmt) != 0 || |
|---|
| 272 | n/a | dest->itemsize != src->itemsize) { |
|---|
| 273 | n/a | return 0; |
|---|
| 274 | n/a | } |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | return 1; |
|---|
| 277 | n/a | } |
|---|
| 278 | n/a | |
|---|
| 279 | n/a | /* Two shapes are equivalent if they are either equal or identical up |
|---|
| 280 | n/a | to a zero element at the same position. For example, in NumPy arrays |
|---|
| 281 | n/a | the shapes [1, 0, 5] and [1, 0, 7] are equivalent. */ |
|---|
| 282 | n/a | static inline int |
|---|
| 283 | n/a | equiv_shape(const Py_buffer *dest, const Py_buffer *src) |
|---|
| 284 | n/a | { |
|---|
| 285 | n/a | int i; |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | if (dest->ndim != src->ndim) |
|---|
| 288 | n/a | return 0; |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | for (i = 0; i < dest->ndim; i++) { |
|---|
| 291 | n/a | if (dest->shape[i] != src->shape[i]) |
|---|
| 292 | n/a | return 0; |
|---|
| 293 | n/a | if (dest->shape[i] == 0) |
|---|
| 294 | n/a | break; |
|---|
| 295 | n/a | } |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | return 1; |
|---|
| 298 | n/a | } |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | /* Check that the logical structure of the destination and source buffers |
|---|
| 301 | n/a | is identical. */ |
|---|
| 302 | n/a | static int |
|---|
| 303 | n/a | equiv_structure(const Py_buffer *dest, const Py_buffer *src) |
|---|
| 304 | n/a | { |
|---|
| 305 | n/a | if (!equiv_format(dest, src) || |
|---|
| 306 | n/a | !equiv_shape(dest, src)) { |
|---|
| 307 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 308 | n/a | "memoryview assignment: lvalue and rvalue have different " |
|---|
| 309 | n/a | "structures"); |
|---|
| 310 | n/a | return 0; |
|---|
| 311 | n/a | } |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | return 1; |
|---|
| 314 | n/a | } |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | /* Base case for recursive multi-dimensional copying. Contiguous arrays are |
|---|
| 317 | n/a | copied with very little overhead. Assumptions: ndim == 1, mem == NULL or |
|---|
| 318 | n/a | sizeof(mem) == shape[0] * itemsize. */ |
|---|
| 319 | n/a | static void |
|---|
| 320 | n/a | copy_base(const Py_ssize_t *shape, Py_ssize_t itemsize, |
|---|
| 321 | n/a | char *dptr, const Py_ssize_t *dstrides, const Py_ssize_t *dsuboffsets, |
|---|
| 322 | n/a | char *sptr, const Py_ssize_t *sstrides, const Py_ssize_t *ssuboffsets, |
|---|
| 323 | n/a | char *mem) |
|---|
| 324 | n/a | { |
|---|
| 325 | n/a | if (mem == NULL) { /* contiguous */ |
|---|
| 326 | n/a | Py_ssize_t size = shape[0] * itemsize; |
|---|
| 327 | n/a | if (dptr + size < sptr || sptr + size < dptr) |
|---|
| 328 | n/a | memcpy(dptr, sptr, size); /* no overlapping */ |
|---|
| 329 | n/a | else |
|---|
| 330 | n/a | memmove(dptr, sptr, size); |
|---|
| 331 | n/a | } |
|---|
| 332 | n/a | else { |
|---|
| 333 | n/a | char *p; |
|---|
| 334 | n/a | Py_ssize_t i; |
|---|
| 335 | n/a | for (i=0, p=mem; i < shape[0]; p+=itemsize, sptr+=sstrides[0], i++) { |
|---|
| 336 | n/a | char *xsptr = ADJUST_PTR(sptr, ssuboffsets, 0); |
|---|
| 337 | n/a | memcpy(p, xsptr, itemsize); |
|---|
| 338 | n/a | } |
|---|
| 339 | n/a | for (i=0, p=mem; i < shape[0]; p+=itemsize, dptr+=dstrides[0], i++) { |
|---|
| 340 | n/a | char *xdptr = ADJUST_PTR(dptr, dsuboffsets, 0); |
|---|
| 341 | n/a | memcpy(xdptr, p, itemsize); |
|---|
| 342 | n/a | } |
|---|
| 343 | n/a | } |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | } |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | /* Recursively copy a source buffer to a destination buffer. The two buffers |
|---|
| 348 | n/a | have the same ndim, shape and itemsize. */ |
|---|
| 349 | n/a | static void |
|---|
| 350 | n/a | copy_rec(const Py_ssize_t *shape, Py_ssize_t ndim, Py_ssize_t itemsize, |
|---|
| 351 | n/a | char *dptr, const Py_ssize_t *dstrides, const Py_ssize_t *dsuboffsets, |
|---|
| 352 | n/a | char *sptr, const Py_ssize_t *sstrides, const Py_ssize_t *ssuboffsets, |
|---|
| 353 | n/a | char *mem) |
|---|
| 354 | n/a | { |
|---|
| 355 | n/a | Py_ssize_t i; |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | assert(ndim >= 1); |
|---|
| 358 | n/a | |
|---|
| 359 | n/a | if (ndim == 1) { |
|---|
| 360 | n/a | copy_base(shape, itemsize, |
|---|
| 361 | n/a | dptr, dstrides, dsuboffsets, |
|---|
| 362 | n/a | sptr, sstrides, ssuboffsets, |
|---|
| 363 | n/a | mem); |
|---|
| 364 | n/a | return; |
|---|
| 365 | n/a | } |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | for (i = 0; i < shape[0]; dptr+=dstrides[0], sptr+=sstrides[0], i++) { |
|---|
| 368 | n/a | char *xdptr = ADJUST_PTR(dptr, dsuboffsets, 0); |
|---|
| 369 | n/a | char *xsptr = ADJUST_PTR(sptr, ssuboffsets, 0); |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | copy_rec(shape+1, ndim-1, itemsize, |
|---|
| 372 | n/a | xdptr, dstrides+1, dsuboffsets ? dsuboffsets+1 : NULL, |
|---|
| 373 | n/a | xsptr, sstrides+1, ssuboffsets ? ssuboffsets+1 : NULL, |
|---|
| 374 | n/a | mem); |
|---|
| 375 | n/a | } |
|---|
| 376 | n/a | } |
|---|
| 377 | n/a | |
|---|
| 378 | n/a | /* Faster copying of one-dimensional arrays. */ |
|---|
| 379 | n/a | static int |
|---|
| 380 | n/a | copy_single(Py_buffer *dest, Py_buffer *src) |
|---|
| 381 | n/a | { |
|---|
| 382 | n/a | char *mem = NULL; |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | assert(dest->ndim == 1); |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | if (!equiv_structure(dest, src)) |
|---|
| 387 | n/a | return -1; |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | if (!last_dim_is_contiguous(dest, src)) { |
|---|
| 390 | n/a | mem = PyMem_Malloc(dest->shape[0] * dest->itemsize); |
|---|
| 391 | n/a | if (mem == NULL) { |
|---|
| 392 | n/a | PyErr_NoMemory(); |
|---|
| 393 | n/a | return -1; |
|---|
| 394 | n/a | } |
|---|
| 395 | n/a | } |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | copy_base(dest->shape, dest->itemsize, |
|---|
| 398 | n/a | dest->buf, dest->strides, dest->suboffsets, |
|---|
| 399 | n/a | src->buf, src->strides, src->suboffsets, |
|---|
| 400 | n/a | mem); |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | if (mem) |
|---|
| 403 | n/a | PyMem_Free(mem); |
|---|
| 404 | n/a | |
|---|
| 405 | n/a | return 0; |
|---|
| 406 | n/a | } |
|---|
| 407 | n/a | |
|---|
| 408 | n/a | /* Recursively copy src to dest. Both buffers must have the same basic |
|---|
| 409 | n/a | structure. Copying is atomic, the function never fails with a partial |
|---|
| 410 | n/a | copy. */ |
|---|
| 411 | n/a | static int |
|---|
| 412 | n/a | copy_buffer(Py_buffer *dest, Py_buffer *src) |
|---|
| 413 | n/a | { |
|---|
| 414 | n/a | char *mem = NULL; |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | assert(dest->ndim > 0); |
|---|
| 417 | n/a | |
|---|
| 418 | n/a | if (!equiv_structure(dest, src)) |
|---|
| 419 | n/a | return -1; |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | if (!last_dim_is_contiguous(dest, src)) { |
|---|
| 422 | n/a | mem = PyMem_Malloc(dest->shape[dest->ndim-1] * dest->itemsize); |
|---|
| 423 | n/a | if (mem == NULL) { |
|---|
| 424 | n/a | PyErr_NoMemory(); |
|---|
| 425 | n/a | return -1; |
|---|
| 426 | n/a | } |
|---|
| 427 | n/a | } |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | copy_rec(dest->shape, dest->ndim, dest->itemsize, |
|---|
| 430 | n/a | dest->buf, dest->strides, dest->suboffsets, |
|---|
| 431 | n/a | src->buf, src->strides, src->suboffsets, |
|---|
| 432 | n/a | mem); |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | if (mem) |
|---|
| 435 | n/a | PyMem_Free(mem); |
|---|
| 436 | n/a | |
|---|
| 437 | n/a | return 0; |
|---|
| 438 | n/a | } |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | /* Initialize strides for a C-contiguous array. */ |
|---|
| 441 | n/a | static inline void |
|---|
| 442 | n/a | init_strides_from_shape(Py_buffer *view) |
|---|
| 443 | n/a | { |
|---|
| 444 | n/a | Py_ssize_t i; |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | assert(view->ndim > 0); |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | view->strides[view->ndim-1] = view->itemsize; |
|---|
| 449 | n/a | for (i = view->ndim-2; i >= 0; i--) |
|---|
| 450 | n/a | view->strides[i] = view->strides[i+1] * view->shape[i+1]; |
|---|
| 451 | n/a | } |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | /* Initialize strides for a Fortran-contiguous array. */ |
|---|
| 454 | n/a | static inline void |
|---|
| 455 | n/a | init_fortran_strides_from_shape(Py_buffer *view) |
|---|
| 456 | n/a | { |
|---|
| 457 | n/a | Py_ssize_t i; |
|---|
| 458 | n/a | |
|---|
| 459 | n/a | assert(view->ndim > 0); |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | view->strides[0] = view->itemsize; |
|---|
| 462 | n/a | for (i = 1; i < view->ndim; i++) |
|---|
| 463 | n/a | view->strides[i] = view->strides[i-1] * view->shape[i-1]; |
|---|
| 464 | n/a | } |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | /* Copy src to a contiguous representation. order is one of 'C', 'F' (Fortran) |
|---|
| 467 | n/a | or 'A' (Any). Assumptions: src has PyBUF_FULL information, src->ndim >= 1, |
|---|
| 468 | n/a | len(mem) == src->len. */ |
|---|
| 469 | n/a | static int |
|---|
| 470 | n/a | buffer_to_contiguous(char *mem, Py_buffer *src, char order) |
|---|
| 471 | n/a | { |
|---|
| 472 | n/a | Py_buffer dest; |
|---|
| 473 | n/a | Py_ssize_t *strides; |
|---|
| 474 | n/a | int ret; |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | assert(src->ndim >= 1); |
|---|
| 477 | n/a | assert(src->shape != NULL); |
|---|
| 478 | n/a | assert(src->strides != NULL); |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | strides = PyMem_Malloc(src->ndim * (sizeof *src->strides)); |
|---|
| 481 | n/a | if (strides == NULL) { |
|---|
| 482 | n/a | PyErr_NoMemory(); |
|---|
| 483 | n/a | return -1; |
|---|
| 484 | n/a | } |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | /* initialize dest */ |
|---|
| 487 | n/a | dest = *src; |
|---|
| 488 | n/a | dest.buf = mem; |
|---|
| 489 | n/a | /* shape is constant and shared: the logical representation of the |
|---|
| 490 | n/a | array is unaltered. */ |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | /* The physical representation determined by strides (and possibly |
|---|
| 493 | n/a | suboffsets) may change. */ |
|---|
| 494 | n/a | dest.strides = strides; |
|---|
| 495 | n/a | if (order == 'C' || order == 'A') { |
|---|
| 496 | n/a | init_strides_from_shape(&dest); |
|---|
| 497 | n/a | } |
|---|
| 498 | n/a | else { |
|---|
| 499 | n/a | init_fortran_strides_from_shape(&dest); |
|---|
| 500 | n/a | } |
|---|
| 501 | n/a | |
|---|
| 502 | n/a | dest.suboffsets = NULL; |
|---|
| 503 | n/a | |
|---|
| 504 | n/a | ret = copy_buffer(&dest, src); |
|---|
| 505 | n/a | |
|---|
| 506 | n/a | PyMem_Free(strides); |
|---|
| 507 | n/a | return ret; |
|---|
| 508 | n/a | } |
|---|
| 509 | n/a | |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | /****************************************************************************/ |
|---|
| 512 | n/a | /* Constructors */ |
|---|
| 513 | n/a | /****************************************************************************/ |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | /* Initialize values that are shared with the managed buffer. */ |
|---|
| 516 | n/a | static inline void |
|---|
| 517 | n/a | init_shared_values(Py_buffer *dest, const Py_buffer *src) |
|---|
| 518 | n/a | { |
|---|
| 519 | n/a | dest->obj = src->obj; |
|---|
| 520 | n/a | dest->buf = src->buf; |
|---|
| 521 | n/a | dest->len = src->len; |
|---|
| 522 | n/a | dest->itemsize = src->itemsize; |
|---|
| 523 | n/a | dest->readonly = src->readonly; |
|---|
| 524 | n/a | dest->format = src->format ? src->format : "B"; |
|---|
| 525 | n/a | dest->internal = src->internal; |
|---|
| 526 | n/a | } |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | /* Copy shape and strides. Reconstruct missing values. */ |
|---|
| 529 | n/a | static void |
|---|
| 530 | n/a | init_shape_strides(Py_buffer *dest, const Py_buffer *src) |
|---|
| 531 | n/a | { |
|---|
| 532 | n/a | Py_ssize_t i; |
|---|
| 533 | n/a | |
|---|
| 534 | n/a | if (src->ndim == 0) { |
|---|
| 535 | n/a | dest->shape = NULL; |
|---|
| 536 | n/a | dest->strides = NULL; |
|---|
| 537 | n/a | return; |
|---|
| 538 | n/a | } |
|---|
| 539 | n/a | if (src->ndim == 1) { |
|---|
| 540 | n/a | dest->shape[0] = src->shape ? src->shape[0] : src->len / src->itemsize; |
|---|
| 541 | n/a | dest->strides[0] = src->strides ? src->strides[0] : src->itemsize; |
|---|
| 542 | n/a | return; |
|---|
| 543 | n/a | } |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | for (i = 0; i < src->ndim; i++) |
|---|
| 546 | n/a | dest->shape[i] = src->shape[i]; |
|---|
| 547 | n/a | if (src->strides) { |
|---|
| 548 | n/a | for (i = 0; i < src->ndim; i++) |
|---|
| 549 | n/a | dest->strides[i] = src->strides[i]; |
|---|
| 550 | n/a | } |
|---|
| 551 | n/a | else { |
|---|
| 552 | n/a | init_strides_from_shape(dest); |
|---|
| 553 | n/a | } |
|---|
| 554 | n/a | } |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | static inline void |
|---|
| 557 | n/a | init_suboffsets(Py_buffer *dest, const Py_buffer *src) |
|---|
| 558 | n/a | { |
|---|
| 559 | n/a | Py_ssize_t i; |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | if (src->suboffsets == NULL) { |
|---|
| 562 | n/a | dest->suboffsets = NULL; |
|---|
| 563 | n/a | return; |
|---|
| 564 | n/a | } |
|---|
| 565 | n/a | for (i = 0; i < src->ndim; i++) |
|---|
| 566 | n/a | dest->suboffsets[i] = src->suboffsets[i]; |
|---|
| 567 | n/a | } |
|---|
| 568 | n/a | |
|---|
| 569 | n/a | /* len = product(shape) * itemsize */ |
|---|
| 570 | n/a | static inline void |
|---|
| 571 | n/a | init_len(Py_buffer *view) |
|---|
| 572 | n/a | { |
|---|
| 573 | n/a | Py_ssize_t i, len; |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | len = 1; |
|---|
| 576 | n/a | for (i = 0; i < view->ndim; i++) |
|---|
| 577 | n/a | len *= view->shape[i]; |
|---|
| 578 | n/a | len *= view->itemsize; |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | view->len = len; |
|---|
| 581 | n/a | } |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | /* Initialize memoryview buffer properties. */ |
|---|
| 584 | n/a | static void |
|---|
| 585 | n/a | init_flags(PyMemoryViewObject *mv) |
|---|
| 586 | n/a | { |
|---|
| 587 | n/a | const Py_buffer *view = &mv->view; |
|---|
| 588 | n/a | int flags = 0; |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | switch (view->ndim) { |
|---|
| 591 | n/a | case 0: |
|---|
| 592 | n/a | flags |= (_Py_MEMORYVIEW_SCALAR|_Py_MEMORYVIEW_C| |
|---|
| 593 | n/a | _Py_MEMORYVIEW_FORTRAN); |
|---|
| 594 | n/a | break; |
|---|
| 595 | n/a | case 1: |
|---|
| 596 | n/a | if (MV_CONTIGUOUS_NDIM1(view)) |
|---|
| 597 | n/a | flags |= (_Py_MEMORYVIEW_C|_Py_MEMORYVIEW_FORTRAN); |
|---|
| 598 | n/a | break; |
|---|
| 599 | n/a | default: |
|---|
| 600 | n/a | if (PyBuffer_IsContiguous(view, 'C')) |
|---|
| 601 | n/a | flags |= _Py_MEMORYVIEW_C; |
|---|
| 602 | n/a | if (PyBuffer_IsContiguous(view, 'F')) |
|---|
| 603 | n/a | flags |= _Py_MEMORYVIEW_FORTRAN; |
|---|
| 604 | n/a | break; |
|---|
| 605 | n/a | } |
|---|
| 606 | n/a | |
|---|
| 607 | n/a | if (view->suboffsets) { |
|---|
| 608 | n/a | flags |= _Py_MEMORYVIEW_PIL; |
|---|
| 609 | n/a | flags &= ~(_Py_MEMORYVIEW_C|_Py_MEMORYVIEW_FORTRAN); |
|---|
| 610 | n/a | } |
|---|
| 611 | n/a | |
|---|
| 612 | n/a | mv->flags = flags; |
|---|
| 613 | n/a | } |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | /* Allocate a new memoryview and perform basic initialization. New memoryviews |
|---|
| 616 | n/a | are exclusively created through the mbuf_add functions. */ |
|---|
| 617 | n/a | static inline PyMemoryViewObject * |
|---|
| 618 | n/a | memory_alloc(int ndim) |
|---|
| 619 | n/a | { |
|---|
| 620 | n/a | PyMemoryViewObject *mv; |
|---|
| 621 | n/a | |
|---|
| 622 | n/a | mv = (PyMemoryViewObject *) |
|---|
| 623 | n/a | PyObject_GC_NewVar(PyMemoryViewObject, &PyMemoryView_Type, 3*ndim); |
|---|
| 624 | n/a | if (mv == NULL) |
|---|
| 625 | n/a | return NULL; |
|---|
| 626 | n/a | |
|---|
| 627 | n/a | mv->mbuf = NULL; |
|---|
| 628 | n/a | mv->hash = -1; |
|---|
| 629 | n/a | mv->flags = 0; |
|---|
| 630 | n/a | mv->exports = 0; |
|---|
| 631 | n/a | mv->view.ndim = ndim; |
|---|
| 632 | n/a | mv->view.shape = mv->ob_array; |
|---|
| 633 | n/a | mv->view.strides = mv->ob_array + ndim; |
|---|
| 634 | n/a | mv->view.suboffsets = mv->ob_array + 2 * ndim; |
|---|
| 635 | n/a | mv->weakreflist = NULL; |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | _PyObject_GC_TRACK(mv); |
|---|
| 638 | n/a | return mv; |
|---|
| 639 | n/a | } |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | /* |
|---|
| 642 | n/a | Return a new memoryview that is registered with mbuf. If src is NULL, |
|---|
| 643 | n/a | use mbuf->master as the underlying buffer. Otherwise, use src. |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | The new memoryview has full buffer information: shape and strides |
|---|
| 646 | n/a | are always present, suboffsets as needed. Arrays are copied to |
|---|
| 647 | n/a | the memoryview's ob_array field. |
|---|
| 648 | n/a | */ |
|---|
| 649 | n/a | static PyObject * |
|---|
| 650 | n/a | mbuf_add_view(_PyManagedBufferObject *mbuf, const Py_buffer *src) |
|---|
| 651 | n/a | { |
|---|
| 652 | n/a | PyMemoryViewObject *mv; |
|---|
| 653 | n/a | Py_buffer *dest; |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | if (src == NULL) |
|---|
| 656 | n/a | src = &mbuf->master; |
|---|
| 657 | n/a | |
|---|
| 658 | n/a | if (src->ndim > PyBUF_MAX_NDIM) { |
|---|
| 659 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 660 | n/a | "memoryview: number of dimensions must not exceed " |
|---|
| 661 | n/a | Py_STRINGIFY(PyBUF_MAX_NDIM)); |
|---|
| 662 | n/a | return NULL; |
|---|
| 663 | n/a | } |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | mv = memory_alloc(src->ndim); |
|---|
| 666 | n/a | if (mv == NULL) |
|---|
| 667 | n/a | return NULL; |
|---|
| 668 | n/a | |
|---|
| 669 | n/a | dest = &mv->view; |
|---|
| 670 | n/a | init_shared_values(dest, src); |
|---|
| 671 | n/a | init_shape_strides(dest, src); |
|---|
| 672 | n/a | init_suboffsets(dest, src); |
|---|
| 673 | n/a | init_flags(mv); |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | mv->mbuf = mbuf; |
|---|
| 676 | n/a | Py_INCREF(mbuf); |
|---|
| 677 | n/a | mbuf->exports++; |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | return (PyObject *)mv; |
|---|
| 680 | n/a | } |
|---|
| 681 | n/a | |
|---|
| 682 | n/a | /* Register an incomplete view: shape, strides, suboffsets and flags still |
|---|
| 683 | n/a | need to be initialized. Use 'ndim' instead of src->ndim to determine the |
|---|
| 684 | n/a | size of the memoryview's ob_array. |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | Assumption: ndim <= PyBUF_MAX_NDIM. */ |
|---|
| 687 | n/a | static PyObject * |
|---|
| 688 | n/a | mbuf_add_incomplete_view(_PyManagedBufferObject *mbuf, const Py_buffer *src, |
|---|
| 689 | n/a | int ndim) |
|---|
| 690 | n/a | { |
|---|
| 691 | n/a | PyMemoryViewObject *mv; |
|---|
| 692 | n/a | Py_buffer *dest; |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | if (src == NULL) |
|---|
| 695 | n/a | src = &mbuf->master; |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | assert(ndim <= PyBUF_MAX_NDIM); |
|---|
| 698 | n/a | |
|---|
| 699 | n/a | mv = memory_alloc(ndim); |
|---|
| 700 | n/a | if (mv == NULL) |
|---|
| 701 | n/a | return NULL; |
|---|
| 702 | n/a | |
|---|
| 703 | n/a | dest = &mv->view; |
|---|
| 704 | n/a | init_shared_values(dest, src); |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | mv->mbuf = mbuf; |
|---|
| 707 | n/a | Py_INCREF(mbuf); |
|---|
| 708 | n/a | mbuf->exports++; |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | return (PyObject *)mv; |
|---|
| 711 | n/a | } |
|---|
| 712 | n/a | |
|---|
| 713 | n/a | /* Expose a raw memory area as a view of contiguous bytes. flags can be |
|---|
| 714 | n/a | PyBUF_READ or PyBUF_WRITE. view->format is set to "B" (unsigned bytes). |
|---|
| 715 | n/a | The memoryview has complete buffer information. */ |
|---|
| 716 | n/a | PyObject * |
|---|
| 717 | n/a | PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags) |
|---|
| 718 | n/a | { |
|---|
| 719 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 720 | n/a | PyObject *mv; |
|---|
| 721 | n/a | int readonly; |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | assert(mem != NULL); |
|---|
| 724 | n/a | assert(flags == PyBUF_READ || flags == PyBUF_WRITE); |
|---|
| 725 | n/a | |
|---|
| 726 | n/a | mbuf = mbuf_alloc(); |
|---|
| 727 | n/a | if (mbuf == NULL) |
|---|
| 728 | n/a | return NULL; |
|---|
| 729 | n/a | |
|---|
| 730 | n/a | readonly = (flags == PyBUF_WRITE) ? 0 : 1; |
|---|
| 731 | n/a | (void)PyBuffer_FillInfo(&mbuf->master, NULL, mem, size, readonly, |
|---|
| 732 | n/a | PyBUF_FULL_RO); |
|---|
| 733 | n/a | |
|---|
| 734 | n/a | mv = mbuf_add_view(mbuf, NULL); |
|---|
| 735 | n/a | Py_DECREF(mbuf); |
|---|
| 736 | n/a | |
|---|
| 737 | n/a | return mv; |
|---|
| 738 | n/a | } |
|---|
| 739 | n/a | |
|---|
| 740 | n/a | /* Create a memoryview from a given Py_buffer. For simple byte views, |
|---|
| 741 | n/a | PyMemoryView_FromMemory() should be used instead. |
|---|
| 742 | n/a | This function is the only entry point that can create a master buffer |
|---|
| 743 | n/a | without full information. Because of this fact init_shape_strides() |
|---|
| 744 | n/a | must be able to reconstruct missing values. */ |
|---|
| 745 | n/a | PyObject * |
|---|
| 746 | n/a | PyMemoryView_FromBuffer(Py_buffer *info) |
|---|
| 747 | n/a | { |
|---|
| 748 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 749 | n/a | PyObject *mv; |
|---|
| 750 | n/a | |
|---|
| 751 | n/a | if (info->buf == NULL) { |
|---|
| 752 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 753 | n/a | "PyMemoryView_FromBuffer(): info->buf must not be NULL"); |
|---|
| 754 | n/a | return NULL; |
|---|
| 755 | n/a | } |
|---|
| 756 | n/a | |
|---|
| 757 | n/a | mbuf = mbuf_alloc(); |
|---|
| 758 | n/a | if (mbuf == NULL) |
|---|
| 759 | n/a | return NULL; |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | /* info->obj is either NULL or a borrowed reference. This reference |
|---|
| 762 | n/a | should not be decremented in PyBuffer_Release(). */ |
|---|
| 763 | n/a | mbuf->master = *info; |
|---|
| 764 | n/a | mbuf->master.obj = NULL; |
|---|
| 765 | n/a | |
|---|
| 766 | n/a | mv = mbuf_add_view(mbuf, NULL); |
|---|
| 767 | n/a | Py_DECREF(mbuf); |
|---|
| 768 | n/a | |
|---|
| 769 | n/a | return mv; |
|---|
| 770 | n/a | } |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | /* Create a memoryview from an object that implements the buffer protocol. |
|---|
| 773 | n/a | If the object is a memoryview, the new memoryview must be registered |
|---|
| 774 | n/a | with the same managed buffer. Otherwise, a new managed buffer is created. */ |
|---|
| 775 | n/a | PyObject * |
|---|
| 776 | n/a | PyMemoryView_FromObject(PyObject *v) |
|---|
| 777 | n/a | { |
|---|
| 778 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 779 | n/a | |
|---|
| 780 | n/a | if (PyMemoryView_Check(v)) { |
|---|
| 781 | n/a | PyMemoryViewObject *mv = (PyMemoryViewObject *)v; |
|---|
| 782 | n/a | CHECK_RELEASED(mv); |
|---|
| 783 | n/a | return mbuf_add_view(mv->mbuf, &mv->view); |
|---|
| 784 | n/a | } |
|---|
| 785 | n/a | else if (PyObject_CheckBuffer(v)) { |
|---|
| 786 | n/a | PyObject *ret; |
|---|
| 787 | n/a | mbuf = (_PyManagedBufferObject *)_PyManagedBuffer_FromObject(v); |
|---|
| 788 | n/a | if (mbuf == NULL) |
|---|
| 789 | n/a | return NULL; |
|---|
| 790 | n/a | ret = mbuf_add_view(mbuf, NULL); |
|---|
| 791 | n/a | Py_DECREF(mbuf); |
|---|
| 792 | n/a | return ret; |
|---|
| 793 | n/a | } |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 796 | n/a | "memoryview: a bytes-like object is required, not '%.200s'", |
|---|
| 797 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 798 | n/a | return NULL; |
|---|
| 799 | n/a | } |
|---|
| 800 | n/a | |
|---|
| 801 | n/a | /* Copy the format string from a base object that might vanish. */ |
|---|
| 802 | n/a | static int |
|---|
| 803 | n/a | mbuf_copy_format(_PyManagedBufferObject *mbuf, const char *fmt) |
|---|
| 804 | n/a | { |
|---|
| 805 | n/a | if (fmt != NULL) { |
|---|
| 806 | n/a | char *cp = PyMem_Malloc(strlen(fmt)+1); |
|---|
| 807 | n/a | if (cp == NULL) { |
|---|
| 808 | n/a | PyErr_NoMemory(); |
|---|
| 809 | n/a | return -1; |
|---|
| 810 | n/a | } |
|---|
| 811 | n/a | mbuf->master.format = strcpy(cp, fmt); |
|---|
| 812 | n/a | mbuf->flags |= _Py_MANAGED_BUFFER_FREE_FORMAT; |
|---|
| 813 | n/a | } |
|---|
| 814 | n/a | |
|---|
| 815 | n/a | return 0; |
|---|
| 816 | n/a | } |
|---|
| 817 | n/a | |
|---|
| 818 | n/a | /* |
|---|
| 819 | n/a | Return a memoryview that is based on a contiguous copy of src. |
|---|
| 820 | n/a | Assumptions: src has PyBUF_FULL_RO information, src->ndim > 0. |
|---|
| 821 | n/a | |
|---|
| 822 | n/a | Ownership rules: |
|---|
| 823 | n/a | 1) As usual, the returned memoryview has a private copy |
|---|
| 824 | n/a | of src->shape, src->strides and src->suboffsets. |
|---|
| 825 | n/a | 2) src->format is copied to the master buffer and released |
|---|
| 826 | n/a | in mbuf_dealloc(). The releasebufferproc of the bytes |
|---|
| 827 | n/a | object is NULL, so it does not matter that mbuf_release() |
|---|
| 828 | n/a | passes the altered format pointer to PyBuffer_Release(). |
|---|
| 829 | n/a | */ |
|---|
| 830 | n/a | static PyObject * |
|---|
| 831 | n/a | memory_from_contiguous_copy(Py_buffer *src, char order) |
|---|
| 832 | n/a | { |
|---|
| 833 | n/a | _PyManagedBufferObject *mbuf; |
|---|
| 834 | n/a | PyMemoryViewObject *mv; |
|---|
| 835 | n/a | PyObject *bytes; |
|---|
| 836 | n/a | Py_buffer *dest; |
|---|
| 837 | n/a | int i; |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | assert(src->ndim > 0); |
|---|
| 840 | n/a | assert(src->shape != NULL); |
|---|
| 841 | n/a | |
|---|
| 842 | n/a | bytes = PyBytes_FromStringAndSize(NULL, src->len); |
|---|
| 843 | n/a | if (bytes == NULL) |
|---|
| 844 | n/a | return NULL; |
|---|
| 845 | n/a | |
|---|
| 846 | n/a | mbuf = (_PyManagedBufferObject *)_PyManagedBuffer_FromObject(bytes); |
|---|
| 847 | n/a | Py_DECREF(bytes); |
|---|
| 848 | n/a | if (mbuf == NULL) |
|---|
| 849 | n/a | return NULL; |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | if (mbuf_copy_format(mbuf, src->format) < 0) { |
|---|
| 852 | n/a | Py_DECREF(mbuf); |
|---|
| 853 | n/a | return NULL; |
|---|
| 854 | n/a | } |
|---|
| 855 | n/a | |
|---|
| 856 | n/a | mv = (PyMemoryViewObject *)mbuf_add_incomplete_view(mbuf, NULL, src->ndim); |
|---|
| 857 | n/a | Py_DECREF(mbuf); |
|---|
| 858 | n/a | if (mv == NULL) |
|---|
| 859 | n/a | return NULL; |
|---|
| 860 | n/a | |
|---|
| 861 | n/a | dest = &mv->view; |
|---|
| 862 | n/a | |
|---|
| 863 | n/a | /* shared values are initialized correctly except for itemsize */ |
|---|
| 864 | n/a | dest->itemsize = src->itemsize; |
|---|
| 865 | n/a | |
|---|
| 866 | n/a | /* shape and strides */ |
|---|
| 867 | n/a | for (i = 0; i < src->ndim; i++) { |
|---|
| 868 | n/a | dest->shape[i] = src->shape[i]; |
|---|
| 869 | n/a | } |
|---|
| 870 | n/a | if (order == 'C' || order == 'A') { |
|---|
| 871 | n/a | init_strides_from_shape(dest); |
|---|
| 872 | n/a | } |
|---|
| 873 | n/a | else { |
|---|
| 874 | n/a | init_fortran_strides_from_shape(dest); |
|---|
| 875 | n/a | } |
|---|
| 876 | n/a | /* suboffsets */ |
|---|
| 877 | n/a | dest->suboffsets = NULL; |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | /* flags */ |
|---|
| 880 | n/a | init_flags(mv); |
|---|
| 881 | n/a | |
|---|
| 882 | n/a | if (copy_buffer(dest, src) < 0) { |
|---|
| 883 | n/a | Py_DECREF(mv); |
|---|
| 884 | n/a | return NULL; |
|---|
| 885 | n/a | } |
|---|
| 886 | n/a | |
|---|
| 887 | n/a | return (PyObject *)mv; |
|---|
| 888 | n/a | } |
|---|
| 889 | n/a | |
|---|
| 890 | n/a | /* |
|---|
| 891 | n/a | Return a new memoryview object based on a contiguous exporter with |
|---|
| 892 | n/a | buffertype={PyBUF_READ, PyBUF_WRITE} and order={'C', 'F'ortran, or 'A'ny}. |
|---|
| 893 | n/a | The logical structure of the input and output buffers is the same |
|---|
| 894 | n/a | (i.e. tolist(input) == tolist(output)), but the physical layout in |
|---|
| 895 | n/a | memory can be explicitly chosen. |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | As usual, if buffertype=PyBUF_WRITE, the exporter's buffer must be writable, |
|---|
| 898 | n/a | otherwise it may be writable or read-only. |
|---|
| 899 | n/a | |
|---|
| 900 | n/a | If the exporter is already contiguous with the desired target order, |
|---|
| 901 | n/a | the memoryview will be directly based on the exporter. |
|---|
| 902 | n/a | |
|---|
| 903 | n/a | Otherwise, if the buffertype is PyBUF_READ, the memoryview will be |
|---|
| 904 | n/a | based on a new bytes object. If order={'C', 'A'ny}, use 'C' order, |
|---|
| 905 | n/a | 'F'ortran order otherwise. |
|---|
| 906 | n/a | */ |
|---|
| 907 | n/a | PyObject * |
|---|
| 908 | n/a | PyMemoryView_GetContiguous(PyObject *obj, int buffertype, char order) |
|---|
| 909 | n/a | { |
|---|
| 910 | n/a | PyMemoryViewObject *mv; |
|---|
| 911 | n/a | PyObject *ret; |
|---|
| 912 | n/a | Py_buffer *view; |
|---|
| 913 | n/a | |
|---|
| 914 | n/a | assert(buffertype == PyBUF_READ || buffertype == PyBUF_WRITE); |
|---|
| 915 | n/a | assert(order == 'C' || order == 'F' || order == 'A'); |
|---|
| 916 | n/a | |
|---|
| 917 | n/a | mv = (PyMemoryViewObject *)PyMemoryView_FromObject(obj); |
|---|
| 918 | n/a | if (mv == NULL) |
|---|
| 919 | n/a | return NULL; |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | view = &mv->view; |
|---|
| 922 | n/a | if (buffertype == PyBUF_WRITE && view->readonly) { |
|---|
| 923 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 924 | n/a | "underlying buffer is not writable"); |
|---|
| 925 | n/a | Py_DECREF(mv); |
|---|
| 926 | n/a | return NULL; |
|---|
| 927 | n/a | } |
|---|
| 928 | n/a | |
|---|
| 929 | n/a | if (PyBuffer_IsContiguous(view, order)) |
|---|
| 930 | n/a | return (PyObject *)mv; |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | if (buffertype == PyBUF_WRITE) { |
|---|
| 933 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 934 | n/a | "writable contiguous buffer requested " |
|---|
| 935 | n/a | "for a non-contiguous object."); |
|---|
| 936 | n/a | Py_DECREF(mv); |
|---|
| 937 | n/a | return NULL; |
|---|
| 938 | n/a | } |
|---|
| 939 | n/a | |
|---|
| 940 | n/a | ret = memory_from_contiguous_copy(view, order); |
|---|
| 941 | n/a | Py_DECREF(mv); |
|---|
| 942 | n/a | return ret; |
|---|
| 943 | n/a | } |
|---|
| 944 | n/a | |
|---|
| 945 | n/a | |
|---|
| 946 | n/a | static PyObject * |
|---|
| 947 | n/a | memory_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) |
|---|
| 948 | n/a | { |
|---|
| 949 | n/a | PyObject *obj; |
|---|
| 950 | n/a | static char *kwlist[] = {"object", NULL}; |
|---|
| 951 | n/a | |
|---|
| 952 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:memoryview", kwlist, |
|---|
| 953 | n/a | &obj)) { |
|---|
| 954 | n/a | return NULL; |
|---|
| 955 | n/a | } |
|---|
| 956 | n/a | |
|---|
| 957 | n/a | return PyMemoryView_FromObject(obj); |
|---|
| 958 | n/a | } |
|---|
| 959 | n/a | |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | /****************************************************************************/ |
|---|
| 962 | n/a | /* Previously in abstract.c */ |
|---|
| 963 | n/a | /****************************************************************************/ |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | typedef struct { |
|---|
| 966 | n/a | Py_buffer view; |
|---|
| 967 | n/a | Py_ssize_t array[1]; |
|---|
| 968 | n/a | } Py_buffer_full; |
|---|
| 969 | n/a | |
|---|
| 970 | n/a | int |
|---|
| 971 | n/a | PyBuffer_ToContiguous(void *buf, Py_buffer *src, Py_ssize_t len, char order) |
|---|
| 972 | n/a | { |
|---|
| 973 | n/a | Py_buffer_full *fb = NULL; |
|---|
| 974 | n/a | int ret; |
|---|
| 975 | n/a | |
|---|
| 976 | n/a | assert(order == 'C' || order == 'F' || order == 'A'); |
|---|
| 977 | n/a | |
|---|
| 978 | n/a | if (len != src->len) { |
|---|
| 979 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 980 | n/a | "PyBuffer_ToContiguous: len != view->len"); |
|---|
| 981 | n/a | return -1; |
|---|
| 982 | n/a | } |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | if (PyBuffer_IsContiguous(src, order)) { |
|---|
| 985 | n/a | memcpy((char *)buf, src->buf, len); |
|---|
| 986 | n/a | return 0; |
|---|
| 987 | n/a | } |
|---|
| 988 | n/a | |
|---|
| 989 | n/a | /* buffer_to_contiguous() assumes PyBUF_FULL */ |
|---|
| 990 | n/a | fb = PyMem_Malloc(sizeof *fb + 3 * src->ndim * (sizeof *fb->array)); |
|---|
| 991 | n/a | if (fb == NULL) { |
|---|
| 992 | n/a | PyErr_NoMemory(); |
|---|
| 993 | n/a | return -1; |
|---|
| 994 | n/a | } |
|---|
| 995 | n/a | fb->view.ndim = src->ndim; |
|---|
| 996 | n/a | fb->view.shape = fb->array; |
|---|
| 997 | n/a | fb->view.strides = fb->array + src->ndim; |
|---|
| 998 | n/a | fb->view.suboffsets = fb->array + 2 * src->ndim; |
|---|
| 999 | n/a | |
|---|
| 1000 | n/a | init_shared_values(&fb->view, src); |
|---|
| 1001 | n/a | init_shape_strides(&fb->view, src); |
|---|
| 1002 | n/a | init_suboffsets(&fb->view, src); |
|---|
| 1003 | n/a | |
|---|
| 1004 | n/a | src = &fb->view; |
|---|
| 1005 | n/a | |
|---|
| 1006 | n/a | ret = buffer_to_contiguous(buf, src, order); |
|---|
| 1007 | n/a | PyMem_Free(fb); |
|---|
| 1008 | n/a | return ret; |
|---|
| 1009 | n/a | } |
|---|
| 1010 | n/a | |
|---|
| 1011 | n/a | |
|---|
| 1012 | n/a | /****************************************************************************/ |
|---|
| 1013 | n/a | /* Release/GC management */ |
|---|
| 1014 | n/a | /****************************************************************************/ |
|---|
| 1015 | n/a | |
|---|
| 1016 | n/a | /* Inform the managed buffer that this particular memoryview will not access |
|---|
| 1017 | n/a | the underlying buffer again. If no other memoryviews are registered with |
|---|
| 1018 | n/a | the managed buffer, the underlying buffer is released instantly and |
|---|
| 1019 | n/a | marked as inaccessible for both the memoryview and the managed buffer. |
|---|
| 1020 | n/a | |
|---|
| 1021 | n/a | This function fails if the memoryview itself has exported buffers. */ |
|---|
| 1022 | n/a | static int |
|---|
| 1023 | n/a | _memory_release(PyMemoryViewObject *self) |
|---|
| 1024 | n/a | { |
|---|
| 1025 | n/a | if (self->flags & _Py_MEMORYVIEW_RELEASED) |
|---|
| 1026 | n/a | return 0; |
|---|
| 1027 | n/a | |
|---|
| 1028 | n/a | if (self->exports == 0) { |
|---|
| 1029 | n/a | self->flags |= _Py_MEMORYVIEW_RELEASED; |
|---|
| 1030 | n/a | assert(self->mbuf->exports > 0); |
|---|
| 1031 | n/a | if (--self->mbuf->exports == 0) |
|---|
| 1032 | n/a | mbuf_release(self->mbuf); |
|---|
| 1033 | n/a | return 0; |
|---|
| 1034 | n/a | } |
|---|
| 1035 | n/a | if (self->exports > 0) { |
|---|
| 1036 | n/a | PyErr_Format(PyExc_BufferError, |
|---|
| 1037 | n/a | "memoryview has %zd exported buffer%s", self->exports, |
|---|
| 1038 | n/a | self->exports==1 ? "" : "s"); |
|---|
| 1039 | n/a | return -1; |
|---|
| 1040 | n/a | } |
|---|
| 1041 | n/a | |
|---|
| 1042 | n/a | Py_FatalError("_memory_release(): negative export count"); |
|---|
| 1043 | n/a | return -1; |
|---|
| 1044 | n/a | } |
|---|
| 1045 | n/a | |
|---|
| 1046 | n/a | static PyObject * |
|---|
| 1047 | n/a | memory_release(PyMemoryViewObject *self, PyObject *noargs) |
|---|
| 1048 | n/a | { |
|---|
| 1049 | n/a | if (_memory_release(self) < 0) |
|---|
| 1050 | n/a | return NULL; |
|---|
| 1051 | n/a | Py_RETURN_NONE; |
|---|
| 1052 | n/a | } |
|---|
| 1053 | n/a | |
|---|
| 1054 | n/a | static void |
|---|
| 1055 | n/a | memory_dealloc(PyMemoryViewObject *self) |
|---|
| 1056 | n/a | { |
|---|
| 1057 | n/a | assert(self->exports == 0); |
|---|
| 1058 | n/a | _PyObject_GC_UNTRACK(self); |
|---|
| 1059 | n/a | (void)_memory_release(self); |
|---|
| 1060 | n/a | Py_CLEAR(self->mbuf); |
|---|
| 1061 | n/a | if (self->weakreflist != NULL) |
|---|
| 1062 | n/a | PyObject_ClearWeakRefs((PyObject *) self); |
|---|
| 1063 | n/a | PyObject_GC_Del(self); |
|---|
| 1064 | n/a | } |
|---|
| 1065 | n/a | |
|---|
| 1066 | n/a | static int |
|---|
| 1067 | n/a | memory_traverse(PyMemoryViewObject *self, visitproc visit, void *arg) |
|---|
| 1068 | n/a | { |
|---|
| 1069 | n/a | Py_VISIT(self->mbuf); |
|---|
| 1070 | n/a | return 0; |
|---|
| 1071 | n/a | } |
|---|
| 1072 | n/a | |
|---|
| 1073 | n/a | static int |
|---|
| 1074 | n/a | memory_clear(PyMemoryViewObject *self) |
|---|
| 1075 | n/a | { |
|---|
| 1076 | n/a | (void)_memory_release(self); |
|---|
| 1077 | n/a | Py_CLEAR(self->mbuf); |
|---|
| 1078 | n/a | return 0; |
|---|
| 1079 | n/a | } |
|---|
| 1080 | n/a | |
|---|
| 1081 | n/a | static PyObject * |
|---|
| 1082 | n/a | memory_enter(PyObject *self, PyObject *args) |
|---|
| 1083 | n/a | { |
|---|
| 1084 | n/a | CHECK_RELEASED(self); |
|---|
| 1085 | n/a | Py_INCREF(self); |
|---|
| 1086 | n/a | return self; |
|---|
| 1087 | n/a | } |
|---|
| 1088 | n/a | |
|---|
| 1089 | n/a | static PyObject * |
|---|
| 1090 | n/a | memory_exit(PyObject *self, PyObject *args) |
|---|
| 1091 | n/a | { |
|---|
| 1092 | n/a | return memory_release((PyMemoryViewObject *)self, NULL); |
|---|
| 1093 | n/a | } |
|---|
| 1094 | n/a | |
|---|
| 1095 | n/a | |
|---|
| 1096 | n/a | /****************************************************************************/ |
|---|
| 1097 | n/a | /* Casting format and shape */ |
|---|
| 1098 | n/a | /****************************************************************************/ |
|---|
| 1099 | n/a | |
|---|
| 1100 | n/a | #define IS_BYTE_FORMAT(f) (f == 'b' || f == 'B' || f == 'c') |
|---|
| 1101 | n/a | |
|---|
| 1102 | n/a | static inline Py_ssize_t |
|---|
| 1103 | n/a | get_native_fmtchar(char *result, const char *fmt) |
|---|
| 1104 | n/a | { |
|---|
| 1105 | n/a | Py_ssize_t size = -1; |
|---|
| 1106 | n/a | |
|---|
| 1107 | n/a | if (fmt[0] == '@') fmt++; |
|---|
| 1108 | n/a | |
|---|
| 1109 | n/a | switch (fmt[0]) { |
|---|
| 1110 | n/a | case 'c': case 'b': case 'B': size = sizeof(char); break; |
|---|
| 1111 | n/a | case 'h': case 'H': size = sizeof(short); break; |
|---|
| 1112 | n/a | case 'i': case 'I': size = sizeof(int); break; |
|---|
| 1113 | n/a | case 'l': case 'L': size = sizeof(long); break; |
|---|
| 1114 | n/a | case 'q': case 'Q': size = sizeof(long long); break; |
|---|
| 1115 | n/a | case 'n': case 'N': size = sizeof(Py_ssize_t); break; |
|---|
| 1116 | n/a | case 'f': size = sizeof(float); break; |
|---|
| 1117 | n/a | case 'd': size = sizeof(double); break; |
|---|
| 1118 | n/a | case '?': size = sizeof(_Bool); break; |
|---|
| 1119 | n/a | case 'P': size = sizeof(void *); break; |
|---|
| 1120 | n/a | } |
|---|
| 1121 | n/a | |
|---|
| 1122 | n/a | if (size > 0 && fmt[1] == '\0') { |
|---|
| 1123 | n/a | *result = fmt[0]; |
|---|
| 1124 | n/a | return size; |
|---|
| 1125 | n/a | } |
|---|
| 1126 | n/a | |
|---|
| 1127 | n/a | return -1; |
|---|
| 1128 | n/a | } |
|---|
| 1129 | n/a | |
|---|
| 1130 | n/a | static inline const char * |
|---|
| 1131 | n/a | get_native_fmtstr(const char *fmt) |
|---|
| 1132 | n/a | { |
|---|
| 1133 | n/a | int at = 0; |
|---|
| 1134 | n/a | |
|---|
| 1135 | n/a | if (fmt[0] == '@') { |
|---|
| 1136 | n/a | at = 1; |
|---|
| 1137 | n/a | fmt++; |
|---|
| 1138 | n/a | } |
|---|
| 1139 | n/a | if (fmt[0] == '\0' || fmt[1] != '\0') { |
|---|
| 1140 | n/a | return NULL; |
|---|
| 1141 | n/a | } |
|---|
| 1142 | n/a | |
|---|
| 1143 | n/a | #define RETURN(s) do { return at ? "@" s : s; } while (0) |
|---|
| 1144 | n/a | |
|---|
| 1145 | n/a | switch (fmt[0]) { |
|---|
| 1146 | n/a | case 'c': RETURN("c"); |
|---|
| 1147 | n/a | case 'b': RETURN("b"); |
|---|
| 1148 | n/a | case 'B': RETURN("B"); |
|---|
| 1149 | n/a | case 'h': RETURN("h"); |
|---|
| 1150 | n/a | case 'H': RETURN("H"); |
|---|
| 1151 | n/a | case 'i': RETURN("i"); |
|---|
| 1152 | n/a | case 'I': RETURN("I"); |
|---|
| 1153 | n/a | case 'l': RETURN("l"); |
|---|
| 1154 | n/a | case 'L': RETURN("L"); |
|---|
| 1155 | n/a | case 'q': RETURN("q"); |
|---|
| 1156 | n/a | case 'Q': RETURN("Q"); |
|---|
| 1157 | n/a | case 'n': RETURN("n"); |
|---|
| 1158 | n/a | case 'N': RETURN("N"); |
|---|
| 1159 | n/a | case 'f': RETURN("f"); |
|---|
| 1160 | n/a | case 'd': RETURN("d"); |
|---|
| 1161 | n/a | case '?': RETURN("?"); |
|---|
| 1162 | n/a | case 'P': RETURN("P"); |
|---|
| 1163 | n/a | } |
|---|
| 1164 | n/a | |
|---|
| 1165 | n/a | return NULL; |
|---|
| 1166 | n/a | } |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | |
|---|
| 1169 | n/a | /* Cast a memoryview's data type to 'format'. The input array must be |
|---|
| 1170 | n/a | C-contiguous. At least one of input-format, output-format must have |
|---|
| 1171 | n/a | byte size. The output array is 1-D, with the same byte length as the |
|---|
| 1172 | n/a | input array. Thus, view->len must be a multiple of the new itemsize. */ |
|---|
| 1173 | n/a | static int |
|---|
| 1174 | n/a | cast_to_1D(PyMemoryViewObject *mv, PyObject *format) |
|---|
| 1175 | n/a | { |
|---|
| 1176 | n/a | Py_buffer *view = &mv->view; |
|---|
| 1177 | n/a | PyObject *asciifmt; |
|---|
| 1178 | n/a | char srcchar, destchar; |
|---|
| 1179 | n/a | Py_ssize_t itemsize; |
|---|
| 1180 | n/a | int ret = -1; |
|---|
| 1181 | n/a | |
|---|
| 1182 | n/a | assert(view->ndim >= 1); |
|---|
| 1183 | n/a | assert(Py_SIZE(mv) == 3*view->ndim); |
|---|
| 1184 | n/a | assert(view->shape == mv->ob_array); |
|---|
| 1185 | n/a | assert(view->strides == mv->ob_array + view->ndim); |
|---|
| 1186 | n/a | assert(view->suboffsets == mv->ob_array + 2*view->ndim); |
|---|
| 1187 | n/a | |
|---|
| 1188 | n/a | asciifmt = PyUnicode_AsASCIIString(format); |
|---|
| 1189 | n/a | if (asciifmt == NULL) |
|---|
| 1190 | n/a | return ret; |
|---|
| 1191 | n/a | |
|---|
| 1192 | n/a | itemsize = get_native_fmtchar(&destchar, PyBytes_AS_STRING(asciifmt)); |
|---|
| 1193 | n/a | if (itemsize < 0) { |
|---|
| 1194 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1195 | n/a | "memoryview: destination format must be a native single " |
|---|
| 1196 | n/a | "character format prefixed with an optional '@'"); |
|---|
| 1197 | n/a | goto out; |
|---|
| 1198 | n/a | } |
|---|
| 1199 | n/a | |
|---|
| 1200 | n/a | if ((get_native_fmtchar(&srcchar, view->format) < 0 || |
|---|
| 1201 | n/a | !IS_BYTE_FORMAT(srcchar)) && !IS_BYTE_FORMAT(destchar)) { |
|---|
| 1202 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1203 | n/a | "memoryview: cannot cast between two non-byte formats"); |
|---|
| 1204 | n/a | goto out; |
|---|
| 1205 | n/a | } |
|---|
| 1206 | n/a | if (view->len % itemsize) { |
|---|
| 1207 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1208 | n/a | "memoryview: length is not a multiple of itemsize"); |
|---|
| 1209 | n/a | goto out; |
|---|
| 1210 | n/a | } |
|---|
| 1211 | n/a | |
|---|
| 1212 | n/a | view->format = (char *)get_native_fmtstr(PyBytes_AS_STRING(asciifmt)); |
|---|
| 1213 | n/a | if (view->format == NULL) { |
|---|
| 1214 | n/a | /* NOT_REACHED: get_native_fmtchar() already validates the format. */ |
|---|
| 1215 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 1216 | n/a | "memoryview: internal error"); |
|---|
| 1217 | n/a | goto out; |
|---|
| 1218 | n/a | } |
|---|
| 1219 | n/a | view->itemsize = itemsize; |
|---|
| 1220 | n/a | |
|---|
| 1221 | n/a | view->ndim = 1; |
|---|
| 1222 | n/a | view->shape[0] = view->len / view->itemsize; |
|---|
| 1223 | n/a | view->strides[0] = view->itemsize; |
|---|
| 1224 | n/a | view->suboffsets = NULL; |
|---|
| 1225 | n/a | |
|---|
| 1226 | n/a | init_flags(mv); |
|---|
| 1227 | n/a | |
|---|
| 1228 | n/a | ret = 0; |
|---|
| 1229 | n/a | |
|---|
| 1230 | n/a | out: |
|---|
| 1231 | n/a | Py_DECREF(asciifmt); |
|---|
| 1232 | n/a | return ret; |
|---|
| 1233 | n/a | } |
|---|
| 1234 | n/a | |
|---|
| 1235 | n/a | /* The memoryview must have space for 3*len(seq) elements. */ |
|---|
| 1236 | n/a | static Py_ssize_t |
|---|
| 1237 | n/a | copy_shape(Py_ssize_t *shape, const PyObject *seq, Py_ssize_t ndim, |
|---|
| 1238 | n/a | Py_ssize_t itemsize) |
|---|
| 1239 | n/a | { |
|---|
| 1240 | n/a | Py_ssize_t x, i; |
|---|
| 1241 | n/a | Py_ssize_t len = itemsize; |
|---|
| 1242 | n/a | |
|---|
| 1243 | n/a | for (i = 0; i < ndim; i++) { |
|---|
| 1244 | n/a | PyObject *tmp = PySequence_Fast_GET_ITEM(seq, i); |
|---|
| 1245 | n/a | if (!PyLong_Check(tmp)) { |
|---|
| 1246 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1247 | n/a | "memoryview.cast(): elements of shape must be integers"); |
|---|
| 1248 | n/a | return -1; |
|---|
| 1249 | n/a | } |
|---|
| 1250 | n/a | x = PyLong_AsSsize_t(tmp); |
|---|
| 1251 | n/a | if (x == -1 && PyErr_Occurred()) { |
|---|
| 1252 | n/a | return -1; |
|---|
| 1253 | n/a | } |
|---|
| 1254 | n/a | if (x <= 0) { |
|---|
| 1255 | n/a | /* In general elements of shape may be 0, but not for casting. */ |
|---|
| 1256 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 1257 | n/a | "memoryview.cast(): elements of shape must be integers > 0"); |
|---|
| 1258 | n/a | return -1; |
|---|
| 1259 | n/a | } |
|---|
| 1260 | n/a | if (x > PY_SSIZE_T_MAX / len) { |
|---|
| 1261 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 1262 | n/a | "memoryview.cast(): product(shape) > SSIZE_MAX"); |
|---|
| 1263 | n/a | return -1; |
|---|
| 1264 | n/a | } |
|---|
| 1265 | n/a | len *= x; |
|---|
| 1266 | n/a | shape[i] = x; |
|---|
| 1267 | n/a | } |
|---|
| 1268 | n/a | |
|---|
| 1269 | n/a | return len; |
|---|
| 1270 | n/a | } |
|---|
| 1271 | n/a | |
|---|
| 1272 | n/a | /* Cast a 1-D array to a new shape. The result array will be C-contiguous. |
|---|
| 1273 | n/a | If the result array does not have exactly the same byte length as the |
|---|
| 1274 | n/a | input array, raise ValueError. */ |
|---|
| 1275 | n/a | static int |
|---|
| 1276 | n/a | cast_to_ND(PyMemoryViewObject *mv, const PyObject *shape, int ndim) |
|---|
| 1277 | n/a | { |
|---|
| 1278 | n/a | Py_buffer *view = &mv->view; |
|---|
| 1279 | n/a | Py_ssize_t len; |
|---|
| 1280 | n/a | |
|---|
| 1281 | n/a | assert(view->ndim == 1); /* ndim from cast_to_1D() */ |
|---|
| 1282 | n/a | assert(Py_SIZE(mv) == 3*(ndim==0?1:ndim)); /* ndim of result array */ |
|---|
| 1283 | n/a | assert(view->shape == mv->ob_array); |
|---|
| 1284 | n/a | assert(view->strides == mv->ob_array + (ndim==0?1:ndim)); |
|---|
| 1285 | n/a | assert(view->suboffsets == NULL); |
|---|
| 1286 | n/a | |
|---|
| 1287 | n/a | view->ndim = ndim; |
|---|
| 1288 | n/a | if (view->ndim == 0) { |
|---|
| 1289 | n/a | view->shape = NULL; |
|---|
| 1290 | n/a | view->strides = NULL; |
|---|
| 1291 | n/a | len = view->itemsize; |
|---|
| 1292 | n/a | } |
|---|
| 1293 | n/a | else { |
|---|
| 1294 | n/a | len = copy_shape(view->shape, shape, ndim, view->itemsize); |
|---|
| 1295 | n/a | if (len < 0) |
|---|
| 1296 | n/a | return -1; |
|---|
| 1297 | n/a | init_strides_from_shape(view); |
|---|
| 1298 | n/a | } |
|---|
| 1299 | n/a | |
|---|
| 1300 | n/a | if (view->len != len) { |
|---|
| 1301 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1302 | n/a | "memoryview: product(shape) * itemsize != buffer size"); |
|---|
| 1303 | n/a | return -1; |
|---|
| 1304 | n/a | } |
|---|
| 1305 | n/a | |
|---|
| 1306 | n/a | init_flags(mv); |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | return 0; |
|---|
| 1309 | n/a | } |
|---|
| 1310 | n/a | |
|---|
| 1311 | n/a | static int |
|---|
| 1312 | n/a | zero_in_shape(PyMemoryViewObject *mv) |
|---|
| 1313 | n/a | { |
|---|
| 1314 | n/a | Py_buffer *view = &mv->view; |
|---|
| 1315 | n/a | Py_ssize_t i; |
|---|
| 1316 | n/a | |
|---|
| 1317 | n/a | for (i = 0; i < view->ndim; i++) |
|---|
| 1318 | n/a | if (view->shape[i] == 0) |
|---|
| 1319 | n/a | return 1; |
|---|
| 1320 | n/a | |
|---|
| 1321 | n/a | return 0; |
|---|
| 1322 | n/a | } |
|---|
| 1323 | n/a | |
|---|
| 1324 | n/a | /* |
|---|
| 1325 | n/a | Cast a copy of 'self' to a different view. The input view must |
|---|
| 1326 | n/a | be C-contiguous. The function always casts the input view to a |
|---|
| 1327 | n/a | 1-D output according to 'format'. At least one of input-format, |
|---|
| 1328 | n/a | output-format must have byte size. |
|---|
| 1329 | n/a | |
|---|
| 1330 | n/a | If 'shape' is given, the 1-D view from the previous step will |
|---|
| 1331 | n/a | be cast to a C-contiguous view with new shape and strides. |
|---|
| 1332 | n/a | |
|---|
| 1333 | n/a | All casts must result in views that will have the exact byte |
|---|
| 1334 | n/a | size of the original input. Otherwise, an error is raised. |
|---|
| 1335 | n/a | */ |
|---|
| 1336 | n/a | static PyObject * |
|---|
| 1337 | n/a | memory_cast(PyMemoryViewObject *self, PyObject *args, PyObject *kwds) |
|---|
| 1338 | n/a | { |
|---|
| 1339 | n/a | static char *kwlist[] = {"format", "shape", NULL}; |
|---|
| 1340 | n/a | PyMemoryViewObject *mv = NULL; |
|---|
| 1341 | n/a | PyObject *shape = NULL; |
|---|
| 1342 | n/a | PyObject *format; |
|---|
| 1343 | n/a | Py_ssize_t ndim = 1; |
|---|
| 1344 | n/a | |
|---|
| 1345 | n/a | CHECK_RELEASED(self); |
|---|
| 1346 | n/a | |
|---|
| 1347 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O", kwlist, |
|---|
| 1348 | n/a | &format, &shape)) { |
|---|
| 1349 | n/a | return NULL; |
|---|
| 1350 | n/a | } |
|---|
| 1351 | n/a | if (!PyUnicode_Check(format)) { |
|---|
| 1352 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1353 | n/a | "memoryview: format argument must be a string"); |
|---|
| 1354 | n/a | return NULL; |
|---|
| 1355 | n/a | } |
|---|
| 1356 | n/a | if (!MV_C_CONTIGUOUS(self->flags)) { |
|---|
| 1357 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1358 | n/a | "memoryview: casts are restricted to C-contiguous views"); |
|---|
| 1359 | n/a | return NULL; |
|---|
| 1360 | n/a | } |
|---|
| 1361 | n/a | if ((shape || self->view.ndim != 1) && zero_in_shape(self)) { |
|---|
| 1362 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1363 | n/a | "memoryview: cannot cast view with zeros in shape or strides"); |
|---|
| 1364 | n/a | return NULL; |
|---|
| 1365 | n/a | } |
|---|
| 1366 | n/a | if (shape) { |
|---|
| 1367 | n/a | CHECK_LIST_OR_TUPLE(shape) |
|---|
| 1368 | n/a | ndim = PySequence_Fast_GET_SIZE(shape); |
|---|
| 1369 | n/a | if (ndim > PyBUF_MAX_NDIM) { |
|---|
| 1370 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1371 | n/a | "memoryview: number of dimensions must not exceed " |
|---|
| 1372 | n/a | Py_STRINGIFY(PyBUF_MAX_NDIM)); |
|---|
| 1373 | n/a | return NULL; |
|---|
| 1374 | n/a | } |
|---|
| 1375 | n/a | if (self->view.ndim != 1 && ndim != 1) { |
|---|
| 1376 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1377 | n/a | "memoryview: cast must be 1D -> ND or ND -> 1D"); |
|---|
| 1378 | n/a | return NULL; |
|---|
| 1379 | n/a | } |
|---|
| 1380 | n/a | } |
|---|
| 1381 | n/a | |
|---|
| 1382 | n/a | mv = (PyMemoryViewObject *) |
|---|
| 1383 | n/a | mbuf_add_incomplete_view(self->mbuf, &self->view, ndim==0 ? 1 : (int)ndim); |
|---|
| 1384 | n/a | if (mv == NULL) |
|---|
| 1385 | n/a | return NULL; |
|---|
| 1386 | n/a | |
|---|
| 1387 | n/a | if (cast_to_1D(mv, format) < 0) |
|---|
| 1388 | n/a | goto error; |
|---|
| 1389 | n/a | if (shape && cast_to_ND(mv, shape, (int)ndim) < 0) |
|---|
| 1390 | n/a | goto error; |
|---|
| 1391 | n/a | |
|---|
| 1392 | n/a | return (PyObject *)mv; |
|---|
| 1393 | n/a | |
|---|
| 1394 | n/a | error: |
|---|
| 1395 | n/a | Py_DECREF(mv); |
|---|
| 1396 | n/a | return NULL; |
|---|
| 1397 | n/a | } |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | |
|---|
| 1400 | n/a | /**************************************************************************/ |
|---|
| 1401 | n/a | /* getbuffer */ |
|---|
| 1402 | n/a | /**************************************************************************/ |
|---|
| 1403 | n/a | |
|---|
| 1404 | n/a | static int |
|---|
| 1405 | n/a | memory_getbuf(PyMemoryViewObject *self, Py_buffer *view, int flags) |
|---|
| 1406 | n/a | { |
|---|
| 1407 | n/a | Py_buffer *base = &self->view; |
|---|
| 1408 | n/a | int baseflags = self->flags; |
|---|
| 1409 | n/a | |
|---|
| 1410 | n/a | CHECK_RELEASED_INT(self); |
|---|
| 1411 | n/a | |
|---|
| 1412 | n/a | /* start with complete information */ |
|---|
| 1413 | n/a | *view = *base; |
|---|
| 1414 | n/a | view->obj = NULL; |
|---|
| 1415 | n/a | |
|---|
| 1416 | n/a | if (REQ_WRITABLE(flags) && base->readonly) { |
|---|
| 1417 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1418 | n/a | "memoryview: underlying buffer is not writable"); |
|---|
| 1419 | n/a | return -1; |
|---|
| 1420 | n/a | } |
|---|
| 1421 | n/a | if (!REQ_FORMAT(flags)) { |
|---|
| 1422 | n/a | /* NULL indicates that the buffer's data type has been cast to 'B'. |
|---|
| 1423 | n/a | view->itemsize is the _previous_ itemsize. If shape is present, |
|---|
| 1424 | n/a | the equality product(shape) * itemsize = len still holds at this |
|---|
| 1425 | n/a | point. The equality calcsize(format) = itemsize does _not_ hold |
|---|
| 1426 | n/a | from here on! */ |
|---|
| 1427 | n/a | view->format = NULL; |
|---|
| 1428 | n/a | } |
|---|
| 1429 | n/a | |
|---|
| 1430 | n/a | if (REQ_C_CONTIGUOUS(flags) && !MV_C_CONTIGUOUS(baseflags)) { |
|---|
| 1431 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1432 | n/a | "memoryview: underlying buffer is not C-contiguous"); |
|---|
| 1433 | n/a | return -1; |
|---|
| 1434 | n/a | } |
|---|
| 1435 | n/a | if (REQ_F_CONTIGUOUS(flags) && !MV_F_CONTIGUOUS(baseflags)) { |
|---|
| 1436 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1437 | n/a | "memoryview: underlying buffer is not Fortran contiguous"); |
|---|
| 1438 | n/a | return -1; |
|---|
| 1439 | n/a | } |
|---|
| 1440 | n/a | if (REQ_ANY_CONTIGUOUS(flags) && !MV_ANY_CONTIGUOUS(baseflags)) { |
|---|
| 1441 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1442 | n/a | "memoryview: underlying buffer is not contiguous"); |
|---|
| 1443 | n/a | return -1; |
|---|
| 1444 | n/a | } |
|---|
| 1445 | n/a | if (!REQ_INDIRECT(flags) && (baseflags & _Py_MEMORYVIEW_PIL)) { |
|---|
| 1446 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1447 | n/a | "memoryview: underlying buffer requires suboffsets"); |
|---|
| 1448 | n/a | return -1; |
|---|
| 1449 | n/a | } |
|---|
| 1450 | n/a | if (!REQ_STRIDES(flags)) { |
|---|
| 1451 | n/a | if (!MV_C_CONTIGUOUS(baseflags)) { |
|---|
| 1452 | n/a | PyErr_SetString(PyExc_BufferError, |
|---|
| 1453 | n/a | "memoryview: underlying buffer is not C-contiguous"); |
|---|
| 1454 | n/a | return -1; |
|---|
| 1455 | n/a | } |
|---|
| 1456 | n/a | view->strides = NULL; |
|---|
| 1457 | n/a | } |
|---|
| 1458 | n/a | if (!REQ_SHAPE(flags)) { |
|---|
| 1459 | n/a | /* PyBUF_SIMPLE or PyBUF_WRITABLE: at this point buf is C-contiguous, |
|---|
| 1460 | n/a | so base->buf = ndbuf->data. */ |
|---|
| 1461 | n/a | if (view->format != NULL) { |
|---|
| 1462 | n/a | /* PyBUF_SIMPLE|PyBUF_FORMAT and PyBUF_WRITABLE|PyBUF_FORMAT do |
|---|
| 1463 | n/a | not make sense. */ |
|---|
| 1464 | n/a | PyErr_Format(PyExc_BufferError, |
|---|
| 1465 | n/a | "memoryview: cannot cast to unsigned bytes if the format flag " |
|---|
| 1466 | n/a | "is present"); |
|---|
| 1467 | n/a | return -1; |
|---|
| 1468 | n/a | } |
|---|
| 1469 | n/a | /* product(shape) * itemsize = len and calcsize(format) = itemsize |
|---|
| 1470 | n/a | do _not_ hold from here on! */ |
|---|
| 1471 | n/a | view->ndim = 1; |
|---|
| 1472 | n/a | view->shape = NULL; |
|---|
| 1473 | n/a | } |
|---|
| 1474 | n/a | |
|---|
| 1475 | n/a | |
|---|
| 1476 | n/a | view->obj = (PyObject *)self; |
|---|
| 1477 | n/a | Py_INCREF(view->obj); |
|---|
| 1478 | n/a | self->exports++; |
|---|
| 1479 | n/a | |
|---|
| 1480 | n/a | return 0; |
|---|
| 1481 | n/a | } |
|---|
| 1482 | n/a | |
|---|
| 1483 | n/a | static void |
|---|
| 1484 | n/a | memory_releasebuf(PyMemoryViewObject *self, Py_buffer *view) |
|---|
| 1485 | n/a | { |
|---|
| 1486 | n/a | self->exports--; |
|---|
| 1487 | n/a | return; |
|---|
| 1488 | n/a | /* PyBuffer_Release() decrements view->obj after this function returns. */ |
|---|
| 1489 | n/a | } |
|---|
| 1490 | n/a | |
|---|
| 1491 | n/a | /* Buffer methods */ |
|---|
| 1492 | n/a | static PyBufferProcs memory_as_buffer = { |
|---|
| 1493 | n/a | (getbufferproc)memory_getbuf, /* bf_getbuffer */ |
|---|
| 1494 | n/a | (releasebufferproc)memory_releasebuf, /* bf_releasebuffer */ |
|---|
| 1495 | n/a | }; |
|---|
| 1496 | n/a | |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | /****************************************************************************/ |
|---|
| 1499 | n/a | /* Optimized pack/unpack for all native format specifiers */ |
|---|
| 1500 | n/a | /****************************************************************************/ |
|---|
| 1501 | n/a | |
|---|
| 1502 | n/a | /* |
|---|
| 1503 | n/a | Fix exceptions: |
|---|
| 1504 | n/a | 1) Include format string in the error message. |
|---|
| 1505 | n/a | 2) OverflowError -> ValueError. |
|---|
| 1506 | n/a | 3) The error message from PyNumber_Index() is not ideal. |
|---|
| 1507 | n/a | */ |
|---|
| 1508 | n/a | static int |
|---|
| 1509 | n/a | type_error_int(const char *fmt) |
|---|
| 1510 | n/a | { |
|---|
| 1511 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1512 | n/a | "memoryview: invalid type for format '%s'", fmt); |
|---|
| 1513 | n/a | return -1; |
|---|
| 1514 | n/a | } |
|---|
| 1515 | n/a | |
|---|
| 1516 | n/a | static int |
|---|
| 1517 | n/a | value_error_int(const char *fmt) |
|---|
| 1518 | n/a | { |
|---|
| 1519 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 1520 | n/a | "memoryview: invalid value for format '%s'", fmt); |
|---|
| 1521 | n/a | return -1; |
|---|
| 1522 | n/a | } |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | static int |
|---|
| 1525 | n/a | fix_error_int(const char *fmt) |
|---|
| 1526 | n/a | { |
|---|
| 1527 | n/a | assert(PyErr_Occurred()); |
|---|
| 1528 | n/a | if (PyErr_ExceptionMatches(PyExc_TypeError)) { |
|---|
| 1529 | n/a | PyErr_Clear(); |
|---|
| 1530 | n/a | return type_error_int(fmt); |
|---|
| 1531 | n/a | } |
|---|
| 1532 | n/a | else if (PyErr_ExceptionMatches(PyExc_OverflowError) || |
|---|
| 1533 | n/a | PyErr_ExceptionMatches(PyExc_ValueError)) { |
|---|
| 1534 | n/a | PyErr_Clear(); |
|---|
| 1535 | n/a | return value_error_int(fmt); |
|---|
| 1536 | n/a | } |
|---|
| 1537 | n/a | |
|---|
| 1538 | n/a | return -1; |
|---|
| 1539 | n/a | } |
|---|
| 1540 | n/a | |
|---|
| 1541 | n/a | /* Accept integer objects or objects with an __index__() method. */ |
|---|
| 1542 | n/a | static long |
|---|
| 1543 | n/a | pylong_as_ld(PyObject *item) |
|---|
| 1544 | n/a | { |
|---|
| 1545 | n/a | PyObject *tmp; |
|---|
| 1546 | n/a | long ld; |
|---|
| 1547 | n/a | |
|---|
| 1548 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1549 | n/a | if (tmp == NULL) |
|---|
| 1550 | n/a | return -1; |
|---|
| 1551 | n/a | |
|---|
| 1552 | n/a | ld = PyLong_AsLong(tmp); |
|---|
| 1553 | n/a | Py_DECREF(tmp); |
|---|
| 1554 | n/a | return ld; |
|---|
| 1555 | n/a | } |
|---|
| 1556 | n/a | |
|---|
| 1557 | n/a | static unsigned long |
|---|
| 1558 | n/a | pylong_as_lu(PyObject *item) |
|---|
| 1559 | n/a | { |
|---|
| 1560 | n/a | PyObject *tmp; |
|---|
| 1561 | n/a | unsigned long lu; |
|---|
| 1562 | n/a | |
|---|
| 1563 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1564 | n/a | if (tmp == NULL) |
|---|
| 1565 | n/a | return (unsigned long)-1; |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | lu = PyLong_AsUnsignedLong(tmp); |
|---|
| 1568 | n/a | Py_DECREF(tmp); |
|---|
| 1569 | n/a | return lu; |
|---|
| 1570 | n/a | } |
|---|
| 1571 | n/a | |
|---|
| 1572 | n/a | static long long |
|---|
| 1573 | n/a | pylong_as_lld(PyObject *item) |
|---|
| 1574 | n/a | { |
|---|
| 1575 | n/a | PyObject *tmp; |
|---|
| 1576 | n/a | long long lld; |
|---|
| 1577 | n/a | |
|---|
| 1578 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1579 | n/a | if (tmp == NULL) |
|---|
| 1580 | n/a | return -1; |
|---|
| 1581 | n/a | |
|---|
| 1582 | n/a | lld = PyLong_AsLongLong(tmp); |
|---|
| 1583 | n/a | Py_DECREF(tmp); |
|---|
| 1584 | n/a | return lld; |
|---|
| 1585 | n/a | } |
|---|
| 1586 | n/a | |
|---|
| 1587 | n/a | static unsigned long long |
|---|
| 1588 | n/a | pylong_as_llu(PyObject *item) |
|---|
| 1589 | n/a | { |
|---|
| 1590 | n/a | PyObject *tmp; |
|---|
| 1591 | n/a | unsigned long long llu; |
|---|
| 1592 | n/a | |
|---|
| 1593 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1594 | n/a | if (tmp == NULL) |
|---|
| 1595 | n/a | return (unsigned long long)-1; |
|---|
| 1596 | n/a | |
|---|
| 1597 | n/a | llu = PyLong_AsUnsignedLongLong(tmp); |
|---|
| 1598 | n/a | Py_DECREF(tmp); |
|---|
| 1599 | n/a | return llu; |
|---|
| 1600 | n/a | } |
|---|
| 1601 | n/a | |
|---|
| 1602 | n/a | static Py_ssize_t |
|---|
| 1603 | n/a | pylong_as_zd(PyObject *item) |
|---|
| 1604 | n/a | { |
|---|
| 1605 | n/a | PyObject *tmp; |
|---|
| 1606 | n/a | Py_ssize_t zd; |
|---|
| 1607 | n/a | |
|---|
| 1608 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1609 | n/a | if (tmp == NULL) |
|---|
| 1610 | n/a | return -1; |
|---|
| 1611 | n/a | |
|---|
| 1612 | n/a | zd = PyLong_AsSsize_t(tmp); |
|---|
| 1613 | n/a | Py_DECREF(tmp); |
|---|
| 1614 | n/a | return zd; |
|---|
| 1615 | n/a | } |
|---|
| 1616 | n/a | |
|---|
| 1617 | n/a | static size_t |
|---|
| 1618 | n/a | pylong_as_zu(PyObject *item) |
|---|
| 1619 | n/a | { |
|---|
| 1620 | n/a | PyObject *tmp; |
|---|
| 1621 | n/a | size_t zu; |
|---|
| 1622 | n/a | |
|---|
| 1623 | n/a | tmp = PyNumber_Index(item); |
|---|
| 1624 | n/a | if (tmp == NULL) |
|---|
| 1625 | n/a | return (size_t)-1; |
|---|
| 1626 | n/a | |
|---|
| 1627 | n/a | zu = PyLong_AsSize_t(tmp); |
|---|
| 1628 | n/a | Py_DECREF(tmp); |
|---|
| 1629 | n/a | return zu; |
|---|
| 1630 | n/a | } |
|---|
| 1631 | n/a | |
|---|
| 1632 | n/a | /* Timings with the ndarray from _testbuffer.c indicate that using the |
|---|
| 1633 | n/a | struct module is around 15x slower than the two functions below. */ |
|---|
| 1634 | n/a | |
|---|
| 1635 | n/a | #define UNPACK_SINGLE(dest, ptr, type) \ |
|---|
| 1636 | n/a | do { \ |
|---|
| 1637 | n/a | type x; \ |
|---|
| 1638 | n/a | memcpy((char *)&x, ptr, sizeof x); \ |
|---|
| 1639 | n/a | dest = x; \ |
|---|
| 1640 | n/a | } while (0) |
|---|
| 1641 | n/a | |
|---|
| 1642 | n/a | /* Unpack a single item. 'fmt' can be any native format character in struct |
|---|
| 1643 | n/a | module syntax. This function is very sensitive to small changes. With this |
|---|
| 1644 | n/a | layout gcc automatically generates a fast jump table. */ |
|---|
| 1645 | n/a | static inline PyObject * |
|---|
| 1646 | n/a | unpack_single(const char *ptr, const char *fmt) |
|---|
| 1647 | n/a | { |
|---|
| 1648 | n/a | unsigned long long llu; |
|---|
| 1649 | n/a | unsigned long lu; |
|---|
| 1650 | n/a | size_t zu; |
|---|
| 1651 | n/a | long long lld; |
|---|
| 1652 | n/a | long ld; |
|---|
| 1653 | n/a | Py_ssize_t zd; |
|---|
| 1654 | n/a | double d; |
|---|
| 1655 | n/a | unsigned char uc; |
|---|
| 1656 | n/a | void *p; |
|---|
| 1657 | n/a | |
|---|
| 1658 | n/a | switch (fmt[0]) { |
|---|
| 1659 | n/a | |
|---|
| 1660 | n/a | /* signed integers and fast path for 'B' */ |
|---|
| 1661 | n/a | case 'B': uc = *((unsigned char *)ptr); goto convert_uc; |
|---|
| 1662 | n/a | case 'b': ld = *((signed char *)ptr); goto convert_ld; |
|---|
| 1663 | n/a | case 'h': UNPACK_SINGLE(ld, ptr, short); goto convert_ld; |
|---|
| 1664 | n/a | case 'i': UNPACK_SINGLE(ld, ptr, int); goto convert_ld; |
|---|
| 1665 | n/a | case 'l': UNPACK_SINGLE(ld, ptr, long); goto convert_ld; |
|---|
| 1666 | n/a | |
|---|
| 1667 | n/a | /* boolean */ |
|---|
| 1668 | n/a | case '?': UNPACK_SINGLE(ld, ptr, _Bool); goto convert_bool; |
|---|
| 1669 | n/a | |
|---|
| 1670 | n/a | /* unsigned integers */ |
|---|
| 1671 | n/a | case 'H': UNPACK_SINGLE(lu, ptr, unsigned short); goto convert_lu; |
|---|
| 1672 | n/a | case 'I': UNPACK_SINGLE(lu, ptr, unsigned int); goto convert_lu; |
|---|
| 1673 | n/a | case 'L': UNPACK_SINGLE(lu, ptr, unsigned long); goto convert_lu; |
|---|
| 1674 | n/a | |
|---|
| 1675 | n/a | /* native 64-bit */ |
|---|
| 1676 | n/a | case 'q': UNPACK_SINGLE(lld, ptr, long long); goto convert_lld; |
|---|
| 1677 | n/a | case 'Q': UNPACK_SINGLE(llu, ptr, unsigned long long); goto convert_llu; |
|---|
| 1678 | n/a | |
|---|
| 1679 | n/a | /* ssize_t and size_t */ |
|---|
| 1680 | n/a | case 'n': UNPACK_SINGLE(zd, ptr, Py_ssize_t); goto convert_zd; |
|---|
| 1681 | n/a | case 'N': UNPACK_SINGLE(zu, ptr, size_t); goto convert_zu; |
|---|
| 1682 | n/a | |
|---|
| 1683 | n/a | /* floats */ |
|---|
| 1684 | n/a | case 'f': UNPACK_SINGLE(d, ptr, float); goto convert_double; |
|---|
| 1685 | n/a | case 'd': UNPACK_SINGLE(d, ptr, double); goto convert_double; |
|---|
| 1686 | n/a | |
|---|
| 1687 | n/a | /* bytes object */ |
|---|
| 1688 | n/a | case 'c': goto convert_bytes; |
|---|
| 1689 | n/a | |
|---|
| 1690 | n/a | /* pointer */ |
|---|
| 1691 | n/a | case 'P': UNPACK_SINGLE(p, ptr, void *); goto convert_pointer; |
|---|
| 1692 | n/a | |
|---|
| 1693 | n/a | /* default */ |
|---|
| 1694 | n/a | default: goto err_format; |
|---|
| 1695 | n/a | } |
|---|
| 1696 | n/a | |
|---|
| 1697 | n/a | convert_uc: |
|---|
| 1698 | n/a | /* PyLong_FromUnsignedLong() is slower */ |
|---|
| 1699 | n/a | return PyLong_FromLong(uc); |
|---|
| 1700 | n/a | convert_ld: |
|---|
| 1701 | n/a | return PyLong_FromLong(ld); |
|---|
| 1702 | n/a | convert_lu: |
|---|
| 1703 | n/a | return PyLong_FromUnsignedLong(lu); |
|---|
| 1704 | n/a | convert_lld: |
|---|
| 1705 | n/a | return PyLong_FromLongLong(lld); |
|---|
| 1706 | n/a | convert_llu: |
|---|
| 1707 | n/a | return PyLong_FromUnsignedLongLong(llu); |
|---|
| 1708 | n/a | convert_zd: |
|---|
| 1709 | n/a | return PyLong_FromSsize_t(zd); |
|---|
| 1710 | n/a | convert_zu: |
|---|
| 1711 | n/a | return PyLong_FromSize_t(zu); |
|---|
| 1712 | n/a | convert_double: |
|---|
| 1713 | n/a | return PyFloat_FromDouble(d); |
|---|
| 1714 | n/a | convert_bool: |
|---|
| 1715 | n/a | return PyBool_FromLong(ld); |
|---|
| 1716 | n/a | convert_bytes: |
|---|
| 1717 | n/a | return PyBytes_FromStringAndSize(ptr, 1); |
|---|
| 1718 | n/a | convert_pointer: |
|---|
| 1719 | n/a | return PyLong_FromVoidPtr(p); |
|---|
| 1720 | n/a | err_format: |
|---|
| 1721 | n/a | PyErr_Format(PyExc_NotImplementedError, |
|---|
| 1722 | n/a | "memoryview: format %s not supported", fmt); |
|---|
| 1723 | n/a | return NULL; |
|---|
| 1724 | n/a | } |
|---|
| 1725 | n/a | |
|---|
| 1726 | n/a | #define PACK_SINGLE(ptr, src, type) \ |
|---|
| 1727 | n/a | do { \ |
|---|
| 1728 | n/a | type x; \ |
|---|
| 1729 | n/a | x = (type)src; \ |
|---|
| 1730 | n/a | memcpy(ptr, (char *)&x, sizeof x); \ |
|---|
| 1731 | n/a | } while (0) |
|---|
| 1732 | n/a | |
|---|
| 1733 | n/a | /* Pack a single item. 'fmt' can be any native format character in |
|---|
| 1734 | n/a | struct module syntax. */ |
|---|
| 1735 | n/a | static int |
|---|
| 1736 | n/a | pack_single(char *ptr, PyObject *item, const char *fmt) |
|---|
| 1737 | n/a | { |
|---|
| 1738 | n/a | unsigned long long llu; |
|---|
| 1739 | n/a | unsigned long lu; |
|---|
| 1740 | n/a | size_t zu; |
|---|
| 1741 | n/a | long long lld; |
|---|
| 1742 | n/a | long ld; |
|---|
| 1743 | n/a | Py_ssize_t zd; |
|---|
| 1744 | n/a | double d; |
|---|
| 1745 | n/a | void *p; |
|---|
| 1746 | n/a | |
|---|
| 1747 | n/a | switch (fmt[0]) { |
|---|
| 1748 | n/a | /* signed integers */ |
|---|
| 1749 | n/a | case 'b': case 'h': case 'i': case 'l': |
|---|
| 1750 | n/a | ld = pylong_as_ld(item); |
|---|
| 1751 | n/a | if (ld == -1 && PyErr_Occurred()) |
|---|
| 1752 | n/a | goto err_occurred; |
|---|
| 1753 | n/a | switch (fmt[0]) { |
|---|
| 1754 | n/a | case 'b': |
|---|
| 1755 | n/a | if (ld < SCHAR_MIN || ld > SCHAR_MAX) goto err_range; |
|---|
| 1756 | n/a | *((signed char *)ptr) = (signed char)ld; break; |
|---|
| 1757 | n/a | case 'h': |
|---|
| 1758 | n/a | if (ld < SHRT_MIN || ld > SHRT_MAX) goto err_range; |
|---|
| 1759 | n/a | PACK_SINGLE(ptr, ld, short); break; |
|---|
| 1760 | n/a | case 'i': |
|---|
| 1761 | n/a | if (ld < INT_MIN || ld > INT_MAX) goto err_range; |
|---|
| 1762 | n/a | PACK_SINGLE(ptr, ld, int); break; |
|---|
| 1763 | n/a | default: /* 'l' */ |
|---|
| 1764 | n/a | PACK_SINGLE(ptr, ld, long); break; |
|---|
| 1765 | n/a | } |
|---|
| 1766 | n/a | break; |
|---|
| 1767 | n/a | |
|---|
| 1768 | n/a | /* unsigned integers */ |
|---|
| 1769 | n/a | case 'B': case 'H': case 'I': case 'L': |
|---|
| 1770 | n/a | lu = pylong_as_lu(item); |
|---|
| 1771 | n/a | if (lu == (unsigned long)-1 && PyErr_Occurred()) |
|---|
| 1772 | n/a | goto err_occurred; |
|---|
| 1773 | n/a | switch (fmt[0]) { |
|---|
| 1774 | n/a | case 'B': |
|---|
| 1775 | n/a | if (lu > UCHAR_MAX) goto err_range; |
|---|
| 1776 | n/a | *((unsigned char *)ptr) = (unsigned char)lu; break; |
|---|
| 1777 | n/a | case 'H': |
|---|
| 1778 | n/a | if (lu > USHRT_MAX) goto err_range; |
|---|
| 1779 | n/a | PACK_SINGLE(ptr, lu, unsigned short); break; |
|---|
| 1780 | n/a | case 'I': |
|---|
| 1781 | n/a | if (lu > UINT_MAX) goto err_range; |
|---|
| 1782 | n/a | PACK_SINGLE(ptr, lu, unsigned int); break; |
|---|
| 1783 | n/a | default: /* 'L' */ |
|---|
| 1784 | n/a | PACK_SINGLE(ptr, lu, unsigned long); break; |
|---|
| 1785 | n/a | } |
|---|
| 1786 | n/a | break; |
|---|
| 1787 | n/a | |
|---|
| 1788 | n/a | /* native 64-bit */ |
|---|
| 1789 | n/a | case 'q': |
|---|
| 1790 | n/a | lld = pylong_as_lld(item); |
|---|
| 1791 | n/a | if (lld == -1 && PyErr_Occurred()) |
|---|
| 1792 | n/a | goto err_occurred; |
|---|
| 1793 | n/a | PACK_SINGLE(ptr, lld, long long); |
|---|
| 1794 | n/a | break; |
|---|
| 1795 | n/a | case 'Q': |
|---|
| 1796 | n/a | llu = pylong_as_llu(item); |
|---|
| 1797 | n/a | if (llu == (unsigned long long)-1 && PyErr_Occurred()) |
|---|
| 1798 | n/a | goto err_occurred; |
|---|
| 1799 | n/a | PACK_SINGLE(ptr, llu, unsigned long long); |
|---|
| 1800 | n/a | break; |
|---|
| 1801 | n/a | |
|---|
| 1802 | n/a | /* ssize_t and size_t */ |
|---|
| 1803 | n/a | case 'n': |
|---|
| 1804 | n/a | zd = pylong_as_zd(item); |
|---|
| 1805 | n/a | if (zd == -1 && PyErr_Occurred()) |
|---|
| 1806 | n/a | goto err_occurred; |
|---|
| 1807 | n/a | PACK_SINGLE(ptr, zd, Py_ssize_t); |
|---|
| 1808 | n/a | break; |
|---|
| 1809 | n/a | case 'N': |
|---|
| 1810 | n/a | zu = pylong_as_zu(item); |
|---|
| 1811 | n/a | if (zu == (size_t)-1 && PyErr_Occurred()) |
|---|
| 1812 | n/a | goto err_occurred; |
|---|
| 1813 | n/a | PACK_SINGLE(ptr, zu, size_t); |
|---|
| 1814 | n/a | break; |
|---|
| 1815 | n/a | |
|---|
| 1816 | n/a | /* floats */ |
|---|
| 1817 | n/a | case 'f': case 'd': |
|---|
| 1818 | n/a | d = PyFloat_AsDouble(item); |
|---|
| 1819 | n/a | if (d == -1.0 && PyErr_Occurred()) |
|---|
| 1820 | n/a | goto err_occurred; |
|---|
| 1821 | n/a | if (fmt[0] == 'f') { |
|---|
| 1822 | n/a | PACK_SINGLE(ptr, d, float); |
|---|
| 1823 | n/a | } |
|---|
| 1824 | n/a | else { |
|---|
| 1825 | n/a | PACK_SINGLE(ptr, d, double); |
|---|
| 1826 | n/a | } |
|---|
| 1827 | n/a | break; |
|---|
| 1828 | n/a | |
|---|
| 1829 | n/a | /* bool */ |
|---|
| 1830 | n/a | case '?': |
|---|
| 1831 | n/a | ld = PyObject_IsTrue(item); |
|---|
| 1832 | n/a | if (ld < 0) |
|---|
| 1833 | n/a | return -1; /* preserve original error */ |
|---|
| 1834 | n/a | PACK_SINGLE(ptr, ld, _Bool); |
|---|
| 1835 | n/a | break; |
|---|
| 1836 | n/a | |
|---|
| 1837 | n/a | /* bytes object */ |
|---|
| 1838 | n/a | case 'c': |
|---|
| 1839 | n/a | if (!PyBytes_Check(item)) |
|---|
| 1840 | n/a | return type_error_int(fmt); |
|---|
| 1841 | n/a | if (PyBytes_GET_SIZE(item) != 1) |
|---|
| 1842 | n/a | return value_error_int(fmt); |
|---|
| 1843 | n/a | *ptr = PyBytes_AS_STRING(item)[0]; |
|---|
| 1844 | n/a | break; |
|---|
| 1845 | n/a | |
|---|
| 1846 | n/a | /* pointer */ |
|---|
| 1847 | n/a | case 'P': |
|---|
| 1848 | n/a | p = PyLong_AsVoidPtr(item); |
|---|
| 1849 | n/a | if (p == NULL && PyErr_Occurred()) |
|---|
| 1850 | n/a | goto err_occurred; |
|---|
| 1851 | n/a | PACK_SINGLE(ptr, p, void *); |
|---|
| 1852 | n/a | break; |
|---|
| 1853 | n/a | |
|---|
| 1854 | n/a | /* default */ |
|---|
| 1855 | n/a | default: goto err_format; |
|---|
| 1856 | n/a | } |
|---|
| 1857 | n/a | |
|---|
| 1858 | n/a | return 0; |
|---|
| 1859 | n/a | |
|---|
| 1860 | n/a | err_occurred: |
|---|
| 1861 | n/a | return fix_error_int(fmt); |
|---|
| 1862 | n/a | err_range: |
|---|
| 1863 | n/a | return value_error_int(fmt); |
|---|
| 1864 | n/a | err_format: |
|---|
| 1865 | n/a | PyErr_Format(PyExc_NotImplementedError, |
|---|
| 1866 | n/a | "memoryview: format %s not supported", fmt); |
|---|
| 1867 | n/a | return -1; |
|---|
| 1868 | n/a | } |
|---|
| 1869 | n/a | |
|---|
| 1870 | n/a | |
|---|
| 1871 | n/a | /****************************************************************************/ |
|---|
| 1872 | n/a | /* unpack using the struct module */ |
|---|
| 1873 | n/a | /****************************************************************************/ |
|---|
| 1874 | n/a | |
|---|
| 1875 | n/a | /* For reasonable performance it is necessary to cache all objects required |
|---|
| 1876 | n/a | for unpacking. An unpacker can handle the format passed to unpack_from(). |
|---|
| 1877 | n/a | Invariant: All pointer fields of the struct should either be NULL or valid |
|---|
| 1878 | n/a | pointers. */ |
|---|
| 1879 | n/a | struct unpacker { |
|---|
| 1880 | n/a | PyObject *unpack_from; /* Struct.unpack_from(format) */ |
|---|
| 1881 | n/a | PyObject *mview; /* cached memoryview */ |
|---|
| 1882 | n/a | char *item; /* buffer for mview */ |
|---|
| 1883 | n/a | Py_ssize_t itemsize; /* len(item) */ |
|---|
| 1884 | n/a | }; |
|---|
| 1885 | n/a | |
|---|
| 1886 | n/a | static struct unpacker * |
|---|
| 1887 | n/a | unpacker_new(void) |
|---|
| 1888 | n/a | { |
|---|
| 1889 | n/a | struct unpacker *x = PyMem_Malloc(sizeof *x); |
|---|
| 1890 | n/a | |
|---|
| 1891 | n/a | if (x == NULL) { |
|---|
| 1892 | n/a | PyErr_NoMemory(); |
|---|
| 1893 | n/a | return NULL; |
|---|
| 1894 | n/a | } |
|---|
| 1895 | n/a | |
|---|
| 1896 | n/a | x->unpack_from = NULL; |
|---|
| 1897 | n/a | x->mview = NULL; |
|---|
| 1898 | n/a | x->item = NULL; |
|---|
| 1899 | n/a | x->itemsize = 0; |
|---|
| 1900 | n/a | |
|---|
| 1901 | n/a | return x; |
|---|
| 1902 | n/a | } |
|---|
| 1903 | n/a | |
|---|
| 1904 | n/a | static void |
|---|
| 1905 | n/a | unpacker_free(struct unpacker *x) |
|---|
| 1906 | n/a | { |
|---|
| 1907 | n/a | if (x) { |
|---|
| 1908 | n/a | Py_XDECREF(x->unpack_from); |
|---|
| 1909 | n/a | Py_XDECREF(x->mview); |
|---|
| 1910 | n/a | PyMem_Free(x->item); |
|---|
| 1911 | n/a | PyMem_Free(x); |
|---|
| 1912 | n/a | } |
|---|
| 1913 | n/a | } |
|---|
| 1914 | n/a | |
|---|
| 1915 | n/a | /* Return a new unpacker for the given format. */ |
|---|
| 1916 | n/a | static struct unpacker * |
|---|
| 1917 | n/a | struct_get_unpacker(const char *fmt, Py_ssize_t itemsize) |
|---|
| 1918 | n/a | { |
|---|
| 1919 | n/a | PyObject *structmodule; /* XXX cache these two */ |
|---|
| 1920 | n/a | PyObject *Struct = NULL; /* XXX in globals? */ |
|---|
| 1921 | n/a | PyObject *structobj = NULL; |
|---|
| 1922 | n/a | PyObject *format = NULL; |
|---|
| 1923 | n/a | struct unpacker *x = NULL; |
|---|
| 1924 | n/a | |
|---|
| 1925 | n/a | structmodule = PyImport_ImportModule("struct"); |
|---|
| 1926 | n/a | if (structmodule == NULL) |
|---|
| 1927 | n/a | return NULL; |
|---|
| 1928 | n/a | |
|---|
| 1929 | n/a | Struct = PyObject_GetAttrString(structmodule, "Struct"); |
|---|
| 1930 | n/a | Py_DECREF(structmodule); |
|---|
| 1931 | n/a | if (Struct == NULL) |
|---|
| 1932 | n/a | return NULL; |
|---|
| 1933 | n/a | |
|---|
| 1934 | n/a | x = unpacker_new(); |
|---|
| 1935 | n/a | if (x == NULL) |
|---|
| 1936 | n/a | goto error; |
|---|
| 1937 | n/a | |
|---|
| 1938 | n/a | format = PyBytes_FromString(fmt); |
|---|
| 1939 | n/a | if (format == NULL) |
|---|
| 1940 | n/a | goto error; |
|---|
| 1941 | n/a | |
|---|
| 1942 | n/a | structobj = PyObject_CallFunctionObjArgs(Struct, format, NULL); |
|---|
| 1943 | n/a | if (structobj == NULL) |
|---|
| 1944 | n/a | goto error; |
|---|
| 1945 | n/a | |
|---|
| 1946 | n/a | x->unpack_from = PyObject_GetAttrString(structobj, "unpack_from"); |
|---|
| 1947 | n/a | if (x->unpack_from == NULL) |
|---|
| 1948 | n/a | goto error; |
|---|
| 1949 | n/a | |
|---|
| 1950 | n/a | x->item = PyMem_Malloc(itemsize); |
|---|
| 1951 | n/a | if (x->item == NULL) { |
|---|
| 1952 | n/a | PyErr_NoMemory(); |
|---|
| 1953 | n/a | goto error; |
|---|
| 1954 | n/a | } |
|---|
| 1955 | n/a | x->itemsize = itemsize; |
|---|
| 1956 | n/a | |
|---|
| 1957 | n/a | x->mview = PyMemoryView_FromMemory(x->item, itemsize, PyBUF_WRITE); |
|---|
| 1958 | n/a | if (x->mview == NULL) |
|---|
| 1959 | n/a | goto error; |
|---|
| 1960 | n/a | |
|---|
| 1961 | n/a | |
|---|
| 1962 | n/a | out: |
|---|
| 1963 | n/a | Py_XDECREF(Struct); |
|---|
| 1964 | n/a | Py_XDECREF(format); |
|---|
| 1965 | n/a | Py_XDECREF(structobj); |
|---|
| 1966 | n/a | return x; |
|---|
| 1967 | n/a | |
|---|
| 1968 | n/a | error: |
|---|
| 1969 | n/a | unpacker_free(x); |
|---|
| 1970 | n/a | x = NULL; |
|---|
| 1971 | n/a | goto out; |
|---|
| 1972 | n/a | } |
|---|
| 1973 | n/a | |
|---|
| 1974 | n/a | /* unpack a single item */ |
|---|
| 1975 | n/a | static PyObject * |
|---|
| 1976 | n/a | struct_unpack_single(const char *ptr, struct unpacker *x) |
|---|
| 1977 | n/a | { |
|---|
| 1978 | n/a | PyObject *v; |
|---|
| 1979 | n/a | |
|---|
| 1980 | n/a | memcpy(x->item, ptr, x->itemsize); |
|---|
| 1981 | n/a | v = PyObject_CallFunctionObjArgs(x->unpack_from, x->mview, NULL); |
|---|
| 1982 | n/a | if (v == NULL) |
|---|
| 1983 | n/a | return NULL; |
|---|
| 1984 | n/a | |
|---|
| 1985 | n/a | if (PyTuple_GET_SIZE(v) == 1) { |
|---|
| 1986 | n/a | PyObject *tmp = PyTuple_GET_ITEM(v, 0); |
|---|
| 1987 | n/a | Py_INCREF(tmp); |
|---|
| 1988 | n/a | Py_DECREF(v); |
|---|
| 1989 | n/a | return tmp; |
|---|
| 1990 | n/a | } |
|---|
| 1991 | n/a | |
|---|
| 1992 | n/a | return v; |
|---|
| 1993 | n/a | } |
|---|
| 1994 | n/a | |
|---|
| 1995 | n/a | |
|---|
| 1996 | n/a | /****************************************************************************/ |
|---|
| 1997 | n/a | /* Representations */ |
|---|
| 1998 | n/a | /****************************************************************************/ |
|---|
| 1999 | n/a | |
|---|
| 2000 | n/a | /* allow explicit form of native format */ |
|---|
| 2001 | n/a | static inline const char * |
|---|
| 2002 | n/a | adjust_fmt(const Py_buffer *view) |
|---|
| 2003 | n/a | { |
|---|
| 2004 | n/a | const char *fmt; |
|---|
| 2005 | n/a | |
|---|
| 2006 | n/a | fmt = (view->format[0] == '@') ? view->format+1 : view->format; |
|---|
| 2007 | n/a | if (fmt[0] && fmt[1] == '\0') |
|---|
| 2008 | n/a | return fmt; |
|---|
| 2009 | n/a | |
|---|
| 2010 | n/a | PyErr_Format(PyExc_NotImplementedError, |
|---|
| 2011 | n/a | "memoryview: unsupported format %s", view->format); |
|---|
| 2012 | n/a | return NULL; |
|---|
| 2013 | n/a | } |
|---|
| 2014 | n/a | |
|---|
| 2015 | n/a | /* Base case for multi-dimensional unpacking. Assumption: ndim == 1. */ |
|---|
| 2016 | n/a | static PyObject * |
|---|
| 2017 | n/a | tolist_base(const char *ptr, const Py_ssize_t *shape, |
|---|
| 2018 | n/a | const Py_ssize_t *strides, const Py_ssize_t *suboffsets, |
|---|
| 2019 | n/a | const char *fmt) |
|---|
| 2020 | n/a | { |
|---|
| 2021 | n/a | PyObject *lst, *item; |
|---|
| 2022 | n/a | Py_ssize_t i; |
|---|
| 2023 | n/a | |
|---|
| 2024 | n/a | lst = PyList_New(shape[0]); |
|---|
| 2025 | n/a | if (lst == NULL) |
|---|
| 2026 | n/a | return NULL; |
|---|
| 2027 | n/a | |
|---|
| 2028 | n/a | for (i = 0; i < shape[0]; ptr+=strides[0], i++) { |
|---|
| 2029 | n/a | const char *xptr = ADJUST_PTR(ptr, suboffsets, 0); |
|---|
| 2030 | n/a | item = unpack_single(xptr, fmt); |
|---|
| 2031 | n/a | if (item == NULL) { |
|---|
| 2032 | n/a | Py_DECREF(lst); |
|---|
| 2033 | n/a | return NULL; |
|---|
| 2034 | n/a | } |
|---|
| 2035 | n/a | PyList_SET_ITEM(lst, i, item); |
|---|
| 2036 | n/a | } |
|---|
| 2037 | n/a | |
|---|
| 2038 | n/a | return lst; |
|---|
| 2039 | n/a | } |
|---|
| 2040 | n/a | |
|---|
| 2041 | n/a | /* Unpack a multi-dimensional array into a nested list. |
|---|
| 2042 | n/a | Assumption: ndim >= 1. */ |
|---|
| 2043 | n/a | static PyObject * |
|---|
| 2044 | n/a | tolist_rec(const char *ptr, Py_ssize_t ndim, const Py_ssize_t *shape, |
|---|
| 2045 | n/a | const Py_ssize_t *strides, const Py_ssize_t *suboffsets, |
|---|
| 2046 | n/a | const char *fmt) |
|---|
| 2047 | n/a | { |
|---|
| 2048 | n/a | PyObject *lst, *item; |
|---|
| 2049 | n/a | Py_ssize_t i; |
|---|
| 2050 | n/a | |
|---|
| 2051 | n/a | assert(ndim >= 1); |
|---|
| 2052 | n/a | assert(shape != NULL); |
|---|
| 2053 | n/a | assert(strides != NULL); |
|---|
| 2054 | n/a | |
|---|
| 2055 | n/a | if (ndim == 1) |
|---|
| 2056 | n/a | return tolist_base(ptr, shape, strides, suboffsets, fmt); |
|---|
| 2057 | n/a | |
|---|
| 2058 | n/a | lst = PyList_New(shape[0]); |
|---|
| 2059 | n/a | if (lst == NULL) |
|---|
| 2060 | n/a | return NULL; |
|---|
| 2061 | n/a | |
|---|
| 2062 | n/a | for (i = 0; i < shape[0]; ptr+=strides[0], i++) { |
|---|
| 2063 | n/a | const char *xptr = ADJUST_PTR(ptr, suboffsets, 0); |
|---|
| 2064 | n/a | item = tolist_rec(xptr, ndim-1, shape+1, |
|---|
| 2065 | n/a | strides+1, suboffsets ? suboffsets+1 : NULL, |
|---|
| 2066 | n/a | fmt); |
|---|
| 2067 | n/a | if (item == NULL) { |
|---|
| 2068 | n/a | Py_DECREF(lst); |
|---|
| 2069 | n/a | return NULL; |
|---|
| 2070 | n/a | } |
|---|
| 2071 | n/a | PyList_SET_ITEM(lst, i, item); |
|---|
| 2072 | n/a | } |
|---|
| 2073 | n/a | |
|---|
| 2074 | n/a | return lst; |
|---|
| 2075 | n/a | } |
|---|
| 2076 | n/a | |
|---|
| 2077 | n/a | /* Return a list representation of the memoryview. Currently only buffers |
|---|
| 2078 | n/a | with native format strings are supported. */ |
|---|
| 2079 | n/a | static PyObject * |
|---|
| 2080 | n/a | memory_tolist(PyMemoryViewObject *mv, PyObject *noargs) |
|---|
| 2081 | n/a | { |
|---|
| 2082 | n/a | const Py_buffer *view = &(mv->view); |
|---|
| 2083 | n/a | const char *fmt; |
|---|
| 2084 | n/a | |
|---|
| 2085 | n/a | CHECK_RELEASED(mv); |
|---|
| 2086 | n/a | |
|---|
| 2087 | n/a | fmt = adjust_fmt(view); |
|---|
| 2088 | n/a | if (fmt == NULL) |
|---|
| 2089 | n/a | return NULL; |
|---|
| 2090 | n/a | if (view->ndim == 0) { |
|---|
| 2091 | n/a | return unpack_single(view->buf, fmt); |
|---|
| 2092 | n/a | } |
|---|
| 2093 | n/a | else if (view->ndim == 1) { |
|---|
| 2094 | n/a | return tolist_base(view->buf, view->shape, |
|---|
| 2095 | n/a | view->strides, view->suboffsets, |
|---|
| 2096 | n/a | fmt); |
|---|
| 2097 | n/a | } |
|---|
| 2098 | n/a | else { |
|---|
| 2099 | n/a | return tolist_rec(view->buf, view->ndim, view->shape, |
|---|
| 2100 | n/a | view->strides, view->suboffsets, |
|---|
| 2101 | n/a | fmt); |
|---|
| 2102 | n/a | } |
|---|
| 2103 | n/a | } |
|---|
| 2104 | n/a | |
|---|
| 2105 | n/a | static PyObject * |
|---|
| 2106 | n/a | memory_tobytes(PyMemoryViewObject *self, PyObject *dummy) |
|---|
| 2107 | n/a | { |
|---|
| 2108 | n/a | Py_buffer *src = VIEW_ADDR(self); |
|---|
| 2109 | n/a | PyObject *bytes = NULL; |
|---|
| 2110 | n/a | |
|---|
| 2111 | n/a | CHECK_RELEASED(self); |
|---|
| 2112 | n/a | |
|---|
| 2113 | n/a | if (MV_C_CONTIGUOUS(self->flags)) { |
|---|
| 2114 | n/a | return PyBytes_FromStringAndSize(src->buf, src->len); |
|---|
| 2115 | n/a | } |
|---|
| 2116 | n/a | |
|---|
| 2117 | n/a | bytes = PyBytes_FromStringAndSize(NULL, src->len); |
|---|
| 2118 | n/a | if (bytes == NULL) |
|---|
| 2119 | n/a | return NULL; |
|---|
| 2120 | n/a | |
|---|
| 2121 | n/a | if (buffer_to_contiguous(PyBytes_AS_STRING(bytes), src, 'C') < 0) { |
|---|
| 2122 | n/a | Py_DECREF(bytes); |
|---|
| 2123 | n/a | return NULL; |
|---|
| 2124 | n/a | } |
|---|
| 2125 | n/a | |
|---|
| 2126 | n/a | return bytes; |
|---|
| 2127 | n/a | } |
|---|
| 2128 | n/a | |
|---|
| 2129 | n/a | static PyObject * |
|---|
| 2130 | n/a | memory_hex(PyMemoryViewObject *self, PyObject *dummy) |
|---|
| 2131 | n/a | { |
|---|
| 2132 | n/a | Py_buffer *src = VIEW_ADDR(self); |
|---|
| 2133 | n/a | PyObject *bytes; |
|---|
| 2134 | n/a | PyObject *ret; |
|---|
| 2135 | n/a | |
|---|
| 2136 | n/a | CHECK_RELEASED(self); |
|---|
| 2137 | n/a | |
|---|
| 2138 | n/a | if (MV_C_CONTIGUOUS(self->flags)) { |
|---|
| 2139 | n/a | return _Py_strhex(src->buf, src->len); |
|---|
| 2140 | n/a | } |
|---|
| 2141 | n/a | |
|---|
| 2142 | n/a | bytes = memory_tobytes(self, dummy); |
|---|
| 2143 | n/a | if (bytes == NULL) |
|---|
| 2144 | n/a | return NULL; |
|---|
| 2145 | n/a | |
|---|
| 2146 | n/a | ret = _Py_strhex(PyBytes_AS_STRING(bytes), Py_SIZE(bytes)); |
|---|
| 2147 | n/a | Py_DECREF(bytes); |
|---|
| 2148 | n/a | |
|---|
| 2149 | n/a | return ret; |
|---|
| 2150 | n/a | } |
|---|
| 2151 | n/a | |
|---|
| 2152 | n/a | static PyObject * |
|---|
| 2153 | n/a | memory_repr(PyMemoryViewObject *self) |
|---|
| 2154 | n/a | { |
|---|
| 2155 | n/a | if (self->flags & _Py_MEMORYVIEW_RELEASED) |
|---|
| 2156 | n/a | return PyUnicode_FromFormat("<released memory at %p>", self); |
|---|
| 2157 | n/a | else |
|---|
| 2158 | n/a | return PyUnicode_FromFormat("<memory at %p>", self); |
|---|
| 2159 | n/a | } |
|---|
| 2160 | n/a | |
|---|
| 2161 | n/a | |
|---|
| 2162 | n/a | /**************************************************************************/ |
|---|
| 2163 | n/a | /* Indexing and slicing */ |
|---|
| 2164 | n/a | /**************************************************************************/ |
|---|
| 2165 | n/a | |
|---|
| 2166 | n/a | static char * |
|---|
| 2167 | n/a | lookup_dimension(Py_buffer *view, char *ptr, int dim, Py_ssize_t index) |
|---|
| 2168 | n/a | { |
|---|
| 2169 | n/a | Py_ssize_t nitems; /* items in the given dimension */ |
|---|
| 2170 | n/a | |
|---|
| 2171 | n/a | assert(view->shape); |
|---|
| 2172 | n/a | assert(view->strides); |
|---|
| 2173 | n/a | |
|---|
| 2174 | n/a | nitems = view->shape[dim]; |
|---|
| 2175 | n/a | if (index < 0) { |
|---|
| 2176 | n/a | index += nitems; |
|---|
| 2177 | n/a | } |
|---|
| 2178 | n/a | if (index < 0 || index >= nitems) { |
|---|
| 2179 | n/a | PyErr_Format(PyExc_IndexError, |
|---|
| 2180 | n/a | "index out of bounds on dimension %d", dim + 1); |
|---|
| 2181 | n/a | return NULL; |
|---|
| 2182 | n/a | } |
|---|
| 2183 | n/a | |
|---|
| 2184 | n/a | ptr += view->strides[dim] * index; |
|---|
| 2185 | n/a | |
|---|
| 2186 | n/a | ptr = ADJUST_PTR(ptr, view->suboffsets, dim); |
|---|
| 2187 | n/a | |
|---|
| 2188 | n/a | return ptr; |
|---|
| 2189 | n/a | } |
|---|
| 2190 | n/a | |
|---|
| 2191 | n/a | /* Get the pointer to the item at index. */ |
|---|
| 2192 | n/a | static char * |
|---|
| 2193 | n/a | ptr_from_index(Py_buffer *view, Py_ssize_t index) |
|---|
| 2194 | n/a | { |
|---|
| 2195 | n/a | char *ptr = (char *)view->buf; |
|---|
| 2196 | n/a | return lookup_dimension(view, ptr, 0, index); |
|---|
| 2197 | n/a | } |
|---|
| 2198 | n/a | |
|---|
| 2199 | n/a | /* Get the pointer to the item at tuple. */ |
|---|
| 2200 | n/a | static char * |
|---|
| 2201 | n/a | ptr_from_tuple(Py_buffer *view, PyObject *tup) |
|---|
| 2202 | n/a | { |
|---|
| 2203 | n/a | char *ptr = (char *)view->buf; |
|---|
| 2204 | n/a | Py_ssize_t dim, nindices = PyTuple_GET_SIZE(tup); |
|---|
| 2205 | n/a | |
|---|
| 2206 | n/a | if (nindices > view->ndim) { |
|---|
| 2207 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2208 | n/a | "cannot index %zd-dimension view with %zd-element tuple", |
|---|
| 2209 | n/a | view->ndim, nindices); |
|---|
| 2210 | n/a | return NULL; |
|---|
| 2211 | n/a | } |
|---|
| 2212 | n/a | |
|---|
| 2213 | n/a | for (dim = 0; dim < nindices; dim++) { |
|---|
| 2214 | n/a | Py_ssize_t index; |
|---|
| 2215 | n/a | index = PyNumber_AsSsize_t(PyTuple_GET_ITEM(tup, dim), |
|---|
| 2216 | n/a | PyExc_IndexError); |
|---|
| 2217 | n/a | if (index == -1 && PyErr_Occurred()) |
|---|
| 2218 | n/a | return NULL; |
|---|
| 2219 | n/a | ptr = lookup_dimension(view, ptr, (int)dim, index); |
|---|
| 2220 | n/a | if (ptr == NULL) |
|---|
| 2221 | n/a | return NULL; |
|---|
| 2222 | n/a | } |
|---|
| 2223 | n/a | return ptr; |
|---|
| 2224 | n/a | } |
|---|
| 2225 | n/a | |
|---|
| 2226 | n/a | /* Return the item at index. In a one-dimensional view, this is an object |
|---|
| 2227 | n/a | with the type specified by view->format. Otherwise, the item is a sub-view. |
|---|
| 2228 | n/a | The function is used in memory_subscript() and memory_as_sequence. */ |
|---|
| 2229 | n/a | static PyObject * |
|---|
| 2230 | n/a | memory_item(PyMemoryViewObject *self, Py_ssize_t index) |
|---|
| 2231 | n/a | { |
|---|
| 2232 | n/a | Py_buffer *view = &(self->view); |
|---|
| 2233 | n/a | const char *fmt; |
|---|
| 2234 | n/a | |
|---|
| 2235 | n/a | CHECK_RELEASED(self); |
|---|
| 2236 | n/a | |
|---|
| 2237 | n/a | fmt = adjust_fmt(view); |
|---|
| 2238 | n/a | if (fmt == NULL) |
|---|
| 2239 | n/a | return NULL; |
|---|
| 2240 | n/a | |
|---|
| 2241 | n/a | if (view->ndim == 0) { |
|---|
| 2242 | n/a | PyErr_SetString(PyExc_TypeError, "invalid indexing of 0-dim memory"); |
|---|
| 2243 | n/a | return NULL; |
|---|
| 2244 | n/a | } |
|---|
| 2245 | n/a | if (view->ndim == 1) { |
|---|
| 2246 | n/a | char *ptr = ptr_from_index(view, index); |
|---|
| 2247 | n/a | if (ptr == NULL) |
|---|
| 2248 | n/a | return NULL; |
|---|
| 2249 | n/a | return unpack_single(ptr, fmt); |
|---|
| 2250 | n/a | } |
|---|
| 2251 | n/a | |
|---|
| 2252 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2253 | n/a | "multi-dimensional sub-views are not implemented"); |
|---|
| 2254 | n/a | return NULL; |
|---|
| 2255 | n/a | } |
|---|
| 2256 | n/a | |
|---|
| 2257 | n/a | /* Return the item at position *key* (a tuple of indices). */ |
|---|
| 2258 | n/a | static PyObject * |
|---|
| 2259 | n/a | memory_item_multi(PyMemoryViewObject *self, PyObject *tup) |
|---|
| 2260 | n/a | { |
|---|
| 2261 | n/a | Py_buffer *view = &(self->view); |
|---|
| 2262 | n/a | const char *fmt; |
|---|
| 2263 | n/a | Py_ssize_t nindices = PyTuple_GET_SIZE(tup); |
|---|
| 2264 | n/a | char *ptr; |
|---|
| 2265 | n/a | |
|---|
| 2266 | n/a | CHECK_RELEASED(self); |
|---|
| 2267 | n/a | |
|---|
| 2268 | n/a | fmt = adjust_fmt(view); |
|---|
| 2269 | n/a | if (fmt == NULL) |
|---|
| 2270 | n/a | return NULL; |
|---|
| 2271 | n/a | |
|---|
| 2272 | n/a | if (nindices < view->ndim) { |
|---|
| 2273 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2274 | n/a | "sub-views are not implemented"); |
|---|
| 2275 | n/a | return NULL; |
|---|
| 2276 | n/a | } |
|---|
| 2277 | n/a | ptr = ptr_from_tuple(view, tup); |
|---|
| 2278 | n/a | if (ptr == NULL) |
|---|
| 2279 | n/a | return NULL; |
|---|
| 2280 | n/a | return unpack_single(ptr, fmt); |
|---|
| 2281 | n/a | } |
|---|
| 2282 | n/a | |
|---|
| 2283 | n/a | static inline int |
|---|
| 2284 | n/a | init_slice(Py_buffer *base, PyObject *key, int dim) |
|---|
| 2285 | n/a | { |
|---|
| 2286 | n/a | Py_ssize_t start, stop, step, slicelength; |
|---|
| 2287 | n/a | |
|---|
| 2288 | n/a | if (PySlice_GetIndicesEx(key, base->shape[dim], |
|---|
| 2289 | n/a | &start, &stop, &step, &slicelength) < 0) { |
|---|
| 2290 | n/a | return -1; |
|---|
| 2291 | n/a | } |
|---|
| 2292 | n/a | |
|---|
| 2293 | n/a | |
|---|
| 2294 | n/a | if (base->suboffsets == NULL || dim == 0) { |
|---|
| 2295 | n/a | adjust_buf: |
|---|
| 2296 | n/a | base->buf = (char *)base->buf + base->strides[dim] * start; |
|---|
| 2297 | n/a | } |
|---|
| 2298 | n/a | else { |
|---|
| 2299 | n/a | Py_ssize_t n = dim-1; |
|---|
| 2300 | n/a | while (n >= 0 && base->suboffsets[n] < 0) |
|---|
| 2301 | n/a | n--; |
|---|
| 2302 | n/a | if (n < 0) |
|---|
| 2303 | n/a | goto adjust_buf; /* all suboffsets are negative */ |
|---|
| 2304 | n/a | base->suboffsets[n] = base->suboffsets[n] + base->strides[dim] * start; |
|---|
| 2305 | n/a | } |
|---|
| 2306 | n/a | base->shape[dim] = slicelength; |
|---|
| 2307 | n/a | base->strides[dim] = base->strides[dim] * step; |
|---|
| 2308 | n/a | |
|---|
| 2309 | n/a | return 0; |
|---|
| 2310 | n/a | } |
|---|
| 2311 | n/a | |
|---|
| 2312 | n/a | static int |
|---|
| 2313 | n/a | is_multislice(PyObject *key) |
|---|
| 2314 | n/a | { |
|---|
| 2315 | n/a | Py_ssize_t size, i; |
|---|
| 2316 | n/a | |
|---|
| 2317 | n/a | if (!PyTuple_Check(key)) |
|---|
| 2318 | n/a | return 0; |
|---|
| 2319 | n/a | size = PyTuple_GET_SIZE(key); |
|---|
| 2320 | n/a | if (size == 0) |
|---|
| 2321 | n/a | return 0; |
|---|
| 2322 | n/a | |
|---|
| 2323 | n/a | for (i = 0; i < size; i++) { |
|---|
| 2324 | n/a | PyObject *x = PyTuple_GET_ITEM(key, i); |
|---|
| 2325 | n/a | if (!PySlice_Check(x)) |
|---|
| 2326 | n/a | return 0; |
|---|
| 2327 | n/a | } |
|---|
| 2328 | n/a | return 1; |
|---|
| 2329 | n/a | } |
|---|
| 2330 | n/a | |
|---|
| 2331 | n/a | static Py_ssize_t |
|---|
| 2332 | n/a | is_multiindex(PyObject *key) |
|---|
| 2333 | n/a | { |
|---|
| 2334 | n/a | Py_ssize_t size, i; |
|---|
| 2335 | n/a | |
|---|
| 2336 | n/a | if (!PyTuple_Check(key)) |
|---|
| 2337 | n/a | return 0; |
|---|
| 2338 | n/a | size = PyTuple_GET_SIZE(key); |
|---|
| 2339 | n/a | for (i = 0; i < size; i++) { |
|---|
| 2340 | n/a | PyObject *x = PyTuple_GET_ITEM(key, i); |
|---|
| 2341 | n/a | if (!PyIndex_Check(x)) |
|---|
| 2342 | n/a | return 0; |
|---|
| 2343 | n/a | } |
|---|
| 2344 | n/a | return 1; |
|---|
| 2345 | n/a | } |
|---|
| 2346 | n/a | |
|---|
| 2347 | n/a | /* mv[obj] returns an object holding the data for one element if obj |
|---|
| 2348 | n/a | fully indexes the memoryview or another memoryview object if it |
|---|
| 2349 | n/a | does not. |
|---|
| 2350 | n/a | |
|---|
| 2351 | n/a | 0-d memoryview objects can be referenced using mv[...] or mv[()] |
|---|
| 2352 | n/a | but not with anything else. */ |
|---|
| 2353 | n/a | static PyObject * |
|---|
| 2354 | n/a | memory_subscript(PyMemoryViewObject *self, PyObject *key) |
|---|
| 2355 | n/a | { |
|---|
| 2356 | n/a | Py_buffer *view; |
|---|
| 2357 | n/a | view = &(self->view); |
|---|
| 2358 | n/a | |
|---|
| 2359 | n/a | CHECK_RELEASED(self); |
|---|
| 2360 | n/a | |
|---|
| 2361 | n/a | if (view->ndim == 0) { |
|---|
| 2362 | n/a | if (PyTuple_Check(key) && PyTuple_GET_SIZE(key) == 0) { |
|---|
| 2363 | n/a | const char *fmt = adjust_fmt(view); |
|---|
| 2364 | n/a | if (fmt == NULL) |
|---|
| 2365 | n/a | return NULL; |
|---|
| 2366 | n/a | return unpack_single(view->buf, fmt); |
|---|
| 2367 | n/a | } |
|---|
| 2368 | n/a | else if (key == Py_Ellipsis) { |
|---|
| 2369 | n/a | Py_INCREF(self); |
|---|
| 2370 | n/a | return (PyObject *)self; |
|---|
| 2371 | n/a | } |
|---|
| 2372 | n/a | else { |
|---|
| 2373 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2374 | n/a | "invalid indexing of 0-dim memory"); |
|---|
| 2375 | n/a | return NULL; |
|---|
| 2376 | n/a | } |
|---|
| 2377 | n/a | } |
|---|
| 2378 | n/a | |
|---|
| 2379 | n/a | if (PyIndex_Check(key)) { |
|---|
| 2380 | n/a | Py_ssize_t index; |
|---|
| 2381 | n/a | index = PyNumber_AsSsize_t(key, PyExc_IndexError); |
|---|
| 2382 | n/a | if (index == -1 && PyErr_Occurred()) |
|---|
| 2383 | n/a | return NULL; |
|---|
| 2384 | n/a | return memory_item(self, index); |
|---|
| 2385 | n/a | } |
|---|
| 2386 | n/a | else if (PySlice_Check(key)) { |
|---|
| 2387 | n/a | PyMemoryViewObject *sliced; |
|---|
| 2388 | n/a | |
|---|
| 2389 | n/a | sliced = (PyMemoryViewObject *)mbuf_add_view(self->mbuf, view); |
|---|
| 2390 | n/a | if (sliced == NULL) |
|---|
| 2391 | n/a | return NULL; |
|---|
| 2392 | n/a | |
|---|
| 2393 | n/a | if (init_slice(&sliced->view, key, 0) < 0) { |
|---|
| 2394 | n/a | Py_DECREF(sliced); |
|---|
| 2395 | n/a | return NULL; |
|---|
| 2396 | n/a | } |
|---|
| 2397 | n/a | init_len(&sliced->view); |
|---|
| 2398 | n/a | init_flags(sliced); |
|---|
| 2399 | n/a | |
|---|
| 2400 | n/a | return (PyObject *)sliced; |
|---|
| 2401 | n/a | } |
|---|
| 2402 | n/a | else if (is_multiindex(key)) { |
|---|
| 2403 | n/a | return memory_item_multi(self, key); |
|---|
| 2404 | n/a | } |
|---|
| 2405 | n/a | else if (is_multislice(key)) { |
|---|
| 2406 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2407 | n/a | "multi-dimensional slicing is not implemented"); |
|---|
| 2408 | n/a | return NULL; |
|---|
| 2409 | n/a | } |
|---|
| 2410 | n/a | |
|---|
| 2411 | n/a | PyErr_SetString(PyExc_TypeError, "memoryview: invalid slice key"); |
|---|
| 2412 | n/a | return NULL; |
|---|
| 2413 | n/a | } |
|---|
| 2414 | n/a | |
|---|
| 2415 | n/a | static int |
|---|
| 2416 | n/a | memory_ass_sub(PyMemoryViewObject *self, PyObject *key, PyObject *value) |
|---|
| 2417 | n/a | { |
|---|
| 2418 | n/a | Py_buffer *view = &(self->view); |
|---|
| 2419 | n/a | Py_buffer src; |
|---|
| 2420 | n/a | const char *fmt; |
|---|
| 2421 | n/a | char *ptr; |
|---|
| 2422 | n/a | |
|---|
| 2423 | n/a | CHECK_RELEASED_INT(self); |
|---|
| 2424 | n/a | |
|---|
| 2425 | n/a | fmt = adjust_fmt(view); |
|---|
| 2426 | n/a | if (fmt == NULL) |
|---|
| 2427 | n/a | return -1; |
|---|
| 2428 | n/a | |
|---|
| 2429 | n/a | if (view->readonly) { |
|---|
| 2430 | n/a | PyErr_SetString(PyExc_TypeError, "cannot modify read-only memory"); |
|---|
| 2431 | n/a | return -1; |
|---|
| 2432 | n/a | } |
|---|
| 2433 | n/a | if (value == NULL) { |
|---|
| 2434 | n/a | PyErr_SetString(PyExc_TypeError, "cannot delete memory"); |
|---|
| 2435 | n/a | return -1; |
|---|
| 2436 | n/a | } |
|---|
| 2437 | n/a | if (view->ndim == 0) { |
|---|
| 2438 | n/a | if (key == Py_Ellipsis || |
|---|
| 2439 | n/a | (PyTuple_Check(key) && PyTuple_GET_SIZE(key)==0)) { |
|---|
| 2440 | n/a | ptr = (char *)view->buf; |
|---|
| 2441 | n/a | return pack_single(ptr, value, fmt); |
|---|
| 2442 | n/a | } |
|---|
| 2443 | n/a | else { |
|---|
| 2444 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2445 | n/a | "invalid indexing of 0-dim memory"); |
|---|
| 2446 | n/a | return -1; |
|---|
| 2447 | n/a | } |
|---|
| 2448 | n/a | } |
|---|
| 2449 | n/a | |
|---|
| 2450 | n/a | if (PyIndex_Check(key)) { |
|---|
| 2451 | n/a | Py_ssize_t index; |
|---|
| 2452 | n/a | if (1 < view->ndim) { |
|---|
| 2453 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2454 | n/a | "sub-views are not implemented"); |
|---|
| 2455 | n/a | return -1; |
|---|
| 2456 | n/a | } |
|---|
| 2457 | n/a | index = PyNumber_AsSsize_t(key, PyExc_IndexError); |
|---|
| 2458 | n/a | if (index == -1 && PyErr_Occurred()) |
|---|
| 2459 | n/a | return -1; |
|---|
| 2460 | n/a | ptr = ptr_from_index(view, index); |
|---|
| 2461 | n/a | if (ptr == NULL) |
|---|
| 2462 | n/a | return -1; |
|---|
| 2463 | n/a | return pack_single(ptr, value, fmt); |
|---|
| 2464 | n/a | } |
|---|
| 2465 | n/a | /* one-dimensional: fast path */ |
|---|
| 2466 | n/a | if (PySlice_Check(key) && view->ndim == 1) { |
|---|
| 2467 | n/a | Py_buffer dest; /* sliced view */ |
|---|
| 2468 | n/a | Py_ssize_t arrays[3]; |
|---|
| 2469 | n/a | int ret = -1; |
|---|
| 2470 | n/a | |
|---|
| 2471 | n/a | /* rvalue must be an exporter */ |
|---|
| 2472 | n/a | if (PyObject_GetBuffer(value, &src, PyBUF_FULL_RO) < 0) |
|---|
| 2473 | n/a | return ret; |
|---|
| 2474 | n/a | |
|---|
| 2475 | n/a | dest = *view; |
|---|
| 2476 | n/a | dest.shape = &arrays[0]; dest.shape[0] = view->shape[0]; |
|---|
| 2477 | n/a | dest.strides = &arrays[1]; dest.strides[0] = view->strides[0]; |
|---|
| 2478 | n/a | if (view->suboffsets) { |
|---|
| 2479 | n/a | dest.suboffsets = &arrays[2]; dest.suboffsets[0] = view->suboffsets[0]; |
|---|
| 2480 | n/a | } |
|---|
| 2481 | n/a | |
|---|
| 2482 | n/a | if (init_slice(&dest, key, 0) < 0) |
|---|
| 2483 | n/a | goto end_block; |
|---|
| 2484 | n/a | dest.len = dest.shape[0] * dest.itemsize; |
|---|
| 2485 | n/a | |
|---|
| 2486 | n/a | ret = copy_single(&dest, &src); |
|---|
| 2487 | n/a | |
|---|
| 2488 | n/a | end_block: |
|---|
| 2489 | n/a | PyBuffer_Release(&src); |
|---|
| 2490 | n/a | return ret; |
|---|
| 2491 | n/a | } |
|---|
| 2492 | n/a | if (is_multiindex(key)) { |
|---|
| 2493 | n/a | char *ptr; |
|---|
| 2494 | n/a | if (PyTuple_GET_SIZE(key) < view->ndim) { |
|---|
| 2495 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2496 | n/a | "sub-views are not implemented"); |
|---|
| 2497 | n/a | return -1; |
|---|
| 2498 | n/a | } |
|---|
| 2499 | n/a | ptr = ptr_from_tuple(view, key); |
|---|
| 2500 | n/a | if (ptr == NULL) |
|---|
| 2501 | n/a | return -1; |
|---|
| 2502 | n/a | return pack_single(ptr, value, fmt); |
|---|
| 2503 | n/a | } |
|---|
| 2504 | n/a | if (PySlice_Check(key) || is_multislice(key)) { |
|---|
| 2505 | n/a | /* Call memory_subscript() to produce a sliced lvalue, then copy |
|---|
| 2506 | n/a | rvalue into lvalue. This is already implemented in _testbuffer.c. */ |
|---|
| 2507 | n/a | PyErr_SetString(PyExc_NotImplementedError, |
|---|
| 2508 | n/a | "memoryview slice assignments are currently restricted " |
|---|
| 2509 | n/a | "to ndim = 1"); |
|---|
| 2510 | n/a | return -1; |
|---|
| 2511 | n/a | } |
|---|
| 2512 | n/a | |
|---|
| 2513 | n/a | PyErr_SetString(PyExc_TypeError, "memoryview: invalid slice key"); |
|---|
| 2514 | n/a | return -1; |
|---|
| 2515 | n/a | } |
|---|
| 2516 | n/a | |
|---|
| 2517 | n/a | static Py_ssize_t |
|---|
| 2518 | n/a | memory_length(PyMemoryViewObject *self) |
|---|
| 2519 | n/a | { |
|---|
| 2520 | n/a | CHECK_RELEASED_INT(self); |
|---|
| 2521 | n/a | return self->view.ndim == 0 ? 1 : self->view.shape[0]; |
|---|
| 2522 | n/a | } |
|---|
| 2523 | n/a | |
|---|
| 2524 | n/a | /* As mapping */ |
|---|
| 2525 | n/a | static PyMappingMethods memory_as_mapping = { |
|---|
| 2526 | n/a | (lenfunc)memory_length, /* mp_length */ |
|---|
| 2527 | n/a | (binaryfunc)memory_subscript, /* mp_subscript */ |
|---|
| 2528 | n/a | (objobjargproc)memory_ass_sub, /* mp_ass_subscript */ |
|---|
| 2529 | n/a | }; |
|---|
| 2530 | n/a | |
|---|
| 2531 | n/a | /* As sequence */ |
|---|
| 2532 | n/a | static PySequenceMethods memory_as_sequence = { |
|---|
| 2533 | n/a | (lenfunc)memory_length, /* sq_length */ |
|---|
| 2534 | n/a | 0, /* sq_concat */ |
|---|
| 2535 | n/a | 0, /* sq_repeat */ |
|---|
| 2536 | n/a | (ssizeargfunc)memory_item, /* sq_item */ |
|---|
| 2537 | n/a | }; |
|---|
| 2538 | n/a | |
|---|
| 2539 | n/a | |
|---|
| 2540 | n/a | /**************************************************************************/ |
|---|
| 2541 | n/a | /* Comparisons */ |
|---|
| 2542 | n/a | /**************************************************************************/ |
|---|
| 2543 | n/a | |
|---|
| 2544 | n/a | #define MV_COMPARE_EX -1 /* exception */ |
|---|
| 2545 | n/a | #define MV_COMPARE_NOT_IMPL -2 /* not implemented */ |
|---|
| 2546 | n/a | |
|---|
| 2547 | n/a | /* Translate a StructError to "not equal". Preserve other exceptions. */ |
|---|
| 2548 | n/a | static int |
|---|
| 2549 | n/a | fix_struct_error_int(void) |
|---|
| 2550 | n/a | { |
|---|
| 2551 | n/a | assert(PyErr_Occurred()); |
|---|
| 2552 | n/a | /* XXX Cannot get at StructError directly? */ |
|---|
| 2553 | n/a | if (PyErr_ExceptionMatches(PyExc_ImportError) || |
|---|
| 2554 | n/a | PyErr_ExceptionMatches(PyExc_MemoryError)) { |
|---|
| 2555 | n/a | return MV_COMPARE_EX; |
|---|
| 2556 | n/a | } |
|---|
| 2557 | n/a | /* StructError: invalid or unknown format -> not equal */ |
|---|
| 2558 | n/a | PyErr_Clear(); |
|---|
| 2559 | n/a | return 0; |
|---|
| 2560 | n/a | } |
|---|
| 2561 | n/a | |
|---|
| 2562 | n/a | /* Unpack and compare single items of p and q using the struct module. */ |
|---|
| 2563 | n/a | static int |
|---|
| 2564 | n/a | struct_unpack_cmp(const char *p, const char *q, |
|---|
| 2565 | n/a | struct unpacker *unpack_p, struct unpacker *unpack_q) |
|---|
| 2566 | n/a | { |
|---|
| 2567 | n/a | PyObject *v, *w; |
|---|
| 2568 | n/a | int ret; |
|---|
| 2569 | n/a | |
|---|
| 2570 | n/a | /* At this point any exception from the struct module should not be |
|---|
| 2571 | n/a | StructError, since both formats have been accepted already. */ |
|---|
| 2572 | n/a | v = struct_unpack_single(p, unpack_p); |
|---|
| 2573 | n/a | if (v == NULL) |
|---|
| 2574 | n/a | return MV_COMPARE_EX; |
|---|
| 2575 | n/a | |
|---|
| 2576 | n/a | w = struct_unpack_single(q, unpack_q); |
|---|
| 2577 | n/a | if (w == NULL) { |
|---|
| 2578 | n/a | Py_DECREF(v); |
|---|
| 2579 | n/a | return MV_COMPARE_EX; |
|---|
| 2580 | n/a | } |
|---|
| 2581 | n/a | |
|---|
| 2582 | n/a | /* MV_COMPARE_EX == -1: exceptions are preserved */ |
|---|
| 2583 | n/a | ret = PyObject_RichCompareBool(v, w, Py_EQ); |
|---|
| 2584 | n/a | Py_DECREF(v); |
|---|
| 2585 | n/a | Py_DECREF(w); |
|---|
| 2586 | n/a | |
|---|
| 2587 | n/a | return ret; |
|---|
| 2588 | n/a | } |
|---|
| 2589 | n/a | |
|---|
| 2590 | n/a | /* Unpack and compare single items of p and q. If both p and q have the same |
|---|
| 2591 | n/a | single element native format, the comparison uses a fast path (gcc creates |
|---|
| 2592 | n/a | a jump table and converts memcpy into simple assignments on x86/x64). |
|---|
| 2593 | n/a | |
|---|
| 2594 | n/a | Otherwise, the comparison is delegated to the struct module, which is |
|---|
| 2595 | n/a | 30-60x slower. */ |
|---|
| 2596 | n/a | #define CMP_SINGLE(p, q, type) \ |
|---|
| 2597 | n/a | do { \ |
|---|
| 2598 | n/a | type x; \ |
|---|
| 2599 | n/a | type y; \ |
|---|
| 2600 | n/a | memcpy((char *)&x, p, sizeof x); \ |
|---|
| 2601 | n/a | memcpy((char *)&y, q, sizeof y); \ |
|---|
| 2602 | n/a | equal = (x == y); \ |
|---|
| 2603 | n/a | } while (0) |
|---|
| 2604 | n/a | |
|---|
| 2605 | n/a | static inline int |
|---|
| 2606 | n/a | unpack_cmp(const char *p, const char *q, char fmt, |
|---|
| 2607 | n/a | struct unpacker *unpack_p, struct unpacker *unpack_q) |
|---|
| 2608 | n/a | { |
|---|
| 2609 | n/a | int equal; |
|---|
| 2610 | n/a | |
|---|
| 2611 | n/a | switch (fmt) { |
|---|
| 2612 | n/a | |
|---|
| 2613 | n/a | /* signed integers and fast path for 'B' */ |
|---|
| 2614 | n/a | case 'B': return *((unsigned char *)p) == *((unsigned char *)q); |
|---|
| 2615 | n/a | case 'b': return *((signed char *)p) == *((signed char *)q); |
|---|
| 2616 | n/a | case 'h': CMP_SINGLE(p, q, short); return equal; |
|---|
| 2617 | n/a | case 'i': CMP_SINGLE(p, q, int); return equal; |
|---|
| 2618 | n/a | case 'l': CMP_SINGLE(p, q, long); return equal; |
|---|
| 2619 | n/a | |
|---|
| 2620 | n/a | /* boolean */ |
|---|
| 2621 | n/a | case '?': CMP_SINGLE(p, q, _Bool); return equal; |
|---|
| 2622 | n/a | |
|---|
| 2623 | n/a | /* unsigned integers */ |
|---|
| 2624 | n/a | case 'H': CMP_SINGLE(p, q, unsigned short); return equal; |
|---|
| 2625 | n/a | case 'I': CMP_SINGLE(p, q, unsigned int); return equal; |
|---|
| 2626 | n/a | case 'L': CMP_SINGLE(p, q, unsigned long); return equal; |
|---|
| 2627 | n/a | |
|---|
| 2628 | n/a | /* native 64-bit */ |
|---|
| 2629 | n/a | case 'q': CMP_SINGLE(p, q, long long); return equal; |
|---|
| 2630 | n/a | case 'Q': CMP_SINGLE(p, q, unsigned long long); return equal; |
|---|
| 2631 | n/a | |
|---|
| 2632 | n/a | /* ssize_t and size_t */ |
|---|
| 2633 | n/a | case 'n': CMP_SINGLE(p, q, Py_ssize_t); return equal; |
|---|
| 2634 | n/a | case 'N': CMP_SINGLE(p, q, size_t); return equal; |
|---|
| 2635 | n/a | |
|---|
| 2636 | n/a | /* floats */ |
|---|
| 2637 | n/a | /* XXX DBL_EPSILON? */ |
|---|
| 2638 | n/a | case 'f': CMP_SINGLE(p, q, float); return equal; |
|---|
| 2639 | n/a | case 'd': CMP_SINGLE(p, q, double); return equal; |
|---|
| 2640 | n/a | |
|---|
| 2641 | n/a | /* bytes object */ |
|---|
| 2642 | n/a | case 'c': return *p == *q; |
|---|
| 2643 | n/a | |
|---|
| 2644 | n/a | /* pointer */ |
|---|
| 2645 | n/a | case 'P': CMP_SINGLE(p, q, void *); return equal; |
|---|
| 2646 | n/a | |
|---|
| 2647 | n/a | /* use the struct module */ |
|---|
| 2648 | n/a | case '_': |
|---|
| 2649 | n/a | assert(unpack_p); |
|---|
| 2650 | n/a | assert(unpack_q); |
|---|
| 2651 | n/a | return struct_unpack_cmp(p, q, unpack_p, unpack_q); |
|---|
| 2652 | n/a | } |
|---|
| 2653 | n/a | |
|---|
| 2654 | n/a | /* NOT REACHED */ |
|---|
| 2655 | n/a | PyErr_SetString(PyExc_RuntimeError, |
|---|
| 2656 | n/a | "memoryview: internal error in richcompare"); |
|---|
| 2657 | n/a | return MV_COMPARE_EX; |
|---|
| 2658 | n/a | } |
|---|
| 2659 | n/a | |
|---|
| 2660 | n/a | /* Base case for recursive array comparisons. Assumption: ndim == 1. */ |
|---|
| 2661 | n/a | static int |
|---|
| 2662 | n/a | cmp_base(const char *p, const char *q, const Py_ssize_t *shape, |
|---|
| 2663 | n/a | const Py_ssize_t *pstrides, const Py_ssize_t *psuboffsets, |
|---|
| 2664 | n/a | const Py_ssize_t *qstrides, const Py_ssize_t *qsuboffsets, |
|---|
| 2665 | n/a | char fmt, struct unpacker *unpack_p, struct unpacker *unpack_q) |
|---|
| 2666 | n/a | { |
|---|
| 2667 | n/a | Py_ssize_t i; |
|---|
| 2668 | n/a | int equal; |
|---|
| 2669 | n/a | |
|---|
| 2670 | n/a | for (i = 0; i < shape[0]; p+=pstrides[0], q+=qstrides[0], i++) { |
|---|
| 2671 | n/a | const char *xp = ADJUST_PTR(p, psuboffsets, 0); |
|---|
| 2672 | n/a | const char *xq = ADJUST_PTR(q, qsuboffsets, 0); |
|---|
| 2673 | n/a | equal = unpack_cmp(xp, xq, fmt, unpack_p, unpack_q); |
|---|
| 2674 | n/a | if (equal <= 0) |
|---|
| 2675 | n/a | return equal; |
|---|
| 2676 | n/a | } |
|---|
| 2677 | n/a | |
|---|
| 2678 | n/a | return 1; |
|---|
| 2679 | n/a | } |
|---|
| 2680 | n/a | |
|---|
| 2681 | n/a | /* Recursively compare two multi-dimensional arrays that have the same |
|---|
| 2682 | n/a | logical structure. Assumption: ndim >= 1. */ |
|---|
| 2683 | n/a | static int |
|---|
| 2684 | n/a | cmp_rec(const char *p, const char *q, |
|---|
| 2685 | n/a | Py_ssize_t ndim, const Py_ssize_t *shape, |
|---|
| 2686 | n/a | const Py_ssize_t *pstrides, const Py_ssize_t *psuboffsets, |
|---|
| 2687 | n/a | const Py_ssize_t *qstrides, const Py_ssize_t *qsuboffsets, |
|---|
| 2688 | n/a | char fmt, struct unpacker *unpack_p, struct unpacker *unpack_q) |
|---|
| 2689 | n/a | { |
|---|
| 2690 | n/a | Py_ssize_t i; |
|---|
| 2691 | n/a | int equal; |
|---|
| 2692 | n/a | |
|---|
| 2693 | n/a | assert(ndim >= 1); |
|---|
| 2694 | n/a | assert(shape != NULL); |
|---|
| 2695 | n/a | assert(pstrides != NULL); |
|---|
| 2696 | n/a | assert(qstrides != NULL); |
|---|
| 2697 | n/a | |
|---|
| 2698 | n/a | if (ndim == 1) { |
|---|
| 2699 | n/a | return cmp_base(p, q, shape, |
|---|
| 2700 | n/a | pstrides, psuboffsets, |
|---|
| 2701 | n/a | qstrides, qsuboffsets, |
|---|
| 2702 | n/a | fmt, unpack_p, unpack_q); |
|---|
| 2703 | n/a | } |
|---|
| 2704 | n/a | |
|---|
| 2705 | n/a | for (i = 0; i < shape[0]; p+=pstrides[0], q+=qstrides[0], i++) { |
|---|
| 2706 | n/a | const char *xp = ADJUST_PTR(p, psuboffsets, 0); |
|---|
| 2707 | n/a | const char *xq = ADJUST_PTR(q, qsuboffsets, 0); |
|---|
| 2708 | n/a | equal = cmp_rec(xp, xq, ndim-1, shape+1, |
|---|
| 2709 | n/a | pstrides+1, psuboffsets ? psuboffsets+1 : NULL, |
|---|
| 2710 | n/a | qstrides+1, qsuboffsets ? qsuboffsets+1 : NULL, |
|---|
| 2711 | n/a | fmt, unpack_p, unpack_q); |
|---|
| 2712 | n/a | if (equal <= 0) |
|---|
| 2713 | n/a | return equal; |
|---|
| 2714 | n/a | } |
|---|
| 2715 | n/a | |
|---|
| 2716 | n/a | return 1; |
|---|
| 2717 | n/a | } |
|---|
| 2718 | n/a | |
|---|
| 2719 | n/a | static PyObject * |
|---|
| 2720 | n/a | memory_richcompare(PyObject *v, PyObject *w, int op) |
|---|
| 2721 | n/a | { |
|---|
| 2722 | n/a | PyObject *res; |
|---|
| 2723 | n/a | Py_buffer wbuf, *vv; |
|---|
| 2724 | n/a | Py_buffer *ww = NULL; |
|---|
| 2725 | n/a | struct unpacker *unpack_v = NULL; |
|---|
| 2726 | n/a | struct unpacker *unpack_w = NULL; |
|---|
| 2727 | n/a | char vfmt, wfmt; |
|---|
| 2728 | n/a | int equal = MV_COMPARE_NOT_IMPL; |
|---|
| 2729 | n/a | |
|---|
| 2730 | n/a | if (op != Py_EQ && op != Py_NE) |
|---|
| 2731 | n/a | goto result; /* Py_NotImplemented */ |
|---|
| 2732 | n/a | |
|---|
| 2733 | n/a | assert(PyMemoryView_Check(v)); |
|---|
| 2734 | n/a | if (BASE_INACCESSIBLE(v)) { |
|---|
| 2735 | n/a | equal = (v == w); |
|---|
| 2736 | n/a | goto result; |
|---|
| 2737 | n/a | } |
|---|
| 2738 | n/a | vv = VIEW_ADDR(v); |
|---|
| 2739 | n/a | |
|---|
| 2740 | n/a | if (PyMemoryView_Check(w)) { |
|---|
| 2741 | n/a | if (BASE_INACCESSIBLE(w)) { |
|---|
| 2742 | n/a | equal = (v == w); |
|---|
| 2743 | n/a | goto result; |
|---|
| 2744 | n/a | } |
|---|
| 2745 | n/a | ww = VIEW_ADDR(w); |
|---|
| 2746 | n/a | } |
|---|
| 2747 | n/a | else { |
|---|
| 2748 | n/a | if (PyObject_GetBuffer(w, &wbuf, PyBUF_FULL_RO) < 0) { |
|---|
| 2749 | n/a | PyErr_Clear(); |
|---|
| 2750 | n/a | goto result; /* Py_NotImplemented */ |
|---|
| 2751 | n/a | } |
|---|
| 2752 | n/a | ww = &wbuf; |
|---|
| 2753 | n/a | } |
|---|
| 2754 | n/a | |
|---|
| 2755 | n/a | if (!equiv_shape(vv, ww)) { |
|---|
| 2756 | n/a | PyErr_Clear(); |
|---|
| 2757 | n/a | equal = 0; |
|---|
| 2758 | n/a | goto result; |
|---|
| 2759 | n/a | } |
|---|
| 2760 | n/a | |
|---|
| 2761 | n/a | /* Use fast unpacking for identical primitive C type formats. */ |
|---|
| 2762 | n/a | if (get_native_fmtchar(&vfmt, vv->format) < 0) |
|---|
| 2763 | n/a | vfmt = '_'; |
|---|
| 2764 | n/a | if (get_native_fmtchar(&wfmt, ww->format) < 0) |
|---|
| 2765 | n/a | wfmt = '_'; |
|---|
| 2766 | n/a | if (vfmt == '_' || wfmt == '_' || vfmt != wfmt) { |
|---|
| 2767 | n/a | /* Use struct module unpacking. NOTE: Even for equal format strings, |
|---|
| 2768 | n/a | memcmp() cannot be used for item comparison since it would give |
|---|
| 2769 | n/a | incorrect results in the case of NaNs or uninitialized padding |
|---|
| 2770 | n/a | bytes. */ |
|---|
| 2771 | n/a | vfmt = '_'; |
|---|
| 2772 | n/a | unpack_v = struct_get_unpacker(vv->format, vv->itemsize); |
|---|
| 2773 | n/a | if (unpack_v == NULL) { |
|---|
| 2774 | n/a | equal = fix_struct_error_int(); |
|---|
| 2775 | n/a | goto result; |
|---|
| 2776 | n/a | } |
|---|
| 2777 | n/a | unpack_w = struct_get_unpacker(ww->format, ww->itemsize); |
|---|
| 2778 | n/a | if (unpack_w == NULL) { |
|---|
| 2779 | n/a | equal = fix_struct_error_int(); |
|---|
| 2780 | n/a | goto result; |
|---|
| 2781 | n/a | } |
|---|
| 2782 | n/a | } |
|---|
| 2783 | n/a | |
|---|
| 2784 | n/a | if (vv->ndim == 0) { |
|---|
| 2785 | n/a | equal = unpack_cmp(vv->buf, ww->buf, |
|---|
| 2786 | n/a | vfmt, unpack_v, unpack_w); |
|---|
| 2787 | n/a | } |
|---|
| 2788 | n/a | else if (vv->ndim == 1) { |
|---|
| 2789 | n/a | equal = cmp_base(vv->buf, ww->buf, vv->shape, |
|---|
| 2790 | n/a | vv->strides, vv->suboffsets, |
|---|
| 2791 | n/a | ww->strides, ww->suboffsets, |
|---|
| 2792 | n/a | vfmt, unpack_v, unpack_w); |
|---|
| 2793 | n/a | } |
|---|
| 2794 | n/a | else { |
|---|
| 2795 | n/a | equal = cmp_rec(vv->buf, ww->buf, vv->ndim, vv->shape, |
|---|
| 2796 | n/a | vv->strides, vv->suboffsets, |
|---|
| 2797 | n/a | ww->strides, ww->suboffsets, |
|---|
| 2798 | n/a | vfmt, unpack_v, unpack_w); |
|---|
| 2799 | n/a | } |
|---|
| 2800 | n/a | |
|---|
| 2801 | n/a | result: |
|---|
| 2802 | n/a | if (equal < 0) { |
|---|
| 2803 | n/a | if (equal == MV_COMPARE_NOT_IMPL) |
|---|
| 2804 | n/a | res = Py_NotImplemented; |
|---|
| 2805 | n/a | else /* exception */ |
|---|
| 2806 | n/a | res = NULL; |
|---|
| 2807 | n/a | } |
|---|
| 2808 | n/a | else if ((equal && op == Py_EQ) || (!equal && op == Py_NE)) |
|---|
| 2809 | n/a | res = Py_True; |
|---|
| 2810 | n/a | else |
|---|
| 2811 | n/a | res = Py_False; |
|---|
| 2812 | n/a | |
|---|
| 2813 | n/a | if (ww == &wbuf) |
|---|
| 2814 | n/a | PyBuffer_Release(ww); |
|---|
| 2815 | n/a | |
|---|
| 2816 | n/a | unpacker_free(unpack_v); |
|---|
| 2817 | n/a | unpacker_free(unpack_w); |
|---|
| 2818 | n/a | |
|---|
| 2819 | n/a | Py_XINCREF(res); |
|---|
| 2820 | n/a | return res; |
|---|
| 2821 | n/a | } |
|---|
| 2822 | n/a | |
|---|
| 2823 | n/a | /**************************************************************************/ |
|---|
| 2824 | n/a | /* Hash */ |
|---|
| 2825 | n/a | /**************************************************************************/ |
|---|
| 2826 | n/a | |
|---|
| 2827 | n/a | static Py_hash_t |
|---|
| 2828 | n/a | memory_hash(PyMemoryViewObject *self) |
|---|
| 2829 | n/a | { |
|---|
| 2830 | n/a | if (self->hash == -1) { |
|---|
| 2831 | n/a | Py_buffer *view = &self->view; |
|---|
| 2832 | n/a | char *mem = view->buf; |
|---|
| 2833 | n/a | Py_ssize_t ret; |
|---|
| 2834 | n/a | char fmt; |
|---|
| 2835 | n/a | |
|---|
| 2836 | n/a | CHECK_RELEASED_INT(self); |
|---|
| 2837 | n/a | |
|---|
| 2838 | n/a | if (!view->readonly) { |
|---|
| 2839 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 2840 | n/a | "cannot hash writable memoryview object"); |
|---|
| 2841 | n/a | return -1; |
|---|
| 2842 | n/a | } |
|---|
| 2843 | n/a | ret = get_native_fmtchar(&fmt, view->format); |
|---|
| 2844 | n/a | if (ret < 0 || !IS_BYTE_FORMAT(fmt)) { |
|---|
| 2845 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 2846 | n/a | "memoryview: hashing is restricted to formats 'B', 'b' or 'c'"); |
|---|
| 2847 | n/a | return -1; |
|---|
| 2848 | n/a | } |
|---|
| 2849 | n/a | if (view->obj != NULL && PyObject_Hash(view->obj) == -1) { |
|---|
| 2850 | n/a | /* Keep the original error message */ |
|---|
| 2851 | n/a | return -1; |
|---|
| 2852 | n/a | } |
|---|
| 2853 | n/a | |
|---|
| 2854 | n/a | if (!MV_C_CONTIGUOUS(self->flags)) { |
|---|
| 2855 | n/a | mem = PyMem_Malloc(view->len); |
|---|
| 2856 | n/a | if (mem == NULL) { |
|---|
| 2857 | n/a | PyErr_NoMemory(); |
|---|
| 2858 | n/a | return -1; |
|---|
| 2859 | n/a | } |
|---|
| 2860 | n/a | if (buffer_to_contiguous(mem, view, 'C') < 0) { |
|---|
| 2861 | n/a | PyMem_Free(mem); |
|---|
| 2862 | n/a | return -1; |
|---|
| 2863 | n/a | } |
|---|
| 2864 | n/a | } |
|---|
| 2865 | n/a | |
|---|
| 2866 | n/a | /* Can't fail */ |
|---|
| 2867 | n/a | self->hash = _Py_HashBytes(mem, view->len); |
|---|
| 2868 | n/a | |
|---|
| 2869 | n/a | if (mem != view->buf) |
|---|
| 2870 | n/a | PyMem_Free(mem); |
|---|
| 2871 | n/a | } |
|---|
| 2872 | n/a | |
|---|
| 2873 | n/a | return self->hash; |
|---|
| 2874 | n/a | } |
|---|
| 2875 | n/a | |
|---|
| 2876 | n/a | |
|---|
| 2877 | n/a | /**************************************************************************/ |
|---|
| 2878 | n/a | /* getters */ |
|---|
| 2879 | n/a | /**************************************************************************/ |
|---|
| 2880 | n/a | |
|---|
| 2881 | n/a | static PyObject * |
|---|
| 2882 | n/a | _IntTupleFromSsizet(int len, Py_ssize_t *vals) |
|---|
| 2883 | n/a | { |
|---|
| 2884 | n/a | int i; |
|---|
| 2885 | n/a | PyObject *o; |
|---|
| 2886 | n/a | PyObject *intTuple; |
|---|
| 2887 | n/a | |
|---|
| 2888 | n/a | if (vals == NULL) |
|---|
| 2889 | n/a | return PyTuple_New(0); |
|---|
| 2890 | n/a | |
|---|
| 2891 | n/a | intTuple = PyTuple_New(len); |
|---|
| 2892 | n/a | if (!intTuple) |
|---|
| 2893 | n/a | return NULL; |
|---|
| 2894 | n/a | for (i=0; i<len; i++) { |
|---|
| 2895 | n/a | o = PyLong_FromSsize_t(vals[i]); |
|---|
| 2896 | n/a | if (!o) { |
|---|
| 2897 | n/a | Py_DECREF(intTuple); |
|---|
| 2898 | n/a | return NULL; |
|---|
| 2899 | n/a | } |
|---|
| 2900 | n/a | PyTuple_SET_ITEM(intTuple, i, o); |
|---|
| 2901 | n/a | } |
|---|
| 2902 | n/a | return intTuple; |
|---|
| 2903 | n/a | } |
|---|
| 2904 | n/a | |
|---|
| 2905 | n/a | static PyObject * |
|---|
| 2906 | n/a | memory_obj_get(PyMemoryViewObject *self) |
|---|
| 2907 | n/a | { |
|---|
| 2908 | n/a | Py_buffer *view = &self->view; |
|---|
| 2909 | n/a | |
|---|
| 2910 | n/a | CHECK_RELEASED(self); |
|---|
| 2911 | n/a | if (view->obj == NULL) { |
|---|
| 2912 | n/a | Py_RETURN_NONE; |
|---|
| 2913 | n/a | } |
|---|
| 2914 | n/a | Py_INCREF(view->obj); |
|---|
| 2915 | n/a | return view->obj; |
|---|
| 2916 | n/a | } |
|---|
| 2917 | n/a | |
|---|
| 2918 | n/a | static PyObject * |
|---|
| 2919 | n/a | memory_nbytes_get(PyMemoryViewObject *self) |
|---|
| 2920 | n/a | { |
|---|
| 2921 | n/a | CHECK_RELEASED(self); |
|---|
| 2922 | n/a | return PyLong_FromSsize_t(self->view.len); |
|---|
| 2923 | n/a | } |
|---|
| 2924 | n/a | |
|---|
| 2925 | n/a | static PyObject * |
|---|
| 2926 | n/a | memory_format_get(PyMemoryViewObject *self) |
|---|
| 2927 | n/a | { |
|---|
| 2928 | n/a | CHECK_RELEASED(self); |
|---|
| 2929 | n/a | return PyUnicode_FromString(self->view.format); |
|---|
| 2930 | n/a | } |
|---|
| 2931 | n/a | |
|---|
| 2932 | n/a | static PyObject * |
|---|
| 2933 | n/a | memory_itemsize_get(PyMemoryViewObject *self) |
|---|
| 2934 | n/a | { |
|---|
| 2935 | n/a | CHECK_RELEASED(self); |
|---|
| 2936 | n/a | return PyLong_FromSsize_t(self->view.itemsize); |
|---|
| 2937 | n/a | } |
|---|
| 2938 | n/a | |
|---|
| 2939 | n/a | static PyObject * |
|---|
| 2940 | n/a | memory_shape_get(PyMemoryViewObject *self) |
|---|
| 2941 | n/a | { |
|---|
| 2942 | n/a | CHECK_RELEASED(self); |
|---|
| 2943 | n/a | return _IntTupleFromSsizet(self->view.ndim, self->view.shape); |
|---|
| 2944 | n/a | } |
|---|
| 2945 | n/a | |
|---|
| 2946 | n/a | static PyObject * |
|---|
| 2947 | n/a | memory_strides_get(PyMemoryViewObject *self) |
|---|
| 2948 | n/a | { |
|---|
| 2949 | n/a | CHECK_RELEASED(self); |
|---|
| 2950 | n/a | return _IntTupleFromSsizet(self->view.ndim, self->view.strides); |
|---|
| 2951 | n/a | } |
|---|
| 2952 | n/a | |
|---|
| 2953 | n/a | static PyObject * |
|---|
| 2954 | n/a | memory_suboffsets_get(PyMemoryViewObject *self) |
|---|
| 2955 | n/a | { |
|---|
| 2956 | n/a | CHECK_RELEASED(self); |
|---|
| 2957 | n/a | return _IntTupleFromSsizet(self->view.ndim, self->view.suboffsets); |
|---|
| 2958 | n/a | } |
|---|
| 2959 | n/a | |
|---|
| 2960 | n/a | static PyObject * |
|---|
| 2961 | n/a | memory_readonly_get(PyMemoryViewObject *self) |
|---|
| 2962 | n/a | { |
|---|
| 2963 | n/a | CHECK_RELEASED(self); |
|---|
| 2964 | n/a | return PyBool_FromLong(self->view.readonly); |
|---|
| 2965 | n/a | } |
|---|
| 2966 | n/a | |
|---|
| 2967 | n/a | static PyObject * |
|---|
| 2968 | n/a | memory_ndim_get(PyMemoryViewObject *self) |
|---|
| 2969 | n/a | { |
|---|
| 2970 | n/a | CHECK_RELEASED(self); |
|---|
| 2971 | n/a | return PyLong_FromLong(self->view.ndim); |
|---|
| 2972 | n/a | } |
|---|
| 2973 | n/a | |
|---|
| 2974 | n/a | static PyObject * |
|---|
| 2975 | n/a | memory_c_contiguous(PyMemoryViewObject *self, PyObject *dummy) |
|---|
| 2976 | n/a | { |
|---|
| 2977 | n/a | CHECK_RELEASED(self); |
|---|
| 2978 | n/a | return PyBool_FromLong(MV_C_CONTIGUOUS(self->flags)); |
|---|
| 2979 | n/a | } |
|---|
| 2980 | n/a | |
|---|
| 2981 | n/a | static PyObject * |
|---|
| 2982 | n/a | memory_f_contiguous(PyMemoryViewObject *self, PyObject *dummy) |
|---|
| 2983 | n/a | { |
|---|
| 2984 | n/a | CHECK_RELEASED(self); |
|---|
| 2985 | n/a | return PyBool_FromLong(MV_F_CONTIGUOUS(self->flags)); |
|---|
| 2986 | n/a | } |
|---|
| 2987 | n/a | |
|---|
| 2988 | n/a | static PyObject * |
|---|
| 2989 | n/a | memory_contiguous(PyMemoryViewObject *self, PyObject *dummy) |
|---|
| 2990 | n/a | { |
|---|
| 2991 | n/a | CHECK_RELEASED(self); |
|---|
| 2992 | n/a | return PyBool_FromLong(MV_ANY_CONTIGUOUS(self->flags)); |
|---|
| 2993 | n/a | } |
|---|
| 2994 | n/a | |
|---|
| 2995 | n/a | PyDoc_STRVAR(memory_obj_doc, |
|---|
| 2996 | n/a | "The underlying object of the memoryview."); |
|---|
| 2997 | n/a | PyDoc_STRVAR(memory_nbytes_doc, |
|---|
| 2998 | n/a | "The amount of space in bytes that the array would use in\n" |
|---|
| 2999 | n/a | " a contiguous representation."); |
|---|
| 3000 | n/a | PyDoc_STRVAR(memory_readonly_doc, |
|---|
| 3001 | n/a | "A bool indicating whether the memory is read only."); |
|---|
| 3002 | n/a | PyDoc_STRVAR(memory_itemsize_doc, |
|---|
| 3003 | n/a | "The size in bytes of each element of the memoryview."); |
|---|
| 3004 | n/a | PyDoc_STRVAR(memory_format_doc, |
|---|
| 3005 | n/a | "A string containing the format (in struct module style)\n" |
|---|
| 3006 | n/a | " for each element in the view."); |
|---|
| 3007 | n/a | PyDoc_STRVAR(memory_ndim_doc, |
|---|
| 3008 | n/a | "An integer indicating how many dimensions of a multi-dimensional\n" |
|---|
| 3009 | n/a | " array the memory represents."); |
|---|
| 3010 | n/a | PyDoc_STRVAR(memory_shape_doc, |
|---|
| 3011 | n/a | "A tuple of ndim integers giving the shape of the memory\n" |
|---|
| 3012 | n/a | " as an N-dimensional array."); |
|---|
| 3013 | n/a | PyDoc_STRVAR(memory_strides_doc, |
|---|
| 3014 | n/a | "A tuple of ndim integers giving the size in bytes to access\n" |
|---|
| 3015 | n/a | " each element for each dimension of the array."); |
|---|
| 3016 | n/a | PyDoc_STRVAR(memory_suboffsets_doc, |
|---|
| 3017 | n/a | "A tuple of integers used internally for PIL-style arrays."); |
|---|
| 3018 | n/a | PyDoc_STRVAR(memory_c_contiguous_doc, |
|---|
| 3019 | n/a | "A bool indicating whether the memory is C contiguous."); |
|---|
| 3020 | n/a | PyDoc_STRVAR(memory_f_contiguous_doc, |
|---|
| 3021 | n/a | "A bool indicating whether the memory is Fortran contiguous."); |
|---|
| 3022 | n/a | PyDoc_STRVAR(memory_contiguous_doc, |
|---|
| 3023 | n/a | "A bool indicating whether the memory is contiguous."); |
|---|
| 3024 | n/a | |
|---|
| 3025 | n/a | |
|---|
| 3026 | n/a | static PyGetSetDef memory_getsetlist[] = { |
|---|
| 3027 | n/a | {"obj", (getter)memory_obj_get, NULL, memory_obj_doc}, |
|---|
| 3028 | n/a | {"nbytes", (getter)memory_nbytes_get, NULL, memory_nbytes_doc}, |
|---|
| 3029 | n/a | {"readonly", (getter)memory_readonly_get, NULL, memory_readonly_doc}, |
|---|
| 3030 | n/a | {"itemsize", (getter)memory_itemsize_get, NULL, memory_itemsize_doc}, |
|---|
| 3031 | n/a | {"format", (getter)memory_format_get, NULL, memory_format_doc}, |
|---|
| 3032 | n/a | {"ndim", (getter)memory_ndim_get, NULL, memory_ndim_doc}, |
|---|
| 3033 | n/a | {"shape", (getter)memory_shape_get, NULL, memory_shape_doc}, |
|---|
| 3034 | n/a | {"strides", (getter)memory_strides_get, NULL, memory_strides_doc}, |
|---|
| 3035 | n/a | {"suboffsets", (getter)memory_suboffsets_get, NULL, memory_suboffsets_doc}, |
|---|
| 3036 | n/a | {"c_contiguous", (getter)memory_c_contiguous, NULL, memory_c_contiguous_doc}, |
|---|
| 3037 | n/a | {"f_contiguous", (getter)memory_f_contiguous, NULL, memory_f_contiguous_doc}, |
|---|
| 3038 | n/a | {"contiguous", (getter)memory_contiguous, NULL, memory_contiguous_doc}, |
|---|
| 3039 | n/a | {NULL, NULL, NULL, NULL}, |
|---|
| 3040 | n/a | }; |
|---|
| 3041 | n/a | |
|---|
| 3042 | n/a | PyDoc_STRVAR(memory_release_doc, |
|---|
| 3043 | n/a | "release($self, /)\n--\n\ |
|---|
| 3044 | n/a | \n\ |
|---|
| 3045 | n/a | Release the underlying buffer exposed by the memoryview object."); |
|---|
| 3046 | n/a | PyDoc_STRVAR(memory_tobytes_doc, |
|---|
| 3047 | n/a | "tobytes($self, /)\n--\n\ |
|---|
| 3048 | n/a | \n\ |
|---|
| 3049 | n/a | Return the data in the buffer as a byte string."); |
|---|
| 3050 | n/a | PyDoc_STRVAR(memory_hex_doc, |
|---|
| 3051 | n/a | "hex($self, /)\n--\n\ |
|---|
| 3052 | n/a | \n\ |
|---|
| 3053 | n/a | Return the data in the buffer as a string of hexadecimal numbers."); |
|---|
| 3054 | n/a | PyDoc_STRVAR(memory_tolist_doc, |
|---|
| 3055 | n/a | "tolist($self, /)\n--\n\ |
|---|
| 3056 | n/a | \n\ |
|---|
| 3057 | n/a | Return the data in the buffer as a list of elements."); |
|---|
| 3058 | n/a | PyDoc_STRVAR(memory_cast_doc, |
|---|
| 3059 | n/a | "cast($self, /, format, *, shape)\n--\n\ |
|---|
| 3060 | n/a | \n\ |
|---|
| 3061 | n/a | Cast a memoryview to a new format or shape."); |
|---|
| 3062 | n/a | |
|---|
| 3063 | n/a | static PyMethodDef memory_methods[] = { |
|---|
| 3064 | n/a | {"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc}, |
|---|
| 3065 | n/a | {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, memory_tobytes_doc}, |
|---|
| 3066 | n/a | {"hex", (PyCFunction)memory_hex, METH_NOARGS, memory_hex_doc}, |
|---|
| 3067 | n/a | {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc}, |
|---|
| 3068 | n/a | {"cast", (PyCFunction)memory_cast, METH_VARARGS|METH_KEYWORDS, memory_cast_doc}, |
|---|
| 3069 | n/a | {"__enter__", memory_enter, METH_NOARGS, NULL}, |
|---|
| 3070 | n/a | {"__exit__", memory_exit, METH_VARARGS, NULL}, |
|---|
| 3071 | n/a | {NULL, NULL} |
|---|
| 3072 | n/a | }; |
|---|
| 3073 | n/a | |
|---|
| 3074 | n/a | |
|---|
| 3075 | n/a | PyTypeObject PyMemoryView_Type = { |
|---|
| 3076 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 3077 | n/a | "memoryview", /* tp_name */ |
|---|
| 3078 | n/a | offsetof(PyMemoryViewObject, ob_array), /* tp_basicsize */ |
|---|
| 3079 | n/a | sizeof(Py_ssize_t), /* tp_itemsize */ |
|---|
| 3080 | n/a | (destructor)memory_dealloc, /* tp_dealloc */ |
|---|
| 3081 | n/a | 0, /* tp_print */ |
|---|
| 3082 | n/a | 0, /* tp_getattr */ |
|---|
| 3083 | n/a | 0, /* tp_setattr */ |
|---|
| 3084 | n/a | 0, /* tp_reserved */ |
|---|
| 3085 | n/a | (reprfunc)memory_repr, /* tp_repr */ |
|---|
| 3086 | n/a | 0, /* tp_as_number */ |
|---|
| 3087 | n/a | &memory_as_sequence, /* tp_as_sequence */ |
|---|
| 3088 | n/a | &memory_as_mapping, /* tp_as_mapping */ |
|---|
| 3089 | n/a | (hashfunc)memory_hash, /* tp_hash */ |
|---|
| 3090 | n/a | 0, /* tp_call */ |
|---|
| 3091 | n/a | 0, /* tp_str */ |
|---|
| 3092 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 3093 | n/a | 0, /* tp_setattro */ |
|---|
| 3094 | n/a | &memory_as_buffer, /* tp_as_buffer */ |
|---|
| 3095 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 3096 | n/a | memory_doc, /* tp_doc */ |
|---|
| 3097 | n/a | (traverseproc)memory_traverse, /* tp_traverse */ |
|---|
| 3098 | n/a | (inquiry)memory_clear, /* tp_clear */ |
|---|
| 3099 | n/a | memory_richcompare, /* tp_richcompare */ |
|---|
| 3100 | n/a | offsetof(PyMemoryViewObject, weakreflist),/* tp_weaklistoffset */ |
|---|
| 3101 | n/a | 0, /* tp_iter */ |
|---|
| 3102 | n/a | 0, /* tp_iternext */ |
|---|
| 3103 | n/a | memory_methods, /* tp_methods */ |
|---|
| 3104 | n/a | 0, /* tp_members */ |
|---|
| 3105 | n/a | memory_getsetlist, /* tp_getset */ |
|---|
| 3106 | n/a | 0, /* tp_base */ |
|---|
| 3107 | n/a | 0, /* tp_dict */ |
|---|
| 3108 | n/a | 0, /* tp_descr_get */ |
|---|
| 3109 | n/a | 0, /* tp_descr_set */ |
|---|
| 3110 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3111 | n/a | 0, /* tp_init */ |
|---|
| 3112 | n/a | 0, /* tp_alloc */ |
|---|
| 3113 | n/a | memory_new, /* tp_new */ |
|---|
| 3114 | n/a | }; |
|---|