# Commits

committed 4bd535d

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

• Participants
• Parent commits 19a423e
• Branches py3k

# File pypy/module/math/interp_math.py

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):
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))

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):

# File pypy/module/math/test/test_math.py

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