-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Async generator/anext with default-tuple-value results in SystemError: <class 'StopIteration'> returned with exception set #128078
Comments
FWIW I think its a bug in implementation of anext. Lines 386 to 388 in 3a570c6
This should clear the The following patch fixes it for me and all async gen tests pass: diff --git a/Objects/iterobject.c b/Objects/iterobject.c
index 135ced9ea1f..b0938690dca 100644
--- a/Objects/iterobject.c
+++ b/Objects/iterobject.c
@@ -384,6 +384,7 @@ anextawaitable_iternext(anextawaitableobject *obj)
return result;
}
if (PyErr_ExceptionMatches(PyExc_StopAsyncIteration)) {
+ PyErr_Clear();
_PyGen_SetStopIterationValue(obj->default_value);
}
return NULL;
|
This is something I've also wondered. We could clear the exception before calling |
I suggest to do both in different PRs, I would prefer to backport the simpler fix. |
Doing both is fine. I'm not on my dev session anymore but if you want to do the |
…terationValue` (#128780) Co-authored-by: Kumar Aditya <[email protected]>
…tStopIterationValue` (pythonGH-128780) (cherry picked from commit 76ffaef) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…tStopIterationValue` (pythonGH-128780) (cherry picked from commit 76ffaef) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…etStopIterationValue` (GH-128780) (#128784) gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue` (GH-128780) (cherry picked from commit 76ffaef) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…etStopIterationValue` (GH-128780) (#128785) gh-128078: Clear exception in `anext` before calling `_PyGen_SetStopIterationValue` (GH-128780) (cherry picked from commit 76ffaef) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…Value` (#128287) Co-authored-by: Kumar Aditya <[email protected]>
…rationValue` (pythonGH-128287) (cherry picked from commit 402b91d) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…rationValue` (pythonGH-128287) (cherry picked from commit 402b91d) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…erationValue` (GH-128287) (#128790) gh-128078: Use `PyErr_SetRaisedException` in `_PyGen_SetStopIterationValue` (GH-128287) (cherry picked from commit 402b91d) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
…erationValue` (GH-128287) (#128789) gh-128078: Use `PyErr_SetRaisedException` in `_PyGen_SetStopIterationValue` (GH-128287) (cherry picked from commit 402b91d) Co-authored-by: Bénédikt Tran <[email protected]> Co-authored-by: Kumar Aditya <[email protected]>
Bug report
Bug description:
I asked a question on stackoverflow because I found a strange error while writing code that makes use an async generator function and the built-in
anext()
function when using a tuple value for thedefault
argument ofanext()
.One of the stackoverflow answers contains a good analysis and a possible cause of the problem.
The failing code is the second line with
anext()
:It raises this unexpected exception:
CPython versions tested on:
3.11, 3.12, 3.13
Operating systems tested on:
macOS
Linked PRs
anext
before calling_PyGen_SetStopIterationValue
#128780anext
before calling_PyGen_SetStopIterationValue
(GH-128780) #128784anext
before calling_PyGen_SetStopIterationValue
(GH-128780) #128785_PyGen_SetStopIterationValue
(follow-up to gh-128780) #128287PyErr_SetRaisedException
in_PyGen_SetStopIterationValue
(GH-128287) #128789PyErr_SetRaisedException
in_PyGen_SetStopIterationValue
(GH-128287) #128790The text was updated successfully, but these errors were encountered: