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

Can't figure out where SyntaxError: can not delete variable 'x' referenced in nested scope us coming from in python shows no traceback #48863

Closed
marduk mannequin opened this issue Dec 9, 2008 · 8 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@marduk
Copy link
Mannequin

marduk mannequin commented Dec 9, 2008

BPO 4613
Nosy @amauryfa, @benjaminp, @merwok
Files
  • syntaxerror.patch
  • 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 = 'https://github.com/amauryfa'
    closed_at = <Date 2014-06-27.06:58:57.580>
    created_at = <Date 2008-12-09.19:36:05.727>
    labels = ['interpreter-core', 'type-bug']
    title = "Can't figure out where SyntaxError: can not delete variable 'x' referenced in nested scope us coming from in python shows no traceback"
    updated_at = <Date 2014-06-27.06:58:57.580>
    user = 'https://bugs.python.org/marduk'

    bugs.python.org fields:

    activity = <Date 2014-06-27.06:58:57.580>
    actor = 'ned.deily'
    assignee = 'amaury.forgeotdarc'
    closed = True
    closed_date = <Date 2014-06-27.06:58:57.580>
    closer = 'ned.deily'
    components = ['Interpreter Core']
    creation = <Date 2008-12-09.19:36:05.727>
    creator = 'marduk'
    dependencies = []
    files = ['12311']
    hgrepos = []
    issue_num = 4613
    keywords = ['patch']
    message_count = 8.0
    messages = ['77443', '77472', '77480', '77483', '77525', '77537', '221662', '221671']
    nosy_count = 5.0
    nosy_names = ['amaury.forgeotdarc', 'benjamin.peterson', 'eric.araujo', 'marduk', 'BreamoreBoy']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue4613'
    versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

    @marduk
    Copy link
    Mannequin Author

    marduk mannequin commented Dec 9, 2008

    Say I have module foo.py:

            def a(x):
               def b():
                   x
               del x
            
    If I run foo.py under Python 2.4.4 I get:
          File "foo.py", line 4
            del x
        SyntaxError: can not delete variable 'x' referenced in nested
        scope
    

    Under Python 2.6 and Python 3.0 I get:

        SyntaxError: can not delete variable 'x' referenced in nested
        scope
    

    The difference is under Python 2.4 I get a traceback with the lineno and
    offending line, but I do not get a traceback in Pythons 2.6 and 3.0.

    This also kinda relates to the 2to3 tool. See:

    http://groups.google.com/group/comp.lang.python/browse_frm/thread/a6600c80f8c3c60c/4d804532ea09aae7

    @marduk marduk mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Dec 9, 2008
    @amauryfa
    Copy link
    Member

    amauryfa commented Dec 9, 2008

    Somehow you caught the only SyntaxError that forgets to add filename and
    line information.
    Patch attached, at the expense of not displaying the variable name (it
    should be obvious if the line is displayed)

    @benjaminp
    Copy link
    Contributor

    +1 compiler_error should be used anyway.

    @marduk
    Copy link
    Mannequin Author

    marduk mannequin commented Dec 10, 2008

    Thanks for looking into this.

    Ok... I applied your patch (actually it does not apply against Python
    3.0 so I had to change it manually).

    Now I'm not sure if this is still an error in the compiler or if it's
    truly a problem on my end, but the line given in the error doesn't
    contain a del statement at all.

    The code basically looks like this:

    def method(self):
        ...
                    success = False
                    e = None
                    try:
                            success, mydepgraph, dropped_tasks =
    resume_depgraph(
                                    self.settings, self.trees,
    self._mtimedb, self.myopts,
                                    myparams, self._spinner,
    skip_unsatisfied=True)
                    except depgraph.UnsatisfiedResumeDep as e:
                            mydepgraph = e.depgraph
                            dropped_tasks = set()

    With the patch, the error occurs at "dropped_tasks = set()" in the
    except clause. The first time that "dropped_tasks" is used in the
    entire module is in the try clause.

    This is a horrible piece of code (I did not write it). Do you think the
    SyntaxError message could be masking what the real error is?

    Again, the python2 version of this module imports fine, but when I run
    2to3 on it I get the SyntaxError. The line of code in question,
    however, is unmodified by 2to3.

    @amauryfa
    Copy link
    Member

    I think I understand. This simple code does not compile with python 3.0:

    def f():
        e = None
        def g():
            e
    try:
        pass
    except Exception as e:
        pass # SyntaxError here???
    

    The reason is that since 3.0, a "del e" statement is generated by the
    compiler (see http://www.python.org/dev/peps/pep-3110/#semantic-changes)
    and this conflicts with the first block of the function.

    This issue is much more complicated than it seemed...
    A solution could be to generate "e=None" instead of "del e".

    @amauryfa
    Copy link
    Member

    I filed bpo-4617 about the hidden "del e" problem.

    Meanwhile, you may try to change some variable names, specially the
    exception target (the 'e' in 'except Exception as e')

    @amauryfa amauryfa self-assigned this Dec 10, 2008
    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jun 26, 2014

    As bpo-4617 has been closed as resolved is there anything left to do here?

    @marduk
    Copy link
    Mannequin Author

    marduk mannequin commented Jun 27, 2014

    You can close this one out. I don't even remember the use case anymore.

    @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