[New-bugs-announce] [issue17792] Unhelpful UnboundLocalError due to del'ing of exception target

Barry A. Warsaw report at bugs.python.org
Fri Apr 19 01:55:22 CEST 2013

New submission from Barry A. Warsaw:

As described here:


the following code will produce an UnboundLocalError when the exception is triggered:

def bad():
    e = None
    except KeyError as e:
    except ValueError as e:

The reason is that the exception handling machinery del's `e` from the local namespace in order to break circular references caused by __traceback__.  The ULE is pretty mysterious and certainly not helpful for figuring out what's going wrong (the blog post above describes how long it took me to find it ;).

Can we do better?  What if instead of del'ing the target, we set it to None?  We wouldn't get a ULE but the fact that print(e) would print None might be just as mysterious.  Any other ideas?

(BTW, there's likely nothing to be done for Python < 3.4.)

messages: 187313
nosy: barry
priority: normal
severity: normal
status: open
title: Unhelpful UnboundLocalError due to del'ing of exception target
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list