Commits

Tobias Pape committed 13a3990

Make sure that a specific IndexError is handled in RPython for Nonterminal in the Parsing rlib

Comments (0)

Files changed (2)

pypy/rlib/parsing/test/test_tree.py

+import py
+
+from pypy.rlib.parsing.tree import Nonterminal, Symbol
+from pypy.rlib.parsing.lexer import Token, SourcePos
+
+class TestTreeAppLevel(object):
+
+    def test_nonterminal_simple(self):
+        pos = SourcePos(1,2,3)
+        tree = Nonterminal(symbol="a", 
+            children=[
+                Symbol(symbol="b", 
+                    additional_info="b", 
+                    token=Token(name="B",
+                        source="b",
+                        source_pos=pos))])
+        assert tree.getsourcepos() == pos
+        
+    def test_nonterminal_nested(self):
+        pos = SourcePos(1,2,3)
+        tree = Nonterminal(symbol="a", 
+            children=[
+                Nonterminal(symbol="c",
+                    children=[
+                       Symbol(symbol="b", 
+                            additional_info="b", 
+                            token=Token(name="B",
+                                source="b",
+                                source_pos=pos))])])
+        assert tree.getsourcepos() == pos
+    
+    def test_nonterminal_simple_empty(self):
+        tree = Nonterminal(symbol="a", 
+            children=[])
+        assert len(tree.children) == 0 # trivial
+        py.test.raises(IndexError, tree.getsourcepos)
+
+    def test_nonterminal_nested_empty(self):
+        tree = Nonterminal(symbol="a", 
+            children=[Nonterminal(symbol="c",
+            children=[Nonterminal(symbol="c",
+            children=[Nonterminal(symbol="c",
+            children=[Nonterminal(symbol="c",
+            children=[Nonterminal(symbol="c",
+            children=[Nonterminal(symbol="c",
+            children=[])])])])])])])
+        assert len(tree.children) != 0 # the not-so-trivial part.
+        py.test.raises(IndexError, tree.getsourcepos)
+
+class BaseTestTreeTranslated(object):
+    
+    def compile(self, f):
+        raise NotImplementedError
+    
+    def test_nonterminal_simple_empty(self):
+        def foo():
+            tree = Nonterminal(symbol="a", 
+                children=[])
+            return tree.getsourcepos()
+        f = self.compile(foo)
+        py.test.raises(IndexError, f)    
+
+    def test_nonterminal_nested_empty(self):
+        def foo():
+            tree = Nonterminal(symbol="a", 
+                children=[Nonterminal(symbol="c",
+                children=[Nonterminal(symbol="c",
+                children=[Nonterminal(symbol="c",
+                children=[Nonterminal(symbol="c",
+                children=[Nonterminal(symbol="c",
+                children=[Nonterminal(symbol="c",
+                children=[])])])])])])])
+            return tree.getsourcepos()
+        f = self.compile(foo)
+        py.test.raises(IndexError, f)    
+
+
+class TestTreeTranslatedLLType(BaseTestTreeTranslated):
+
+    def compile(self, f):
+        from pypy.translator.c.test.test_genc import compile
+        return compile(f, [])
+
+class TestTreeTranslatedOOType(BaseTestTreeTranslated):
+    
+    def compile(self, f):
+        from pypy.translator.cli.test.runtest import compile_function
+        return compile_function(f, [], auto_raise_exc=True, exctrans=True)
+
+

pypy/rlib/parsing/tree.py

         self.symbol = symbol
 
     def getsourcepos(self):
-        return self.children[0].getsourcepos()
+        try:
+            return self.children[0].getsourcepos()
+        except IndexError:
+            raise
 
     def __str__(self):
         return "%s(%s)" % (self.symbol, ", ".join([str(c) for c in self.children]))