[pypy-svn] r77768 - in pypy/branch/fast-forward/pypy/interpreter/astcompiler: . test

afa at codespeak.net afa at codespeak.net
Sun Oct 10 22:32:58 CEST 2010


Author: afa
Date: Sun Oct 10 22:32:56 2010
New Revision: 77768

Modified:
   pypy/branch/fast-forward/pypy/interpreter/astcompiler/symtable.py
   pypy/branch/fast-forward/pypy/interpreter/astcompiler/test/test_symtable.py
Log:
Add syntax warning: "import * only allowed at module level"


Modified: pypy/branch/fast-forward/pypy/interpreter/astcompiler/symtable.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/astcompiler/symtable.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/astcompiler/symtable.py	Sun Oct 10 22:32:56 2010
@@ -237,6 +237,7 @@
     def note_import_star(self, imp):
         self.optimized = False
         self.import_star = imp
+        return True
 
     def note_variable_arg(self, vararg):
         self.has_variable_arg = True
@@ -382,7 +383,11 @@
     def visit_ImportFrom(self, imp):
         for alias in imp.names:
             if self._visit_alias(alias):
-                self.scope.note_import_star(imp)
+                if self.scope.note_import_star(imp):
+                    msg = "import * only allowed at module level"
+                    misc.syntax_warning(
+                        self.space, msg, self.compile_info.filename,
+                        imp.lineno, imp.col_offset)
 
     def _visit_alias(self, alias):
         assert isinstance(alias, ast.alias)

Modified: pypy/branch/fast-forward/pypy/interpreter/astcompiler/test/test_symtable.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/astcompiler/test/test_symtable.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/astcompiler/test/test_symtable.py	Sun Oct 10 22:32:56 2010
@@ -309,6 +309,17 @@
             exc = py.test.raises(SyntaxError, self.mod_scope, input).value
             assert exc.msg == error + " contains a nested function with free variables"
 
+    def test_importstar_warning(self, capfd):
+        self.mod_scope("def f():\n    from re import *")
+        _, err1 = capfd.readouterr()
+
+        self.mod_scope("if 1:\n    from re import *")
+        _, err2 = capfd.readouterr()
+
+        capfd.close()
+        assert     "import * only allowed at module level" in err1
+        assert not "import * only allowed at module level" in err2
+
     def test_exec(self):
         self.mod_scope("exec 'hi'")
         scp = self.func_scope("def f(): exec 'hi'")



More information about the Pypy-commit mailing list