def test_roundrobin_failure_lengthhint3(): # Check if by adding the different lengths it could lead to overflow. # We use two iterables both with sys.maxsize length. it1 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it2 = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = roundrobin(it1, it2) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_grouper_lengthhint_failure2(): of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = grouper(of_it, 2) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_accumulate_lengthhint_failure2(): of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) acc = accumulate(of_it) with pytest.raises(OverflowError): operator.length_hint(acc) with pytest.raises(OverflowError): list(acc)
def test_sideeffects_failure_lengthhint2(): # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = sideeffects(of_it, return_None) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_roundrobin_failure_lengthhint2(): # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = roundrobin(of_it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_intersperse_lengthhint_failure2(): # This is the easy way to overflow the length_hint: If the iterable itself # has a length_hint > sys.maxsize of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = intersperse(of_it, 2) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure5(): # Like the test above but this time with a "started" merge of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = merge(toT([1, 2, 3]), of_it) next(it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_intersperse_lengthhint_failure3(): # The length_hint method multiplies the length_hint of the iterable with # 2 (and adds/subtracts 1) so it's actually possible to have overflow even # if the length of the iterable doesn't trigger the overflow! of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = intersperse(of_it, 2) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure4(): # Overflow could also happen when adding length_hints that individually are # below the sys.maxsize # In this case we have 3 + sys.maxsize of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize) it = merge(toT([1, 2, 3]), of_it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_replicate_failure_lengthhint3(): # It is also possible that the length_hint overflows when the length is # below maxsize but "times * length" is above maxsize. # In this case length = maxsize / 2 but times = 3 of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize // 2) it = replicate(of_it, 3) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_successive_failure_lengthhint2(): # This only checks for overflow if the length_hint is above PY_SSIZE_T_MAX. # In theory that would be possible because with times the length would be # shorter but "length_hint" throws the exception so we propagate it. of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = successive(of_it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_merge_lengthhint_failure3(): # Like the test case above but this time we take one item because # internally an unstarted "merge" and started "merge" are treated # differently of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize + 1) it = merge(of_it) next(it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)
def test_replicate_failure_lengthhint4(): # There is also the possibility that "length * times" does not overflow # but adding the "times - timescurrent" afterwards will overflow. # That's a bit tricky, but it seems that ((2**x-1) // 10) * 10 + 9 > 2**x-1 # is true for x=15, 31, 63 and 127 so it's possible by setting the times to # 10 and the length to sys.maxsize // 10. The 9 are because the first item # is already popped and should be replicated 9 more times. of_it = _hf.OverflowLengthHint(toT([1, 2, 3]), sys.maxsize // 10) it = replicate(of_it, 10) next(it) with pytest.raises(OverflowError): operator.length_hint(it) with pytest.raises(OverflowError): list(it)