[pypy-commit] pypy default: fix longs

fijal noreply at buildbot.pypy.org
Mon Mar 25 21:56:55 CET 2013


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r62755:a832d5973ed3
Date: 2013-03-25 13:56 -0700
http://bitbucket.org/pypy/pypy/changeset/a832d5973ed3/

Log:	fix longs

diff --git a/pypy/objspace/std/longobject.py b/pypy/objspace/std/longobject.py
--- a/pypy/objspace/std/longobject.py
+++ b/pypy/objspace/std/longobject.py
@@ -6,35 +6,16 @@
 from pypy.objspace.std.multimethod import FailedToImplementArgs
 from pypy.objspace.std.intobject import W_IntObject
 from pypy.objspace.std.noneobject import W_NoneObject
-from rpython.rlib.rbigint import rbigint, SHIFT
-
-class W_AbstractLongObject(W_Object):
-    __slots__ = ()
-
-    def is_w(self, space, w_other):
-        if not isinstance(w_other, W_AbstractLongObject):
-            return False
-        if self.user_overridden_class or w_other.user_overridden_class:
-            return self is w_other
-        return space.bigint_w(self).eq(space.bigint_w(w_other))
-
-    def immutable_unique_id(self, space):
-        if self.user_overridden_class:
-            return None
-        from pypy.objspace.std.model import IDTAG_LONG as tag
-        b = space.bigint_w(self)
-        b = b.lshift(3).or_(rbigint.fromint(tag))
-        return space.newlong_from_rbigint(b)
-
-    def unwrap(w_self, space): #YYYYYY
-        return w_self.longval()
+from rpython.rlib.rbigint import rbigint
+from pypy.objspace.std.longtype import long_typedef, W_AbstractLongObject
 
 
 class W_LongObject(W_AbstractLongObject):
     """This is a wrapper of rbigint."""
-    from pypy.objspace.std.longtype import long_typedef as typedef
     _immutable_fields_ = ['num']
 
+    typedef = long_typedef
+
     def __init__(w_self, l):
         w_self.num = l # instance of rbigint
 
@@ -88,6 +69,9 @@
         return self.num.tofloat()
 
     def int(self, space):
+        if (type(self) is not W_LongObject and
+            space.is_overloaded(self, space.w_long, '__int__')):
+            return W_Object.int(self, space)
         try:
             return space.newint(self.num.toint())
         except OverflowError:
diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py
--- a/pypy/objspace/std/longtype.py
+++ b/pypy/objspace/std/longtype.py
@@ -1,9 +1,11 @@
 from pypy.interpreter.error import OperationError
 from pypy.interpreter import typedef
-from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
+from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\
+     interpindirect2app
+from pypy.objspace.std.model import W_Object
+from pypy.objspace.std.stdtypedef import StdTypeDef
 from pypy.objspace.std.strutil import string_to_bigint, ParseStringError
+from rpython.rlib.rbigint import rbigint
 
 def descr_conjugate(space, w_int):
     return space.long(w_int)
@@ -12,7 +14,6 @@
 @unwrap_spec(w_x = WrappedDefault(0))
 def descr__new__(space, w_longtype, w_x, w_base=None):
     from pypy.objspace.std.longobject import W_LongObject
-    from rpython.rlib.rbigint import rbigint
     if space.config.objspace.std.withsmalllong:
         from pypy.objspace.std.smalllongobject import W_SmallLongObject
     else:
@@ -56,7 +57,7 @@
         else:
             try:
                 s = space.str_w(w_value)
-            except OperationError, e:
+            except OperationError:
                 raise OperationError(space.w_TypeError,
                                      space.wrap("long() can't convert non-string "
                                                 "with explicit base"))
@@ -114,6 +115,30 @@
 
 # ____________________________________________________________
 
+class W_AbstractLongObject(W_Object):
+    __slots__ = ()
+
+    def is_w(self, space, w_other):
+        if not isinstance(w_other, W_AbstractLongObject):
+            return False
+        if self.user_overridden_class or w_other.user_overridden_class:
+            return self is w_other
+        return space.bigint_w(self).eq(space.bigint_w(w_other))
+
+    def immutable_unique_id(self, space):
+        if self.user_overridden_class:
+            return None
+        from pypy.objspace.std.model import IDTAG_LONG as tag
+        b = space.bigint_w(self)
+        b = b.lshift(3).or_(rbigint.fromint(tag))
+        return space.newlong_from_rbigint(b)
+
+    def unwrap(w_self, space): #YYYYYY
+        return w_self.longval()
+
+    def int(self, space):
+        raise NotImplementedError
+
 long_typedef = StdTypeDef("long",
     __doc__ = '''long(x[, base]) -> integer
 
@@ -129,5 +154,6 @@
     real = typedef.GetSetProperty(descr_get_real),
     imag = typedef.GetSetProperty(descr_get_imag),
     bit_length = interp2app(bit_length),
+    __int__ = interpindirect2app(W_AbstractLongObject.int),
 )
 long_typedef.registermethods(globals())
diff --git a/pypy/objspace/std/test/test_longobject.py b/pypy/objspace/std/test/test_longobject.py
--- a/pypy/objspace/std/test/test_longobject.py
+++ b/pypy/objspace/std/test/test_longobject.py
@@ -231,13 +231,13 @@
 
     def test_math_log(self):
         import math
-        raises(ValueError, math.log, 0L) 
-        raises(ValueError, math.log, -1L) 
-        raises(ValueError, math.log, -2L) 
+        raises(ValueError, math.log, 0L)
+        raises(ValueError, math.log, -1L)
+        raises(ValueError, math.log, -2L)
         raises(ValueError, math.log, -(1L << 10000))
-        #raises(ValueError, math.log, 0) 
-        raises(ValueError, math.log, -1) 
-        raises(ValueError, math.log, -2) 
+        #raises(ValueError, math.log, 0)
+        raises(ValueError, math.log, -1)
+        raises(ValueError, math.log, -2)
 
     def test_long(self):
         import sys
@@ -327,3 +327,11 @@
         class A(long): pass
         b = A(5).real
         assert type(b) is long
+
+    def test__int__(self):
+        class A(long):
+            def __int__(self):
+                return 42
+
+        assert int(long(3)) == long(3)
+        assert int(A(13)) == 42


More information about the pypy-commit mailing list