[New-bugs-announce] [issue24321] interaction of nonlocal and except leading to incorrect behavior

whitequark report at bugs.python.org
Fri May 29 03:55:26 CEST 2015


New submission from whitequark:

To reproduce in Python 3.4.2:

def f():
    x = None
    def g():
        nonlocal x
        try:
            raise Exception()
        except Exception as x:
            pass
    g()
    # ↓ UnboundLocalError: local variable 'x' referenced before assignment
    print("x", x)
f()

Compare this to:

def f():
    x = None
    def g():
        nonlocal x
        with open("/dev/null") as x:
            pass
    g()
    print("x", x)
f()

(which prints: x <_io.TextIOWrapper name='/dev/null' mode='r' encoding='UTF-8'>)

----------
components: Interpreter Core
messages: 244356
nosy: whitequark
priority: normal
severity: normal
status: open
title: interaction of nonlocal and except leading to incorrect behavior
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24321>
_______________________________________


More information about the New-bugs-announce mailing list