Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interaction of nonlocal and except leading to incorrect behavior #68509

Closed
whitequark mannequin opened this issue May 29, 2015 · 3 comments
Closed

interaction of nonlocal and except leading to incorrect behavior #68509

whitequark mannequin opened this issue May 29, 2015 · 3 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@whitequark
Copy link
Mannequin

whitequark mannequin commented May 29, 2015

BPO 24321
Nosy @benjaminp, @bitdancer, @vadmium, @1st1

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-05-29.04:44:26.952>
created_at = <Date 2015-05-29.01:55:26.015>
labels = ['interpreter-core', 'type-bug', 'invalid']
title = 'interaction of nonlocal and except leading to incorrect behavior'
updated_at = <Date 2015-05-29.11:55:17.065>
user = 'https://bugs.python.org/whitequark'

bugs.python.org fields:

activity = <Date 2015-05-29.11:55:17.065>
actor = 'r.david.murray'
assignee = 'none'
closed = True
closed_date = <Date 2015-05-29.04:44:26.952>
closer = 'ned.deily'
components = ['Interpreter Core']
creation = <Date 2015-05-29.01:55:26.015>
creator = 'whitequark'
dependencies = []
files = []
hgrepos = []
issue_num = 24321
keywords = []
message_count = 3.0
messages = ['244356', '244357', '244368']
nosy_count = 5.0
nosy_names = ['benjamin.peterson', 'r.david.murray', 'martin.panter', 'yselivanov', 'whitequark']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue24321'
versions = ['Python 3.4']

@whitequark
Copy link
Mannequin Author

whitequark mannequin commented May 29, 2015

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'>)

@whitequark whitequark mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels May 29, 2015
@vadmium
Copy link
Member

vadmium commented May 29, 2015

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.”

@bitdancer
Copy link
Member

Indeed, if you replace the except clause with a 'del x', you get the same UnboundLocalError. This is working as designed.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants