import errno


def retry_when_interrupt(f, *a, **kw):
    """
    Call C{f} with the given arguments, handling C{EINTR} by retrying.
    
    @param f: A function to call.
    @param *a: Positional arguments to pass to C{f}.
    @param **kw: Keyword arguments to pass to C{f}.

    @return: Whatever C{f} returns.
    
    @raise: Whatever C{f} raises, except for C{IOError} or C{OSError} with
        C{errno} set to C{EINTR}.
    """
    while True:
        try:
            return f(*a, **kw)
        except (IOError, OSError) as e:
            if e.args[0] == errno.EINTR:
                continue
            raise