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

Function PyArray_FromIter

numpy/core/src/multiarray/ctors.c:3800–3949  ·  view source on GitHub ↗

NUMPY_API * * steals a reference to dtype (which cannot be NULL) */

Source from the content-addressed store, hash-verified

3798 * steals a reference to dtype (which cannot be NULL)
3799 */
3800NPY_NO_EXPORT PyObject *
3801PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count)
3802{
3803 PyObject *iter = NULL;
3804 PyArrayObject *ret = NULL;
3805 npy_intp i, elsize, elcount;
3806
3807 if (dtype == NULL) {
3808 return NULL;
3809 }
3810
3811 iter = PyObject_GetIter(obj);
3812 if (iter == NULL) {
3813 goto done;
3814 }
3815
3816 if (PyDataType_ISUNSIZED(dtype)) {
3817 /* If this error is removed, the `ret` allocation may need fixing */
3818 PyErr_SetString(PyExc_ValueError,
3819 "Must specify length when using variable-size data-type.");
3820 goto done;
3821 }
3822 if (count < 0) {
3823 elcount = PyObject_LengthHint(obj, 0);
3824 if (elcount < 0) {
3825 goto done;
3826 }
3827 }
3828 else {
3829 elcount = count;
3830 }
3831
3832 elsize = dtype->elsize;
3833
3834 /*
3835 * Note that PyArray_DESCR(ret) may not match dtype. There are exactly
3836 * two cases where this can happen: empty strings/bytes/void (rejected
3837 * above) and subarray dtypes (supported by sticking with `dtype`).
3838 */
3839 Py_INCREF(dtype);
3840 ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype, 1,
3841 &elcount, NULL,NULL, 0, NULL);
3842 if (ret == NULL) {
3843 goto done;
3844 }
3845
3846 char *item = PyArray_BYTES(ret);
3847 for (i = 0; i < count || count == -1; i++, item += elsize) {
3848 PyObject *value = PyIter_Next(iter);
3849 if (value == NULL) {
3850 if (PyErr_Occurred()) {
3851 /* Fetching next item failed perhaps due to exhausting iterator */
3852 goto done;
3853 }
3854 break;
3855 }
3856
3857 if (NPY_UNLIKELY(i >= elcount) && elsize != 0) {

Callers 1

array_fromiterFunction · 0.85

Calls 11

PyArray_NewFromDescrFunction · 0.85
PyArray_BYTESFunction · 0.85
PyDataMem_UserRENEWFunction · 0.85
PyArray_HANDLERFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_PackFunction · 0.85
PyArray_DATAFunction · 0.85
_array_fill_stridesFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_ITEMSIZEFunction · 0.85

Tested by

no test coverage detected