[Python-ideas] with-except-finally blocks
Andrew Barnert
abarnert at yahoo.com
Thu Apr 16 09:56:18 CEST 2015
On Thursday, April 16, 2015 12:33 AM, James Edwards <jheiv at jheiv.com> wrote:
> > On Thu, Apr 16, 2015 at 3:22 AM, Andrew Barnert
> <abarnert at yahoo.com.dmarc.invalid> wrote:
>> Anyway, I like the idea, but does the grammar work? A with that has an
> optional except and finally at the same level seems potentially ambiguous. For
> example:
>>
>> with spam:
>> pass
>> with eggs:
>> pass
>> finally:
>> pass
>
> Is this any more (or less) ambiguous than:
>
> if cond1:
> pass
> if cond2:
> pass
> else:
> pass
>
> And we have no issue with that (at least not syntactically).
Duh.
Just to make sure, I hacked up a quick patch. (Diff attached.) This gets you enough to test the syntax:
$ ./python.exe
>>> import ast
>>> print(ast.dump(ast.parse('with:\n pass\nwith:\n pass\nfinally:\n pass\n')))
Module(body=[With(items=[withitem(context_expr=Name(id='spam', ctx=Load()), optional_vars=None)], body=[Pass()], handlers=[], orelse=[], finalbody=[]), With(items=[withitem(context_expr=Name(id='eggs', ctx=Load()), optional_vars=None)], body=[Pass()], handlers=[], orelse=[], finalbody=[]), With(items=[], body=[Pass()], handlers=[], orelse=[], finalbody=[Pass()])])
That's one With statement with no except/else/finally, and one With statement with a finally, just as you'd expect.
(Of course the compiler just ignores the except/else/finally bits.)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: with.diff
Type: application/octet-stream
Size: 17056 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150416/ae27bbe9/attachment-0001.obj>
More information about the Python-ideas
mailing list