def get_and_check_size(iterator, n):
    """Check if the iterator is length n and return consumed elements.

    Consumes the next n+1 elements if possible (up to the end of the iterator).
    Returns (is_length_n, elems)
    """
    elements = []
    try:
        for _ in range(n):
            elements.append(next(iterator))
    except StopIteration:
        return False, elements

    try:
        elements.append(next(iterator))
        return False, elements
    except StopIteration:
        pass

    assert(len(elements) == n)
    return True, elements