summaryrefslogtreecommitdiffstats
path: root/Lib/contextlib.py
Commit message (Collapse)AuthorAgeFilesLines
* gh-102799: use sys.exception() instead of sys.exc_info() in contextlib (#103311)Irit Katriel2023-07-201-22/+32
| | | Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* gh-105726: Add `__slots__` to `AbstractContextManager` and ↵Grigoriev Semyon2023-07-161-0/+4
| | | | | `AbstractAsyncContextManager` (#106771) Co-authored-by: Kumar Aditya <kumaraditya@python.org>
* gh-103791: Make contextlib.suppress also act on exceptions within an ↵Łukasz Langa2023-04-241-1/+10
| | | | | ExceptionGroup (#103792) Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
* gh-95882: fix regression in the traceback of exceptions propagated from ↵Thomas Grainger2023-01-031-1/+4
| | | | inside a contextlib context manager (#95883)
* gh-96348: Deprecate the 3-arg signature of coroutine.throw and ↵Ofey Chan2022-09-301-2/+2
| | | | generator.throw (GH-96428)
* gh-92118: fix traceback of exceptions propagated from inside a ↵Irit Katriel2022-05-041-0/+3
| | | | contextlib.contextmanager (GH-92202)
* bpo-38415: Remove redundant AsyncContextDecorator.__call__ override from ↵Thomas Grainger2022-02-261-8/+0
| | | | _AsyncGeneratorContextManager (GH-30233)
* bpo-25625: add contextlib.chdir (GH-28271)Filipe Laíns2021-10-191-1/+18
| | | | | | | | 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>
* bpo-44594: fix (Async)ExitStack handling of __context__ (gh-27089)John Belmonte2021-10-041-4/+4
| | | | | | | | | * bpo-44594: fix (Async)ExitStack handling of __context__ Make enter_context(foo()) / enter_async_context(foo()) equivalent to `[async] with foo()` regarding __context__ when an exception is raised. Previously exceptions would be caught and re-raised with the wrong context when explicitly overriding __context__ with None.
* bpo-38415: Allow using @asynccontextmanager-made ctx managers as decorators ↵Jason Fried2021-09-231-0/+8
| | | | (GH-16667)
* bpo-44566: resolve differences between asynccontextmanager and ↵Thomas Grainger2021-07-201-45/+59
| | | | contextmanager (#27024)
* bpo-44471: Change error type for bad objects in ExitStack.enter_context() ↵Serhiy Storchaka2021-06-291-6/+17
| | | | | | | | | (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.
* Fix typos in multiple files (GH-26689)Binbin2021-06-131-1/+1
| | | Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
* bpo-42395: Add aclosing to __all__ (GH-23356)Tom Gringauz2020-11-171-1/+1
| | | Automerge-Triggered-By: GH:asvetlov
* bpo-41543: contextlib.nullcontext can fill in for an async context manager ↵Tom Gringauz2020-11-091-1/+7
| | | | | (GH-21870) Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
* bpo-40816 Add AsyncContextDecorator class (GH-20516)Kazantcev Andrey2020-11-051-1/+24
| | | | Co-authored-by: Yury Selivanov <yury@edgedb.com>
* bpo-41229: Update docs for explicit aclose()-required cases and add ↵Joongi Kim2020-11-021-0/+26
| | | | | | | | | | | | | | | | | | | 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.
* bpo-39481: Implementation for PEP 585 (#18239)Guido van Rossum2020-04-071-1/+5
| | | | | | | | | | | | This implements things like `list[int]`, which returns an object of type `types.GenericAlias`. This object mostly acts as a proxy for `list`, but has attributes `__origin__` and `__args__` that allow recovering the parts (with values `list` and `(int,)`. There is also an approximate notion of type variables; e.g. `list[T]` has a `__parameters__` attribute equal to `(T,)`. Type variables are objects of type `typing.TypeVar`.
* [3.9] bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-12620)Serhiy Storchaka2019-06-051-34/+2
| | | Turn deprecation warnings added in 3.8 into TypeError.
* bpo-37116: Use PEP 570 syntax for positional-only parameters. (GH-13700)Serhiy Storchaka2019-06-011-4/+2
|
* bpo-36542: Allow to overwrite the signature for Python functions. (GH-12705)Serhiy Storchaka2019-05-061-0/+2
|
* bpo-36492: Deprecate passing some arguments as keyword arguments. (GH-12637)Serhiy Storchaka2019-04-011-4/+36
| | | | | | | | | | | | | | | | | | | | | | Deprecated passing the following arguments as keyword arguments: - "func" in functools.partialmethod(), weakref.finalize(), profile.Profile.runcall(), cProfile.Profile.runcall(), bdb.Bdb.runcall(), trace.Trace.runfunc() and curses.wrapper(). - "function" in unittest.addModuleCleanup() and unittest.TestCase.addCleanup(). - "fn" in the submit() method of concurrent.futures.ThreadPoolExecutor and concurrent.futures.ProcessPoolExecutor. - "callback" in contextlib.ExitStack.callback(), contextlib.AsyncExitStack.callback() and contextlib.AsyncExitStack.push_async_callback(). - "c" and "typeid" in the create() method of multiprocessing.managers.Server and multiprocessing.managers.SharedMemoryServer. - "obj" in weakref.finalize(). Also allowed to pass arbitrary keyword arguments (even "self" and "func") if the above arguments are passed as positional argument.
* bpo-33786: Fix asynchronous generators to handle GeneratorExit in athrow() ↵Yury Selivanov2018-06-081-1/+1
| | | | (GH-7467)
* bpo-33265: use an actual method instead of a method-like function in ↵jdemeyer2018-04-131-8/+3
| | | | | | ExitStack (GH-6456) `MethodType` has the exact semantics that `ExitStack` needs, so we can avoid creating a Python level closure.
* bpo-30306: release arguments of contextmanager (GH-1500)Martin Teichmann2018-01-281-0/+3
| | | | | | | | | | | | | | | | The arguments to a generator function which is declared as a contextmanager are stored inside the context manager, and thus are kept alive, even when it is used as a regular context manager, and not as a function decorator (where it needs the original arguments to recreate the generator on each call). This is a possible unnecessary memory leak, so this changes contextmanager.__enter__ to release the saved arguments, as that method being called means that particular CM instance isn't going to need to recreate the underlying generator. Patch by Martin Teichmann.
* bpo-29302: Implement contextlib.AsyncExitStack. (#4790)Ilya Kulakov2018-01-251-46/+205
|
* bpo-30241: implement contextlib.AbstractAsyncContextManager (#1412)Jelle Zijlstra2017-12-141-2/+25
|
* bpo-10049: Add a "no-op" (null) context manager to contextlib (GH-4464)Jesse-Bakker2017-11-231-1/+22
| | | | | Adds a simpler and faster alternative to ExitStack for handling single optional context managers without having to change the lexical structure of your code.
* bpo-30266: support "= None" pattern in AbstractContextManager (#1448)Jelle Zijlstra2017-06-091-3/+2
| | | | | contextlib.AbstractContextManager now supports anti-registration by setting __enter__ = None or __exit__ = None, following the pattern introduced in bpo-25958.
* bpo-29679: Implement @contextlib.asynccontextmanager (#360)Jelle Zijlstra2017-05-011-6/+93
|
* bpo-29692: contextlib.contextmanager may incorrectly unchain RuntimeError ↵svelankar2017-04-111-6/+6
| | | | | | | | | | (GH-949) contextlib._GeneratorContextManager.__exit__ includes a special case to deal with PEP 479 RuntimeErrors created when `StopIteration` is thrown into the context manager body. Previously this check was too permissive, and undid one level of chaining on *all* RuntimeError instances, not just those that wrapped a StopIteration instance.
* Clarify exception handler scope in contextlibamosonn2017-03-011-1/+2
| | | Moved explicit raise from inside try to try...else.
* bpo-27122: Fix comment to point to correct issue number (#47)Nathaniel J. Smith2017-02-121-1/+1
| | | | | | It took me quite a bit to figure out what this was referring to, since the given issue number is wrong, and the original commit message I found through git blame lists a different, also wrong issue number... see https://bugs.python.org/issue27122#msg279449
* Issue #27123: When an exception is raised within the context beingGregory P. Smith2016-06-141-0/+3
|\ | | | | | | | | | | | | managed by a contextlib.ExitStack() and one of the exit stack generators catches and raises it in a chain, do not re-raise the original exception when exiting, let the new chained one through. This avoids the PEP 479 bug described in issue25782.
| * Issue #27123: When an exception is raised within the context beingGregory P. Smith2016-06-141-0/+3
| | | | | | | | | | | | | | managed by a contextlib.ExitStack() and one of the exit stack generators catches and raises it in a chain, do not re-raise the original exception when exiting, let the new chained one through. This avoids the PEP 479 bug described in issue25782.
* | Normalize whitespaceBrett Cannon2016-04-081-1/+1
| |
* | Issue #25609: Introduce contextlib.AbstractContextManager andBrett Cannon2016-04-081-11/+31
|/ | | | typing.ContextManager.
* Issue #24336: The contextmanager decorator now works with functions withSerhiy Storchaka2015-06-281-3/+3
|\ | | | | | | keyword arguments called "func" and "self". Patch by Martin Panter.
| * Issue #24336: The contextmanager decorator now works with functions withSerhiy Storchaka2015-06-281-3/+3
| | | | | | | | keyword arguments called "func" and "self". Patch by Martin Panter.
* | PEP 479: Change StopIteration handling inside generators.Yury Selivanov2015-05-091-2/+9
| | | | | | | | Closes issue #22906.
* | Issue #22389: Add contextlib.redirect_stderr().Berker Peksag2014-11-281-13/+27
|/
* Merge removal of issue 20317 debugging code from 3.3Nick Coghlan2014-01-241-3/+1
|\
| * Issue 20317: Remove debugging code from contextlibNick Coghlan2014-01-241-3/+1
| | | | | | | | | | | | | | - Alex J Burke noticed a debugging raise in the commit that fixed the original bug reported in issue 20317 - this showed that multiple iterations through the affected loop wasn't actually being tested
* | Merge #20317 from 3.3Nick Coghlan2014-01-221-1/+9
|\ \ | |/
| * Issue #20317: Don't create a reference loop in ExitStackNick Coghlan2014-01-221-1/+9
| |
* | Close #19403: make contextlib.redirect_stdout reentrantNick Coghlan2013-11-031-8/+4
| |
* | Close #19330 by using public classes in contextlibNick Coghlan2013-10-261-41/+39
| | | | | | | | | | | | | | - added test cases to ensure docstrings are reasonable - also updates various comments in contextlib for accuracy - identifed #19404 as an issue making it difficult to provide good help output on generator based context manager instances
* | contextlib doc updates and refactoringNick Coghlan2013-10-191-29/+52
| | | | | | | | | | | | | | | | | | | | | | - explain single use, reusable and reentrant in docs - converted suppress to a reentrant class based impl - converted redirect_stdout to a reusable impl - moved both suppress and redirect_stdout behind a functional facade - added reentrancy tests for the updated suppress - added reusability tests for the updated redirect_stdio - slightly cleaned up an exception from contextmanager
* | Close #19266: contextlib.ignore -> contextlib.suppressNick Coghlan2013-10-171-4/+4
| | | | | | | | Patch by Zero Piraeus.
* | Rename contextlib.ignored() to contextlib.ignore().Raymond Hettinger2013-10-111-3/+3
| |