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

contextlib.aclosing() #84394

Closed
belm0 mannequin opened this issue Apr 7, 2020 · 9 comments
Closed

contextlib.aclosing() #84394

belm0 mannequin opened this issue Apr 7, 2020 · 9 comments
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@belm0
Copy link
Mannequin

belm0 mannequin commented Apr 7, 2020

BPO 40213
Nosy @ncoghlan, @belm0, @njsmith, @asvetlov, @agronholm, @serhiy-storchaka, @1st1, @corona10, @belm0
PRs
  • bpo-40213: add contextlib.aclosing() #22640
  • bpo-41229: Update docs for explicit aclose()-required cases and add contextlib.aclosing() method #21545
  • 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-11.08:36:18.251>
    created_at = <Date 2020-04-07.02:13:32.631>
    labels = ['type-feature', 'library', '3.10']
    title = 'contextlib.aclosing()'
    updated_at = <Date 2021-06-11.08:36:18.250>
    user = 'https://github.com/belm0'

    bugs.python.org fields:

    activity = <Date 2021-06-11.08:36:18.250>
    actor = 'John Belmonte'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-06-11.08:36:18.251>
    closer = 'John Belmonte'
    components = ['Library (Lib)']
    creation = <Date 2020-04-07.02:13:32.631>
    creator = 'John Belmonte'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40213
    keywords = ['patch']
    message_count = 9.0
    messages = ['365885', '365894', '372905', '372908', '372909', '372910', '372911', '377257', '395618']
    nosy_count = 10.0
    nosy_names = ['ncoghlan', 'jbelmonte', 'njs', 'asvetlov', 'alex.gronholm', 'serhiy.storchaka', 'yselivanov', 'nikicat', 'corona10', 'John Belmonte']
    pr_nums = ['22640', '21545']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40213'
    versions = ['Python 3.10']

    @belm0
    Copy link
    Mannequin Author

    belm0 mannequin commented Apr 7, 2020

    Please add aclosing() to contextlib, the async equivalent of closing().

    It's needed to ensure deterministic call of aclose() on the resource object at block exit.

    It's been available in the async_generator module for some time. However that module is intended to provide async generators to Python 3.5, so it's odd for apps using modern Python versions to depend on it only for aclosing().

    https://github.com/python-trio/async_generator/blob/22eddc191c2ae3fc152ca13cf2d6fa55ac3f1568/async_generator/_util.py#L6

    @belm0 belm0 mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Apr 7, 2020
    @agronholm
    Copy link
    Mannequin

    agronholm mannequin commented Apr 7, 2020

    Seconded.

    @serhiy-storchaka
    Copy link
    Member

    Do you have use cases or want to add it for pure "symmetry" reasons?

    contextlib.closing() was added when context managers were new, and many classes that own resources did have the close() method but did not support the context manager protocol. Now most of these classes are context managers, and new classes usually are written with the support of the context manager protocol from beginning. The usefulness of contextlib.closing() is much smaller now.

    @agronholm
    Copy link
    Mannequin

    agronholm mannequin commented Jul 3, 2020

    They are both still useful, particularly with third party libraries. Just yesterday I found myself looking for aclosing() in contextlib, only to remember this issue.

    @belm0
    Copy link
    Mannequin Author

    belm0 mannequin commented Jul 3, 2020

    Given the lack of deterministic cleanup for iterators (https://www.python.org/dev/peps/pep-0533/), aclosing() is the way to ensure deterministic cleanup given any API using async iteration.

    @belm0
    Copy link
    Mannequin Author

    belm0 mannequin commented Jul 3, 2020

    highlighting from PEP-533:

    Async generators cannot do cleanup at all without some mechanism for deterministic cleanup that people will actually use, and async generators are particularly likely to hold resources like file descriptors.

    @agronholm
    Copy link
    Mannequin

    agronholm mannequin commented Jul 3, 2020

    I think the most important use case for these is closing async generators deterministically, since they don't support the async context manager protocol.

    @corona10
    Copy link
    Member

    @belm0 belm0 mannequin added 3.10 only security fixes and removed 3.9 only security fixes labels Oct 11, 2020
    @belm0
    Copy link
    Mannequin Author

    belm0 mannequin commented Jun 11, 2021

    merged for Python 3.10

    @belm0 belm0 mannequin closed this as completed Jun 11, 2021
    @belm0 belm0 mannequin closed this as completed Jun 11, 2021
    @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.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants