<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jan 22, 2019 at 3:11 PM Paul Ferrell <<a href="mailto:pflarr@gmail.com">pflarr@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I've found that almost any time I'm writing a 'with' block, it's doing<br>
something that could throw an exception. As a result, each of those<br>
'with' blocks needs to be nested within a 'try' block. Due to the<br>
nature of 'with', it is rarely (if ever) the case that the try block<br>
contains anything other than the with block itself.<br>
<br>
As a result, I would like to propose that the syntax for 'with' blocks<br>
be changed such that they can be accompanied by 'except', 'finally',<br>
and/or 'else' blocks as per a standard 'try' block. These would handle<br>
exceptions that occur in the 'with' block, including the execution of<br>
the applicable __enter__ and __exit__ methods.<br>
<br>
Example:<br>
<br>
try:<br>
    with open(path) as myfile:<br>
      ...   # Do stuff with file<br>
except (OSError, IOError) as err:<br>
    logger.error("Failed to read/open file {}: {}".format(path, err)<br>
<br>
The above would turn into simply:<br>
<br>
with open(path) as myfile:<br>
    ... # Do stuff with file<br>
except (OSError, IOError) as err:<br>
    logger.error(...)<br>
<br></blockquote><div><br></div><div>It definitely makes sense, both the problem and the proposed solution.</div><div><br></div><div>The thing that concerns me is that any such problem and solution seems</div><div>to apply equally to any other kind of block. Why not allow excepts on fo</div><div> loops, for example?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
I think this is rather straightforward in meaning and easy to read,<br>
and simplifies some unnecessary nesting. I see this as the natural<br>
evolution of what 'with'<br>
is all about - replacing necessary try-finally blocks with something<br>
more elegant. We just didn't include the 'except' portion.<br>
<br>
I'm a bit hesitant to put this out there. I'm not worried about it<br>
getting shot down - that's kind of the point here. I'm just pretty<br>
strongly against to unnecessary syntactical additions to the language.<br>
This though, I think I can except. It introduces no new concepts and<br>
requires no special knowledge to use. There's no question about what<br>
is going on when you read it.<br>
<br>
-- <br>
Paul Ferrell<br>
<a href="mailto:pflarr@gmail.com" target="_blank">pflarr@gmail.com</a><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><p style="color:rgb(0,0,0);font-family:overpass,sans-serif;font-weight:bold;margin:0px;padding:0px;font-size:14px;text-transform:uppercase">CALVIN SPEALMAN<br></p><p style="color:rgb(0,0,0);font-family:overpass,sans-serif;font-size:10px;margin:0px 0px 4px;text-transform:uppercase"><span>SENIOR QUALITY ENGINEER</span></p><span style="font-family:overpass,sans-serif;font-size:10px;margin:0px;color:rgb(153,153,153)"></span><span style="color:rgb(0,0,0);font-family:overpass,sans-serif;font-size:medium"></span><span style="color:rgb(0,0,0);font-family:overpass,sans-serif;font-size:medium"></span><p style="font-family:overpass,sans-serif;margin:0px 0px 6px;font-size:10px;color:rgb(153,153,153)"><span style="margin:0px;padding:0px"><a href="mailto:cspealma@redhat.com" target="_blank">cspealma@redhat.com</a> </span> <span>M: <a href="tel:+1.336.210.5107" target="_blank">+1.336.210.5107</a></span><span><span></span></span></p><a href="https://red.ht/sig" target="_blank"><img src="https://www.redhat.com/files/brand/email/sig-redhat.png" width="90" height="auto"></a><div><a href="https://redhat.com/trusted" style="color:rgb(204,0,0);font-weight:bold" target="_blank">TRIED. TESTED. TRUSTED.</a></div></div></div></div>