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.

Title: Can't figure out where SyntaxError: can not delete variable 'x' referenced in nested scope us coming from in python shows no traceback
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.1, Python 3.2, Python 2.7
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: amaury.forgeotdarc Nosy List: BreamoreBoy, amaury.forgeotdarc, benjamin.peterson, eric.araujo, marduk
Priority: normal Keywords: patch

Created on 2008-12-09 19:36 by marduk, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
syntaxerror.patch amaury.forgeotdarc, 2008-12-09 23:43 review
Messages (8)
msg77443 - (view) Author: Albert Hopkins (marduk) Date: 2008-12-09 19:36
Say I have module

        def a(x):
           def b():
           del x
If I run under Python 2.4.4 I get:

          File "", line 4
            del x
        SyntaxError: can not delete variable 'x' referenced in nested

Under Python 2.6 and Python 3.0 I get:

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

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:
msg77472 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-09 23:43
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)
msg77480 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-12-10 02:59
+1 compiler_error should be used anyway.
msg77483 - (view) Author: Albert Hopkins (marduk) Date: 2008-12-10 04:44
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
                        success, mydepgraph, dropped_tasks =
                                self.settings, self.trees,
self._mtimedb, self.myopts,
                                myparams, self._spinner,
                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.
msg77525 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-10 09:43
I think I understand. This simple code does not compile with python 3.0:

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

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

The reason is that since 3.0, a "del e" statement is generated by the
compiler (see
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".
msg77537 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-10 12:26
I filed issue4617 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')
msg221662 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-26 23:14
As issue 4617 has been closed as resolved is there anything left to do here?
msg221671 - (view) Author: Albert Hopkins (marduk) Date: 2014-06-27 01:36
You can close this one out.  I don't even remember the use case anymore.
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48863
2014-06-27 06:58:57ned.deilysetstatus: open -> closed
resolution: out of date
stage: resolved
2014-06-27 01:36:09marduksetmessages: + msg221671
2014-06-26 23:14:02BreamoreBoysetnosy: + BreamoreBoy
messages: + msg221662
2010-11-28 05:22:54eric.araujosetnosy: + eric.araujo

versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 3.0
2008-12-10 12:26:35amaury.forgeotdarcsetpriority: critical -> normal
assignee: amaury.forgeotdarc
messages: + msg77537
2008-12-10 09:43:57amaury.forgeotdarcsetpriority: critical
messages: + msg77525
2008-12-10 04:44:36marduksetmessages: + msg77483
2008-12-10 02:59:44benjamin.petersonsetkeywords: - needs review
nosy: + benjamin.peterson
messages: + msg77480
2008-12-09 23:43:18amaury.forgeotdarcsetkeywords: + needs review, patch
files: + syntaxerror.patch
messages: + msg77472
nosy: + amaury.forgeotdarc
2008-12-09 19:36:05mardukcreate