KEMBAR78
gh-104770: Let generator.close() return value by ntessore · Pull Request #104771 · python/cpython · GitHub
Skip to content

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.

@ghost
Copy link

ghost 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).

@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
@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>
@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
@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.

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