8000 gh-128078: Simplify logic in `_PyGen_SetStopIterationValue` (follow-up to gh-128780) by picnixz · Pull Request #128287 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-128078: Simplify logic in _PyGen_SetStopIterationValue (follow-up to gh-128780) #128287

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 8 commits into from
Jan 13, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
correctly handle unexpected exceptions
  • Loading branch information
picnixz committed Jan 3, 2025
commit fed26a9f1c72cbe4804ff05c1232bc586f7d326f
20 changes: 11 additions & 9 deletions Objects/genobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,20 +643,22 @@ _PyGen_SetStopIterationValue(PyObject *value)
return 0;
}

// Since _PyGen_SetStopIterationValue() is meant to create
// a StopItertation or substitute one for a StopAsyncItertation
// Since _PyGen_SetStopIterationValue() is called to create a
// StopItertation or substitute one for a StopAsyncItertation,
// an exception of another type should not already be set.
PyObject *run_ex = PyErr_Occurred();
if (run_ex) {
if (!PyErr_GivenExceptionMatches(run_ex, PyExc_StopAsyncIteration)) {
PyObject *old_exc = PyErr_GetRaisedException();
if (old_exc) {
if (!PyErr_GivenExceptionMatches(old_exc, PyExc_StopAsyncIteration)) {
// Replace existing bad exception with a SystemError instead.
PyErr_Format(PyExc_SystemError,
"%s:%d: unexpected caller exception: %R",
__FILE__, __LINE__, run_ex);
PyErr_BadInternalCall();
// Set the previous bad exception to the cause of the SystemError.
PyObject *new_exc = PyErr_GetRaisedException();
PyException_SetCause(new_exc, old_exc /* stolen */);
PyErr_SetRaisedException(new_exc);
return -1;
}
PyErr_Clear();
}
assert(!PyErr_Occurred());

/* Construct an exception instance manually with
* PyObject_CallOneArg and pass it to PyErr_SetObject.
Expand Down
Loading
0