* 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`,
| 348 | * it must be known that `obj` should represent a scalar. |
| 349 | */ |
| 350 | static inline PyArray_Descr * |
| 351 | find_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 | /* |
no test coverage detected