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

Code before:
    // 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, balling 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;
    NPY_ARRAYMETHOD_FLAGS flags = 0;
    if (method->get_strided_loop(&context, 1, 0, strides,
                                 &strided_loop, &auxdata, &flags) < 0) {
        // Clear error, may just be because of wrong assumption.
        PyErr_Clear();
        goto bail;
    }
    /*
     * Call loop with single element, checking floating point errors.
     */
    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)) {
            // Check for any unmasked floating point errors (note: faster
            // than _check_ufunc_fperr as one doesn't need mask up front).
            int fpe_errors = npy_get_floatstatus();
            if (fpe_errors) {
                if (PyUFunc_GiveFloatingpointErrors(

Review comment (suggestion):
Do the strides really matter here? Slightly worrying :).  (FWIW, I use either `elsize` or `0` for these often.  Mainly because we might actually have contiguous special loops in theory, while scalar ones are rare.)

Code after:
    // 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;
    NPY_ARRAYMETHOD_FLAGS flags = 0;
    if (method->get_strided_loop(&context, 1, 0, strides,
                                 &strided_loop, &auxdata, &flags) < 0) {
        ret = -1;  // Should not happen, so raise error if it does anyway.
        goto bail;
    }
    /*
     * Call loop with single element, checking floating point errors.
     */
    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)) {
            // Check for any unmasked floating point errors (note: faster
            // than _check_ufunc_fperr as one doesn't need mask up front).
            int fpe_errors = npy_get_floatstatus();
            if (fpe_errors) {
                if (PyUFunc_GiveFloatingpointErrors(
                        ufunc_get_name_cstr(ufunc), fpe_errors) < 0) {