MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_BufferConverter

Function PyArray_BufferConverter

numpy/core/src/multiarray/conversion_utils.c:275–316  ·  view source on GitHub ↗

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... */

Source from the content-addressed store, hash-verified

273 * memory...
274 */
275NPY_NO_EXPORT int
276PyArray_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,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected