Skip to content

Commit

Permalink
bpo-42669: Document that except rejects nested tuples (pythonGH-23822)
Browse files Browse the repository at this point in the history
In Python 2, it was possible to use `except` with a nested tuple, and occasionally natural.  For example, `zope.formlib.interfaces.InputErrors` is a tuple of several exception classes, and one might reasonably think to do something like this:

    try:
        self.getInputValue()
        return True
    except (InputErrors, SomethingElse):
        return False

As of Python 3.0, this raises `TypeError: catching classes that do not inherit from BaseException is not allowed` instead: one must instead either break it up into multiple `except` clauses or flatten the tuple.  However, the reference documentation was never updated to match this new restriction.  Make it clear that the definition is no longer recursive.

Automerge-Triggered-By: GH:ericvsmith
  • Loading branch information
cjwatson authored Dec 20, 2020
1 parent b0398a4 commit c95f8bc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Doc/reference/compound_stmts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ present, must be last; it matches any exception. For an except clause with an
expression, that expression is evaluated, and the clause matches the exception
if the resulting object is "compatible" with the exception. An object is
compatible with an exception if it is the class or a base class of the exception
object or a tuple containing an item compatible with the exception.
object, or a tuple containing an item that is the class or a base class of
the exception object.

If no except clause matches the exception, the search for an exception handler
continues in the surrounding code and on the invocation stack. [#]_
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,7 @@ Zachary Ware
Barry Warsaw
Steve Waterbury
Bob Watson
Colin Watson
David Watson
Aaron Watters
Henrik Weber
Expand Down

0 comments on commit c95f8bc

Please sign in to comment.