Repository: numpy/numpy
File: numpy/_core/src/umath/ufunc_object.c (C)

Code before:
    }
    else if (is_anyscalar_exact(obj)) {
        dt = PyArray_DescrFromScalar(obj);
        if (!PyDataType_ISNUMBER(dt)) {
            goto fail;
        }
        data[0] = scalar_value(obj, dt);
    }
    else {
        return -2;
    }
    // Try getting info from the (private) cache.  Fall back if not found,
    // so that the the dtype gets registered and things will work next time.
    PyArray_DTypeMeta *op_dtypes[2] = {NPY_DTYPE(dt), NULL};
    PyObject *info = PyArrayIdentityHash_GetItem(
        (PyArrayIdentityHash *)ufunc->_dispatch_cache,
        (PyObject **)op_dtypes);
    if (info == NULL) {
        goto fail;
    }
    // Try getting method, falling back if not an arraymethod (e.g., a promotor)
    PyArrayMethodObject *method = (PyArrayMethodObject *)PyTuple_GET_ITEM(info, 1);
    if (!PyObject_TypeCheck(method, &PyArrayMethod_Type)) {
        goto fail;
    }
    // Try getting loop, for speed assuming output and input dtype are the same.
    PyArrayMethod_Context context;
    PyArray_Descr *descrs[2] = {dt, dt};
    NPY_context_init(&context, descrs);
    context.caller = (PyObject *)ufunc;
    context.method = method;
    npy_intp strides[2] = {0, 0};  // 0 ensures scalar math, not SIMD for half.
    PyArrayMethod_StridedLoop *strided_loop;
    NpyAuxData *auxdata = NULL;
    NPY_ARRAYMETHOD_FLAGS flags = 0;
    if (method->get_strided_loop(&context, 1, 0, strides,
                                 &strided_loop, &auxdata, &flags) < 0) {
        PyErr_Clear();  // Clear error, may just be because of wrong assumption.
        goto fail;
    }
    // Call loop with single element.
    if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)) {
        npy_clear_floatstatus();
    }
    npy_intp n = 1;
    ret = strided_loop(&context, data, &n, strides, auxdata);
    NPY_AUXDATA_FREE(auxdata);
    if (ret == 0) {
        if (!(flags & NPY_METH_NO_FLOATINGPOINT_ERRORS)
                && npy_get_floatstatus() != 0) {
            // If floating point errors occurred, decide what to do with them.

Review comment (suggestion):
At least, I think we need to check that the DType types all match up.  But is it so bad to call `resolve_descriptors` and bail here?

EDIT: Maybe you can fast-path this if the default `resolve_descriptors` is used, in which case we can just use the default descriptor for the result (or check that the type matches for it).

Code after:
    else if (PyComplex_CheckExact(obj)) {
        Py_complex oop = PyComplex_AsCComplex(obj);
        if (error_converting(oop.real)) {
            return -1;  // should never happen; pass on it if does.
        }
        *(double *)in = oop.real;
        *(double *)(in+sizeof(double)) = oop.imag;
        dt = PyArray_DescrFromType(NPY_COMPLEX128);
    }
    else if (is_anyscalar_exact(obj)) {
        dt = PyArray_DescrFromScalar(obj);
        if (!PyDataType_ISNUMBER(dt)) {
            goto bail;
        }
        data[0] = scalar_value(obj, dt);
    }
    else {
        return -2;
    }
    /*
     * Check the ufunc supports our descriptor, bailing (return -2) if not.
     */
    // Try getting info from the (private) cache.  Fall back if not found,
    // so that the the dtype gets registered and things will work next time.
    PyArray_DTypeMeta *op_dtypes[2] = {NPY_DTYPE(dt), NULL};
    PyObject *info = PyArrayIdentityHash_GetItem(  // borrowed reference.
        (PyArrayIdentityHash *)ufunc->_dispatch_cache,
        (PyObject **)op_dtypes);
    if (info == NULL) {
        goto bail;
    }
    // Check actual dtype is correct (can be wrong with promotion).
    PyObject *all_dtypes = PyTuple_GET_ITEM(info, 0);
    if ((PyTuple_GET_ITEM(all_dtypes, 0) != (PyObject *)NPY_DTYPE(dt)) ||
        (PyTuple_GET_ITEM(all_dtypes, 1) != (PyObject *)NPY_DTYPE(dt))) {
        goto bail;
    }
    // Get method, bailing if not an arraymethod (e.g., a promotor).
    PyArrayMethodObject *method = (PyArrayMethodObject *)PyTuple_GET_ITEM(info, 1);
    if (!PyObject_TypeCheck(method, &PyArrayMethod_Type)) {
        goto bail;
    }
    // Get loop, requiring that the output and input dtype are the same.
    PyArrayMethod_Context context;
    PyArray_Descr *descrs[2] = {dt, dt};
    NPY_context_init(&context, descrs);
    context.caller = (PyObject *)ufunc;
    context.method = method;
    npy_intp strides[2] = {0, 0};  // 0 ensures scalar math, not SIMD for half.
    PyArrayMethod_StridedLoop *strided_loop;
    NpyAuxData *auxdata = NULL;