]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
bpo-41229: Update docs for explicit aclose()-required cases and add contextlib.aclosi...
authorJoongi Kim <joongi@lablup.com>
Mon, 2 Nov 2020 08:02:48 +0000 (17:02 +0900)
committerGitHub <noreply@github.com>
Mon, 2 Nov 2020 08:02:48 +0000 (00:02 -0800)
commit6e8dcdaaa49d4313bf9fab9f9923ca5828fbb10e
treefc23f29d61f7fa5d98c51f5adce53c9f32a726e4
parente9208f0e74d8c37542a750476ff272143fa8f67c
bpo-41229: Update docs for explicit aclose()-required cases and add contextlib.aclosing() method (GH-21545)

This is a PR to:

 * Add `contextlib.aclosing` which ia analogous to `contextlib.closing` but for async-generators with an explicit test case for [bpo-41229]()
 * Update the docs to describe when we need explicit `aclose()` invocation.

which are motivated by the following issues, articles, and examples:

 * [bpo-41229]()
 * https://github.com/njsmith/async_generator
 * https://vorpus.org/blog/some-thoughts-on-asynchronous-api-design-in-a-post-asyncawait-world/#cleanup-in-generators-and-async-generators
 * https://www.python.org/dev/peps/pep-0533/
 * https://github.com/achimnol/aiotools/blob/ef7bf0cea7af/src/aiotools/context.py#L152

Particuarly regarding [PEP-533](https://www.python.org/dev/peps/pep-0533/), its acceptance (`__aiterclose__()`) would make this little addition of `contextlib.aclosing()` unnecessary for most use cases, but until then this could serve as a good counterpart and analogy to `contextlib.closing()`. The same applies for `contextlib.closing` with `__iterclose__()`.
Also, still there are other use cases, e.g., when working with non-generator objects with `aclose()` methods.
Doc/library/contextlib.rst
Doc/reference/expressions.rst
Lib/contextlib.py
Lib/test/test_contextlib_async.py
Misc/NEWS.d/next/Library/2020-07-19-20-10-41.bpo-41229.p8rJa2.rst [new file with mode: 0644]