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

Code before:
        }
        PyTuple_SET_ITEM(result, out_i, ret_i);
    }

  finish:
    Py_DECREF(wrap);
    Py_DECREF(wrap_type);
    return result;
  fail:
    /* Fail path ensures result_arrays are fully cleared */
    Py_XDECREF(result);
    Py_DECREF(wrap);
    Py_DECREF(wrap_type);
    for (int i = 0; i < ufunc->nout; i++) {
        Py_CLEAR(result_arrays[i]);
    }
    return NULL;
}

/*
 * Check whether the input object is a known scalar and whether the ufunc has
 * a suitable inner loop for it, which takes and returns the data type of the
 * input (this function is not called if output or any other argument was given).
 * If a loop was found, call it and store the result.
 *
 * Returns -2 if a short-cut is not possible, 0 on success and -1 on error.
 */
static int
try_trivial_scalar_call(
    PyUFuncObject *ufunc, PyObject *const obj, PyObject **result)
{
    assert(ufunc->nin == 1 && ufunc->nout == 1 && !ufunc->core_enabled);
    npy_clongdouble cin, cout;  // aligned storage, using longest type.
    char *in = (char *)&cin, *out = (char *)&cout;
    char *data[] = {in, out};
    int ret = -2;
    PyArray_Descr *dt;
    /*
     * For supported input, get input pointer and descriptor. Otherwise, bail.
     */
    if (obj == Py_False || obj == Py_True) {
        *(npy_bool *)in = (obj == Py_True);
        dt = PyArray_DescrFromType(NPY_BOOL);
    }
    else if (PyFloat_CheckExact(obj)) {
        *(double *)in = PyFloat_AS_DOUBLE(obj);
        dt = PyArray_DescrFromType(NPY_FLOAT64);
    }
    else if (PyLong_CheckExact(obj)) {
        int overflow;
        long val = PyLong_AsLongAndOverflow(obj, &overflow);

Review comment (suggestion):
N.B. with a clean slate I would use 1 for success and 0 for "not applicable" (matches `PyObject_GetOptionalAttr ` etc.).
But there is no point unless we fix the other `try_` functions here.

Code after:
        }
        PyTuple_SET_ITEM(result, out_i, ret_i);
    }

  finish:
    Py_DECREF(wrap);
    Py_DECREF(wrap_type);
    return result;
  fail:
    /* Fail path ensures result_arrays are fully cleared */
    Py_XDECREF(result);
    Py_DECREF(wrap);
    Py_DECREF(wrap_type);
    for (int i = 0; i < ufunc->nout; i++) {
        Py_CLEAR(result_arrays[i]);
    }
    return NULL;
}

/*
 * Check whether the input object is a known scalar and whether the ufunc has
 * a suitable inner loop for it, which takes and returns the data type of the
 * input (this function is not called if output or any other argument was given).
 * If a loop was found, call it and store the result.
 *
 * Returns -2 if a short-cut is not possible, 0 on success and -1 on error.
 */
static int
try_trivial_scalar_call(
    PyUFuncObject *ufunc, PyObject *const obj, PyObject **result)
{
    assert(ufunc->nin == 1 && ufunc->nout == 1 && !ufunc->core_enabled);
    npy_clongdouble cin, cout;  // aligned storage, using longest type.
    char *in = (char *)&cin, *out = (char *)&cout;
    char *data[] = {in, out};
    int ret = -2;
    PyArray_Descr *dt;
    /*
     * For supported input, get input pointer and descriptor. Otherwise, bail.
     */
    if (obj == Py_False || obj == Py_True) {
        *(npy_bool *)in = (obj == Py_True);
        dt = PyArray_DescrFromType(NPY_BOOL);
    }
    else if (PyFloat_CheckExact(obj)) {
        *(double *)in = PyFloat_AS_DOUBLE(obj);
        dt = PyArray_DescrFromType(NPY_FLOAT64);
    }
    else if (PyLong_CheckExact(obj)) {
        int overflow;
        npy_intp val = PyLong_AsLongAndOverflow(obj, &overflow);