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

Document why generators don't support the context management protocol #58022

Closed
yak mannequin opened this issue Jan 18, 2012 · 10 comments
Closed

Document why generators don't support the context management protocol #58022

yak mannequin opened this issue Jan 18, 2012 · 10 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@yak
Copy link
Mannequin

yak mannequin commented Jan 18, 2012

BPO 13814
Nosy @terryjreedy, @ncoghlan, @ezio-melotti, @bitdancer, @meadori, @vadmium, @miss-islington
PRs
  • bpo-13814: Explain why generators are not context managers #26835
  • [3.10] bpo-13814: Explain why generators are not context managers (GH-26835) #26836
  • [3.9] bpo-13814: Explain why generators are not context managers (GH-26835) #26837
  • Files
  • design.patch: This should add in some documentation to the design faqs
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-06-21.22:03:33.244>
    created_at = <Date 2012-01-18.10:43:15.677>
    labels = ['3.11', 'type-feature', '3.9', '3.10', 'docs']
    title = "Document why generators don't support the context management protocol"
    updated_at = <Date 2021-06-26.14:49:27.526>
    user = 'https://bugs.python.org/yak'

    bugs.python.org fields:

    activity = <Date 2021-06-26.14:49:27.526>
    actor = 'terry.reedy'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2021-06-21.22:03:33.244>
    closer = 'terry.reedy'
    components = ['Documentation']
    creation = <Date 2012-01-18.10:43:15.677>
    creator = 'yak'
    dependencies = []
    files = ['38955']
    hgrepos = []
    issue_num = 13814
    keywords = ['patch']
    message_count = 10.0
    messages = ['151530', '151532', '151717', '151763', '240820', '396285', '396290', '396293', '396294', '396552']
    nosy_count = 9.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'ezio.melotti', 'r.david.murray', 'meador.inge', 'docs@python', 'yak', 'martin.panter', 'miss-islington']
    pr_nums = ['26835', '26836', '26837']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13814'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    @yak
    Copy link
    Mannequin Author

    yak mannequin commented Jan 18, 2012

    Generators should support the with statement with __exit__ calling self.close().

    with genfunc() as g:
        for item in g:
            print(item)

    @yak yak mannequin added the type-feature A feature request or enhancement label Jan 18, 2012
    @ezio-melotti
    Copy link
    Member

    If you want to call .close() automatically on something you can use contextlib.closing(): http://docs.python.org/library/contextlib.html#contextlib.closing

    @terryjreedy
    Copy link
    Member

    Calling g.close() is pointless for a generator used in normal pull mode and run to completion, as in the example. The generator is already 'closed', so g.close() does not do anything useful. See
    http://docs.python.org/py3k/reference/expressions.html#yield-expressions

    The method was added to be used with .send() so that generators used in push mode could be told to finish up when there is nothing more to send. For such rare uses, contextlib.closing should usually be sufficient, I think.

    So I think this issue should be closed.

    @ncoghlan
    Copy link
    Contributor

    Generators deliberately don't support the context management protocol. This is so that they raise an explicit TypeError or AttributeError (pointing out that __exit__ is missing) if you leave out the @contextmanager decorator when you're using a generator to write an actual context manager.

    Generators supporting the context management protocol natively would turn that into a far more subtle (and confusing) error: your code would silently fail to invoke the generator body.

    Ensuring this common error remains easy to detect is far more important than making it easier to invoke close() on a generator object (particularly when contextlib.closing() already makes that very easy).

    @ncoghlan ncoghlan added the docs Documentation in the Doc dir label Jan 22, 2012
    @ncoghlan ncoghlan changed the title Generators as context managers. Document why generators don't support the context management protocol Jan 22, 2012
    @bitdancer
    Copy link
    Member

    Looks like the right approach, I hadn't thought of the design FAQ, but it makes sense as a place to put it. I made a couple of review comments.

    @iritkatriel iritkatriel added easy 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Jun 21, 2021
    @terryjreedy
    Copy link
    Member

    I added a simplified answer after a similar question about assignment and CMs.

    @terryjreedy terryjreedy removed the easy label Jun 21, 2021
    @terryjreedy
    Copy link
    Member

    New changeset 51f45d0 by Terry Jan Reedy in branch 'main':
    bpo-13814: Explain why generators are not context managers (GH-26835)
    51f45d0

    @terryjreedy
    Copy link
    Member

    New changeset 1e16217 by Miss Islington (bot) in branch '3.10':
    bpo-13814: Explain why generators are not context managers (GH-26835)
    1e16217

    @terryjreedy
    Copy link
    Member

    New changeset d881002 by Miss Islington (bot) in branch '3.9':
    bpo-13814: Explain why generators are not context managers (GH-26835)
    d881002

    @terryjreedy
    Copy link
    Member

    Note: Rietveld patch reviews are no longer accessible so I could not look at R. David's comments.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants