[pypy-svn] r58977 - in pypy/branch/2.5-merge/pypy/interpreter: pyparser test

antocuni at codespeak.net antocuni at codespeak.net
Sat Oct 11 15:49:54 CEST 2008


Author: antocuni
Date: Sat Oct 11 15:49:53 2008
New Revision: 58977

Modified:
   pypy/branch/2.5-merge/pypy/interpreter/pyparser/astbuilder.py
   pypy/branch/2.5-merge/pypy/interpreter/test/test_syntax.py
Log:
(antocuni, arigo)

fix some non-sense in the parser



Modified: pypy/branch/2.5-merge/pypy/interpreter/pyparser/astbuilder.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/interpreter/pyparser/astbuilder.py	(original)
+++ pypy/branch/2.5-merge/pypy/interpreter/pyparser/astbuilder.py	Sat Oct 11 15:49:53 2008
@@ -760,6 +760,9 @@
     else:
         token = atoms[2]
         assert isinstance(token, TokenObject)
+        assert token.name == builder.parser.tokens['NAME']
+        if token.get_value() != 'as':
+            raise SyntaxError("invalid syntax", token.lineno, token.col)
         varexpr = atoms[3]
         var = to_lvalue(varexpr, consts.OP_ASSIGN)
         body = atoms[5]
@@ -791,11 +794,15 @@
         if index < l:
             token = atoms[index]
             assert isinstance(token, TokenObject)
-            if token.get_value() == 'as':
-                token = atoms[index+1]
-                assert isinstance(token, TokenObject)
-                as_name = token.get_value()
-                index += 2
+            if token.name == builder.parser.tokens['NAME']:
+                if token.get_value() == 'as':
+                    token = atoms[index+1]
+                    assert isinstance(token, TokenObject)
+                    as_name = token.get_value()
+                    index += 2
+                else:
+                    raise SyntaxError("invalid syntax", token.lineno, token.col)
+
         names.append((name, as_name))
         # move forward until next ','
         # XXX: what is it supposed to do ?
@@ -867,11 +874,14 @@
             if index < l:
                 token = tokens[index]
                 assert isinstance(token, TokenObject)
-                if token.get_value() == 'as':
-                    token = tokens[index+1]
-                    assert isinstance(token, TokenObject)
-                    as_name = token.get_value()
-                    index += 2
+                if token.name == builder.parser.tokens['NAME']:
+                    if token.get_value() == 'as':
+                        token = tokens[index+1]
+                        assert isinstance(token, TokenObject)
+                        as_name = token.get_value()
+                        index += 2
+                    else:
+                        raise SyntaxError("invalid syntax", token.lineno, token.col)
             names.append((name, as_name))
             if index < l: # case ','
                 index += 1

Modified: pypy/branch/2.5-merge/pypy/interpreter/test/test_syntax.py
==============================================================================
--- pypy/branch/2.5-merge/pypy/interpreter/test/test_syntax.py	(original)
+++ pypy/branch/2.5-merge/pypy/interpreter/test/test_syntax.py	Sat Oct 11 15:49:53 2008
@@ -537,6 +537,22 @@
 
         exec "from __future__ import with_statement\nimport sys as foo"
 
+    def test_missing_as_SyntaxError(self):
+        snippets = [
+            "import os.path a bar ",
+            "from os import path a bar",
+            """from __future__ import with_statement
+with foo a bar:
+    pass
+"""]
+        for snippet in snippets:
+            try:
+                exec snippet
+            except SyntaxError:
+                pass
+            else:
+                assert False, "%s: did not raise SyntaxError" % snippet
+
 
     def test_with_propagate_compileflag(self):
         s = """from __future__ import with_statement



More information about the Pypy-commit mailing list