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

cfbolz at codespeak.net cfbolz at codespeak.net
Sat Jul 30 00:30:14 CEST 2005


Author: cfbolz
Date: Sat Jul 30 00:30:13 2005
New Revision: 15389

Modified:
   pypy/dist/pypy/rpython/llinterp.py
   pypy/dist/pypy/rpython/test/test_llinterp.py
Log:
(arigo, cfbolz):
added overflowing operations to the llinterpreter

Modified: pypy/dist/pypy/rpython/llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/llinterp.py	(original)
+++ pypy/dist/pypy/rpython/llinterp.py	Sat Jul 30 00:30:13 2005
@@ -351,16 +351,6 @@
         assert type(c) is float
         return math.fmod(b,c)
 
-    def op_int_invert(self, a):
-        assert type(a) is int
-        return ~a
-
-    def op_uint_invert(self, a):
-        assert type(a) is r_uint
-        return ~a
-
-    
-
     # operations on pyobjects!
     for opname in opimpls.keys():
         exec py.code.Source("""
@@ -415,7 +405,22 @@
                 func = opimpls[%(opname)r]
                 return %(adjust_result)s(func(x, y))
         """ % locals()).compile()
-    for opname in 'is_true', 'neg':
+        if typ is int:
+            opname += '_ovf'
+            exec py.code.Source("""
+                def op_%(opnameprefix)s_%(pureopname)s_ovf(self, x, y):
+                    assert isinstance(x, %(typname)s)
+                    assert isinstance(y, %(typname)s)
+                    func = opimpls[%(opname)r]
+                    try:
+                        return %(adjust_result)s(func(x, y))
+                    except OverflowError, e:
+                        self.make_llexception(e)
+            """ % locals()).compile()
+            funcname = "op_%(opnameprefix)s_%(pureopname)s_ovf" % locals()
+            setattr(LLFrame, funcname, globals()[funcname])
+            
+    for opname in 'is_true', 'neg', 'abs', 'invert':
         assert opname in opimpls
         if typ is int and opname not in ops_returning_a_bool:
             adjust_result = 'intmask'
@@ -427,6 +432,20 @@
                 func = opimpls[%(opname)r]
                 return %(adjust_result)s(func(x))
         """ % locals()).compile()
+        if typ is int:
+            opname += '_ovf'
+            exec py.code.Source("""
+                def op_%(opnameprefix)s_%(opname)s_ovf(self, x):
+                    assert isinstance(x, %(typname)s)
+                    func = opimpls[%(opname)r]
+                    try:
+                        return %(adjust_result)s(func(x))
+                    except OverflowError, e:
+                        self.make_llexception(e)
+            """ % locals()).compile()
+            funcname = "op_%(opnameprefix)s_%(opname)s_ovf" % locals()
+            setattr(LLFrame, funcname, globals()[funcname])
+            
 
 for opname in ('gt', 'lt', 'ge', 'ne', 'le', 'eq'):
     assert opname in opimpls

Modified: pypy/dist/pypy/rpython/test/test_llinterp.py
==============================================================================
--- pypy/dist/pypy/rpython/test/test_llinterp.py	(original)
+++ pypy/dist/pypy/rpython/test/test_llinterp.py	Sat Jul 30 00:30:13 2005
@@ -8,7 +8,7 @@
 from pypy.rpython.rint import signed_repr
 from pypy.rpython import rstr
 from pypy.annotation.model import lltype_to_annotation
-from pypy.rpython.rarithmetic import r_uint
+from pypy.rpython.rarithmetic import r_uint, ovfcheck
 
 # switch on logging of interp to show more info on failing tests
 
@@ -258,6 +258,28 @@
     res = interpret(f, [_1L, _2L], someobjects=True)
     assert res._obj.value == 3L
 
+def test_ovf():
+    import sys
+    def f(x):
+        try:
+            return ovfcheck(sys.maxint + x)
+        except OverflowError:
+            return 1
+    res = interpret(f, [1])
+    assert res == 1
+    res = interpret(f, [0])
+    assert res == sys.maxint
+    def g(x):
+        try:
+            return ovfcheck(abs(x))
+        except OverflowError:
+            return 42
+    res = interpret(g, [-sys.maxint - 1])
+    assert res == 42
+    res = interpret(g, [-15])
+    assert res == 15
+    
+
 def test_obj_obj_is():
     def f(x,y):
         return x is y



More information about the Pypy-commit mailing list