[pypy-svn] pypy default: Don't rely on automatic long->complex delegation for comparisons.

amauryfa commits-noreply at bitbucket.org
Thu Jan 27 01:53:37 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: 
Changeset: r41377:35736592b41b
Date: 2011-01-27 01:49 +0100
http://bitbucket.org/pypy/pypy/changeset/35736592b41b/

Log:	Don't rely on automatic long->complex delegation for comparisons.
	Instead, use the comparison between long and float, which does the
	right thing when the long is too large to be converted to a float.

diff --git a/pypy/objspace/std/complexobject.py b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -3,6 +3,7 @@
 from pypy.objspace.std.model import registerimplementation, W_Object
 from pypy.objspace.std.register_all import register_all
 from pypy.objspace.std.floatobject import W_FloatObject, _hash_float
+from pypy.objspace.std.longobject import W_LongObject
 from pypy.rlib.rarithmetic import (
     formatd, DTSF_STR_PRECISION, isinf, isnan, copysign)
 
@@ -209,6 +210,22 @@
     return space.newbool((w_complex1.realval != w_complex2.realval) or 
             (w_complex1.imagval != w_complex2.imagval))
 
+def eq__Complex_Long(space, w_complex1, w_long2):
+    if w_complex1.imagval:
+        return space.w_False
+    return space.eq(space.newfloat(w_complex1.realval), w_long2)
+
+def eq__Long_Complex(space, w_long1, w_complex2):
+    return eq__Complex_Long(space, w_complex2, w_long1)
+
+def ne__Complex_Long(space, w_complex1, w_long2):
+    if w_complex1.imagval:
+        return space.w_True
+    return space.ne(space.newfloat(w_complex1.realval), w_long2)
+
+def ne__Long_Complex(space, w_long1, w_complex2):
+    return ne__Complex_Long(space, w_complex2, w_long1)
+
 def lt__Complex_Complex(space, w_complex1, w_complex2):
     raise OperationError(space.w_TypeError, space.wrap('cannot compare complex numbers using <, <=, >, >='))
 

diff --git a/pypy/objspace/std/test/test_complexobject.py b/pypy/objspace/std/test/test_complexobject.py
--- a/pypy/objspace/std/test/test_complexobject.py
+++ b/pypy/objspace/std/test/test_complexobject.py
@@ -137,7 +137,6 @@
 
     def test_richcompare(self):
         h = self.helper
-        h.raises(OverflowError, complex.__eq__, 1+1j, 1L<<10000)
         h.assertEqual(complex.__lt__(1+1j, None), NotImplemented)
         h.assertIs(complex.__eq__(1+1j, 1+1j), True)
         h.assertIs(complex.__eq__(1+1j, 2+2j), False)
@@ -147,6 +146,11 @@
         h.raises(TypeError, complex.__le__, 1+1j, 2+2j)
         h.raises(TypeError, complex.__gt__, 1+1j, 2+2j)
         h.raises(TypeError, complex.__ge__, 1+1j, 2+2j)
+        large = 1 << 10000
+        assert not (5+0j) == large
+        assert not large == (5+0j)
+        assert (5+0j) != large
+        assert large != (5+0j)
 
     def test_mod(self):
         h = self.helper


More information about the Pypy-commit mailing list