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

Function _buffer_info_new

numpy/core/src/multiarray/buffer.c:459–567  ·  view source on GitHub ↗

Fill in the info structure */

Source from the content-addressed store, hash-verified

457
458/* Fill in the info structure */
459static _buffer_info_t*
460_buffer_info_new(PyObject *obj, int flags)
461{
462 /*
463 * Note that the buffer info is cached as PyLongObjects making them appear
464 * like unreachable lost memory to valgrind.
465 */
466 _buffer_info_t *info;
467 _tmp_string_t fmt = {NULL, 0, 0};
468 int k;
469 PyArray_Descr *descr = NULL;
470 int err = 0;
471
472 if (PyArray_IsScalar(obj, Void)) {
473 info = PyObject_Malloc(sizeof(_buffer_info_t));
474 if (info == NULL) {
475 PyErr_NoMemory();
476 goto fail;
477 }
478 info->ndim = 0;
479 info->shape = NULL;
480 info->strides = NULL;
481
482 descr = PyArray_DescrFromScalar(obj);
483 if (descr == NULL) {
484 goto fail;
485 }
486 }
487 else {
488 assert(PyArray_Check(obj));
489 PyArrayObject * arr = (PyArrayObject *)obj;
490
491 info = PyObject_Malloc(sizeof(_buffer_info_t) +
492 sizeof(Py_ssize_t) * PyArray_NDIM(arr) * 2);
493 if (info == NULL) {
494 PyErr_NoMemory();
495 goto fail;
496 }
497 /* Fill in shape and strides */
498 info->ndim = PyArray_NDIM(arr);
499
500 if (info->ndim == 0) {
501 info->shape = NULL;
502 info->strides = NULL;
503 }
504 else {
505 info->shape = (npy_intp *)((char *)info + sizeof(_buffer_info_t));
506 assert((size_t)info->shape % sizeof(npy_intp) == 0);
507 info->strides = info->shape + PyArray_NDIM(arr);
508
509 /*
510 * Some buffer users may expect a contiguous buffer to have well
511 * formatted strides also when a dimension is 1, but we do not
512 * guarantee this internally. Thus, recalculate strides for
513 * contiguous arrays.
514 */
515 int f_contiguous = (flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS;
516 if (PyArray_IS_C_CONTIGUOUS(arr) && !(

Callers 1

_buffer_get_infoFunction · 0.85

Calls 8

PyArray_DescrFromScalarFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_ITEMSIZEFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_DESCRFunction · 0.85
_buffer_format_stringFunction · 0.85
_append_charFunction · 0.85

Tested by

no test coverage detected