Author barry
Recipients barry
Date 2013-04-18.23:55:22
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366329322.51.0.489771554551.issue17792@psf.upfronthosting.co.za>
In-reply-to
Content
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.)
History
Date User Action Args
2013-04-18 23:55:22barrysetrecipients: + barry
2013-04-18 23:55:22barrysetmessageid: <1366329322.51.0.489771554551.issue17792@psf.upfronthosting.co.za>
2013-04-18 23:55:22barrylinkissue17792 messages
2013-04-18 23:55:22barrycreate