[New-bugs-announce] [issue4617] SyntaxError when free variable name is also an exception target

Amaury Forgeot d'Arc report at bugs.python.org
Wed Dec 10 13:18:43 CET 2008


New submission from Amaury Forgeot d'Arc <amauryfa at gmail.com>:

This issue comes from issue4613. The following code raises a
SyntaxError("can not delete variable 'e' referenced in nested scope"):

def f():
    e = None
    def g():
        e

    try:
        pass
    except Exception as e:
        pass # SyntaxError here???

The reason is because of
http://www.python.org/dev/peps/pep-3110/#semantic-changes, a "del e"
statement is inserted.

The above code is correct, and should work.
I suggest that the limitation: "can not delete variable referenced in
nested scope" could be removed. 

After all, the "variable referenced" has no value before it is set,
accessing it raises either NameError("free variable referenced before
assignment in enclosing scope") or UnboundLocalError("local variable
referenced before assignment")

The Attached patch adds a DELETE_DEREF opcode, that removes the value of
a cell variable, and put it in a "before assignment" state.

Some compiler experts should review it. Few regressions are possible,
since the new opcode is emitted where a SyntaxError was previously
raised. The patch could also be applied to 2.7, even if it is less
critical there.

Tests are to come, but I'd like other's suggestions.

----------
files: delete_deref.patch
keywords: needs review, patch
messages: 77536
nosy: amaury.forgeotdarc
priority: release blocker
severity: normal
status: open
title: SyntaxError when free variable name is also an exception target
versions: Python 2.7, Python 3.0
Added file: http://bugs.python.org/file12318/delete_deref.patch

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


More information about the New-bugs-announce mailing list