[pypy-svn] r13465 - in pypy/dist/pypy/rpython: . test

tismer at codespeak.net tismer at codespeak.net
Thu Jun 16 02:49:36 CEST 2005


Author: tismer
Date: Thu Jun 16 02:49:35 2005
New Revision: 13465

Modified:
   pypy/dist/pypy/rpython/interp.py
   pypy/dist/pypy/rpython/test/test_interp.py
Log:
exceptions work quite nicely, now.
There is more work neededto handle
functions that call functions that raise.
Will get that ready in a few, hopefully.

Modified: pypy/dist/pypy/rpython/interp.py
==============================================================================
--- pypy/dist/pypy/rpython/interp.py	(original)
+++ pypy/dist/pypy/rpython/interp.py	Thu Jun 16 02:49:35 2005
@@ -51,6 +51,9 @@
 
     def eval_function(self, func, args=()): 
         graph = self.flowgraphs[func]
+        return self.eval_graph(graph,args)
+
+    def eval_graph(self, graph, args=()): 
         nextblock = graph.startblock
         while 1: 
             self.fillvars(nextblock, args) 
@@ -66,13 +69,12 @@
             self.eval_operation(op) 
 
         # determine nextblock and/or return value 
-        if len(block.exits) == 0: 
+        if len(block.exits) == 0:
             # return block
             if len(block.inputargs) == 2:
                 # exception
                 etypevar, evaluevar = block.getvariables()
                 etype = self.getval(etypevar)
-                #rint etype
                 evalue = self.getval(evaluevar)
                 # watch out, these are _ptr's
                 raise RPythonError(etype, evalue)
@@ -107,37 +109,38 @@
         # obj should be pointer 
         setattr(obj, fieldname, fieldvalue) # is that right?  -- yes
     
-    def op_direct_call(self,f,*args):
+    def op_direct_call(self, f, *args):
         # XXX the logic should be:
         #       if f._obj has a graph attribute, interpret
         #       that graph without looking at _callable
-        res = self.eval_function(f._obj._callable,args)
-        return res
-    
-    def op_malloc(self,obj):
+        if hasattr(f._obj, 'graph'):
+            return self.eval_graph(f._obj.graph, args)
+        return self.eval_function(f._obj._callable, args)
+
+    def op_malloc(self, obj):
         return malloc(obj)
     
-    def op_getfield(self,obj,field):
+    def op_getfield(self, obj, field):
         # assert: obj should be pointer
-        result = getattr(obj,field)
+        result = getattr(obj, field)
         # check the difference between op_getfield and op_getsubstruct:
         # the former returns the real field, the latter a pointer to it
         assert typeOf(result) == getattr(typeOf(obj).TO, field)
         return result
 
-    def op_getsubstruct(self,obj,field):
+    def op_getsubstruct(self, obj, field):
         # assert: obj should be pointer
-        result = getattr(obj,field)
+        result = getattr(obj, field)
         # check the difference between op_getfield and op_getsubstruct:
         # the former returns the real field, the latter a pointer to it
         assert typeOf(result) == Ptr(getattr(typeOf(obj).TO, field))
         return result
 
-    def op_malloc_varsize(self,obj,size):
-        return malloc(obj,size)
+    def op_malloc_varsize(self, obj, size):
+        return malloc(obj, size)
 
-    def op_getarraysubstruct(self,array,index):
-        assert isinstance(array,_ptr)
+    def op_getarraysubstruct(self, array, index):
+        assert isinstance(array, _ptr)
         return array[index]
         # the diff between op_getarrayitem and op_getarraysubstruct
         # is the same as between op_getfield and op_getsubstruct

Modified: pypy/dist/pypy/rpython/test/test_interp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_interp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_interp.py	Thu Jun 16 02:49:35 2005
@@ -5,10 +5,22 @@
 from pypy.rpython.rtyper import RPythonTyper 
 from pypy.rpython.interp import LLInterpreter, RPythonError
 from pypy.translator.translator import Translator 
+from pypy.rpython.lltype import pyobjectptr
+
+def find_exception(exc):
+    assert isinstance(exc, RPythonError)
+    import exceptions
+    klass, inst = exc.args
+    func = typer.getexceptiondata().ll_pyexcclass2exc
+    for cls in exceptions.__dict__.values():
+        if type(cls) is type(Exception):
+            if func(pyobjectptr(cls)).typeptr == klass:
+                return cls
 
 def gengraph(func, argtypes=[]): 
     t = Translator(func)
     t.annotate(argtypes)
+    global typer # we need it for find_exception
     typer = RPythonTyper(t.annotator)
     typer.specialize()
     #t.view()
@@ -42,10 +54,17 @@
     res = interpret(raise_exception, [41])
     assert res == 41
     info = raises(RPythonError, interpret, raise_exception, [42])
-    # XXX inspect which exception this was.
-    # rtyper.getexceptiondata().ll_exception_match()
-    # llexitcase not available here
-    # maybe I use pyexcclass2exc ???
+    assert find_exception(info.value) is IndexError
+    info = raises(RPythonError, interpret, raise_exception, [43])
+    assert find_exception(info.value) is ValueError
+
+def XXXtest_call_raise():
+    res = interpret(call_raise_intercept, [41])
+    assert res == 41
+    info = raises(RPythonError, interpret, call_raise_intercept, [42])
+    assert find_exception(info.value) is IndexError
+    info = raises(RPythonError, interpret, call_raise_intercept, [43])
+    assert find_exception(info.value) is TypeError
 
 def test_while_simple(): 
     res = interpret(while_simple, [3])
@@ -114,8 +133,15 @@
 def raise_exception(i):
     if i == 42:
         raise IndexError
+    elif i == 43:
+        raise ValueError
     return i
 
+def call_raise_intercept(i):
+    try:
+        return raise_exception(i)
+    except ValueError:
+        raise TypeError
 #__________________________________________________________________
 # interactive playing 
 



More information about the Pypy-commit mailing list