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

Function _discover_array_parameters

numpy/core/src/multiarray/array_coercion.c:1363–1408  ·  view source on GitHub ↗

* Python API function to expose the dtype+shape discovery functionality * directly. */

Source from the content-addressed store, hash-verified

1361 * directly.
1362 */
1363NPY_NO_EXPORT PyObject *
1364_discover_array_parameters(PyObject *NPY_UNUSED(self),
1365 PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
1366{
1367 PyObject *obj;
1368 npy_dtype_info dt_info = {NULL, NULL};
1369 npy_intp shape[NPY_MAXDIMS];
1370
1371 NPY_PREPARE_ARGPARSER;
1372 if (npy_parse_arguments(
1373 "_discover_array_parameters", args, len_args, kwnames,
1374 "", NULL, &obj,
1375 "|dtype", &PyArray_DTypeOrDescrConverterOptional, &dt_info,
1376 NULL, NULL, NULL) < 0) {
1377 /* fixed is last to parse, so never necessary to clean up */
1378 return NULL;
1379 }
1380
1381 coercion_cache_obj *coercion_cache = NULL;
1382 PyObject *out_dtype = NULL;
1383 int ndim = PyArray_DiscoverDTypeAndShape(
1384 obj, NPY_MAXDIMS, shape,
1385 &coercion_cache,
1386 dt_info.dtype, dt_info.descr, (PyArray_Descr **)&out_dtype, 0);
1387 Py_XDECREF(dt_info.dtype);
1388 Py_XDECREF(dt_info.descr);
1389 if (ndim < 0) {
1390 return NULL;
1391 }
1392 npy_free_coercion_cache(coercion_cache);
1393 if (out_dtype == NULL) {
1394 /* Empty sequence, report this as None. */
1395 out_dtype = Py_None;
1396 Py_INCREF(Py_None);
1397 }
1398
1399 PyObject *shape_tuple = PyArray_IntTupleFromIntp(ndim, shape);
1400 if (shape_tuple == NULL) {
1401 return NULL;
1402 }
1403
1404 PyObject *res = PyTuple_Pack(2, (PyObject *)out_dtype, shape_tuple);
1405 Py_DECREF(out_dtype);
1406 Py_DECREF(shape_tuple);
1407 return res;
1408}

Calls 3

npy_free_coercion_cacheFunction · 0.85
PyArray_IntTupleFromIntpFunction · 0.85

Tested by 2