
On Thursday, April 16, 2015 12:33 AM, James Edwards <jheiv@jheiv.com> wrote:
On Thu, Apr 16, 2015 at 3:22 AM, Andrew Barnert <abarnert@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.)