[pypy-commit] pypy py3k: a bunch of fixes for complex, includes removing floordiv, divmod, and mod

alex_gaynor noreply at buildbot.pypy.org
Mon Nov 7 21:24:05 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: py3k
Changeset: r48882:cf45cbaaa331
Date: 2011-11-07 15:23 -0500
http://bitbucket.org/pypy/pypy/changeset/cf45cbaaa331/

Log:	a bunch of fixes for complex, includes removing floordiv, divmod,
	and mod

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
@@ -63,17 +63,6 @@
             ir = (i1 * ratio - r1) / denom
         return W_ComplexObject(rr,ir)
 
-    def divmod(self, space, other):
-        space.warn(
-            "complex divmod(), // and % are deprecated",
-            space.w_DeprecationWarning
-        )
-        w_div = self.div(other)
-        div = math.floor(w_div.realval)
-        w_mod = self.sub(
-            W_ComplexObject(other.realval * div, other.imagval * div))
-        return (W_ComplexObject(div, 0), w_mod)
-
     def pow(self, other):
         r1, i1 = self.realval, self.imagval
         r2, i2 = other.realval, other.imagval
@@ -160,26 +149,6 @@
 
 truediv__Complex_Complex = div__Complex_Complex
 
-def mod__Complex_Complex(space, w_complex1, w_complex2):
-    try:
-        return w_complex1.divmod(space, w_complex2)[1]
-    except ZeroDivisionError, e:
-        raise OperationError(space.w_ZeroDivisionError, space.wrap(str(e)))
-
-def divmod__Complex_Complex(space, w_complex1, w_complex2):
-    try:
-        div, mod = w_complex1.divmod(space, w_complex2)
-    except ZeroDivisionError, e:
-        raise OperationError(space.w_ZeroDivisionError, space.wrap(str(e)))
-    return space.newtuple([div, mod])
-
-def floordiv__Complex_Complex(space, w_complex1, w_complex2):
-    # don't care about the slight slowdown you get from using divmod
-    try:
-        return w_complex1.divmod(space, w_complex2)[0]
-    except ZeroDivisionError, e:
-        raise OperationError(space.w_ZeroDivisionError, space.wrap(str(e)))
-
 def pow__Complex_Complex_ANY(space, w_complex, w_exponent, thirdArg):
     if not space.is_w(thirdArg, space.w_None):
         raise OperationError(space.w_ValueError, space.wrap('complex modulo'))
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
@@ -1,10 +1,13 @@
+from __future__ import print_function
+
 import py
-from pypy.objspace.std.complexobject import W_ComplexObject, \
-    pow__Complex_Complex_ANY
-from pypy.objspace.std import complextype as cobjtype
+
+from pypy.objspace.std import complextype as cobjtype, StdObjSpace
+from pypy.objspace.std.complexobject import (W_ComplexObject,
+    pow__Complex_Complex_ANY)
 from pypy.objspace.std.multimethod import FailedToImplement
 from pypy.objspace.std.stringobject import W_StringObject
-from pypy.objspace.std import StdObjSpace
+
 
 EPS = 1e-9
 
@@ -134,7 +137,7 @@
         from random import random
         # XXX this test passed but took waaaaay to long
         # look at dist/lib-python/modified-2.5.2/test/test_complex.py
-        #simple_real = [float(i) for i in xrange(-5, 6)]
+        #simple_real = [float(i) for i in range(-5, 6)]
         simple_real = [-2.0, 0.0, 1.0]
         simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
         for x in simple_complex:
@@ -147,7 +150,7 @@
         self.check_div(complex(1e-200, 1e-200), 1+0j)
 
         # Just for fun.
-        for i in xrange(100):
+        for i in range(100):
             self.check_div(complex(random(), random()),
                            complex(random(), random()))
 
@@ -160,8 +163,7 @@
         raises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
 
     def test_floordiv(self):
-        assert self.almost_equal(complex.__floordiv__(3+0j, 1.5+0j), 2)
-        raises(ZeroDivisionError, complex.__floordiv__, 3+0j, 0+0j)
+        raises(TypeError, "3+0j // 0+0j")
 
     def test_coerce(self):
         raises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000)
@@ -183,13 +185,11 @@
         assert large != (5+0j)
 
     def test_mod(self):
-        raises(ZeroDivisionError, (1+1j).__mod__, 0+0j)
-
         a = 3.33+4.43j
-        raises(ZeroDivisionError, "a % 0")
+        raises(TypeError, "a % a")
 
     def test_divmod(self):
-        raises(ZeroDivisionError, divmod, 1+1j, 0+0j)
+        raises(TypeError, divmod, 1+1j, 0+0j)
 
     def test_pow(self):
         assert self.almost_equal(pow(1+1j, 0+0j), 1.0)
@@ -221,7 +221,7 @@
 
     def test_boolcontext(self):
         from random import random
-        for i in xrange(100):
+        for i in range(100):
             assert complex(random() + 1e-6, random() + 1e-6)
         assert not complex(0.0, 0.0)
 
@@ -354,13 +354,13 @@
         raises(TypeError, complex, float2(None))
 
     def test_hash(self):
-        for x in xrange(-30, 30):
+        for x in range(-30, 30):
             assert hash(x) == hash(complex(x, 0))
             x /= 3.0    # now check against floating point
             assert hash(x) == hash(complex(x, 0.))
 
     def test_abs(self):
-        nums = [complex(x/3., y/7.) for x in xrange(-9,9) for y in xrange(-9,9)]
+        nums = [complex(x/3., y/7.) for x in range(-9,9) for y in range(-9,9)]
         for num in nums:
             assert self.almost_equal((num.real**2 + num.imag**2)  ** 0.5, abs(num))
 
@@ -409,7 +409,7 @@
         try:
             pth = tempfile.mktemp()
             fo = open(pth,"wb")
-            print >>fo, a, b
+            print(a, b, file=fo)
             fo.close()
             fo = open(pth, "rb")
             res = fo.read()


More information about the pypy-commit mailing list