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-reentrant-context-managers
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/K7M62JLBCR5USTJV5IRILJNQEYSJQ4QY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to