[pypy-svn] r75752 - in pypy/branch/fast-forward/pypy: interpreter module/math objspace/flow objspace/std objspace/std/test

benjamin at codespeak.net benjamin at codespeak.net
Fri Jul 2 02:05:54 CEST 2010


Author: benjamin
Date: Fri Jul  2 02:05:52 2010
New Revision: 75752

Modified:
   pypy/branch/fast-forward/pypy/interpreter/baseobjspace.py
   pypy/branch/fast-forward/pypy/module/math/__init__.py
   pypy/branch/fast-forward/pypy/module/math/interp_math.py
   pypy/branch/fast-forward/pypy/objspace/flow/operation.py
   pypy/branch/fast-forward/pypy/objspace/std/builtinshortcut.py
   pypy/branch/fast-forward/pypy/objspace/std/floatobject.py
   pypy/branch/fast-forward/pypy/objspace/std/intobject.py
   pypy/branch/fast-forward/pypy/objspace/std/longobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py
   pypy/branch/fast-forward/pypy/objspace/std/test/test_longobject.py
Log:
add math.trunc and support for floats, ints, and longs

Modified: pypy/branch/fast-forward/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/fast-forward/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/fast-forward/pypy/interpreter/baseobjspace.py	Fri Jul  2 02:05:52 2010
@@ -1213,6 +1213,7 @@
     ('getslice',        'getslice',  3, ['__getslice__']),
     ('setslice',        'setslice',  4, ['__setslice__']),
     ('delslice',        'delslice',  3, ['__delslice__']),
+    ('trunc',           'trunc',     1, ['__trunc__']),
     ('pos',             'pos',       1, ['__pos__']),
     ('neg',             'neg',       1, ['__neg__']),
     ('nonzero',         'truth',     1, ['__nonzero__']),

Modified: pypy/branch/fast-forward/pypy/module/math/__init__.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/math/__init__.py	(original)
+++ pypy/branch/fast-forward/pypy/module/math/__init__.py	Fri Jul  2 02:05:52 2010
@@ -37,5 +37,6 @@
        'acos'           : 'interp_math.acos',
        'isinf'          : 'interp_math.isinf',
        'isnan'          : 'interp_math.isnan',
+       'trunc'          : 'interp_math.trunc',
 }
 

Modified: pypy/branch/fast-forward/pypy/module/math/interp_math.py
==============================================================================
--- pypy/branch/fast-forward/pypy/module/math/interp_math.py	(original)
+++ pypy/branch/fast-forward/pypy/module/math/interp_math.py	Fri Jul  2 02:05:52 2010
@@ -47,6 +47,11 @@
     return space.wrap(r)
 math2._annspecialcase_ = 'specialize:arg(1)'
 
+def trunc(space, w_x):
+    """Truncate x."""
+    return space.trunc(w_x)
+trunc.unwrap_spec = [ObjSpace, W_Root]
+
 def copysign(space, x, y):
     """Return x with the sign of y."""
     # No exceptions possible.

Modified: pypy/branch/fast-forward/pypy/objspace/flow/operation.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/flow/operation.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/flow/operation.py	Fri Jul  2 02:05:52 2010
@@ -188,6 +188,7 @@
     ('nonzero',         operator.truth),
     ('is_true',         bool),
     ('is_true',         operator.truth),
+    ('trunc',           unsupported),
     ('abs' ,            abs),
     ('hex',             hex),
     ('oct',             oct),

Modified: pypy/branch/fast-forward/pypy/objspace/std/builtinshortcut.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/builtinshortcut.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/builtinshortcut.py	Fri Jul  2 02:05:52 2010
@@ -35,7 +35,7 @@
                  'setattr', 'delattr', 'userdel',  # mostly for non-builtins
                  'get', 'set', 'delete',   # uncommon (except on functions)
                  'getslice', 'setslice', 'delslice',  # see below
-                 'delitem',                       # rare stuff?
+                 'delitem', 'trunc',              # rare stuff?
                  'abs', 'hex', 'oct',             # rare stuff?
                  'pos', 'divmod', 'cmp',          # rare stuff?
                  'float', 'long', 'coerce',       # rare stuff?

Modified: pypy/branch/fast-forward/pypy/objspace/std/floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/floatobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/floatobject.py	Fri Jul  2 02:05:52 2010
@@ -8,7 +8,7 @@
 from pypy.objspace.std.noneobject import W_NoneObject
 from pypy.objspace.std.longobject import W_LongObject
 from pypy.rlib.rarithmetic import ovfcheck_float_to_int, intmask, isinf, isnan
-from pypy.rlib.rarithmetic import formatd, LONG_BIT
+from pypy.rlib.rarithmetic import formatd, LONG_BIT, FL_MAXINT, FL_MININT
 from pypy.rlib.rbigint import rbigint
 from pypy.tool.sourcetools import func_with_new_name
 
@@ -73,6 +73,15 @@
     except OverflowError:
         raise OperationError(space.w_OverflowError,
                              space.wrap("cannot convert float infinity to long"))
+def trunc__Float(space, w_floatobj):
+    whole = math.modf(w_floatobj.floatval)[1]
+    if FL_MININT < whole < FL_MAXINT:
+        return space.newint(int(whole))
+    try:
+        return W_LongObject.fromfloat(w_floatobj.floatval)
+    except OverflowError:
+        raise OperationError(space.w_OverflowError,
+                             space.wrap("cannot convert infinity to long"))
 
 def float_w__Float(space, w_float):
     return w_float.floatval

Modified: pypy/branch/fast-forward/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/intobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/intobject.py	Fri Jul  2 02:05:52 2010
@@ -305,6 +305,7 @@
     return wrapint(space, a)
 get_integer = int__Int
 pos__Int = int__Int
+trunc__Int = int__Int
 
 def index__Int(space, w_int1):
     return get_integer(space, w_int1)

Modified: pypy/branch/fast-forward/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/longobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/longobject.py	Fri Jul  2 02:05:52 2010
@@ -77,6 +77,7 @@
         return w_long1
     l = w_long1.num
     return W_LongObject(l)
+trunc__Long = long__Long
 
 def long__Int(space, w_intobj):
     return W_LongObject.fromint(space, w_intobj.intval)

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_floatobject.py	Fri Jul  2 02:05:52 2010
@@ -322,6 +322,17 @@
         float.__getformat__("double")
         raises(ValueError, float.__getformat__, "random")
 
+    def test_trunc(self):
+        import math
+        assert math.trunc(1.5) == 1
+        assert math.trunc(-1.5) == -1
+        assert math.trunc(1.999999) == 1
+        assert math.trunc(-1.999999) == -1
+        assert math.trunc(-0.999999) == -0
+        assert math.trunc(-100.999) == -100
+        raises(OverflowError, math.trunc, float("inf"))
+
+
     def test_multimethod_slice(self):
         assert 5 .__add__(3.14) is NotImplemented
         assert 3.25 .__add__(5) == 8.25

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_intobject.py	Fri Jul  2 02:05:52 2010
@@ -285,6 +285,11 @@
 
 class AppTestInt:
 
+    def test_trunc(self):
+        import math
+        assert math.trunc(1) == 1
+        assert math.trunc(-1) == -1
+
     def test_int_callable(self):
         assert 43 == int(43)
 

Modified: pypy/branch/fast-forward/pypy/objspace/std/test/test_longobject.py
==============================================================================
--- pypy/branch/fast-forward/pypy/objspace/std/test/test_longobject.py	(original)
+++ pypy/branch/fast-forward/pypy/objspace/std/test/test_longobject.py	Fri Jul  2 02:05:52 2010
@@ -40,6 +40,12 @@
 
 
 class AppTestLong:
+
+    def test_trunc(self):
+        import math
+        assert math.trunc(1L) == 1L
+        assert math.trunc(-1L) == -1L
+
     def test_add(self):
         assert int(123L + 12443L) == 123 + 12443
         assert -20 + 2 + 3L + True == -14L



More information about the Pypy-commit mailing list