Commit Graph

105 Commits

Author SHA1 Message Date
Hugo van Kemenade f31a89bb90 Python 3.15.0b1 2026-05-07 16:26:31 +03:00
Alex Grönholm 29251396a9 gh-125862: Keep ContextDecorator open across generator/coroutine execution (GH-136212)
ContextDecorator and AsyncContextDecorator (and therefore @contextmanager
and @asynccontextmanager used as decorators) now detect generator,
coroutine, and asynchronous generator functions and emit a wrapper of the
matching kind, so the context manager spans iteration or await rather than
just the call that constructs the lazy object.  Wrapped generators are
explicitly closed when iteration ends.

For asynchronous generator wrappers, values passed via asend() and
exceptions via athrow() are not forwarded to the wrapped generator.

AsyncContextDecorator now also accepts synchronous functions and
generators, returning an asynchronous wrapper; ContextDecorator remains
the recommended choice for those.

inspect.isgeneratorfunction(), iscoroutinefunction(), and
isasyncgenfunction() now return True for the decorated result when the
input is of that kind.

---------

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2026-04-28 05:26:38 +00:00
C.A.M. Gerlach 9124bc194a Hide "object" prefix on dunders in contextlib docs & selectivly link some more (#145436) 2026-03-02 23:10:26 +02:00
Hugo van Kemenade 15b216f30d Python 3.15.0a6 2026-02-11 14:23:15 +02:00
Ned Batchelder 638d22c6e7 Docs: module pages should not link to themselves (#144505)
* Docs: module pages should not link to themselves

* fix header punctuation
2026-02-06 06:48:27 -05:00
Serhiy Storchaka f73d2e7003 gh-144386: Add support for descriptors in ExitStack and AsyncExitStack (#144420)
__enter__(), __exit__(), __aenter__(), and __aexit__() can now be
arbitrary descriptors, not only normal methods, for consistency with the
"with" and "async with" statements.
2026-02-04 13:20:18 +02:00
Cody Maloney 8b9606a2c5 gh-139452: Clarify redirect_stdout, stderr behavior (gh-139490) 2025-10-08 08:28:29 -05:00
Liam DeVoe 4a88bbacb3 Docs: Add a missing trailing full stop (#130561) 2025-04-10 14:51:20 +03:00
sobolevn 5ec4bf86b7 gh-121970: Replace .. coroutine{method,function} with :async: (#130448)
Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
2025-02-22 17:54:43 +00:00
Jelle Zijlstra 9328db7652 Fix indentation for contextlib.asynccontextmanager docs (#127333) 2024-11-27 06:38:55 -08:00
Ville Skyttä bc264eac3a Docs: spelling and grammar fixes (#122084)
Corrected some grammar and spelling issues in documentation.

Co-authored-by: Russell Keith-Magee <russell@keith-magee.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-07-22 09:14:25 +08:00
Irit Katriel 5c02ea8bae gh-119287: clarify doc on BaseExceptionGroup.derive and link to it from contextlib.suppress (#119657) 2024-06-05 11:56:01 +00:00
Zachary Ware f3fc800d5f contextlib docs: Clean up redundant 'up' after 'cleanup' (GH-119867)
Reported by Michael Kass on docs@
2024-05-31 17:19:54 +00:00
Daniel Hollas 82cd8fee31 Fix a typo in the contextlib documentation (#114507) 2024-01-24 05:16:31 +01:00
Ville Skyttä 9af9ac153a gh-66944: Note that the contextlib.closing example is for illustrative purposes (#112198)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
2024-01-23 01:00:53 +00:00
Alex Waygood e4c0876033 gh-101100: Fix Sphinx nits in library/contextlib.rst (#112870) 2023-12-08 13:17:57 +00:00
Zac Hatfield-Dodds d61313bdb1 gh-103791: handle BaseExceptionGroup in contextlib.suppress() (#111910) 2023-11-10 13:32:36 +00:00
Adam Turner 63acf78d71 GH-101100: Fix reference warnings for `__enter__ and __exit__` (#110112) 2023-10-19 18:05:17 +03:00
Łukasz Langa 22bed58e53 gh-103791: Make contextlib.suppress also act on exceptions within an ExceptionGroup (#103792)
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
2023-04-24 22:17:02 +00:00
Stanley 3b33c2010a Formatting fixes in contextlib docs (#98111) 2022-10-11 20:56:32 -07:00
luzpaz 830513754d gh-93675: Fix typos in Doc/ (GH-93676)
Closes #93675
2022-06-21 21:10:45 +02:00
Thaddeus1499 958f21c5cd gh-92082: contextlib docs: Change aclosing from a class to a function for consistency (#92155)
Signed-off-by: prwatson <prwatson@redhat.com>
2022-05-02 10:20:30 -06:00
vidhya 86384cf83f bpo-28516: document contextlib.ExitStack.__enter__ behavior (GH-31636)
The enter_context is updated with following information: 'The :meth:`__enter__` method 
      returns the ExitStack instance, and performs no additional operations.'

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2022-03-27 21:31:32 -07:00
Zbigniew Siciarz 4dd82194f4 bpo-45568: Actually use @asynccontextmanager in usage example (GH-29151)
Automerge-Triggered-By: GH:asvetlov
2021-11-25 04:30:48 -08:00
Thomas Grainger a774285e7d bpo-25625: [doc] fix async/aync typo (GH-29091) 2021-10-20 15:51:07 +02:00
Filipe Laíns 3592980f91 bpo-25625: add contextlib.chdir (GH-28271)
Added non parallel-safe :func:`~contextlib.chdir` context manager to change
the current working directory and then restore it on exit. Simple wrapper
around :func:`~os.chdir`.

Signed-off-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Łukasz Langa <lukasz@langa.pl>
2021-10-20 00:19:27 +02:00
Noah Kantrowitz be42c06bb0 Update URLs in comments and metadata to use HTTPS (GH-27458) 2021-07-30 15:54:46 +02:00
Julien Palard 86eeeb4259 Doc: fix a rst tag. (GH-26965) 2021-06-30 11:31:04 +02:00
Serhiy Storchaka 6cb145d23f bpo-44471: Change error type for bad objects in ExitStack.enter_context() (GH-26820)
A TypeError is now raised instead of an AttributeError in
ExitStack.enter_context() and AsyncExitStack.enter_async_context()
for objects which do not support the context manager or
asynchronous context manager protocols correspondingly.
2021-06-29 11:28:15 +03:00
Irit Katriel dda9ecbfec bpo-44279: revert 'exceptions are raised' back to 'exceptions occur' (GH-26492) 2021-06-04 23:07:57 +01:00
MapleCCC 87272b70f1 bpo-44279: [doc] reword contextlib.suppress documentation (GH-26428) 2021-06-01 21:15:30 +01:00
Peter Law 46db39d7bd bpo-41147: [doc] contextlib.redirect_stdout() provides the new stream as context var (GH-21199) 2021-05-26 15:13:09 +01:00
naglis c054e8f78f bpo-40975: [doc] Identify AsyncExitStack.enter_async_context()/aclose() as coroutine methods (GH-20870) 2021-05-19 21:36:05 +01:00
Andre Delfino 52cd6d5e1b Use the zero argument form of super() in examples for Python3 docs. (GH-22314) 2021-04-26 15:13:54 -07:00
Tom Gringauz a117167d8d bpo-41543: contextlib.nullcontext can fill in for an async context manager (GH-21870)
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
2020-11-09 14:34:07 +02:00
kj 133aa2d581 [docs] fix wrongly named AsyncContextDecorator (GH-23164)
Also added versionchanged.
2020-11-05 18:16:27 +02:00
Kazantcev Andrey 178695b7ae bpo-40816 Add AsyncContextDecorator class (GH-20516)
Co-authored-by: Yury Selivanov <yury@edgedb.com>
2020-11-05 10:52:24 +02:00
Joongi Kim 6e8dcdaaa4 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.
2020-11-02 00:02:48 -08:00
Serhiy Storchaka 142566c028 [3.9] bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-12620)
Turn deprecation warnings added in 3.8 into TypeError.
2019-06-05 18:22:31 +03:00
Serhiy Storchaka 2085bd0877 bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-13700) 2019-06-01 11:00:15 +03:00
Serhiy Storchaka 2b57c43f21 bpo-35506: Remove redundant and incorrect links from keywords. (GH-11174) 2018-12-19 08:09:46 +02:00
Nathaniel J. Smith a3c88ef12c Clarify that AsyncExitStack works with coroutine functions (GH-9405)
The docs were ambiguous about whether you pass in a coroutine function
or a coroutine object, e.g. is it:

  aestack.push_async_exit(some_async_func)

or

  aestack.push_async_exit(some_async_func())

(It's the first one.)
2018-09-18 14:27:59 -07:00
Alexander Vasin 416cbce22d Fixed typo with asynccontextmanager code example (GH-8845)
`yield conn`, instead of just `yield`.
2018-08-24 19:38:11 -07:00
Matthias Bussonnier bde782bb59 bpo-33468: Add try-finally contextlib.contextmanager example (GH-7816) 2018-07-24 00:10:56 +03:00
Daniel Porteous c287545d62 bpo-34067: Include a more easily understood example for nullcontext (GH-8158)
Include a more easily understood example for nullcontext
2018-07-09 23:49:29 +10:00
Ilya Kulakov 1aa094f740 bpo-29302: Implement contextlib.AsyncExitStack. (#4790) 2018-01-25 15:51:18 -05:00
Jelle Zijlstra 176baa326b bpo-30241: implement contextlib.AbstractAsyncContextManager (#1412) 2017-12-13 20:19:17 -05:00
Jesse-Bakker 0784a2e5b1 bpo-10049: Add a "no-op" (null) context manager to contextlib (GH-4464)
Adds a simpler and faster alternative to ExitStack for handling
single optional context managers without having to change the
lexical structure of your code.
2017-11-23 10:23:28 +10:00
Jelle Zijlstra 2e624690bd bpo-29679: Implement @contextlib.asynccontextmanager (#360) 2017-04-30 18:25:58 -07:00
Vinay Sajip dd917f84e3 Closes #27904: Improved logging statements to defer formatting until needed. 2016-08-31 08:22:29 +01:00