def walk_callbacks(obj, func, log=None):
    """Call func(callback, args) for all callbacks and keep only those
    callbacks for which the function returns True."""
    callbacks = obj._callbacks
    if isinstance(callbacks, Node):
        node = callbacks
        try:
            if not func(node.data, node.extra):
                obj._callbacks = None
        except Exception:
            if log is None:
                log = logging.get_logger()
            log.exception('uncaught exception in callback')
    elif isinstance(callbacks, dllist):
        for node in callbacks:
            try:
                if func(node.data, node.extra):
                    continue
                callbacks.remove(node)
            except Exception:
                if log is None:
                    log = logging.get_logger()
                log.exception('uncaught exception in callback')
        if not callbacks:
            obj._callbacks = None