[pypy-svn] r30122 - in pypy/dist/pypy/translator/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Mon Jul 17 15:06:00 CEST 2006


Author: antocuni
Date: Mon Jul 17 15:05:53 2006
New Revision: 30122

Modified:
   pypy/dist/pypy/translator/cli/opcodes.py
   pypy/dist/pypy/translator/cli/test/runtest.py
   pypy/dist/pypy/translator/cli/test/test_op.py
Log:
Added support for division/modulo operations with divide-by-zero
checking.



Modified: pypy/dist/pypy/translator/cli/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/cli/opcodes.py	(original)
+++ pypy/dist/pypy/translator/cli/opcodes.py	Mon Jul 17 15:05:53 2006
@@ -29,9 +29,11 @@
         # if overflow, raise a pypy's OverflowError
         'catch [mscorlib]System.OverflowException {',
         'newobj instance void class exceptions.OverflowError::.ctor()',
-#        'dup',
-#        'ldsfld class Object_meta pypy.runtime.Constants::exceptions_OverflowError_meta',
-#        'stfld class Object_meta Object::meta',
+        'throw }',
+
+        # DivideByZeroException --> ZeroDivisionError
+        'catch [mscorlib]System.DivideByZeroException {',
+        'newobj instance void class exceptions.ZeroDivisionError::.ctor()',
         'throw }',
 
         '%s: nop' % label      # continue normal execution
@@ -92,6 +94,7 @@
     'int_sub':                  'sub',
     'int_mul':                  'mul',
     'int_floordiv':             'div',
+    'int_floordiv_zer':         _check('div'),
     'int_mod':                  'rem',
     'int_lt':                   'clt',
     'int_le':                   _not('cgt'),
@@ -124,8 +127,8 @@
 
     'int_rshift_ovf':           'shr', # these can't overflow!
     'int_xor_ovf':              'xor',
-    'int_floordiv_ovf_zer':     None,  # what's the meaning?
-    'int_mod_ovf_zer':          None,
+    'int_floordiv_ovf_zer':     _check('div'),
+    'int_mod_ovf_zer':          _check('rem'),
 
     'uint_is_true':             [PushAllArgs, 'ldc.i4.0', 'cgt.un'],
     'uint_neg':                 None,      # What's the meaning?

Modified: pypy/dist/pypy/translator/cli/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/cli/test/runtest.py	Mon Jul 17 15:05:53 2006
@@ -135,18 +135,23 @@
         ann = t.buildannotator()
         ann.build_types(func, annotation)
 
-    # quick hack: force exceptions.Exception to be rendered
-    def raiseValueError():
-        raise ValueError
-    ann.build_types(raiseValueError, [])
+    # quick hack: force some exceptions to be rendered
+    def raiseExceptions(switch):
+        if switch == 0:
+            raise ValueError
+        elif switch == 1:
+            raise OverflowError
+        else:
+            raise ZeroDivisionError
+    ann.build_types(raiseExceptions, [int])
 
     t.buildrtyper(type_system="ootype").specialize()
     main_graph = t.graphs[0]
 
     # XXX: horrible hack :-(
     for graph in t.graphs:
-        if graph.name == 'raiseValueError':
-            raiseValueError_graph = graph
+        if graph.name == 'raiseExceptions':
+            raiseExceptions_graph = graph
 
     if getoption('view'):
        t.view()
@@ -157,7 +162,7 @@
         tmpdir = udir
 
     return GenCli(tmpdir, t, TestEntryPoint(main_graph, True),
-                  pending_graphs=[raiseValueError_graph])
+                  pending_graphs=[raiseExceptions_graph])
 
 class CliFunctionWrapper(object):
     def __init__(self, exe_name):

Modified: pypy/dist/pypy/translator/cli/test/test_op.py
==============================================================================
--- pypy/dist/pypy/translator/cli/test/test_op.py	(original)
+++ pypy/dist/pypy/translator/cli/test/test_op.py	Mon Jul 17 15:05:53 2006
@@ -1,10 +1,32 @@
+from pypy.translator.cli.test.runtest import CliTest
 from pypy.translator.cli.test.runtest import check
-from pypy.rpython.rarithmetic import r_uint, r_ulonglong, r_longlong
+from pypy.rpython.rarithmetic import r_uint, r_ulonglong, r_longlong, ovfcheck
 from pypy.annotation import model as annmodel
 import sys
 
 char = annmodel.SomeChar()
 
+class TestOperations(CliTest):
+    def test_div_zero(self):
+        def fn(x, y):
+            try:
+                return x/y
+            except ZeroDivisionError:
+                return -1
+        assert self.interpret(fn, [10, 0]) == -1
+
+    def test_mod_ovf_zer(self):
+        def fn(x, y):
+            try:
+                return ovfcheck(x % y)
+            except OverflowError:
+                return -1
+            except ZeroDivisionError:
+                return -2
+        assert self.interpret(fn, [10, 3]) == 1
+        assert self.interpret(fn, [10, 0]) == -2
+
+
 def test_op():
     yield check, op_any_ge, [int, int], (42, 42)
     yield check, op_any_ge, [int, int], (13, 42)



More information about the Pypy-commit mailing list