
June 23, 2019
1:14 p.m.
On Mon, Jun 24, 2019 at 4:03 AM James Lu <jamtlu@gmail.com> wrote:
If a function that tends to return a context manager returns None, that should not mean an error occurred. If an error or unexpected condition occurred, an exception should be thrown. Errors and exceptions should result in the code within the with statement not executing.
We could add a new constant to Python, “Dont.” It’s a global falsey Singleton and a context manager that causes the code inside “with” not to execute.
A cleaner way to handle this would be an exception. Unfortunately, the simple trick of suppressing the exception in __exit__ doesn't work, as __exit__ isn't called if __enter__ itself fails. Maybe you can use ExitStack to manage this? ChrisA