Maciej Fijalkowski avatar Maciej Fijalkowski committed a832d59

fix longs

Comments (0)

Files changed (3)

pypy/objspace/std/longobject.py

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

pypy/objspace/std/longtype.py

 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)
 @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:
         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"))
 
 # ____________________________________________________________
 
+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
 
     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())

pypy/objspace/std/test/test_longobject.py

 
     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
         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
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.