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

Function PyArray_FillWithScalar

numpy/core/src/multiarray/convert.c:385–432  ·  view source on GitHub ↗

NUMPY_API*/

Source from the content-addressed store, hash-verified

383
384/*NUMPY_API*/
385NPY_NO_EXPORT int
386PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj)
387{
388
389 if (PyArray_FailUnlessWriteable(arr, "assignment destination") < 0) {
390 return -1;
391 }
392
393 /*
394 * If we knew that the output array has at least one element, we would
395 * not actually need a helping buffer, we always null it, just in case.
396 *
397 * (The longlong here should help with alignment.)
398 */
399 npy_longlong value_buffer_stack[4] = {0};
400 char *value_buffer_heap = NULL;
401 char *value = (char *)value_buffer_stack;
402 PyArray_Descr *descr = PyArray_DESCR(arr);
403
404 if ((size_t)descr->elsize > sizeof(value_buffer_stack)) {
405 /* We need a large temporary buffer... */
406 value_buffer_heap = PyObject_Calloc(1, descr->elsize);
407 if (value_buffer_heap == NULL) {
408 PyErr_NoMemory();
409 return -1;
410 }
411 value = value_buffer_heap;
412 }
413 if (PyArray_Pack(descr, value, obj) < 0) {
414 PyMem_FREE(value_buffer_heap);
415 return -1;
416 }
417
418 /*
419 * There is no cast anymore, the above already coerced using scalar
420 * coercion rules
421 */
422 int retcode = raw_array_assign_scalar(
423 PyArray_NDIM(arr), PyArray_DIMS(arr), descr,
424 PyArray_BYTES(arr), PyArray_STRIDES(arr),
425 descr, value);
426
427 if (PyDataType_REFCHK(descr)) {
428 PyArray_ClearBuffer(descr, value, 0, 1, 1);
429 }
430 PyMem_FREE(value_buffer_heap);
431 return retcode;
432}
433
434/*
435 * Internal function to fill an array with zeros.

Callers 1

array_fillFunction · 0.85

Calls 9

PyArray_DESCRFunction · 0.85
PyArray_PackFunction · 0.85
raw_array_assign_scalarFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_BYTESFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_ClearBufferFunction · 0.85

Tested by

no test coverage detected