def call_user_func(settings_module, func_name, *args, **kwargs):
    """Call a user-supplied settings function and clean it up afterwards.

    settings_module may be None, or the function may not exist.
    If the function exists, it is called with the specified *args and **kwargs,
    and the result is returned.
    """
    if settings_module:
        if hasattr(settings_module, func_name):
            func = getattr(settings_module, func_name)
            try:
                return func(*args, **kwargs)
            finally:
                delattr(settings_module, func_name)
