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

Function array_can_cast_safely

numpy/core/src/multiarray/multiarraymodule.c:3588–3644  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3586}
3587
3588static PyObject *
3589array_can_cast_safely(PyObject *NPY_UNUSED(self),
3590 PyObject *const *args, Py_ssize_t len_args, PyObject *kwnames)
3591{
3592 PyObject *from_obj = NULL;
3593 PyArray_Descr *d1 = NULL;
3594 PyArray_Descr *d2 = NULL;
3595 int ret;
3596 PyObject *retobj = NULL;
3597 NPY_CASTING casting = NPY_SAFE_CASTING;
3598
3599 NPY_PREPARE_ARGPARSER;
3600 if (npy_parse_arguments("can_cast", args, len_args, kwnames,
3601 "from_", NULL, &from_obj,
3602 "to", &PyArray_DescrConverter2, &d2,
3603 "|casting", &PyArray_CastingConverter, &casting,
3604 NULL, NULL, NULL) < 0) {
3605 goto finish;
3606 }
3607 if (d2 == NULL) {
3608 PyErr_SetString(PyExc_TypeError,
3609 "did not understand one of the types; 'None' not accepted");
3610 goto finish;
3611 }
3612
3613 /* If the first parameter is an object or scalar, use CanCastArrayTo */
3614 if (PyArray_Check(from_obj)) {
3615 ret = PyArray_CanCastArrayTo((PyArrayObject *)from_obj, d2, casting);
3616 }
3617 else if (PyArray_IsScalar(from_obj, Generic) ||
3618 PyArray_IsPythonNumber(from_obj)) {
3619 PyArrayObject *arr;
3620 arr = (PyArrayObject *)PyArray_FROM_O(from_obj);
3621 if (arr == NULL) {
3622 goto finish;
3623 }
3624 ret = PyArray_CanCastArrayTo(arr, d2, casting);
3625 Py_DECREF(arr);
3626 }
3627 /* Otherwise use CanCastTypeTo */
3628 else {
3629 if (!PyArray_DescrConverter2(from_obj, &d1) || d1 == NULL) {
3630 PyErr_SetString(PyExc_TypeError,
3631 "did not understand one of the types; 'None' not accepted");
3632 goto finish;
3633 }
3634 ret = PyArray_CanCastTypeTo(d1, d2, casting);
3635 }
3636
3637 retobj = ret ? Py_True : Py_False;
3638 Py_INCREF(retobj);
3639
3640 finish:
3641 Py_XDECREF(d1);
3642 Py_XDECREF(d2);
3643 return retobj;
3644}
3645

Callers

nothing calls this directly

Calls 3

PyArray_CanCastArrayToFunction · 0.85
PyArray_DescrConverter2Function · 0.85
PyArray_CanCastTypeToFunction · 0.85

Tested by

no test coverage detected