Skip to content

Commit

Permalink
pythongh-126366: Fix crash if __iter__ raises an exception during `…
Browse files Browse the repository at this point in the history
…yield from` (python#126369)
  • Loading branch information
ZeroIntensity authored Nov 5, 2024
1 parent 407c036 commit 1371295
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 6 deletions.
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 @@ -2811,11 +2811,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

0 comments on commit 1371295

Please sign in to comment.