On Mon, Jun 24, 2019 at 4:03 AM James Lu <jam...@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
_______________________________________________
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/IOG5IH6QWKAEABUCPTLZHV7GDB424UM5/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to