[pypy-commit] pypy stdlib-2.7.9: provide complex.__int__ method

bdkearns noreply at buildbot.pypy.org
Fri Dec 19 08:17:37 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: stdlib-2.7.9
Changeset: r75034:04003981e306
Date: 2014-12-19 02:15 -0500
http://bitbucket.org/pypy/pypy/changeset/04003981e306/

Log:	provide complex.__int__ method

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
@@ -274,8 +274,7 @@
         return space.newlong_from_rbigint(val)
 
     def int(self, space):
-        raise oefmt(space.w_TypeError,
-                    "can't convert complex to int; use int(abs(z))")
+        raise oefmt(space.w_TypeError, "can't convert complex to int")
 
     def _to_complex(self, space, w_obj):
         if isinstance(w_obj, W_ComplexObject):
@@ -381,8 +380,7 @@
         return space.newbool((self.realval != 0.0) or (self.imagval != 0.0))
 
     def descr_float(self, space):
-        raise oefmt(space.w_TypeError,
-                    "can't convert complex to float; use abs(z)")
+        raise oefmt(space.w_TypeError, "can't convert complex to float")
 
     def descr_neg(self, space):
         return W_ComplexObject(-self.realval, -self.imagval)
@@ -603,6 +601,7 @@
     __coerce__ = interp2app(W_ComplexObject.descr_coerce),
     __format__ = interp2app(W_ComplexObject.descr_format),
     __nonzero__ = interp2app(W_ComplexObject.descr_nonzero),
+    __int__ = interp2app(W_ComplexObject.int),
     __float__ = interp2app(W_ComplexObject.descr_float),
     __neg__ = interp2app(W_ComplexObject.descr_neg),
     __pos__ = interp2app(W_ComplexObject.descr_pos),
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
@@ -161,6 +161,12 @@
     def test_coerce(self):
         raises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)
 
+    def test_convert(self):
+        exc = raises(TypeError, complex.__int__, 3j)
+        assert str(exc.value) == "can't convert complex to int"
+        exc = raises(TypeError, complex.__float__, 3j)
+        assert str(exc.value) == "can't convert complex to float"
+
     def test_richcompare(self):
         assert complex.__lt__(1+1j, None) is NotImplemented
         assert complex.__eq__(1+1j, 2+2j) is False


More information about the pypy-commit mailing list