Skip to content
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

gh-104770: Let generator.close() return value #104771

Merged
merged 9 commits into from
May 23, 2023
Merged

gh-104770: Let generator.close() return value #104771

merged 9 commits into from
May 23, 2023

Conversation

ntessore
Copy link
Contributor

@ntessore ntessore commented May 22, 2023

This changeset alters generator.close() to return the value of a StopIteration it encounters. Conceptually, the change is simple: instead of lumping StopIteration in with GeneratorExit and ignoring it, the exception is handled specially and its value returned.

Practically, handling the exception is a little awkward because no high-level API exists (afaict) to retrieve the value of a StopIteration. I would be happy for any pointers on how to handle this better, if possible.


📚 Documentation preview 📚: https://cpython-previews--104771.org.readthedocs.build/

@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@cpython-cla-bot
Copy link

cpython-cla-bot bot commented May 22, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also asking @iritkatriel to review -- I'm not sure if poking at tstate->current_exception is the right way to access the current exception. Maybe the brand new PyPyErr_GetRaisedException() is the API to use? (Then you don't even need to get the tstate).

Doc/reference/expressions.rst Outdated Show resolved Hide resolved
Doc/reference/expressions.rst Outdated Show resolved Hide resolved
Objects/genobject.c Outdated Show resolved Hide resolved
Objects/genobject.c Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@gvanrossum gvanrossum requested a review from iritkatriel May 23, 2023 00:05
@ntessore
Copy link
Contributor Author

I have made the requested changes; please review again.

In the docs, I made the wording of the "returns return value" situation sound as natural as I could, but it might need more polishing.

I didn't know about PyErr_GetRaisedException(), thanks! That fits perfectly here. I don't suppose there's a brand new API to extract the value from an exception?

@bedevere-bot
Copy link

Thanks for making the requested changes!

@gvanrossum: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from gvanrossum May 23, 2023 08:16
Objects/genobject.c Outdated Show resolved Hide resolved
Objects/genobject.c Outdated Show resolved Hide resolved
Objects/genobject.c Outdated Show resolved Hide resolved
@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Objects/genobject.c Outdated Show resolved Hide resolved
@ntessore
Copy link
Contributor Author

Thanks @iritkatriel, that is much better. I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@gvanrossum, @iritkatriel: please review the changes made to this pull request.

@bedevere-bot bedevere-bot requested a review from iritkatriel May 23, 2023 15:17
Objects/genobject.c Outdated Show resolved Hide resolved
@gvanrossum
Copy link
Member

I'll leave the rest of the review to Irit.

Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fine, a couple of comments on documentation.

Doc/reference/expressions.rst Outdated Show resolved Hide resolved
Doc/reference/expressions.rst Outdated Show resolved Hide resolved
ntessore and others added 2 commits May 23, 2023 19:37
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic! I will merge now.

@gvanrossum gvanrossum merged commit d56c933 into python:main May 23, 2023
@ntessore ntessore deleted the generator-close-return branch May 23, 2023 20:53
@ntessore
Copy link
Contributor Author

Thanks again Irit and Guido for your help and patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants