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

Function find_scalar_descriptor

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

* Discover the correct descriptor from a known DType class and scalar. * If the fixed DType can discover a dtype instance/descr all is fine, * if it cannot and DType is used instead, a cast will have to be tried. * * @param fixed_DType A user provided fixed DType, can be NULL * @param DType A discovered DType (by discover_dtype_from_pyobject); * this can be identical to `fixed_DType`,

Source from the content-addressed store, hash-verified

348 * it must be known that `obj` should represent a scalar.
349 */
350static inline PyArray_Descr *
351find_scalar_descriptor(
352 PyArray_DTypeMeta *fixed_DType, PyArray_DTypeMeta *DType,
353 PyObject *obj)
354{
355 PyArray_Descr *descr;
356
357 if (DType == NULL && fixed_DType == NULL) {
358 /* No known DType and no fixed one means we go to object. */
359 return PyArray_DescrFromType(NPY_OBJECT);
360 }
361 else if (DType == NULL) {
362 /*
363 * If no DType is known/found, give the fixed give one a second
364 * chance. This allows for example string, to call `str(obj)` to
365 * figure out the length for arbitrary objects.
366 */
367 descr = NPY_DT_CALL_discover_descr_from_pyobject(fixed_DType, obj);
368 }
369 else {
370 descr = NPY_DT_CALL_discover_descr_from_pyobject(DType, obj);
371 }
372 if (descr == NULL) {
373 return NULL;
374 }
375 if (fixed_DType == NULL) {
376 return descr;
377 }
378
379 Py_SETREF(descr, PyArray_CastDescrToDType(descr, fixed_DType));
380 return descr;
381}
382
383
384/*

Callers 1

handle_scalarFunction · 0.85

Calls 1

PyArray_CastDescrToDTypeFunction · 0.85

Tested by

no test coverage detected