Skip to content

Error on the Context Managers section ? #686

@ewjoachim

Description

@ewjoachim

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions