From b8122feb3276a8a495c31c7a1109b2ae12293b51 Mon Sep 17 00:00:00 2001 From: David Goodger Date: Sat, 7 May 2005 12:13:49 +0000 Subject: [PATCH] added PEP 341, "Unifying try-except and try-finally", by Reinhold Birkenfeld --- pep-0000.txt | 3 ++ pep-0341.txt | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 pep-0341.txt diff --git a/pep-0000.txt b/pep-0000.txt index a405c2fba..50b721edb 100644 --- a/pep-0000.txt +++ b/pep-0000.txt @@ -118,6 +118,7 @@ Index by Category S 337 Logging Usage in the Standard Library Dubner S 338 Executing modules inside packages with '-m' Coghlan S 340 Anonymous Block Statements GvR + S 341 Unifying try-except and try-finally Birkenfeld S 754 IEEE 754 Floating Point Special Values Warnes Finished PEPs (done, implemented in CVS) @@ -376,6 +377,7 @@ Numerical Index S 338 Executing modules inside packages with '-m' Coghlan I 339 How to Change CPython's Bytecode Cannon S 340 Anonymous Block Statements GvR + S 341 Unifying try-except and try-finally Birkenfeld SR 666 Reject Foolish Indentation Creighton S 754 IEEE 754 Floating Point Special Values Warnes I 3000 Python 3.0 Plans Kuchling, Cannon @@ -405,6 +407,7 @@ Owners Batista, Facundo facundo@taniquetil.com.ar Baxter, Anthony anthony@interlink.com.au Bellman, Thomas bellman+pep-divmod@lysator.liu.se + Birkenfeld, Reinhold reinhold-birkenfeld-nospam@wolke7.net Cannon, Brett brett@python.org Carlson, Josiah jcarlson@uci.edu Carroll, W Isaac icarroll@pobox.com diff --git a/pep-0341.txt b/pep-0341.txt new file mode 100644 index 000000000..6574d96d5 --- /dev/null +++ b/pep-0341.txt @@ -0,0 +1,116 @@ +PEP: 341 +Title: Unifying try-except and try-finally +Version: $Revision$ +Last-Modified: $Date$ +Author: Reinhold Birkenfeld +Status: Draft +Type: Standards Track +Content-Type: text/plain +Created: 04-May-2005 +Post-History: + + +Abstract + + This PEP proposes a change in the syntax and semantics of try + statements to allow combined try-except-finally blocks. This + means in short that it would be valid to write + + try: + + except Exception: + + finally: + + + +Rationale/Proposal + + There are many use cases for the try-except statement and + for the try-finally statement per se; however, often one needs + to catch exceptions and execute some cleanup code afterwards. + It is slightly annoying and not very intelligible that + one has to write + + f = None + try: + try: + f = open(filename) + text = f.read() + except IOError: + print 'An error occured' + finally: + if f: + f.close() + + So it is proposed that a construction like this + + try: + + except Ex1: + + + else: + + finally: + + + be exactly the same as the legacy + + try: + try: + + except Ex1: + + + else: + + finally: + + + This is backwards compatible, and every try statement that is + legal today would continue to work. + + +Changes to the grammar + + The grammar for the try statement, which is currently + + try_stmt: ('try' ':' suite (except_clause ':' suite)+ + ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite) + + would have to become + + try_stmt: ('try' ':' suite (except_clause ':' suite)+ + ['else' ':' suite] ['finally' ':' suite] | + 'try' ':' suite (except_clause ':' suite)* + ['else' ':' suite] 'finally' ':' suite) + + +Implementation + + As the PEP author currently does not have sufficient knowledge + of the CPython implementation, he is unfortunately not able + to deliver one. + + However, according to Guido, it should be a piece of cake to + implement[1] -- at least for a core hacker. + + +References + + [1] http://mail.python.org/pipermail/python-dev/2005-May/053319.html + + +Copyright + + This document has been placed in the public domain. + + + +Local Variables: +mode: indented-text +indent-tabs-mode: nil +sentence-end-double-space: t +fill-column: 70 +End: