Philip Jenvey avatar Philip Jenvey committed 4bd535d

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

Comments (0)

Files changed (2)

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

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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.