def get_fileno(fd_or_obj):
    """Returns the file descriptor number for the given `fd_or_obj`"""
    try:
        return int(fd_or_obj)
    except:
        if hasattr(fd_or_obj, 'fileno') and callable(fd_or_obj.fileno):
            return fd_or_obj.fileno()
    raise TypeError("Unable to get fd from %s" % fd_or_obj)