def _expand_ellipsis(args, rank):
    """ Expand ellipsis objects and fill in missing axes.
    """
    n_el = sum(1 for arg in args if arg is Ellipsis)
    if n_el > 1:
        raise ValueError('Only one ellipsis may be used.')
    elif n_el == 0 and len(args) != rank:
        args = args + (Ellipsis,)
    final_args = []
    n_args = len(args)
    for arg in args:
        if arg is Ellipsis:
            final_args.extend((slice(None, None, None),) * (rank - n_args + 1))
        else:
            final_args.append(arg)
    if len(final_args) > rank:
        raise TypeError('Argument sequence too long')
    return final_args
