def _supports_fileno(stream):
    """
    Returns True if 'stream' supports the file descriptor and allows fileno().
    """
    if hasattr(stream, 'fileno'):
        try:
            stream.fileno()
            return True
        except IOError:
            return False
    else:
        return False