def convert_to_dgl_func(pyfunc):
    """Convert a python function to DGL function

    Parameters
    ----------
    pyfunc : python function
        The python function to be converted.

    Returns
    -------
    dglfunc: dgl.nd.Function
        The converted dgl function.
    """
    local_pyfunc = pyfunc

    def cfun(args, type_codes, num_args, ret, _):
        """ctypes function"""
        num_args = (
            num_args.value if isinstance(num_args, ctypes.c_int) else num_args
        )
        pyargs = (
            C_TO_PY_ARG_SWITCH[type_codes[i]](args[i]) for i in range(num_args)
        )
        # pylint: disable=broad-except
        try:
            rv = local_pyfunc(*pyargs)
        except Exception:
            msg = traceback.format_exc()
            _LIB.DGLAPISetLastError(c_str(msg))
            return -1

        if rv is not None:
            if isinstance(rv, tuple):
                raise ValueError(
                    "PackedFunction can only support one return value"
                )
            temp_args = []
            values, tcodes, _ = _make_dgl_args((rv,), temp_args)
            if not isinstance(ret, DGLRetValueHandle):
                ret = DGLRetValueHandle(ret)
            check_call(
                _LIB.DGLCFuncSetReturn(ret, values, tcodes, ctypes.c_int(1))
            )
            _ = temp_args
            _ = rv
        return 0

    handle = FunctionHandle()
    f = DGLPackedCFunc(cfun)
    # NOTE: We will need to use python-api to increase ref count of the f
    # DGL_FREE_PYOBJ will be called after it is no longer needed.
    pyobj = ctypes.py_object(f)
    ctypes.pythonapi.Py_IncRef(pyobj)
    check_call(
        _LIB.DGLFuncCreateFromCFunc(
            f, pyobj, DGL_FREE_PYOBJ, ctypes.byref(handle)
        )
    )
    return _CLASS_FUNCTION(handle, False)
Convert a python function to DGL function

    Parameters
    ----------
    pyfunc : python function
        The python function to be converted.

    Returns
    -------
    dglfunc: dgl.nd.Function
        The converted dgl function.
    
python/dgl/_ffi/_ctypes/function.py