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 <rep...@bugs.python.org>
<http://bugs.python.org/issue17792>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to