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

Function PyArray_Item_INCREF

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

NUMPY_API * XINCREF all objects in a single array item. This is complicated for * structured datatypes where the position of objects needs to be extracted. * The function is execute recursively for each nested field or subarrays dtype * such as as `np.dtype([("field1", "O"), ("field2", "f,O", (3,2))])` */

Source from the content-addressed store, hash-verified

120 * such as as `np.dtype([("field1", "O"), ("field2", "f,O", (3,2))])`
121 */
122NPY_NO_EXPORT void
123PyArray_Item_INCREF(char *data, PyArray_Descr *descr)
124{
125 PyObject *temp;
126
127 if (!PyDataType_REFCHK(descr)) {
128 return;
129 }
130 if (descr->type_num == NPY_OBJECT) {
131 memcpy(&temp, data, sizeof(temp));
132 Py_XINCREF(temp);
133 }
134 else if (PyDataType_HASFIELDS(descr)) {
135 PyObject *key, *value, *title = NULL;
136 PyArray_Descr *new;
137 int offset;
138 Py_ssize_t pos = 0;
139
140 while (PyDict_Next(descr->fields, &pos, &key, &value)) {
141 if (NPY_TITLE_KEY(key, value)) {
142 continue;
143 }
144 if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset,
145 &title)) {
146 return;
147 }
148 PyArray_Item_INCREF(data + offset, new);
149 }
150 }
151 else if (PyDataType_HASSUBARRAY(descr)) {
152 int size, i, inner_elsize;
153
154 inner_elsize = descr->subarray->base->elsize;
155 if (inner_elsize == 0) {
156 /* There cannot be any elements, so return */
157 return;
158 }
159 /* Subarrays are always contiguous in memory */
160 size = descr->elsize / inner_elsize;
161
162 for (i = 0; i < size; i++){
163 /* Recursively increment the reference count of subarray elements */
164 PyArray_Item_INCREF(data + i * inner_elsize,
165 descr->subarray->base);
166 }
167 }
168 else {
169 /* This path should not be reachable. */
170 assert(0);
171 }
172 return;
173}
174
175
176/*NUMPY_API

Callers 3

array_flat_setFunction · 0.85
PyArray_FromScalarFunction · 0.85
PyArray_INCREFFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected