[Python-Dev] Implementation of PEP 341
ncoghlan at gmail.com
Sun Nov 13 13:27:26 CET 2005
Thomas Lee wrote:
> Hi all,
> I've been using Python for a few years and, as of a few days ago,
> finally decided to put the effort into contributing code back to the
> I'm attempting to implement PEP 341 (unification of try/except and
> try/finally) against HEAD. However, this being my first attempt at a
> change to the syntax there's been a bit of a learning curve.
Thanks for having a go at this.
> I've modified Grammar/Grammer to use the new try_stmt grammar, updated
> Parser/Python.asdl to accept a stmt* finalbody for TryExcept instances
> and modified Python/ast.c to handle the changes to Python.asdl -
> generating an AST for the finalbody.
Consider leaving the AST definition alone, and simply changing the frontend
parser to process:
almost precisely as if it were written:
That is, generate a TryExcept inside a TryFinally at the AST level, rather
than trying to give TryExcept the ability to handle a finally block directly.
Specifically, if you've determined that a finally clause is present in the
extended statement in Python/ast.c, do something like:
inner_seq = asdl_seq_new(1)
TryExcept(body_seq, handlers, else_seq, LINENO(n))
return TryFinally(inner_seq, finally_seq, LINENO(n))
body_seq and else_seq actually have meaningful names like suite_seq1 and
suite_seq2 in the current code ;)
Semantics-wise, this is exactly the behaviour we want, and making it pure
syntactic sugar means the backend doesn't need to care about the new syntax at
all. It also significantly lessens the risk of the change causing any problems
in the compilation of normal try-except blocks.
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev