Author: Philip Jenvey <[email protected]>
Branch: py3k
Changeset: r59016:4bd535d8791e
Date: 2012-11-19 21:03 -0800
http://bitbucket.org/pypy/pypy/changeset/4bd535d8791e/

Log:    math.ceil/floor/trunc now fallback to their special methods

diff --git a/pypy/module/math/interp_math.py b/pypy/module/math/interp_math.py
--- a/pypy/module/math/interp_math.py
+++ b/pypy/module/math/interp_math.py
@@ -60,6 +60,9 @@
 
 def trunc(space, w_x):
     """Truncate x."""
+    w_descr = space.lookup(w_x, '__trunc__')
+    if w_descr is not None:
+        return space.get_and_call_function(w_descr, w_x)
     return space.trunc(w_x)
 
 def copysign(space, w_x, w_y):
@@ -158,6 +161,9 @@
        Return the floor of x as a float.
        This is the largest integral value <= x.
     """
+    w_descr = space.lookup(w_x, '__floor__')
+    if w_descr is not None:
+        return space.get_and_call_function(w_descr, w_x)
     x = _get_double(space, w_x)
     return space.wrap(math.floor(x))
 
@@ -247,6 +253,9 @@
        Return the ceiling of x as a float.
        This is the smallest integral value >= x.
     """
+    w_descr = space.lookup(w_x, '__ceil__')
+    if w_descr is not None:
+        return space.get_and_call_function(w_descr, w_x)
     return math1(space, math.ceil, w_x)
 
 def sinh(space, w_x):
diff --git a/pypy/module/math/test/test_math.py 
b/pypy/module/math/test/test_math.py
--- a/pypy/module/math/test/test_math.py
+++ b/pypy/module/math/test/test_math.py
@@ -278,3 +278,12 @@
         skip('sign of nan is undefined')
         import math
         assert math.copysign(1.0, float('-nan')) == -1.0
+
+    def test_special_methods(self):
+        import math
+        class Z:
+            pass
+        for i, name in enumerate(('ceil', 'floor', 'trunc')):
+            setattr(Z, '__{}__'.format(name), lambda self: i)
+            func = getattr(math, name)
+            assert func(Z()) == i
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to