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

tismer at codespeak.net tismer at codespeak.net
Wed Jun 15 22:52:00 CEST 2005


Author: tismer
Date: Wed Jun 15 22:51:59 2005
New Revision: 13457

Modified:
   pypy/dist/pypy/rpython/interp.py
   pypy/dist/pypy/rpython/lltype.py
   pypy/dist/pypy/rpython/test/test_interp.py
Log:
started a bit with exceptions.
stunned a bit how complicated exceptions look,
in comparison to simple objects like number results?

Modified: pypy/dist/pypy/rpython/interp.py
==============================================================================
--- pypy/dist/pypy/rpython/interp.py	(original)
+++ pypy/dist/pypy/rpython/interp.py	Wed Jun 15 22:51:59 2005
@@ -2,6 +2,8 @@
 from pypy.rpython.lltype import _ptr
 import py
 
+class RPythonError(Exception):
+    pass
 
 class LLInterpreter(object): 
     """ low level interpreter working with concrete values. """ 
@@ -65,7 +67,15 @@
 
         # determine nextblock and/or return value 
         if len(block.exits) == 0: 
-            # return block 
+            # 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)
             resultvar, = block.getvariables()
             result = self.getval(resultvar) 
 #            self.log.operation("returning", result) 
@@ -80,7 +90,10 @@
     def eval_operation(self, operation): 
 #        self.log.operation("considering", operation) 
         ophandler = self.getoperationhandler(operation.opname) 
-        vals = [self.getval(x) for x in operation.args]
+        vals = [self.getval(x) for x in operation.args]
+        # if these special cases pile up, do something better here
+        if operation.opname == 'cast_pointer':
+            vals.insert(0, operation.result.concretetype)
         retval = ophandler(*vals) 
         self.setvar(operation.result, retval)
 
@@ -127,8 +140,11 @@
         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
-        
+        # is the same as between op_getfield and op_getsubstruct
+
+    def op_cast_pointer(self, tp, obj):
+        # well, actually this is what's now in the globals.
+        return cast_pointer(tp, obj)
 # __________________________________________________________
 # primitive operations 
 from pypy.objspace.flow.operation import FunctionByName 

Modified: pypy/dist/pypy/rpython/lltype.py
==============================================================================
--- pypy/dist/pypy/rpython/lltype.py	(original)
+++ pypy/dist/pypy/rpython/lltype.py	Wed Jun 15 22:51:59 2005
@@ -55,6 +55,14 @@
         finally:
             TLS.nested_hash_level -= 1
 
+    # due to this dynamic hash value, we must forbid
+    # pickling, until we have an algorithm for that
+    def __reduce_ex__(self, *args):
+        raise Exception('%s insts cannot be pickled, yet. __hash__ is not'
+                        ' constant during reconstruction.' %
+                        self.__class__.__name__)
+    __reduce__ = __reduce_ex__
+
     def __repr__(self):
         return '<%s>' % (self,)
 

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	Wed Jun 15 22:51:59 2005
@@ -2,7 +2,7 @@
 import py
 py.magic.autopath()
 from pypy.rpython.rtyper import RPythonTyper 
-from pypy.rpython.interp import LLInterpreter 
+from pypy.rpython.interp import LLInterpreter, RPythonError
 from pypy.translator.translator import Translator 
 
 def gengraph(func, argtypes=[]): 
@@ -36,6 +36,15 @@
     assert res == 43 
     res = interpret(simple_ifs, [1])
     assert res == 42 
+
+def test_raise():
+    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 ???
 
 def test_while_simple(): 
     res = interpret(while_simple, [3])
@@ -99,7 +108,12 @@
     while i > 0: 
         sum += i
         i -= 1
-    return sum 
+    return sum
+
+def raise_exception(i):
+    if i == 42:
+        raise IndexError
+    return i
 
 #__________________________________________________________________
 # interactive playing 



More information about the Pypy-commit mailing list