On Mon, Jul 29, 2019 at 2:58 PM Guido van Rossum <gu...@python.org> wrote:

> I am *guessing* the problem here is something like this:
>
> with open(filename) as f:
>     data = f.read()
>
> raises an exception if the open() call fails, but putting try... except
> IOError: ... around the whole thing also catches I/O errors in the read()
> call (or anything else you might put in the body). Other solutions are more
> verbose and run other risks. For other types of context managers it may be
> worse because the error you want to catch occurs in the __enter__() call
> that's implicit in the with-clause (for open(), __enter__() is just "return
> self" since all the work happens before).
>

Isn't the appropriate solution to that one as follows?  It's pretty clear
what you're trying to do, so the extra couple of lines aren't all that
obtrusive.

try:
    f = open('x', 'r')
except IOError as whatever:
    handle whatever
else:
    with f:
        data = f.read()
_______________________________________________
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/Q3ULDZG6FSNQPHRNKTL7CC4BGF2AEAXW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to