Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-104770: Let generator.close() return value#104771
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from5 commits
347d478cd94e7ff134f7f2175dc2ecfaa1f678332a206dcd0c0c4b51d51a4caFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -595,12 +595,19 @@ is already executing raises a :exc:`ValueError` exception. | ||
| .. method:: generator.close() | ||
| Raises a :exc:`GeneratorExit` at the point where the generator function was | ||
| paused. If the generator function then returns a value (by catching | ||
| :exc:`GeneratorExit`), it is returned by :meth:`close`. If the generator | ||
ntessore marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| function is already closed, or raises :exc:`GeneratorExit` (by not catching | ||
| the exception), :meth:`close` returns :const:`None`. If the generator | ||
| yields a value, a :exc:`RuntimeError` is raised. If the generator raises | ||
| any other exception, it is propagated to the caller. If the generator has | ||
| already exited due to an exception or normal exit, :meth:`close` returns | ||
| :const:`None`. | ||
ntessore marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. versionchanged:: 3.13 | ||
| If a generator returns a value after being closed, the value is returned | ||
| by :meth:`close`. | ||
| .. index:: single: yield; examples | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| If a generator returns a value after being closed, the value is returned | ||
| by:meth:`generator.close`. | ||
ntessore marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -408,9 +408,16 @@ gen_close(PyGenObject *gen, PyObject *args) | ||
| PyErr_SetString(PyExc_RuntimeError, msg); | ||
| return NULL; | ||
| } | ||
| if (PyErr_ExceptionMatches(PyExc_StopIteration)) { | ||
ntessore marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| /* retrieve the StopIteration exception instance being handled, and | ||
| * extract its value */ | ||
| PyObject *exc = PyErr_GetRaisedException(); /* clears the error indicator */ | ||
| PyObject *value = Py_NewRef(((PyStopIterationObject *)exc)->value); | ||
| Py_DECREF(exc); | ||
ntessore marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return value; | ||
| } | ||
| if (PyErr_ExceptionMatches(PyExc_GeneratorExit)) { | ||
| PyErr_Clear(); /* ignore this error */ | ||
| Py_RETURN_NONE; | ||
| } | ||
| return NULL; | ||