Make decorator-based context managers reentrant

Hi, I was wondering why decorator-based context managers are not reentrant or even reusable. They could be made reusable by initializing the generator in __enter__() instead of __init__(). They could be made reentrant by storing generatos in a stack. A similar thing has been implemented for redirect_stdout in 8e113b418df7d0c8480e1e2de29a385e1f31b15b. I understand that not all generator-based context managers can be reentrant by their nature. But currently it is impossible to make them reentrant, which IMHO is an unnecessary restriction. I could find very little discussion on this. Are there any strong reasons why it was implemented this way or should I create a pull request to change it? (My specific use case is a TUI application where I want to make sure that the terminal is restored to a usable state on exit, but also need to step in and out of the TUI on SIGTSTP/SIGCONT.) thanks, tobias https://docs.python.org/3/library/contextlib.html#single-use-reusable-and-re...


On 30/03/2023 09.21, Barry wrote:
I mean mean it as used in the docs: More sophisticated context managers may be “reentrant”. These context managers can not only be used in multiple with statements, but may also be used inside a with statement that is already using the same context manager.


On 30/03/2023 09.21, Barry wrote:
I mean mean it as used in the docs: More sophisticated context managers may be “reentrant”. These context managers can not only be used in multiple with statements, but may also be used inside a with statement that is already using the same context manager.
participants (2)
-
Barry
-
Tobias Bengfort