[pypy-svn] r61322 - in pypy/trunk/pypy/interpreter: . astcompiler/test pyparser test

fijal at codespeak.net fijal at codespeak.net
Sun Jan 25 11:11:49 CET 2009


Author: fijal
Date: Sun Jan 25 11:11:47 2009
New Revision: 61322

Modified:
   pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py
   pypy/trunk/pypy/interpreter/pycompiler.py
   pypy/trunk/pypy/interpreter/pyparser/error.py
   pypy/trunk/pypy/interpreter/pyparser/grammar.py
   pypy/trunk/pypy/interpreter/pyparser/pythonparse.py
   pypy/trunk/pypy/interpreter/test/test_compiler.py
Log:
a go at IndentationError. not fixing now everything yet


Modified: pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py
==============================================================================
--- pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py	(original)
+++ pypy/trunk/pypy/interpreter/astcompiler/test/test_compiler.py	Sun Jan 25 11:11:47 2009
@@ -630,3 +630,27 @@
         """)
         decl = str(decl) + '\n'
         yield self.simple_test, decl, 'r', None
+
+    def test_indentation_error(self):
+        source = py.code.Source("""
+        x
+         y
+        """)
+        try:
+            self.simple_test(source, None, None)
+        except IndentationError, e:
+            assert e.msg == 'unexpected indent'
+        else:
+            raise Exception("DID NOT RAISE")
+
+    def test_no_indent(self):
+        source = py.code.Source("""
+        def f():
+        xxx
+        """)
+        try:
+            self.simple_test(source, None, None)
+        except IndentationError, e:
+            assert e.msg == 'expected an indented block'
+        else:
+            raise Exception("DID NOT RAISE")

Modified: pypy/trunk/pypy/interpreter/pycompiler.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pycompiler.py	(original)
+++ pypy/trunk/pypy/interpreter/pycompiler.py	Sun Jan 25 11:11:47 2009
@@ -227,7 +227,7 @@
         self.compiler_flags = self.futureFlags.allowed_flags
 
     def compile(self, source, filename, mode, flags):
-        from pypy.interpreter.pyparser.error import SyntaxError
+        from pypy.interpreter.pyparser.error import SyntaxError, IndentationError
         from pypy.interpreter import astcompiler
         from pypy.interpreter.astcompiler.pycodegen import ModuleCodeGenerator
         from pypy.interpreter.astcompiler.pycodegen import InteractiveCodeGenerator
@@ -252,10 +252,12 @@
             self.parser.parse_source(source, mode, builder, flags)
             ast_tree = builder.rule_stack[-1]
             encoding = builder.source_encoding
+        except IndentationError, e:
+            raise OperationError(space.w_IndentationError,
+                                 e.wrap_info(space, filename))
         except SyntaxError, e:
             raise OperationError(space.w_SyntaxError,
                                  e.wrap_info(space, filename))
-
         ast_tree = opt.optimize_ast_tree(space, ast_tree)
 
         if not space.is_w(self.w_compile_hook, space.w_None):

Modified: pypy/trunk/pypy/interpreter/pyparser/error.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/error.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/error.py	Sun Jan 25 11:11:47 2009
@@ -23,6 +23,8 @@
                                              self.offset,
                                              self.text)
 
+class IndentationError(SyntaxError):
+    pass
 
 class ASTError(Exception):
     def __init__(self, msg, ast_node ):

Modified: pypy/trunk/pypy/interpreter/pyparser/grammar.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/grammar.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/grammar.py	Sun Jan 25 11:11:47 2009
@@ -188,7 +188,6 @@
     emptytoken_in_first_set = False
     _match_cache = None
     args = []
-
     symbols = {} # dirty trick to provide a symbols mapping while printing (and not putting it in every object)
 
     _attrs_ = ['parser', 'codename', 'args',

Modified: pypy/trunk/pypy/interpreter/pyparser/pythonparse.py
==============================================================================
--- pypy/trunk/pypy/interpreter/pyparser/pythonparse.py	(original)
+++ pypy/trunk/pypy/interpreter/pyparser/pythonparse.py	Sun Jan 25 11:11:47 2009
@@ -6,7 +6,7 @@
 """
 from pypy.interpreter import gateway
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.pyparser.error import SyntaxError
+from pypy.interpreter.pyparser.error import SyntaxError, IndentationError
 from pypy.interpreter.pyparser.pythonlexer import Source, match_encoding_declaration
 from pypy.interpreter.astcompiler.consts import CO_FUTURE_WITH_STATEMENT
 import pypy.interpreter.pyparser.pytoken as pytoken
@@ -140,8 +140,11 @@
 
         if not target.match(src, builder):
             tok, line, lnum, pos = src.most_recent_token()
+            if tok.codename == self.tokens['INDENT']:
+                raise IndentationError("unexpected indent", lnum, pos, line)
+            if tok.codename == self.tokens['DEDENT']:
+                raise IndentationError("unexpected dedent", lnum, pos, line)
             raise SyntaxError("invalid syntax", lnum, pos, line)
-            # return None
         return builder
 
     def update_rules_references(self):

Modified: pypy/trunk/pypy/interpreter/test/test_compiler.py
==============================================================================
--- pypy/trunk/pypy/interpreter/test/test_compiler.py	(original)
+++ pypy/trunk/pypy/interpreter/test/test_compiler.py	Sun Jan 25 11:11:47 2009
@@ -750,4 +750,16 @@
         output = s.getvalue()
         assert 'BINARY_ADD' not in output
 
+class AppTestExceptions:
+    def test_indentation_error(self):
+        source = """if 1:
+        x
+         y
+        """
+        try:
+            exec source
+        except IndentationError:
+            pass
+        else:
+            raise Exception("DID NOT RAISE")
 



More information about the Pypy-commit mailing list