[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:

http://www.wefearchange.org/2013/04/python-3-language-gotcha-and-short.html

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

def bad():
    e = None
    try:
        do_something()
    except KeyError as e:
        print('ke')
    except ValueError as e:
        print('ve')
    print(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>
<http://bugs.python.org/issue17792>
_______________________________________


More information about the New-bugs-announce mailing list