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

Error on the Context Managers section ? #686

Closed
ewjoachim opened this issue Mar 14, 2016 · 1 comment
Closed

Error on the Context Managers section ? #686

ewjoachim opened this issue Mar 14, 2016 · 1 comment

Comments

@ewjoachim
Copy link
Contributor

So what's said here is :

from contextlib import contextmanager

@contextmanager
def custom_open(filename):
    f = open(filename)
    yield f
    f.close()

with custom_open('file') as f:
    contents = f.read()

But as far as I undestand, we shoud wrap the yield into a try: block with the close in the finally:, otherwise a exception might allow leaving the with block without calling close. I might do a PR for this, but I wanted to check with someone if there was a reason the code was shown this way and/or if I was mistaken altogether...

@kennethreitz
Copy link
Contributor

Without looking too much into this, it sounds like a good improvement.

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

No branches or pull requests

2 participants