PEP 3110 – Catching Exceptions in Python 3000
- Catching Exceptions in Python 3000
- Collin Winter <collinwinter at google.com>
- Standards Track
This PEP introduces changes intended to help eliminate ambiguities in Python’s grammar, simplify exception classes, simplify garbage collection for exceptions and reduce the size of the language in Python 3.0.
exceptclauses in Python 2.x present a syntactic ambiguity where the parser cannot differentiate whether
except <expression>, <expression>:
should be interpreted as
except <type>, <type>:
except <type>, <name>:
Python 2 opts for the latter semantic, at the cost of requiring the former to be parenthesized, like so
except (<type>, <type>):
- As specified in PEP 352, the ability to treat exceptions
as tuples will be removed, meaning this code will no longer work
except os.error, (errno, errstr):
Because the automatic unpacking will no longer be possible, it is desirable to remove the ability to use tuples as
- As specified in PEP 344, exception instances in Python 3
will possess a
__traceback__attribute. The Open Issues section of that PEP includes a paragraph on garbage collection difficulties caused by this attribute, namely a “exception -> traceback -> stack frame -> exception” reference cycle, whereby all locals are kept in scope until the next GC run. This PEP intends to resolve this issue by adding a cleanup semantic to
exceptclauses in Python 3 whereby the target name is deleted at the end of the
- In the spirit of “there should be one – and preferably only one
– obvious way to do it”, it is desirable to consolidate
duplicate functionality. To this end, the
exc_tracebackattributes of the
sysmodule  will be removed in favor of
sys.exc_info(), which provides the same information. These attributes are already listed in PEP 3100 as targeted for removal.
In Python 3, the grammar for
except statements will change
except_clause: 'except' [test [',' test]]
except_clause: 'except' [test ['as' NAME]]
The use of
as in place of the comma token means that
except (AttributeError, os.error):
can be clearly understood as a tuple of exception classes. This new syntax was first proposed by Greg Ewing  and endorsed (, ) by the BDFL.
Further, the restriction of the token following
NAME means that only valid identifiers can be used as
Note that the grammar above always requires parenthesized tuples as exception classes. That way, the ambiguous
except A, B:
which would mean different things in Python 2.x and 3.x – leading to hard-to-catch bugs – cannot legally occur in 3.x code.
In order to resolve the garbage collection issue related to PEP 344,
except statements in Python 3 will generate additional bytecode to
delete the target, thus eliminating the reference cycle.
The source-to-source translation, as suggested by Phillip J. Eby
try: try_body except E as N: except_body ...
gets translated to (in Python 2.5 terms)
try: try_body except E, N: try: except_body finally: N = None del N ...
An implementation has already been checked into the py3k (formerly “p3yk”) branch .
except clauses will need to be changed.
clauses with identifier targets will be converted from
except E, N:
except E as N:
except clauses with non-tuple, non-identifier targets
a.b.c[d]) will need to be converted from
except E, T:
except E as t: T = t
Both of these cases can be handled by Guido van Rossum’s
utility  using the
except fixer .
except clauses with tuple targets will need to be converted
manually, on a case-by-case basis. These changes will usually need
to be accompanied by changes to the exception classes themselves.
While these changes generally cannot be automated, the
utility is able to point out cases where the target of an
clause is a tuple, simplifying conversion.
Situations where it is necessary to keep an exception instance around
past the end of the
except suite can be easily translated like so
try: ... except E as N: ... ...
try: ... except E as N: n = N ... ...
This way, when
N is deleted at the end of the block,
persist and can be used as normal.
Lastly, all uses of the
exc_traceback attributes will need to be removed. They can be
sys.exc_info() respectively, a transformation that can be
2.6 - 3.0 Compatibility
In order to facilitate forwards compatibility between Python 2.6 and 3.0,
except ... as ...: syntax will be backported to the 2.x series. The
grammar will thus change from:
except_clause: 'except' [test [',' test]]
except_clause: 'except' [test [('as' | ',') test]]
The end-of-suite cleanup semantic for
except statements will not be
included in the 2.x series of releases.
Replacing or Dropping “sys.exc_info()”
The idea of dropping
sys.exc_info() or replacing it with a
sys.exception attribute or a
has been raised several times on python-3000 (,
) and mentioned in PEP 344’s “Open Issues” section.
2to3 fixer to replace calls to
and some attribute accesses would be trivial, it would be far more
difficult for static analysis to find and fix functions that expect
the values from
sys.exc_info() as arguments. Similarly, this does
not address the need to rewrite the documentation for all APIs that
are defined in terms of
This PEP was implemented in revisions 53342  and 53349
. Support for the new
except syntax in 2.6 was
implemented in revision 55446 .
-  (1, 2)
This document has been placed in the public domain.
Last modified: 2022-01-21 11:03:51 GMT