[pypy-svn] r78912 - in pypy/trunk/pypy/rlib: . test

antocuni at codespeak.net antocuni at codespeak.net
Tue Nov 9 12:07:10 CET 2010


Author: antocuni
Date: Tue Nov  9 12:07:09 2010
New Revision: 78912

Modified:
   pypy/trunk/pypy/rlib/rarithmetic.py
   pypy/trunk/pypy/rlib/test/test_rarithmetic.py
Log:
don't crash if you try to compare an r_singlefloat with == or !=. Some places in the translation rely on being able to compare random stuff without crashing.  Make sure we still get a crash if we use == or != in rpython


Modified: pypy/trunk/pypy/rlib/rarithmetic.py
==============================================================================
--- pypy/trunk/pypy/rlib/rarithmetic.py	(original)
+++ pypy/trunk/pypy/rlib/rarithmetic.py	Tue Nov  9 12:07:09 2010
@@ -501,6 +501,11 @@
     def __cmp__(self, other):
         raise TypeError("not supported on r_singlefloat instances")
 
+    def __eq__(self, other):
+        return self.__class__ is other.__class__ and self._bytes == other._bytes
+
+    def __ne__(self, other):
+        return not self.__eq__(other)
 
 class For_r_singlefloat_values_Entry(extregistry.ExtRegistryEntry):
     _type_ = r_singlefloat

Modified: pypy/trunk/pypy/rlib/test/test_rarithmetic.py
==============================================================================
--- pypy/trunk/pypy/rlib/test/test_rarithmetic.py	(original)
+++ pypy/trunk/pypy/rlib/test/test_rarithmetic.py	Tue Nov  9 12:07:09 2010
@@ -325,6 +325,15 @@
     assert float(x) != 2.1
     assert abs(float(x) - 2.1) < 1E-6
 
+def test_r_singlefloat_eq():
+    x = r_singlefloat(2.5)       # exact number
+    y = r_singlefloat(2.5)
+    assert x == y
+    assert not x != y
+    assert not x == 2.5
+    assert x != 2.5
+    py.test.raises(TypeError, "x>y")
+
 class BaseTestRarithmetic(BaseRtypingTest):
     def test_formatd(self):
         from pypy.rlib.rarithmetic import formatd
@@ -358,7 +367,17 @@
         assert res == 1.0
 
         res = self.interpret(f, [1])
-        assert res == 1e-100                 
+        assert res == 1e-100
+
+    def test_compare_singlefloat_crashes(self):
+        from pypy.rlib.rarithmetic import r_singlefloat
+        from pypy.rpython.error import MissingRTypeOperation
+        def f(x):
+            a = r_singlefloat(x)
+            b = r_singlefloat(x+1)
+            return a == b
+        py.test.raises(MissingRTypeOperation, "self.interpret(f, [42.0])")
+
 
 class TestLLtype(BaseTestRarithmetic, LLRtypeMixin):
     pass



More information about the Pypy-commit mailing list