Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 39 additions & 27 deletions mypyc/test-data/irbuild-statements.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 21 additions & 0 deletions mypyc/test-data/run-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading