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-110378: Close invalid generators in contextmanager and asynccontextmanager #110499

Merged

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 7, 2023

contextmanager and asynccontextmanager context managers now close an invalid underlying generator object that yields more then one value.

…contextmanager

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
Comment on lines +152 to +155
try:
raise RuntimeError("generator didn't stop")
finally:
self.gen.close()
Copy link
Contributor

Choose a reason for hiding this comment

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

👍🏻

I sometimes use the try:finally: construct around flow breaking instructions as well. Here it nicely chains exceptions if self.gen.close() raises.

@ambv ambv merged commit 96fed66 into python:main Oct 10, 2023
@miss-islington
Copy link
Contributor

Thanks @serhiy-storchaka for the PR, and @ambv for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 10, 2023
…contextmanager (pythonGH-110499)

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
(cherry picked from commit 96fed66)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 10, 2023
…contextmanager (pythonGH-110499)

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
(cherry picked from commit 96fed66)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Oct 10, 2023

GH-110588 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Oct 10, 2023
@bedevere-app
Copy link

bedevere-app bot commented Oct 10, 2023

GH-110589 is a backport of this pull request to the 3.11 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Oct 10, 2023
ambv pushed a commit that referenced this pull request Oct 10, 2023
…ccontextmanager (GH-110499) (#110588)

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
(cherry picked from commit 96fed66)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
ambv added a commit that referenced this pull request Oct 10, 2023
…ccontextmanager (GH-110499) (#110589)

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
(cherry picked from commit 96fed66)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
…contextmanager (pythonGH-110499)

contextmanager and asynccontextmanager context managers now close an invalid
underlying generator object that yields more then one value.
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.

3 participants