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

Function _fillobject

numpy/core/src/multiarray/refcount.c:400–463  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

398}
399
400static void
401_fillobject(char *optr, PyObject *obj, PyArray_Descr *dtype)
402{
403 if (!PyDataType_FLAGCHK(dtype, NPY_ITEM_REFCOUNT)) {
404 PyObject *arr;
405
406 if ((obj == Py_None) ||
407 (PyLong_Check(obj) && PyLong_AsLong(obj) == 0)) {
408 return;
409 }
410 /* Clear possible long conversion error */
411 PyErr_Clear();
412 Py_INCREF(dtype);
413 arr = PyArray_NewFromDescr(&PyArray_Type, dtype,
414 0, NULL, NULL, NULL,
415 0, NULL);
416 if (arr!=NULL) {
417 dtype->f->setitem(obj, optr, arr);
418 }
419 Py_XDECREF(arr);
420 }
421 if (dtype->type_num == NPY_OBJECT) {
422 Py_XINCREF(obj);
423 memcpy(optr, &obj, sizeof(obj));
424 }
425 else if (PyDataType_HASFIELDS(dtype)) {
426 PyObject *key, *value, *title = NULL;
427 PyArray_Descr *new;
428 int offset;
429 Py_ssize_t pos = 0;
430
431 while (PyDict_Next(dtype->fields, &pos, &key, &value)) {
432 if (NPY_TITLE_KEY(key, value)) {
433 continue;
434 }
435 if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset, &title)) {
436 return;
437 }
438 _fillobject(optr + offset, obj, new);
439 }
440 }
441 else if (PyDataType_HASSUBARRAY(dtype)) {
442 int size, i, inner_elsize;
443
444 inner_elsize = dtype->subarray->base->elsize;
445 if (inner_elsize == 0) {
446 /* There cannot be any elements, so return */
447 return;
448 }
449 /* Subarrays are always contiguous in memory */
450 size = dtype->elsize / inner_elsize;
451
452 /* Call _fillobject on each item recursively. */
453 for (i = 0; i < size; i++){
454 _fillobject(optr, obj, dtype->subarray->base);
455 optr += inner_elsize;
456 }
457 }

Callers 1

PyArray_FillObjectArrayFunction · 0.85

Calls 1

PyArray_NewFromDescrFunction · 0.85

Tested by

no test coverage detected