Repository: numpy/numpy
File: numpy/_core/src/umath/dispatching.cpp (C++)

Code before:
    PyUFunc_LoopSlot *slot;
    for (slot = slots; slot->name != NULL; slot++) {
        PyObject *ufunc = npy_import("numpy", slot->name);
        if (ufunc == NULL) {
            return -1;
        }

        if (ufunc == sort) {
            PyArray_DTypeMeta *dtype = slot->spec->dtypes[0];

            PyBoundArrayMethodObject *sort_meth = PyArrayMethod_FromSpec_int(slot->spec, 0);
            if (sort_meth == NULL) {
                PyErr_Format(PyExc_TypeError, "Failed to create sort method for %R", dtype);
                return -1;
            }

            NPY_DT_SLOTS(dtype)->sort_meth = sort_meth->method;
            Py_INCREF(sort_meth->method);
            Py_DECREF(sort_meth);
        }
        else if (ufunc == argsort) {
            PyArray_DTypeMeta *dtype = slot->spec->dtypes[0];

            PyBoundArrayMethodObject *argsort_meth = PyArrayMethod_FromSpec_int(slot->spec, 0);
            if (argsort_meth == NULL) {
                PyErr_Format(PyExc_TypeError, "Failed to create argsort method for %R", dtype);
                return -1;
            }

            NPY_DT_SLOTS(dtype)->argsort_meth = argsort_meth->method;
            Py_INCREF(argsort_meth->method);
            Py_DECREF(argsort_meth);
        }
        else {
            if (!PyObject_TypeCheck(ufunc, &PyUFunc_Type)) {
                PyErr_Format(PyExc_TypeError, "%s was not a ufunc!", slot->name);
                Py_DECREF(ufunc);
                return -1;
            }

            int ret = PyUFunc_AddLoopFromSpec_int(ufunc, slot->spec, 0);
            Py_DECREF(ufunc);
            if (ret < 0) {
                return -1;
            }
        }
    }

    return 0;
}


Review comment (suggestion):
Doesn't matter, but FYI: this probably eats an in-flight exception.  But chaining is a bit tedious to repeat multiple times IMO.

Code after:

        if (ufunc == npy_runtime_imports.sort) {
            Py_DECREF(ufunc);

            PyArray_DTypeMeta *dtype = slot->spec->dtypes[0];
            PyBoundArrayMethodObject *sort_meth = PyArrayMethod_FromSpec_int(slot->spec, 0);
            if (sort_meth == NULL) {
                return -1;
            }

            NPY_DT_SLOTS(dtype)->sort_meth = sort_meth->method;
            Py_INCREF(sort_meth->method);
            Py_DECREF(sort_meth);
        }
        else if (ufunc == npy_runtime_imports.argsort) {
            Py_DECREF(ufunc);

            PyArray_DTypeMeta *dtype = slot->spec->dtypes[0];
            PyBoundArrayMethodObject *argsort_meth = PyArrayMethod_FromSpec_int(slot->spec, 0);
            if (argsort_meth == NULL) {
                return -1;
            }

            NPY_DT_SLOTS(dtype)->argsort_meth = argsort_meth->method;
            Py_INCREF(argsort_meth->method);
            Py_DECREF(argsort_meth);
        }
        else {
            if (!PyObject_TypeCheck(ufunc, &PyUFunc_Type)) {
                PyErr_Format(PyExc_TypeError, "%s was not a ufunc!", slot->name);
                Py_DECREF(ufunc);
                return -1;
            }

            int ret = PyUFunc_AddLoopFromSpec_int(ufunc, slot->spec, 0);
            Py_DECREF(ufunc);
            if (ret < 0) {
                return -1;
            }
        }
    }

    return 0;
}


/**
 * Resolves the implementation to use, this uses typical multiple dispatching
 * methods of finding the best matching implementation or resolver.
 * (Based on `isinstance()`, the knowledge that non-abstract DTypes cannot
 * be subclassed is used, however.)