Comment 5 for bug 1102593

Revision history for this message
Barry Warsaw (barry) wrote :

Found it!

Here's the problem: in Python 3, the targets of except clauses are deleted from the current namespace after the exception is handled. This is so the circular references created during the exception handling are eliminated. This is detailed in the Python 3 language reference: http://docs.python.org/3/reference/compound_stmts.html#except

The tricky thing here is that `e = None` set before the try/except doesn't help because if one of the excepts get triggered, the equivalent of `del e` is executed, which removes it from the namespace and causes the UnboundLocalError. FWIW, the `del e` behavior does not occur in Python 2, so this wouldn't crash. It's certainly an odd corner case of the language.

The fix is to use a different name to capture the exception for later printing than is used in the except clause. Should be an easy fix and I'll update trunk. Attached is a boiled down example.