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

Code before:
            }
        }
        /* Create a cast using the state of the legacy casting setup defined
         * during the setup of the DType.
         *
         * Ideally we would do this when we create the DType, but legacy user
         * DTypes don't have a way to signal that a DType is done setting up
         * casts. Without such a mechanism, the safest way to know that a
         * DType is done setting up is to register the cast lazily the first
         * time a user does the cast.
         *
         * We *could* register the casts when we create the wrapping
         * DTypeMeta, but that means the internals of the legacy user DType
         * system would need to update the state of the casting safety flags
         * in the cast implementations stored on the DTypeMeta. That's an
         * inversion of abstractions and would be tricky to do without
         * creating circular dependencies inside NumPy.
         */
        if (PyArray_AddLegacyWrapping_CastingImpl(from, to, -1) < 0) {
            return NULL;
        }
        /* castingimpls is unconditionally filled by
         * AddLegacyWrapping_CastingImpl, so this won't create a recursive
         * critical section
         */
        return PyArray_GetCastingImpl(from, to);
    }
}

static PyObject *
ensure_castingimpl_exists(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
{
    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 (!return_error) {
        res = create_casting_impl(from, to);
        if (res == NULL) {
            return_error = 1;

Review comment (suggestion):
`None` gets inserted into to castingimpls later on before exiting the critical section so there's no need to do it here

Code after:
            }
        }
        /* Create a cast using the state of the legacy casting setup defined
         * during the setup of the DType.
         *
         * Ideally we would do this when we create the DType, but legacy user
         * DTypes don't have a way to signal that a DType is done setting up
         * casts. Without such a mechanism, the safest way to know that a
         * DType is done setting up is to register the cast lazily the first
         * time a user does the cast.
         *
         * We *could* register the casts when we create the wrapping
         * DTypeMeta, but that means the internals of the legacy user DType
         * system would need to update the state of the casting safety flags
         * in the cast implementations stored on the DTypeMeta. That's an
         * inversion of abstractions and would be tricky to do without
         * creating circular dependencies inside NumPy.
         */
        if (PyArray_AddLegacyWrapping_CastingImpl(from, to, -1) < 0) {
            return NULL;
        }
        /* castingimpls is unconditionally filled by
         * AddLegacyWrapping_CastingImpl, so this won't create a recursive
         * critical section
         */
        return PyArray_GetCastingImpl(from, to);
    }
}

static PyObject *
ensure_castingimpl_exists(PyArray_DTypeMeta *from, PyArray_DTypeMeta *to)
{
    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;
        }