Author: Manuel Jacob <m...@manueljacob.de>
Branch: py3.6
Changeset: r91954:1c770e1ebaa0
Date: 2017-07-22 01:59 +0200
http://bitbucket.org/pypy/pypy/changeset/1c770e1ebaa0/

Log:    Test and fix: a global declaration after an assignment with the same
        name is now a SyntaxError instead of only a warning.

diff --git a/pypy/interpreter/astcompiler/symtable.py 
b/pypy/interpreter/astcompiler/symtable.py
--- a/pypy/interpreter/astcompiler/symtable.py
+++ b/pypy/interpreter/astcompiler/symtable.py
@@ -492,9 +492,7 @@
                 else:
                     msg = "name '%s' is used prior to global declaration" % \
                         (name,)
-                misc.syntax_warning(self.space, msg,
-                                    self.compile_info.filename,
-                                    glob.lineno, glob.col_offset)
+                raise SyntaxError(msg, glob.lineno, glob.col_offset)
             self.note_symbol(name, SYM_GLOBAL)
 
     def visit_Nonlocal(self, nonl):
@@ -519,9 +517,7 @@
                 else:
                     msg = "name '%s' is used prior to nonlocal declaration" % \
                         (name,)
-                misc.syntax_warning(self.space, msg,
-                                    self.compile_info.filename,
-                                    nonl.lineno, nonl.col_offset)
+                raise SyntaxError(msg, nonl.lineno, nonl.col_offset)
 
             self.note_symbol(name, SYM_NONLOCAL)
 
diff --git a/pypy/interpreter/astcompiler/test/test_symtable.py 
b/pypy/interpreter/astcompiler/test/test_symtable.py
--- a/pypy/interpreter/astcompiler/test/test_symtable.py
+++ b/pypy/interpreter/astcompiler/test/test_symtable.py
@@ -318,6 +318,14 @@
         x = g.lookup_role('x')
         assert x == symtable.SYM_GLOBAL
 
+    def test_global_after_assignment(self):
+        src = ("def f():\n"
+               "    x = 1\n"
+               "    global x\n")
+        exc = py.test.raises(SyntaxError, self.func_scope, src).value
+        assert exc.lineno == 3
+        assert exc.msg == "name 'x' is assigned to before global declaration"
+
     def test_nonlocal(self):
         src = """
 x = 1
@@ -396,6 +404,14 @@
         assert exc.msg == "name 'x' is parameter and nonlocal"
         assert exc.lineno == 4
 
+    def test_nonlocal_after_assignment(self):
+        src = ("def f():\n"
+               "    x = 1\n"
+               "    nonlocal x\n")
+        exc = py.test.raises(SyntaxError, self.func_scope, src).value
+        assert exc.lineno == 3
+        assert exc.msg == "name 'x' is assigned to before nonlocal declaration"
+
     def test_optimization(self):
         assert not self.mod_scope("").can_be_optimized
         assert not self.class_scope("class x: pass").can_be_optimized
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to