This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author martin.panter
Recipients benjamin.peterson, martin.panter, whitequark, yselivanov
Date 2015-05-29.04:01:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1432872077.59.0.126314985304.issue24321@psf.upfronthosting.co.za>
In-reply-to
Content
The first example seems to behave as I would expect. The UnboundLocalError is raised by the print() call, because the “x” variable has been deleted by the exception handler. Equivalent code without using “nonlocal”:

>>> def f():
...     x = None
...     try:
...         raise Exception()
...     except Exception as x:
...         pass
...     print("x", x)  # UnboundLocal due to exception handler
... 
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in f
UnboundLocalError: local variable 'x' referenced before assignment

In both cases, I think this is correct behaviour. See <https://docs.python.org/3.4/reference/compound_stmts.html#except>, which says “When an exception has been assigned using ‘as target’, it is cleared at the end of the except clause.”
History
Date User Action Args
2015-05-29 04:01:17martin.pantersetrecipients: + martin.panter, benjamin.peterson, yselivanov, whitequark
2015-05-29 04:01:17martin.pantersetmessageid: <1432872077.59.0.126314985304.issue24321@psf.upfronthosting.co.za>
2015-05-29 04:01:17martin.panterlinkissue24321 messages
2015-05-29 04:01:16martin.pantercreate