Skip to content

gh-126366: Fix crash if __iter__ raises an exception during yield from #126369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2024
Merged
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
13 changes: 13 additions & 0 deletions Lib/test/test_yield_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,19 @@ def outer():
self.assertIsNone(caught.exception.__context__)
self.assert_stop_iteration(g)

def test_throws_in_iter(self):
# See GH-126366: NULL pointer dereference if __iter__
# threw an exception.
class Silly:
def __iter__(self):
raise RuntimeError("nobody expects the spanish inquisition")

def my_generator():
yield from Silly()

with self.assertRaisesRegex(RuntimeError, "nobody expects the spanish inquisition"):
next(iter(my_generator()))


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when using ``yield from`` on an object that raises an exception in
its ``__iter__``.
5 changes: 3 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2791,11 +2791,12 @@ dummy_func(
}
else {
/* `iterable` is not a generator. */
iter = PyStackRef_FromPyObjectSteal(PyObject_GetIter(iterable_o));
PyObject *iter_o = PyObject_GetIter(iterable_o);
DEAD(iterable);
if (PyStackRef_IsNull(iter)) {
if (iter_o == NULL) {
ERROR_NO_POP();
}
iter = PyStackRef_FromPyObjectSteal(iter_o);
DECREF_INPUTS();
}
}
Expand Down
5 changes: 3 additions & 2 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Tools/jit/ignore-tests-emulated-linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ test.test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen1
test.test_socket.RecvmsgSCMRightsStreamTest.testCmsgTruncLen2Minus1
test.test_subprocess.POSIXProcessTestCase.test_exception_bad_args_0
test.test_subprocess.POSIXProcessTestCase.test_exception_bad_executable
test.test_subprocess.POSIXProcessTestCase.test_vfork_used_when_expected
test.test_subprocess.ProcessTestCase.test_cwd_with_relative_arg
test.test_subprocess.ProcessTestCase.test_cwd_with_relative_executable
test.test_subprocess.ProcessTestCase.test_empty_env
Expand Down
Loading