[pypy-commit] pypy py3.6: #3033 Test and fix for float.__round__(None)

arigo pypy.commits at gmail.com
Sat Jun 29 04:03:53 EDT 2019


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.6
Changeset: r96881:d985f090d5c1
Date: 2019-06-29 10:03 +0200
http://bitbucket.org/pypy/pypy/changeset/d985f090d5c1/

Log:	#3033 Test and fix for float.__round__(None)

diff --git a/pypy/objspace/std/floatobject.py b/pypy/objspace/std/floatobject.py
--- a/pypy/objspace/std/floatobject.py
+++ b/pypy/objspace/std/floatobject.py
@@ -929,7 +929,7 @@
     # Algorithm copied directly from CPython
     x = w_float.floatval
 
-    if w_ndigits is None:
+    if space.is_none(w_ndigits):
         # single-argument round: round to nearest integer
         rounded = rfloat.round_away(x)
         if math.fabs(x - rounded) == 0.5:
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -156,7 +156,7 @@
         # efficient for W_IntObject
         from pypy.objspace.std.longobject import newlong
 
-        if w_ndigits is None:
+        if space.is_none(w_ndigits):
             return self.int(space)
 
         ndigits = space.bigint_w(space.index(w_ndigits))
diff --git a/pypy/objspace/std/test/test_floatobject.py b/pypy/objspace/std/test/test_floatobject.py
--- a/pypy/objspace/std/test/test_floatobject.py
+++ b/pypy/objspace/std/test/test_floatobject.py
@@ -324,6 +324,7 @@
         assert -2.0 == round(-1.5)
         assert -2.0 == round(-1.5, 0)
         assert -2.0 == round(-1.5, 0)
+        assert -2.0 == round(-1.5, None)
         assert 22.2 == round(22.222222, 1)
         assert 20.0 == round(22.22222, -1)
         assert 0.0 == round(22.22222, -2)
@@ -334,6 +335,11 @@
         assert math.copysign(1., round(-123.456, -700)) == -1.
         assert round(2.5, 0) == 2.0
 
+    def test_round_special_method(self):
+        assert 2.0 == 1.9 .__round__()
+        assert -2.0 == -1.5 .__round__(None)
+        assert 20.0 == 22.22222 .__round__(-1)
+
     def test_special_float_method(self):
         class a(object):
             def __float__(self):
diff --git a/pypy/objspace/std/test/test_intobject.py b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -743,6 +743,14 @@
         x = -b
         assert x.__rsub__(2) == (2 + b)
 
+    def test_round_special_method(self):
+        assert 567 .__round__(-1) == 570
+        assert 567 .__round__() == 567
+        import sys
+        if '__pypy__' in sys.builtin_module_names:
+            assert 567 .__round__(None) == 567    # fails on CPython
+
+
 class AppTestIntShortcut(AppTestInt):
     spaceconfig = {"objspace.std.intshortcut": True}
 


More information about the pypy-commit mailing list