[pypy-svn] r5102 - in pypy/trunk/src/pypy/objspace/std: . test

arigo at codespeak.net arigo at codespeak.net
Wed Jun 16 14:17:12 CEST 2004


Author: arigo
Date: Wed Jun 16 14:17:12 2004
New Revision: 5102

Modified:
   pypy/trunk/src/pypy/objspace/std/intobject.py
   pypy/trunk/src/pypy/objspace/std/longtype.py
   pypy/trunk/src/pypy/objspace/std/multimethod.py
   pypy/trunk/src/pypy/objspace/std/test/test_intobject.py
Log:
- 'int/int' gives a float if true division
- added a hack to include the long.__xyz__ methods within the
  int.__xyz__ methods, so that ints are automatically coerced into
  longs in case of overflow
- added an overflow-gives-long test



Modified: pypy/trunk/src/pypy/objspace/std/intobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/intobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/intobject.py	Wed Jun 16 14:17:12 2004
@@ -166,9 +166,19 @@
     return W_IntObject(space, z)
 
 def _truediv(space, w_int1, w_int2):
-    # cannot implement, since it gives floats
-    raise FailedToImplement(space.w_OverflowError,
-                            space.wrap("integer division"))
+    x = w_int1.intval
+    y = w_int2.intval
+    try:
+        z = x // y
+        t = x % y
+    except ZeroDivisionError:
+        raise OperationError(space.w_ZeroDivisionError,
+                             space.wrap("integer division by zero"))
+    except OverflowError:
+        return space.div(space.newfloat(x), w_int2)
+    if t != 0:   # gives a float
+        return space.div(space.newfloat(x), w_int2)
+    return W_IntObject(space, z)
 
 def mod__Int_Int(space, w_int1, w_int2):
     x = w_int1.intval

Modified: pypy/trunk/src/pypy/objspace/std/longtype.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/longtype.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/longtype.py	Wed Jun 16 14:17:12 2004
@@ -1,4 +1,5 @@
 from pypy.objspace.std.stdtypedef import *
+from pypy.objspace.std.inttype import int_typedef
 
 def descr__new__(space, w_longtype, w_value=None):
     from pypy.objspace.std.longobject import W_LongObject
@@ -31,3 +32,6 @@
 long_typedef = StdTypeDef("long",
     __new__ = newmethod(descr__new__),
     )
+# hack to allow automatic int to long conversion: the int.__xyz__ methods
+# will fall back to their long.__xyz__ counterparts if they fail
+long_typedef.could_also_match = int_typedef

Modified: pypy/trunk/src/pypy/objspace/std/multimethod.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/multimethod.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/multimethod.py	Wed Jun 16 14:17:12 2004
@@ -365,7 +365,8 @@
         # Only accept an exact match; having merely subclass should
         # be taken care of by the general look-up rules.
         t = types[self.bound_position].typedef
-        return t is self.typeclass
+        return t is self.typeclass or (
+            getattr(t, 'could_also_match', None) is self.typeclass)
 
     def __get__(self, space, cls=None):
         if space is None:

Modified: pypy/trunk/src/pypy/objspace/std/test/test_intobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/test/test_intobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/test/test_intobject.py	Wed Jun 16 14:17:12 2004
@@ -316,5 +316,10 @@
         assert (1 << 0) == 1
         assert (1 >> 0) == 1
 
+    def test_overflow(self):
+        import sys
+        n = sys.maxint + 1
+        self.assert_(isinstance(n, long))
+
 if __name__ == '__main__':
     testit.main()



More information about the Pypy-commit mailing list