Skip to content

[mypyc] Propagate exceptions raised by an iterator when unpacking it - #22566

Open
Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-unpack-iterator-error
Open

Jahongir-Qurbonov wants to merge 1 commit into
python:masterfrom
Jahongir-Qurbonov:mypyc-unpack-iterator-error

Conversation

@Jahongir-Qurbonov

Copy link
Copy Markdown
Contributor

Fixes mypyc/mypyc#1259.

Unpacking an iterator (a, b = it, where it isn't a tuple or list) uses PyIter_Next, which returns NULL both when the iterator is exhausted and when it raises an exception. The exception wasn't checked for, so:

  • If the iterator raised before producing enough items, the exception was replaced with ValueError: not enough values to unpack.
  • If it raised in the final call that checks that there are no extra items, the assignment succeeded with the exception still set, and it caused a SystemError somewhere later.

Now we propagate the exception in both cases, like for loops do. This also fixes the items before the starred target in starred assignments (a, *b, c = it). The remaining items are collected using PySequence_List, which already propagated exceptions.

The new run test fails without the fix.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unpacking an iterator ignores exceptions raised by the iterator

1 participant