Repository: numpy/numpy
File: numpy/_core/src/multiarray/convert_datatype.c (C)

Code before:
{
    int return_error = 0;
    PyObject *res = NULL;

    /* Need to create the cast. This might happen at runtime so we enter a
       critical section to avoid races */

    Py_BEGIN_CRITICAL_SECTION(NPY_DT_SLOTS(from)->castingimpls);

    /* check if another thread filled it while this thread was blocked on
       acquiring the critical section */
    if (PyDict_GetItemRef(NPY_DT_SLOTS(from)->castingimpls, (PyObject *)to,
                          &res) < 0) {
        return_error = 1;
    }

    if (res == NULL && !return_error) {
        res = create_casting_impl(from, to);
        if (res == NULL) {
            return_error = 1;
        }
    }
    if (!return_error &&
        PyDict_SetItem(NPY_DT_SLOTS(from)->castingimpls,
                       (PyObject *)to, res) < 0) {
        return_error = 1;
    }
    Py_END_CRITICAL_SECTION();
    if (return_error) {
        Py_XDECREF(res);
        return NULL;
    }
    if (from == to && res == Py_None) {
        PyErr_Format(PyExc_RuntimeError,
                "Internal NumPy error, within-DType cast missing for %S!", from);
        Py_DECREF(res);
        return NULL;
    }
    return res;
}

/**
 * Fetch the casting implementation from one DType to another.
 *
 * @param from The implementation to cast from
 * @param to The implementation to cast to
 *
 * @returns A castingimpl (PyArrayDTypeMethod *), None or NULL with an
 *          error set.
 */
NPY_NO_EXPORT PyObject *

Review comment (suggestion):
I think it would be slightly nicer to keep this inside the `res == NULL` branch (if it isn't we don't need to set it again).

The first if could go into an `else if (res == NULL)` (that includes this).
If you are than OK with using `Py_CLEAR(res)` on this line, you can delete `return_error` (with the funny thing that the first `if` does nothing).

To me that would look nicer, I think, but happy to keep this.

Code after:
{
    int return_error = 0;
    PyObject *res = NULL;

    /* Need to create the cast. This might happen at runtime so we enter a
       critical section to avoid races */

    Py_BEGIN_CRITICAL_SECTION(NPY_DT_SLOTS(from)->castingimpls);

    /* check if another thread filled it while this thread was blocked on
       acquiring the critical section */
    if (PyDict_GetItemRef(NPY_DT_SLOTS(from)->castingimpls, (PyObject *)to,
                          &res) < 0) {
        return_error = 1;
    }
    else if (res == NULL) {
        res = create_casting_impl(from, to);
        if (res == NULL) {
            return_error = 1;
        }
        else if (PyDict_SetItem(NPY_DT_SLOTS(from)->castingimpls,
                                (PyObject *)to, res) < 0) {
            return_error = 1;
        }
    }
    Py_END_CRITICAL_SECTION();
    if (return_error) {
        Py_XDECREF(res);
        return NULL;
    }
    if (from == to && res == Py_None) {
        PyErr_Format(PyExc_RuntimeError,
                "Internal NumPy error, within-DType cast missing for %S!", from);
        Py_DECREF(res);
        return NULL;
    }
    return res;
}

/**
 * Fetch the casting implementation from one DType to another.
 *
 * @param from The implementation to cast from
 * @param to The implementation to cast to
 *
 * @returns A castingimpl (PyArrayDTypeMethod *), None or NULL with an
 *          error set.
 */
NPY_NO_EXPORT PyObject *
PyArray_GetCastingImpl(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
{