Change in scope of handled exception variable in Python 3?

Baptiste Lepilleur baptiste.lepilleur at gmail.com
Fri Dec 31 02:40:39 EST 2010


Hi,
I stumbled on a small bug with httplib2 that I reduced to the example below.

It seems that with Python 3, when an exception is handled it "unbound" the
previously declared local variable. This did not occurs with Python 2.5.

It is a Python 3 feature? I did not find anything in the what's news, but
it's hard to search... (notes: I'm using Python 3.1.2)

---
def main():
    msg = 'a message'
    try:
        raise ValueError( 'An error' )
    except ValueError as msg:
        pass
    return msg

main()

python localmask.py
Traceback (most recent call last):
  File "localmask.py", line 12, in <module>
    main()
  File "localmask.py", line 10, in main
    return msg
UnboundLocalError: local variable 'msg' referenced before assignment
---
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101231/5bb42db0/attachment-0001.html>


More information about the Python-list mailing list