From e319d7302841f8ccd2f60aa8059dd0c903e6c9a3 Mon Sep 17 00:00:00 2001 From: Jahongir Qurbonov Date: Thu, 26 Nov 2026 23:23:37 +0500 Subject: [PATCH] [mypyc] Propagate exceptions raised by an iterator when unpacking it PyIter_Next returns NULL both when the iterator is exhausted and when it raises an exception, but the exception wasn't checked for, so it was replaced with ValueError, or left set after a successful assignment. --- mypyc/irbuild/builder.py | 5 ++ mypyc/test-data/irbuild-statements.test | 66 +++++++++++++++---------- mypyc/test-data/run-misc.test | 21 ++++++++ 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/mypyc/irbuild/builder.py b/mypyc/irbuild/builder.py index 8c7cd7f0607b..85f40b741c8e 100644 --- a/mypyc/irbuild/builder.py +++ b/mypyc/irbuild/builder.py @@ -157,6 +157,7 @@ from mypyc.irbuild.vec import vec_set_item from mypyc.options import CompilerOptions from mypyc.primitives.dict_ops import dict_get_item_op, dict_set_item_op +from mypyc.primitives.exc_ops import no_err_occurred_op from mypyc.primitives.generic_ops import iter_op, next_op, py_setattr_op from mypyc.primitives.list_ops import ( list_get_item_int64_op, @@ -1041,6 +1042,7 @@ def process_iterator_tuple_assignment_helper( self.add(Branch(ritem, error_block, ok_block, Branch.IS_ERROR)) self.activate_block(error_block) + self.call_c(no_err_occurred_op, [], line) self.add( RaiseStandardError(RaiseStandardError.VALUE_ERROR, "not enough values to unpack", line) ) @@ -1067,6 +1069,8 @@ def process_iterator_tuple_assignment( self.add(Branch(ritem, error_block, ok_block, Branch.IS_ERROR)) self.activate_block(error_block) + # PyIter_Next returns NULL also if the iterator raised an exception + self.call_c(no_err_occurred_op, [], line) self.add( RaiseStandardError( RaiseStandardError.VALUE_ERROR, "not enough values to unpack", line @@ -1121,6 +1125,7 @@ def process_iterator_tuple_assignment( self.add(Unreachable()) self.activate_block(ok_block) + self.call_c(no_err_occurred_op, [], line) for litem, ritem in zip(target.items, values): self.assign(litem, ritem, line) diff --git a/mypyc/test-data/irbuild-statements.test b/mypyc/test-data/irbuild-statements.test index 4146d1f97124..ff52d4204e1c 100644 --- a/mypyc/test-data/irbuild-statements.test +++ b/mypyc/test-data/irbuild-statements.test @@ -597,34 +597,40 @@ L0: return 1 def from_any(a): a, r0, r1 :: object - r2 :: bool - r3 :: object - r4 :: bool - r5 :: object + r2 :: bit + r3 :: bool + r4 :: object + r5 :: bit r6 :: bool + r7 :: object + r8 :: bool + r9 :: bit x, y :: object L0: r0 = PyObject_GetIter(a) r1 = PyIter_Next(r0) if is_error(r1) goto L1 else goto L2 L1: - r2 = raise ValueError('not enough values to unpack') + r2 = CPy_NoErrOccurred() + r3 = raise ValueError('not enough values to unpack') unreachable L2: - r3 = PyIter_Next(r0) - if is_error(r3) goto L3 else goto L4 + r4 = PyIter_Next(r0) + if is_error(r4) goto L3 else goto L4 L3: - r4 = raise ValueError('not enough values to unpack') + r5 = CPy_NoErrOccurred() + r6 = raise ValueError('not enough values to unpack') unreachable L4: - r5 = PyIter_Next(r0) - if is_error(r5) goto L6 else goto L5 + r7 = PyIter_Next(r0) + if is_error(r7) goto L6 else goto L5 L5: - r6 = raise ValueError('too many values to unpack') + r8 = raise ValueError('too many values to unpack') unreachable L6: + r9 = CPy_NoErrOccurred() x = r1 - y = r3 + y = r4 return 1 [case testMultiAssignmentCoercions] @@ -659,36 +665,42 @@ L0: return 1 def from_any(a): a, r0, r1 :: object - r2 :: bool - r3 :: object - r4 :: bool - r5 :: object + r2 :: bit + r3 :: bool + r4 :: object + r5 :: bit r6 :: bool - r7, x :: int + r7 :: object + r8 :: bool + r9 :: bit + r10, x :: int y :: object L0: r0 = PyObject_GetIter(a) r1 = PyIter_Next(r0) if is_error(r1) goto L1 else goto L2 L1: - r2 = raise ValueError('not enough values to unpack') + r2 = CPy_NoErrOccurred() + r3 = raise ValueError('not enough values to unpack') unreachable L2: - r3 = PyIter_Next(r0) - if is_error(r3) goto L3 else goto L4 + r4 = PyIter_Next(r0) + if is_error(r4) goto L3 else goto L4 L3: - r4 = raise ValueError('not enough values to unpack') + r5 = CPy_NoErrOccurred() + r6 = raise ValueError('not enough values to unpack') unreachable L4: - r5 = PyIter_Next(r0) - if is_error(r5) goto L6 else goto L5 + r7 = PyIter_Next(r0) + if is_error(r7) goto L6 else goto L5 L5: - r6 = raise ValueError('too many values to unpack') + r8 = raise ValueError('too many values to unpack') unreachable L6: - r7 = unbox(int, r1) - x = r7 - y = r3 + r9 = CPy_NoErrOccurred() + r10 = unbox(int, r1) + x = r10 + y = r4 return 1 [case testMultiAssignmentNested] diff --git a/mypyc/test-data/run-misc.test b/mypyc/test-data/run-misc.test index 9b29b43fb830..6f68f0567d30 100644 --- a/mypyc/test-data/run-misc.test +++ b/mypyc/test-data/run-misc.test @@ -434,6 +434,27 @@ def test_unpack_iterator_errors() -> None: c.x, *c.y, c.z = gen(1) assert not any(s.startswith("set") for s in log), log +def gen_raise(n: int) -> Iterator[int]: + for i in range(n): + yield i + raise KeyError("from iterator") + +def unpack_two(n: int) -> tuple[int, int]: + a, b = gen_raise(n) + return a, b + +def unpack_star(n: int) -> tuple[int, list[int], int]: + a, *b, c = gen_raise(n) + return a, b, c + +def test_unpack_iterator_raises() -> None: + for n in range(3): + with assertRaises(KeyError, "from iterator"): + unpack_two(n) + for n in range(4): + with assertRaises(KeyError, "from iterator"): + unpack_star(n) + [case testUnpack] from typing import List