Helper function for getting a file descriptor.

def get_fd(file_or_fd, default=None):
    """Helper function for getting a file descriptor."""
    fd = file_or_fd
    if fd is None:
        fd = default
    if hasattr(fd, "fileno"):
        fd = fd.fileno()
    return fd