NUMPY_API * Get buffer chunk from object * * this function takes a Python object which exposes the (single-segment) * buffer interface and returns a pointer to the data segment * * You should increment the reference count by one of buf->base * if you will hang on to a reference * * You only get a borrowed reference to the object. Do not free the * memory... */
| 273 | * memory... |
| 274 | */ |
| 275 | NPY_NO_EXPORT int |
| 276 | PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf) |
| 277 | { |
| 278 | Py_buffer view; |
| 279 | |
| 280 | buf->ptr = NULL; |
| 281 | buf->flags = NPY_ARRAY_BEHAVED; |
| 282 | buf->base = NULL; |
| 283 | if (obj == Py_None) { |
| 284 | return NPY_SUCCEED; |
| 285 | } |
| 286 | |
| 287 | if (PyObject_GetBuffer(obj, &view, |
| 288 | PyBUF_ANY_CONTIGUOUS|PyBUF_WRITABLE|PyBUF_SIMPLE) != 0) { |
| 289 | PyErr_Clear(); |
| 290 | buf->flags &= ~NPY_ARRAY_WRITEABLE; |
| 291 | if (PyObject_GetBuffer(obj, &view, |
| 292 | PyBUF_ANY_CONTIGUOUS|PyBUF_SIMPLE) != 0) { |
| 293 | return NPY_FAIL; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | buf->ptr = view.buf; |
| 298 | buf->len = (npy_intp) view.len; |
| 299 | |
| 300 | /* |
| 301 | * In Python 3 both of the deprecated functions PyObject_AsWriteBuffer and |
| 302 | * PyObject_AsReadBuffer that this code replaces release the buffer. It is |
| 303 | * up to the object that supplies the buffer to guarantee that the buffer |
| 304 | * sticks around after the release. |
| 305 | */ |
| 306 | PyBuffer_Release(&view); |
| 307 | |
| 308 | /* Point to the base of the buffer object if present */ |
| 309 | if (PyMemoryView_Check(obj)) { |
| 310 | buf->base = PyMemoryView_GET_BASE(obj); |
| 311 | } |
| 312 | if (buf->base == NULL) { |
| 313 | buf->base = obj; |
| 314 | } |
| 315 | return NPY_SUCCEED; |
| 316 | } |
| 317 | |
| 318 | /*NUMPY_API |
| 319 | * Get axis from an object (possibly None) -- a converter function, |
nothing calls this directly
no outgoing calls
no test coverage detected