Commits

Philip Jenvey committed c67fd8f

beginnings of int/bool multimethod removal

  • Participants
  • Parent commits 91a199b
  • Branches remove-intlong-smm

Comments (0)

Files changed (10)

File pypy/module/micronumpy/interp_boxes.py

 from pypy.objspace.std.floattype import float_typedef
 from pypy.objspace.std.stringtype import str_typedef
 from pypy.objspace.std.unicodetype import unicode_typedef, unicode_from_object
-from pypy.objspace.std.inttype import int_typedef
+from pypy.objspace.std.intobject import W_IntObject
 from pypy.objspace.std.complextype import complex_typedef
 from rpython.rlib.rarithmetic import LONG_BIT
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rlib.rstring import StringBuilder
 
 
-MIXIN_32 = (int_typedef,) if LONG_BIT == 32 else ()
-MIXIN_64 = (int_typedef,) if LONG_BIT == 64 else ()
+MIXIN_32 = (W_IntObject.typedef,) if LONG_BIT == 32 else ()
+MIXIN_64 = (W_IntObject.typedef,) if LONG_BIT == 64 else ()
 
 # Is this the proper place for this?
 ENABLED_LONG_DOUBLE = False

File pypy/objspace/std/boolobject.py

+from rpython.rlib.rarithmetic import r_uint
 from rpython.rlib.rbigint import rbigint
-from rpython.rlib.rarithmetic import r_uint
-from pypy.interpreter.error import OperationError
-from pypy.objspace.std import newformat
-from pypy.objspace.std.model import registerimplementation, W_Object
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.intobject import W_IntObject
 
+from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
+from pypy.objspace.std.intobject import W_AbstractIntObject, W_IntObject
+from pypy.objspace.std.stdtypedef import StdTypeDef
 
-class W_BoolObject(W_Object):
-    from pypy.objspace.std.booltype import bool_typedef as typedef
+
+class W_BoolObject(W_AbstractIntObject):
+
     _immutable_fields_ = ['boolval']
 
     def __init__(self, boolval):
     def int(self, space):
         return space.newint(int(self.boolval))
 
-registerimplementation(W_BoolObject)
+    def descr_repr(self, space):
+        return space.wrap('True' if self.boolval else 'False')
+
+    descr_str = descr_repr
+
+    def descr_nonzero(self, space):
+        return self
+
+    def descr_and(self, space, w_other):
+        if not isinstance(w_other, W_BoolObject):
+            return W_AbstractIntObject.descr_and(self, space, w_other)
+        return space.newbool(self.boolval & w_other.boolval)
+
+    def descr_or(self, space, w_other):
+        if not isinstance(w_other, W_BoolObject):
+            return W_AbstractIntObject.descr_or(self, space, w_other)
+        return space.newbool(self.boolval | w_other.boolval)
+
+    def descr_xor(self, space, w_other):
+        if not isinstance(w_other, W_BoolObject):
+            return W_AbstractIntObject.descr_xor(self, space, w_other)
+        return space.newbool(self.boolval ^ w_other.boolval)
 
 W_BoolObject.w_False = W_BoolObject(False)
 W_BoolObject.w_True  = W_BoolObject(True)
 
-# bool-to-int delegation requires translating the .boolvar attribute
-# to an .intval one
-def delegate_Bool2IntObject(space, w_bool):
-    return W_IntObject(int(w_bool.boolval))
+@unwrap_spec(w_obj=WrappedDefault(False))
+def descr__new__(space, w_booltype, w_obj):
+    space.w_bool.check_user_subclass(w_booltype)
+    return space.newbool(space.is_true(w_obj)) # XXX: method call?
 
+# ____________________________________________________________
 
-def nonzero__Bool(space, w_bool):
-    return w_bool
+W_BoolObject.typedef = StdTypeDef("bool", W_IntObject.typedef,
+    __doc__ = """bool(x) -> bool
 
-def repr__Bool(space, w_bool):
-    if w_bool.boolval:
-        return space.wrap('True')
-    else:
-        return space.wrap('False')
-
-def and__Bool_Bool(space, w_bool1, w_bool2):
-    return space.newbool(w_bool1.boolval & w_bool2.boolval)
-
-def or__Bool_Bool(space, w_bool1, w_bool2):
-    return space.newbool(w_bool1.boolval | w_bool2.boolval)
-
-def xor__Bool_Bool(space, w_bool1, w_bool2):
-    return space.newbool(w_bool1.boolval ^ w_bool2.boolval)
-
-str__Bool = repr__Bool
-
-def format__Bool_ANY(space, w_bool, w_format_spec):
-    return newformat.run_formatter(
-            space, w_format_spec, "format_int_or_long", w_bool,
-            newformat.INT_KIND)
-
-register_all(vars())
+Returns True when the argument x is true, False otherwise.
+The builtins True and False are the only two instances of the class bool.
+The class bool is a subclass of the class int, and cannot be subclassed.""",
+    __new__ = interp2app(descr__new__),
+    __repr__ = interp2app(W_BoolObject.descr_repr),
+    __str__ = interp2app(W_BoolObject.descr_str),
+                                  # XXX: might as well declare interp2app directly here for nonzero/and/etc
+    )
+W_BoolObject.typedef.acceptable_as_base_class = False

File pypy/objspace/std/booltype.py

-from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
-from pypy.objspace.std.stdtypedef import StdTypeDef
-from pypy.objspace.std.inttype import int_typedef
-
-@unwrap_spec(w_obj = WrappedDefault(False))
-def descr__new__(space, w_booltype, w_obj):
-    space.w_bool.check_user_subclass(w_booltype)
-    if space.is_true(w_obj):
-        return space.w_True
-    else:
-        return space.w_False
-
-# ____________________________________________________________
-
-bool_typedef = StdTypeDef("bool", int_typedef,
-    __doc__ = '''bool(x) -> bool
-
-Returns True when the argument x is true, False otherwise.
-The builtins True and False are the only two instances of the class bool.
-The class bool is a subclass of the class int, and cannot be subclassed.''',
-    __new__ = interp2app(descr__new__),
-    )
-bool_typedef.acceptable_as_base_class = False

File pypy/objspace/std/frame.py

     w_1 = f.popvalue()
     if (type(w_1) is intobject.W_IntObject and
         type(w_2) is intobject.W_IntObject):
-        try:
-            w_result = intobject.add__Int_Int(f.space, w_1, w_2)
-        except FailedToImplement:
+        w_result = w_1.descr_add(f.space, w_2)
+        if f.space.is_w(w_result, f.space.w_NotImplemented):
             w_result = f.space.add(w_1, w_2)
     else:
         w_result = f.space.add(w_1, w_2)

File pypy/objspace/std/intobject.py

 """
 
 from rpython.rlib import jit
-from rpython.rlib.rarithmetic import LONG_BIT, is_valid_int, ovfcheck, r_uint
+from rpython.rlib.rarithmetic import (
+    LONG_BIT, is_valid_int, ovfcheck, string_to_int, r_uint)
 from rpython.rlib.rbigint import rbigint
+from rpython.rlib.objectmodel import instantiate
+from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError
+from rpython.tool.sourcetools import func_with_new_name
 
-from pypy.interpreter.error import OperationError
+from pypy.interpreter import typedef
+from pypy.interpreter.buffer import Buffer
+from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.gateway import (
+    WrappedDefault, interp2app, interpindirect2app, unwrap_spec)
 from pypy.objspace.std import newformat
-from pypy.objspace.std.inttype import W_AbstractIntObject
-from pypy.objspace.std.model import W_Object, registerimplementation
-from pypy.objspace.std.multimethod import FailedToImplementArgs
-from pypy.objspace.std.noneobject import W_NoneObject
-from pypy.objspace.std.register_all import register_all
+from pypy.objspace.std.model import W_Object
+from pypy.objspace.std.stdtypedef import StdTypeDef
+
+
+class W_AbstractIntObject(W_Object):
+    __slots__ = ()
+
+    def int(self, space):
+        raise NotImplementedError
+
+    def descr_format(self, space, w_format_spec):
+        return newformat.run_formatter(space, w_format_spec,
+                                       "format_int_or_long", self,
+                                       newformat.INT_KIND)
+
+    def descr_hash(self, space):
+        # unlike CPython, we don't special-case the value -1 in most of
+        # our hash functions, so there is not much sense special-casing
+        # it here either.  Make sure this is consistent with the hash of
+        # floats and longs.
+        return self.int(space)
+
+    def descr_coerce(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+        # XXX: have to call space.int on w_other: 2
+        # .__coerce__(True) -> (2, 1): actually cpython doesn't do
+        # this, so i don't care!
+        return space.newtuple([self, w_other])
+
+    def _make_descr_binop(opname):
+        # XXX: func_renamer or func_with_new_name?
+        import operator
+        from rpython.tool.sourcetools import func_renamer
+        op = getattr(operator, opname)
+
+        @func_renamer('descr_' + opname)
+        def descr_binop(self, space, w_other):
+            if not space.isinstance_w(w_other, space.w_int):
+                return space.w_NotImplemented
+
+            x = space.int_w(self)
+            y = space.int_w(w_other)
+            try:
+                z = ovfcheck(op(x, y))
+            except OverflowError:
+                w_long1 = _delegate_Int2Long(space, self)
+                # XXX: this needs to be _delegate_Int2Long(space,
+                # space.int(w_other)) to support bools. so maybe delegation
+                # should work against space.int_w(w_other)
+                w_long2 = _delegate_Int2Long(space, w_other)
+                return getattr(space, opname)(w_long1, w_long2)
+            return wrapint(space, z)
+
+        @func_renamer('descr_r' + opname)
+        def descr_rbinop(self, space, w_other):
+            if not space.isinstance_w(w_other, space.w_int):
+                return space.w_NotImplemented
+
+            x = space.int_w(self)
+            y = space.int_w(w_other)
+            try:
+                z = ovfcheck(op(y, x))
+            except OverflowError:
+                w_long1 = _delegate_Int2Long(space, self)
+                # XXX: this needs to be _delegate_Int2Long(space,
+                # space.int(w_other)) to support bools. so maybe delegation
+                # should work against space.int_w(w_other)
+                w_long2 = _delegate_Int2Long(space, w_other)
+                return getattr(space, opname)(w_long2, w_long1)
+            return wrapint(space, z)
+
+        return descr_binop, descr_rbinop
+
+    descr_add, descr_radd = _make_descr_binop('add')
+    descr_sub, descr_rsub = _make_descr_binop('sub')
+    descr_mul, descr_rmul = _make_descr_binop('mul')
+
+    def _make_descr_cmp(opname):
+        import operator
+        op = getattr(operator, opname)
+        def f(self, space, w_other):
+            if not space.isinstance_w(w_other, space.w_int):
+                return space.w_NotImplemented
+
+            i = space.int_w(self)
+            j = space.int_w(w_other)
+            return space.newbool(op(i, j))
+        return func_with_new_name(f, "descr_" + opname)
+
+    descr_lt = _make_descr_cmp('lt')
+    descr_le = _make_descr_cmp('le')
+    descr_eq = _make_descr_cmp('eq')
+    descr_ne = _make_descr_cmp('ne')
+    descr_gt = _make_descr_cmp('gt')
+    descr_ge = _make_descr_cmp('ge')
+
+    def descr_floordiv(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        x = space.int_w(self)
+        y = space.int_w(w_other)
+        try:
+            z = ovfcheck(x // y)
+        except ZeroDivisionError:
+            raise operationerrfmt(space.w_ZeroDivisionError,
+                                  "integer division by zero")
+        except OverflowError:
+            w_long1 = _delegate_Int2Long(space, self)
+            w_long2 = _delegate_Int2Long(space, w_other)
+            return space.floordiv(w_long1, w_long2)
+        return wrapint(space, z)
+
+    descr_div = func_with_new_name(descr_floordiv, 'descr_div')
+
+    def descr_truediv(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        x = float(space.int_w(self))
+        y = float(space.int_w(w_other))
+        if y == 0.0:
+            raise operationerrfmt(space.w_ZeroDivisionError,
+                                  "division by zero")
+        return space.wrap(x / y)
+
+    def descr_mod(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        x = space.int_w(self)
+        y = space.int_w(w_other)
+        try:
+            z = ovfcheck(x % y)
+        except ZeroDivisionError:
+            raise operationerrfmt(space.w_ZeroDivisionError,
+                                  "integer modulo by zero")
+        except OverflowError:
+            w_long1 = _delegate_Int2Long(space, self)
+            w_long2 = _delegate_Int2Long(space, w_other)
+            return space.mod(w_long1, w_long2)
+        return wrapint(space, z)
+
+    def descr_divmod(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        x = space.int_w(self)
+        y = space.int_w(w_other)
+        try:
+            z = ovfcheck(x // y)
+        except ZeroDivisionError:
+            raise operationerrfmt(space.w_ZeroDivisionError,
+                                  "integer divmod by zero")
+        except OverflowError:
+            w_long1 = _delegate_Int2Long(space, self)
+            w_long2 = _delegate_Int2Long(space, w_other)
+            return space.divmod(w_long1, w_long2)
+
+        # no overflow possible
+        m = x % y
+        w = space.wrap
+        return space.newtuple([w(z), w(m)])
+
+    @unwrap_spec(w_modulus=WrappedDefault(None))
+    def descr_pow(self, space, w_exponent, w_modulus):
+        if not space.isinstance_w(w_exponent, space.w_int):
+            return space.w_NotImplemented
+        if space.is_none(w_modulus):
+            z = 0
+        elif space.isinstance_w(w_modulus, space.w_int):
+            # XXX: handle long... overflow?
+            z = space.int_w(w_modulus)
+            if z == 0:
+                raise operationerrfmt(space.w_ValueError,
+                                      "pow() 3rd argument cannot be 0")
+        else:
+            return self._delegate2longpow(space, w_exponent, w_modulus)
+            #return space.w_NotImplemented
+
+        x = space.int_w(self)
+        y = space.int_w(w_exponent)
+        try:
+            return space.wrap(_pow_impl(space, x, y, z))
+        except NotImplementedError:
+            return self._delegate2longpow(space, w_exponent, w_modulus)
+
+    def _delegate2longpow(self, space, w_exponent, w_modulus):
+        # XXX: gross
+        w_long1 = _delegate_Int2Long(space, self)
+        w_exponent = _delegate_Int2Long(space, w_exponent)
+        if not space.is_none(w_modulus):
+            w_modulus = _delegate_Int2Long(space, w_modulus)
+        return space.pow(w_long1, w_exponent, w_modulus)
+
+    @unwrap_spec(w_modulus=WrappedDefault(None))
+    def descr_rpow(self, space, w_base, w_modulus):
+        if not space.isinstance_w(w_base, space.w_int):
+            return space.w_NotImplemented
+        # XXX: this seems like trouble?
+        return space.pow(w_base, self, w_modulus)
+
+    def descr_neg(self, space):
+        a = space.int_w(self)
+        try:
+            x = ovfcheck(-a)
+        except OverflowError:
+            w_long1 = _delegate_Int2Long(space, self)
+            return space.neg(w_long1)
+        return wrapint(space, x)
+
+    def descr_abs(self, space):
+        return self.int(space) if space.int_w(self) >= 0 else self.descr_neg(space)
+
+    def descr_nonzero(self, space):
+        return space.newbool(space.int_w(self) != 0)
+
+    def descr_invert(self, space):
+        return wrapint(space, ~space.int_w(self))
+
+    def descr_lshift(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        a = space.int_w(self)
+        b = space.int_w(w_other)
+        if r_uint(b) < LONG_BIT: # 0 <= b < LONG_BIT
+            try:
+                c = ovfcheck(a << b)
+            except OverflowError:
+                w_long1 = _delegate_Int2Long(space, self)
+                w_long2 = _delegate_Int2Long(space, w_other)
+                return space.lshift(w_long1, w_long2)
+            return wrapint(space, c)
+        if b < 0:
+            raise operationerrfmt(space.w_ValueError, "negative shift count")
+        else: # b >= LONG_BIT
+            if a == 0:
+                return self.int(space)
+            w_long1 = _delegate_Int2Long(space, self)
+            w_long2 = _delegate_Int2Long(space, w_other)
+            return space.lshift(w_long1, w_long2)
+
+    def descr_rshift(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        a = space.int_w(self)
+        b = space.int_w(w_other)
+        if r_uint(b) >= LONG_BIT: # not (0 <= b < LONG_BIT)
+            if b < 0:
+                raise operationerrfmt(space.w_ValueError,
+                                      "negative shift count")
+            else: # b >= LONG_BIT
+                if a == 0:
+                    return self.int(space)
+                if a < 0:
+                    a = -1
+                else:
+                    a = 0
+        else:
+            a = a >> b
+        return wrapint(space, a)
+
+    def descr_and(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        a = space.int_w(self)
+        b = space.int_w(w_other)
+        res = a & b
+        return wrapint(space, res)
+
+    def descr_or(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        a = space.int_w(self)
+        b = space.int_w(w_other)
+        res = a | b
+        return wrapint(space, res)
+
+    def descr_xor(self, space, w_other):
+        if not space.isinstance_w(w_other, space.w_int):
+            return space.w_NotImplemented
+
+        a = space.int_w(self)
+        b = space.int_w(w_other)
+        res = a ^ b
+        return wrapint(space, res)
+
+    descr_rand = func_with_new_name(descr_and, 'descr_rand')
+    descr_ror = func_with_new_name(descr_or, 'descr_ror')
+    descr_rxor = func_with_new_name(descr_xor, 'descr_rxor')
+
+    def descr_pos(self, space):
+        return self.int(space)
+
+    descr_trunc = func_with_new_name(descr_pos, 'descr_trunc')
+
+    def descr_index(self, space):
+        return self.int(space)
+
+    def descr_float(self, space):
+        a = space.int_w(self)
+        x = float(a)
+        return space.newfloat(x)
+
+    def descr_oct(self, space):
+        return space.wrap(oct(space.int_w(self)))
+
+    def descr_hex(self, space):
+        return space.wrap(hex(space.int_w(self)))
+
+    def descr_getnewargs(self, space):
+        return space.newtuple([wrapint(space, space.int_w(self))])
 
 
 class W_IntObject(W_AbstractIntObject):
     __slots__ = 'intval'
     _immutable_fields_ = ['intval']
 
-    from pypy.objspace.std.inttype import int_typedef as typedef
-
     def __init__(self, intval):
         assert is_valid_int(intval)
         self.intval = intval
         """representation for debugging purposes"""
         return "%s(%d)" % (self.__class__.__name__, self.intval)
 
+    def is_w(self, space, w_other):
+        if not isinstance(w_other, W_AbstractIntObject):
+            return False
+        if self.user_overridden_class or w_other.user_overridden_class:
+            return self is w_other
+        return space.int_w(self) == space.int_w(w_other)
+
+    def immutable_unique_id(self, space):
+        if self.user_overridden_class:
+            return None
+        from pypy.objspace.std.model import IDTAG_INT as tag
+        b = space.bigint_w(self)
+        b = b.lshift(3).or_(rbigint.fromint(tag))
+        return space.newlong_from_rbigint(b)
+
     def unwrap(self, space):
         return int(self.intval)
     int_w = unwrap
         a = self.intval
         return space.newint(a)
 
-registerimplementation(W_IntObject)
+    def descr_repr(self, space):
+        res = str(self.intval)
+        return space.wrap(res)
 
-def repr__Int(space, w_int1):
-    a = w_int1.intval
-    res = str(a)
-    return space.wrap(res)
+    descr_str = func_with_new_name(descr_repr, 'descr_str')
 
-str__Int = repr__Int
-
-def format__Int_ANY(space, w_int, w_format_spec):
-    return newformat.run_formatter(space, w_format_spec, "format_int_or_long",
-                                   w_int, newformat.INT_KIND)
-
-def declare_new_int_comparison(opname):
-    import operator
-    from rpython.tool.sourcetools import func_with_new_name
-    op = getattr(operator, opname)
-    def f(space, w_int1, w_int2):
-        i = w_int1.intval
-        j = w_int2.intval
-        return space.newbool(op(i, j))
-    name = "%s__Int_Int" % (opname,)
-    return func_with_new_name(f, name), name
-
-for op in ['lt', 'le', 'eq', 'ne', 'gt', 'ge']:
-    func, name = declare_new_int_comparison(op)
-    globals()[name] = func
-
-def hash__Int(space, w_int1):
-    # unlike CPython, we don't special-case the value -1 in most of our
-    # hash functions, so there is not much sense special-casing it here either.
-    # Make sure this is consistent with the hash of floats and longs.
-    return w_int1.int(space)
-
-# coerce
-def coerce__Int_Int(space, w_int1, w_int2):
-    return space.newtuple([w_int1, w_int2])
-
-
-def add__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x + y)
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer addition"))
-    return space.newint(z)
-
-def sub__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x - y)
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer substraction"))
-    return space.newint(z)
-
-def mul__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x * y)
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer multiplication"))
-    return space.newint(z)
-
-def floordiv__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x // y)
-    except ZeroDivisionError:
-        raise OperationError(space.w_ZeroDivisionError,
-                             space.wrap("integer division by zero"))
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer division"))
-    return space.newint(z)
-div__Int_Int = floordiv__Int_Int
-
-def truediv__Int_Int(space, w_int1, w_int2):
-    x = float(w_int1.intval)
-    y = float(w_int2.intval)
-    if y == 0.0:
-        raise FailedToImplementArgs(space.w_ZeroDivisionError,
-                                    space.wrap("float division"))
-    return space.wrap(x / y)
-
-def mod__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x % y)
-    except ZeroDivisionError:
-        raise OperationError(space.w_ZeroDivisionError,
-                             space.wrap("integer modulo by zero"))
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer modulo"))
-    return space.newint(z)
-
-def divmod__Int_Int(space, w_int1, w_int2):
-    x = w_int1.intval
-    y = w_int2.intval
-    try:
-        z = ovfcheck(x // y)
-    except ZeroDivisionError:
-        raise OperationError(space.w_ZeroDivisionError,
-                             space.wrap("integer divmod by zero"))
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer modulo"))
-    # no overflow possible
-    m = x % y
-    w = space.wrap
-    return space.newtuple([w(z), w(m)])
+def _delegate_Int2Long(space, w_intobj):
+    from pypy.objspace.std.longobject import W_LongObject
+    return W_LongObject.fromint(space, w_intobj.int_w(space))
 
 
 # helper for pow()
 @jit.look_inside_iff(lambda space, iv, iw, iz:
                      jit.isconstant(iw) and jit.isconstant(iz))
-def _impl_int_int_pow(space, iv, iw, iz):
+def _pow_impl(space, iv, iw, iz):
     if iw < 0:
         if iz != 0:
-            raise OperationError(space.w_TypeError,
-                             space.wrap("pow() 2nd argument "
-                 "cannot be negative when 3rd argument specified"))
+            msg = ("pow() 2nd argument cannot be negative when 3rd argument "
+                   "specified")
+            raise operationerrfmt(space.w_TypeError, msg)
         ## bounce it, since it always returns float
-        raise FailedToImplementArgs(space.w_ValueError,
-                                space.wrap("integer exponentiation"))
+        raise NotImplementedError
     temp = iv
     ix = 1
     try:
         if iz:
             ix = ix % iz
     except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer exponentiation"))
+        raise NotImplementedError
     return ix
 
-def pow__Int_Int_Int(space, w_int1, w_int2, w_int3):
-    x = w_int1.intval
-    y = w_int2.intval
-    z = w_int3.intval
-    if z == 0:
+# ____________________________________________________________
+
+def descr_conjugate(space, w_int):
+    "Returns self, the complex conjugate of any int."
+    return space.int(w_int)
+
+def descr_bit_length(space, w_int):
+    """int.bit_length() -> int
+
+    Number of bits necessary to represent self in binary.
+    >>> bin(37)
+    '0b100101'
+    >>> (37).bit_length()
+    6
+    """
+    val = space.int_w(w_int)
+    if val < 0:
+        val = -val
+    bits = 0
+    while val:
+        bits += 1
+        val >>= 1
+    return space.wrap(bits)
+
+
+def wrapint(space, x):
+    if space.config.objspace.std.withprebuiltint:
+        from pypy.objspace.std.intobject import W_IntObject
+        lower = space.config.objspace.std.prebuiltintfrom
+        upper =  space.config.objspace.std.prebuiltintto
+        # use r_uint to perform a single comparison (this whole function
+        # is getting inlined into every caller so keeping the branching
+        # to a minimum is a good idea)
+        index = r_uint(x - lower)
+        if index >= r_uint(upper - lower):
+            w_res = instantiate(W_IntObject)
+        else:
+            w_res = W_IntObject.PREBUILT[index]
+        # obscure hack to help the CPU cache: we store 'x' even into
+        # a prebuilt integer's intval.  This makes sure that the intval
+        # field is present in the cache in the common case where it is
+        # quickly reused.  (we could use a prefetch hint if we had that)
+        w_res.intval = x
+        return w_res
+    else:
+        from pypy.objspace.std.intobject import W_IntObject
+        return W_IntObject(x)
+
+# ____________________________________________________________
+
+@jit.elidable
+def string_to_int_or_long(space, string, base=10):
+    w_longval = None
+    value = 0
+    try:
+        value = string_to_int(string, base)
+    except ParseStringError, e:
         raise OperationError(space.w_ValueError,
-                             space.wrap("pow() 3rd argument cannot be 0"))
-    return space.wrap(_impl_int_int_pow(space, x, y, z))
+                             space.wrap(e.msg))
+    except ParseStringOverflowError, e:
+        w_longval = retry_to_w_long(space, e.parser)
+    return value, w_longval
 
-def pow__Int_Int_None(space, w_int1, w_int2, w_int3):
-    x = w_int1.intval
-    y = w_int2.intval
-    return space.wrap(_impl_int_int_pow(space, x, y, 0))
+def retry_to_w_long(space, parser):
+    parser.rewind()
+    try:
+        bigint = rbigint._from_numberstring_parser(parser)
+    except ParseStringError, e:
+        raise OperationError(space.w_ValueError,
+                             space.wrap(e.msg))
+    return space.newlong_from_rbigint(bigint)
 
-def neg__Int(space, w_int1):
-    a = w_int1.intval
-    try:
-        x = ovfcheck(-a)
-    except OverflowError:
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer negation"))
-    return space.newint(x)
-get_negint = neg__Int
+@unwrap_spec(w_x = WrappedDefault(0))
+def descr__new__(space, w_inttype, w_x, w_base=None):
+    from pypy.objspace.std.intobject import W_IntObject
+    w_longval = None
+    w_value = w_x     # 'x' is the keyword argument name in CPython
+    value = 0
+    if w_base is None:
+        ok = False
+        # check for easy cases
+        if type(w_value) is W_IntObject:
+            value = w_value.intval
+            ok = True
+        elif space.isinstance_w(w_value, space.w_str):
+            value, w_longval = string_to_int_or_long(space, space.str_w(w_value))
+            ok = True
+        elif space.isinstance_w(w_value, space.w_unicode):
+            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+            string = unicode_to_decimal_w(space, w_value)
+            value, w_longval = string_to_int_or_long(space, string)
+            ok = True
+        else:
+            # If object supports the buffer interface
+            try:
+                w_buffer = space.buffer(w_value)
+            except OperationError, e:
+                if not e.match(space, space.w_TypeError):
+                    raise
+            else:
+                buf = space.interp_w(Buffer, w_buffer)
+                value, w_longval = string_to_int_or_long(space, buf.as_str())
+                ok = True
 
+        if not ok:
+            # otherwise, use the __int__() or the __trunc__() methods
+            w_obj = w_value
+            if space.lookup(w_obj, '__int__') is None:
+                w_obj = space.trunc(w_obj)
+            w_obj = space.int(w_obj)
+            # 'int(x)' should return what x.__int__() returned, which should
+            # be an int or long or a subclass thereof.
+            if space.is_w(w_inttype, space.w_int):
+                return w_obj
+            # int_w is effectively what we want in this case,
+            # we cannot construct a subclass of int instance with an
+            # an overflowing long
+            try:
+                value = space.int_w(w_obj)
+            except OperationError, e:
+                if e.match(space, space.w_TypeError):
+                    raise OperationError(space.w_ValueError,
+                        space.wrap("value can't be converted to int"))
+                raise e
+    else:
+        base = space.int_w(w_base)
 
-def abs__Int(space, w_int1):
-    if w_int1.intval >= 0:
-        return w_int1.int(space)
+        if space.isinstance_w(w_value, space.w_unicode):
+            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+            s = unicode_to_decimal_w(space, w_value)
+        else:
+            try:
+                s = space.str_w(w_value)
+            except OperationError, e:
+                raise OperationError(space.w_TypeError,
+                                     space.wrap("int() can't convert non-string "
+                                                "with explicit base"))
+
+        value, w_longval = string_to_int_or_long(space, s, base)
+
+    if w_longval is not None:
+        if not space.is_w(w_inttype, space.w_int):
+            raise OperationError(space.w_OverflowError,
+                                 space.wrap(
+                "long int too large to convert to int"))
+        return w_longval
+    elif space.is_w(w_inttype, space.w_int):
+        # common case
+        return wrapint(space, value)
     else:
-        return get_negint(space, w_int1)
+        w_obj = space.allocate_instance(W_IntObject, w_inttype)
+        W_IntObject.__init__(w_obj, value)
+        return w_obj
 
-def nonzero__Int(space, w_int1):
-    return space.newbool(w_int1.intval != 0)
+def descr_get_numerator(space, w_obj):
+    return space.int(w_obj)
 
-def invert__Int(space, w_int1):
-    x = w_int1.intval
-    a = ~x
-    return space.newint(a)
+def descr_get_denominator(space, w_obj):
+    return space.wrap(1)
 
-def lshift__Int_Int(space, w_int1, w_int2):
-    a = w_int1.intval
-    b = w_int2.intval
-    if r_uint(b) < LONG_BIT: # 0 <= b < LONG_BIT
-        try:
-            c = ovfcheck(a << b)
-        except OverflowError:
-            raise FailedToImplementArgs(space.w_OverflowError,
-                                    space.wrap("integer left shift"))
-        return space.newint(c)
-    if b < 0:
-        raise OperationError(space.w_ValueError,
-                             space.wrap("negative shift count"))
-    else: #b >= LONG_BIT
-        if a == 0:
-            return w_int1.int(space)
-        raise FailedToImplementArgs(space.w_OverflowError,
-                                space.wrap("integer left shift"))
+def descr_get_real(space, w_obj):
+    return space.int(w_obj)
 
-def rshift__Int_Int(space, w_int1, w_int2):
-    a = w_int1.intval
-    b = w_int2.intval
-    if r_uint(b) >= LONG_BIT: # not (0 <= b < LONG_BIT)
-        if b < 0:
-            raise OperationError(space.w_ValueError,
-                                 space.wrap("negative shift count"))
-        else: # b >= LONG_BIT
-            if a == 0:
-                return w_int1.int(space)
-            if a < 0:
-                a = -1
-            else:
-                a = 0
-    else:
-        a = a >> b
-    return space.newint(a)
+def descr_get_imag(space, w_obj):
+    return space.wrap(0)
 
-def and__Int_Int(space, w_int1, w_int2):
-    a = w_int1.intval
-    b = w_int2.intval
-    res = a & b
-    return space.newint(res)
+# ____________________________________________________________
 
-def xor__Int_Int(space, w_int1, w_int2):
-    a = w_int1.intval
-    b = w_int2.intval
-    res = a ^ b
-    return space.newint(res)
 
-def or__Int_Int(space, w_int1, w_int2):
-    a = w_int1.intval
-    b = w_int2.intval
-    res = a | b
-    return space.newint(res)
+W_IntObject.typedef = StdTypeDef("int",
+    __doc__ = '''int(x[, base]) -> integer
 
-def pos__Int(self, space):
-    return self.int(space)
-trunc__Int = pos__Int
+Convert a string or number to an integer, if possible.  A floating point
+argument will be truncated towards zero (this does not include a string
+representation of a floating point number!)  When converting a string, use
+the optional base.  It is an error to supply a base when converting a
+non-string. If the argument is outside the integer range a long object
+will be returned instead.''',
+    __new__ = interp2app(descr__new__),
 
-def index__Int(space, w_int1):
-    return w_int1.int(space)
+    conjugate = interp2app(descr_conjugate),
+    bit_length = interp2app(descr_bit_length),
+    numerator = typedef.GetSetProperty(descr_get_numerator),
+    denominator = typedef.GetSetProperty(descr_get_denominator),
+    real = typedef.GetSetProperty(descr_get_real),
+    imag = typedef.GetSetProperty(descr_get_imag),
+    __int__ = interpindirect2app(W_AbstractIntObject.int),
 
-def float__Int(space, w_int1):
-    a = w_int1.intval
-    x = float(a)
-    return space.newfloat(x)
+    __format__ = interpindirect2app(W_AbstractIntObject.descr_format),
+    __hash__ = interpindirect2app(W_AbstractIntObject.descr_hash),
+    __coerce__ = interpindirect2app(W_AbstractIntObject.descr_coerce),
 
-def oct__Int(space, w_int1):
-    return space.wrap(oct(w_int1.intval))
+    __add__ = interpindirect2app(W_AbstractIntObject.descr_add),
+    __radd__ = interpindirect2app(W_AbstractIntObject.descr_radd),
+    __sub__ = interpindirect2app(W_AbstractIntObject.descr_sub),
+    __rsub__ = interpindirect2app(W_AbstractIntObject.descr_rsub),
+    __mul__ = interpindirect2app(W_AbstractIntObject.descr_mul),
+    __rmul__ = interpindirect2app(W_AbstractIntObject.descr_rmul),
+    __lt__ = interpindirect2app(W_AbstractIntObject.descr_lt),
+    __le__ = interpindirect2app(W_AbstractIntObject.descr_le),
+    __eq__ = interpindirect2app(W_AbstractIntObject.descr_eq),
+    __ne__ = interpindirect2app(W_AbstractIntObject.descr_ne),
+    __gt__ = interpindirect2app(W_AbstractIntObject.descr_gt),
+    __ge__ = interpindirect2app(W_AbstractIntObject.descr_ge),
 
-def hex__Int(space, w_int1):
-    return space.wrap(hex(w_int1.intval))
+    __floordiv__ = interpindirect2app(W_AbstractIntObject.descr_floordiv),
+    __div__ = interpindirect2app(W_AbstractIntObject.descr_div),
+    __truediv__ = interpindirect2app(W_AbstractIntObject.descr_truediv),
+    __mod__ = interpindirect2app(W_AbstractIntObject.descr_mod),
+    __divmod__ = interpindirect2app(W_AbstractIntObject.descr_divmod),
 
-def getnewargs__Int(space, w_int1):
-    return space.newtuple([space.newint(w_int1.intval)])
+    __pow__ = interpindirect2app(W_AbstractIntObject.descr_pow),
+    __rpow__ = interpindirect2app(W_AbstractIntObject.descr_rpow),
+    __neg__ = interpindirect2app(W_AbstractIntObject.descr_neg),
+    __abs__ = interpindirect2app(W_AbstractIntObject.descr_abs),
+    __nonzero__ = interpindirect2app(W_AbstractIntObject.descr_nonzero),
+    __invert__ = interpindirect2app(W_AbstractIntObject.descr_invert),
+    __lshift__ = interpindirect2app(W_AbstractIntObject.descr_lshift),
+    __rshift__ = interpindirect2app(W_AbstractIntObject.descr_rshift),
+    __and__ = interpindirect2app(W_AbstractIntObject.descr_and),
+    __rand__ = interpindirect2app(W_AbstractIntObject.descr_rand),
+    __xor__ = interpindirect2app(W_AbstractIntObject.descr_xor),
+    __rxor__ = interpindirect2app(W_AbstractIntObject.descr_rxor),
+    __or__ = interpindirect2app(W_AbstractIntObject.descr_or),
+    __ror__ = interpindirect2app(W_AbstractIntObject.descr_ror),
+    __pos__ = interpindirect2app(W_AbstractIntObject.descr_pos),
+    __trunc__ = interpindirect2app(W_AbstractIntObject.descr_trunc),
+    __index__ = interpindirect2app(W_AbstractIntObject.descr_index),
+    __float__ = interpindirect2app(W_AbstractIntObject.descr_float),
+    __oct__ = interpindirect2app(W_AbstractIntObject.descr_oct),
+    __hex__ = interpindirect2app(W_AbstractIntObject.descr_hex),
+    __getnewargs__ = interpindirect2app(W_AbstractIntObject.descr_getnewargs),
 
-
-register_all(vars())
+    __repr__ = interp2app(W_IntObject.descr_repr),
+    __str__ = interp2app(W_IntObject.descr_str),
+)

File pypy/objspace/std/inttype.py

-from pypy.interpreter import typedef
-from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault,\
-     interpindirect2app
-from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.buffer import Buffer
-from pypy.objspace.std.register_all import register_all
-from pypy.objspace.std.stdtypedef import StdTypeDef, SMM
-from pypy.objspace.std.model import W_Object
-from rpython.rlib.rarithmetic import r_uint, string_to_int
-from rpython.rlib.objectmodel import instantiate
-from rpython.rlib.rbigint import rbigint
-from rpython.rlib.rstring import ParseStringError, ParseStringOverflowError
-from rpython.rlib import jit
-
-# ____________________________________________________________
-
-def descr_conjugate(space, w_int):
-    "Returns self, the complex conjugate of any int."
-    return space.int(w_int)
-
-def descr_bit_length(space, w_int):
-    """int.bit_length() -> int
-
-    Number of bits necessary to represent self in binary.
-    >>> bin(37)
-    '0b100101'
-    >>> (37).bit_length()
-    6
-    """
-    val = space.int_w(w_int)
-    if val < 0:
-        val = -val
-    bits = 0
-    while val:
-        bits += 1
-        val >>= 1
-    return space.wrap(bits)
-
-
-def wrapint(space, x):
-    if space.config.objspace.std.withprebuiltint:
-        from pypy.objspace.std.intobject import W_IntObject
-        lower = space.config.objspace.std.prebuiltintfrom
-        upper =  space.config.objspace.std.prebuiltintto
-        # use r_uint to perform a single comparison (this whole function
-        # is getting inlined into every caller so keeping the branching
-        # to a minimum is a good idea)
-        index = r_uint(x - lower)
-        if index >= r_uint(upper - lower):
-            w_res = instantiate(W_IntObject)
-        else:
-            w_res = W_IntObject.PREBUILT[index]
-        # obscure hack to help the CPU cache: we store 'x' even into
-        # a prebuilt integer's intval.  This makes sure that the intval
-        # field is present in the cache in the common case where it is
-        # quickly reused.  (we could use a prefetch hint if we had that)
-        w_res.intval = x
-        return w_res
-    else:
-        from pypy.objspace.std.intobject import W_IntObject
-        return W_IntObject(x)
-
-# ____________________________________________________________
-
-@jit.elidable
-def string_to_int_or_long(space, string, base=10):
-    w_longval = None
-    value = 0
-    try:
-        value = string_to_int(string, base)
-    except ParseStringError, e:
-        raise OperationError(space.w_ValueError,
-                             space.wrap(e.msg))
-    except ParseStringOverflowError, e:
-        w_longval = retry_to_w_long(space, e.parser)
-    return value, w_longval
-
-def retry_to_w_long(space, parser):
-    parser.rewind()
-    try:
-        bigint = rbigint._from_numberstring_parser(parser)
-    except ParseStringError, e:
-        raise OperationError(space.w_ValueError,
-                             space.wrap(e.msg))
-    return space.newlong_from_rbigint(bigint)
-
-@unwrap_spec(w_x = WrappedDefault(0))
-def descr__new__(space, w_inttype, w_x, w_base=None):
-    from pypy.objspace.std.intobject import W_IntObject
-    w_longval = None
-    w_value = w_x     # 'x' is the keyword argument name in CPython
-    value = 0
-    if w_base is None:
-        ok = False
-        # check for easy cases
-        if type(w_value) is W_IntObject:
-            value = w_value.intval
-            ok = True
-        elif space.isinstance_w(w_value, space.w_str):
-            value, w_longval = string_to_int_or_long(space, space.str_w(w_value))
-            ok = True
-        elif space.isinstance_w(w_value, space.w_unicode):
-            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
-            string = unicode_to_decimal_w(space, w_value)
-            value, w_longval = string_to_int_or_long(space, string)
-            ok = True
-        else:
-            # If object supports the buffer interface
-            try:
-                w_buffer = space.buffer(w_value)
-            except OperationError, e:
-                if not e.match(space, space.w_TypeError):
-                    raise
-            else:
-                buf = space.interp_w(Buffer, w_buffer)
-                value, w_longval = string_to_int_or_long(space, buf.as_str())
-                ok = True
-
-        if not ok:
-            # otherwise, use the __int__() or the __trunc__() methods
-            w_obj = w_value
-            if space.lookup(w_obj, '__int__') is None:
-                w_obj = space.trunc(w_obj)
-            w_obj = space.int(w_obj)
-            # 'int(x)' should return what x.__int__() returned, which should
-            # be an int or long or a subclass thereof.
-            if space.is_w(w_inttype, space.w_int):
-                return w_obj
-            # int_w is effectively what we want in this case,
-            # we cannot construct a subclass of int instance with an
-            # an overflowing long
-            try:
-                value = space.int_w(w_obj)
-            except OperationError, e:
-                if e.match(space, space.w_TypeError):
-                    raise OperationError(space.w_ValueError,
-                        space.wrap("value can't be converted to int"))
-                raise e
-    else:
-        base = space.int_w(w_base)
-
-        if space.isinstance_w(w_value, space.w_unicode):
-            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
-            s = unicode_to_decimal_w(space, w_value)
-        else:
-            try:
-                s = space.str_w(w_value)
-            except OperationError, e:
-                raise OperationError(space.w_TypeError,
-                                     space.wrap("int() can't convert non-string "
-                                                "with explicit base"))
-
-        value, w_longval = string_to_int_or_long(space, s, base)
-
-    if w_longval is not None:
-        if not space.is_w(w_inttype, space.w_int):
-            raise OperationError(space.w_OverflowError,
-                                 space.wrap(
-                "long int too large to convert to int"))
-        return w_longval
-    elif space.is_w(w_inttype, space.w_int):
-        # common case
-        return wrapint(space, value)
-    else:
-        w_obj = space.allocate_instance(W_IntObject, w_inttype)
-        W_IntObject.__init__(w_obj, value)
-        return w_obj
-
-def descr_get_numerator(space, w_obj):
-    return space.int(w_obj)
-
-def descr_get_denominator(space, w_obj):
-    return space.wrap(1)
-
-def descr_get_real(space, w_obj):
-    return space.int(w_obj)
-
-def descr_get_imag(space, w_obj):
-    return space.wrap(0)
-
-# ____________________________________________________________
-
-class W_AbstractIntObject(W_Object):
-    __slots__ = ()
-
-    def is_w(self, space, w_other):
-        if not isinstance(w_other, W_AbstractIntObject):
-            return False
-        if self.user_overridden_class or w_other.user_overridden_class:
-            return self is w_other
-        return space.int_w(self) == space.int_w(w_other)
-
-    def immutable_unique_id(self, space):
-        if self.user_overridden_class:
-            return None
-        from pypy.objspace.std.model import IDTAG_INT as tag
-        b = space.bigint_w(self)
-        b = b.lshift(3).or_(rbigint.fromint(tag))
-        return space.newlong_from_rbigint(b)
-
-    def int(self, space):
-        raise NotImplementedError
-
-int_typedef = StdTypeDef("int",
-    __doc__ = '''int(x[, base]) -> integer
-
-Convert a string or number to an integer, if possible.  A floating point
-argument will be truncated towards zero (this does not include a string
-representation of a floating point number!)  When converting a string, use
-the optional base.  It is an error to supply a base when converting a
-non-string. If the argument is outside the integer range a long object
-will be returned instead.''',
-    __new__ = interp2app(descr__new__),
-    conjugate = interp2app(descr_conjugate),
-    bit_length = interp2app(descr_bit_length),
-    numerator = typedef.GetSetProperty(descr_get_numerator),
-    denominator = typedef.GetSetProperty(descr_get_denominator),
-    real = typedef.GetSetProperty(descr_get_real),
-    imag = typedef.GetSetProperty(descr_get_imag),
-    __int__ = interpindirect2app(W_AbstractIntObject.int),
-)
-int_typedef.registermethods(globals())

File pypy/objspace/std/model.py

         # All the Python types that we want to provide in this StdObjSpace
         class result:
             from pypy.objspace.std.objecttype import object_typedef
-            from pypy.objspace.std.booltype   import bool_typedef
-            from pypy.objspace.std.inttype    import int_typedef
             from pypy.objspace.std.floattype  import float_typedef
             from pypy.objspace.std.complextype  import complex_typedef
             from pypy.objspace.std.basestringtype import basestring_typedef
         self.pythontypes.append(setobject.W_SetObject.typedef)
         self.pythontypes.append(setobject.W_FrozensetObject.typedef)
         self.pythontypes.append(iterobject.W_AbstractSeqIterObject.typedef)
+        self.pythontypes.append(intobject.W_IntObject.typedef)
+        self.pythontypes.append(boolobject.W_BoolObject.typedef)
 
         # the set of implementation types
         self.typeorder = {
         # XXX build these lists a bit more automatically later
 
         self.typeorder[boolobject.W_BoolObject] += [
-            (intobject.W_IntObject,     boolobject.delegate_Bool2IntObject),
+#            (intobject.W_IntObject,     boolobject.delegate_Bool2IntObject),
             (floatobject.W_FloatObject, floatobject.delegate_Bool2Float),
             (longobject.W_LongObject,   longobject.delegate_Bool2Long),
             (complexobject.W_ComplexObject, complexobject.delegate_Bool2Complex),

File pypy/objspace/std/objspace.py

 from pypy.objspace.std.typeobject import W_TypeObject
 
 # types
-from pypy.objspace.std.inttype import wrapint
+from pypy.objspace.std.intobject import wrapint
 from pypy.objspace.std.stringtype import wrapstr
 from pypy.objspace.std.unicodetype import wrapunicode
 

File pypy/objspace/std/test/test_intobject.py

                 return n
             n += 1
 
-    def _unwrap_nonimpl(self, func, *args, **kwds):
-        """ make sure that the expected exception occours, and unwrap it """
-        try:
-            res = func(*args, **kwds)
-            raise Exception, "should have failed but returned '%s'!" %repr(res)
-        except FailedToImplement, arg:
-            return arg.get_w_type(self.space)
-
     def test_int_w(self):
         assert self.space.int_w(self.space.wrap(42)) == 42
 
     def test_repr(self):
         x = 1
         f1 = iobj.W_IntObject(x)
-        result = iobj.repr__Int(self.space, f1)
+        result = f1.descr_repr(self.space)
         assert self.space.unwrap(result) == repr(x)
 
     def test_str(self):
         x = 12345
         f1 = iobj.W_IntObject(x)
-        result = iobj.str__Int(self.space, f1)
+        result = f1.descr_str(self.space)
         assert self.space.unwrap(result) == str(x)
 
     def test_hash(self):
         x = 42
         f1 = iobj.W_IntObject(x)
-        result = iobj.hash__Int(self.space, f1)
+        result = f1.descr_hash(self.space)
         assert result.intval == hash(x)
 
     def test_compare(self):
                     wx = iobj.W_IntObject(x)
                     wy = iobj.W_IntObject(y)
                     res = getattr(operator, op)(x, y)
-                    method = getattr(iobj, '%s__Int_Int' % op)
-                    myres = method(self.space, wx, wy)
+                    method = getattr(wx, 'descr_%s' % op)
+                    myres = method(self.space, wy)
                     assert self.space.unwrap(myres) == res
                     
     def test_add(self):
+        space = self.space
         x = 1
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        result = iobj.add__Int_Int(self.space, f1, f2)
+        result = f1.descr_add(space, f2)
         assert result.intval == x+y
         x = sys.maxint
         y = 1
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.add__Int_Int, self.space, f1, f2))
+        v = f1.descr_add(space, f2)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(x + y))
 
     def test_sub(self):
+        space = self.space
         x = 1
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        result = iobj.sub__Int_Int(self.space, f1, f2)
+        result = f1.descr_sub(space, f2)
         assert result.intval == x-y
         x = sys.maxint
         y = -1
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.sub__Int_Int, self.space, f1, f2))
+        v = f1.descr_sub(space, f2)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(sys.maxint - -1))
 
     def test_mul(self):
+        space = self.space
         x = 2
         y = 3
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        result = iobj.mul__Int_Int(self.space, f1, f2)
+        result = f1.descr_mul(space, f2)
         assert result.intval == x*y
         x = -sys.maxint-1
         y = -1
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.mul__Int_Int, self.space, f1, f2))
+        v = f1.descr_mul(space, f2)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(x * y))
 
     def test_div(self):
+        space = self.space
         for i in range(10):
             res = i//3
             f1 = iobj.W_IntObject(i)
             f2 = iobj.W_IntObject(3)
-            result = iobj.div__Int_Int(self.space, f1, f2)
+            result = f1.descr_div(space, f2)
             assert result.intval == res
         x = -sys.maxint-1
         y = -1
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.div__Int_Int, self.space, f1, f2))
+        v = f1.descr_div(space, f2)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(x / y))
 
     def test_mod(self):
         x = 1
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.mod__Int_Int(self.space, f1, f2)
+        v = f1.descr_mod(self.space, f2)
         assert v.intval == x % y
         # not that mod cannot overflow
 
     def test_divmod(self):
+        space = self.space
         x = 1
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        ret = iobj.divmod__Int_Int(self.space, f1, f2)
-        v, w = self.space.unwrap(ret)
+        ret = f1.descr_divmod(space, f2)
+        v, w = space.unwrap(ret)
         assert (v, w) == divmod(x, y)
         x = -sys.maxint-1
         y = -1
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.divmod__Int_Int, self.space, f1, f2))
+        v = f1.descr_divmod(space, f2)
+        w_q, w_r = space.fixedview(v, 2)
+        assert space.isinstance_w(w_q, space.w_long)
+        expected = divmod(x, y)
+        assert space.bigint_w(w_q).eq(rbigint.fromlong(expected[0]))
+        # no overflow possible
+        assert space.unwrap(w_r) == expected[1]
 
     def test_pow_iii(self):
         x = 10
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
         f3 = iobj.W_IntObject(z)
-        v = iobj.pow__Int_Int_Int(self.space, f1, f2, f3)
+        v = f1.descr_pow(self.space, f2, f3)
         assert v.intval == pow(x, y, z)
         f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, -1, 42)]
         self.space.raises_w(self.space.w_TypeError,
-                            iobj.pow__Int_Int_Int,
-                            self.space, f1, f2, f3)
+                            f1.descr_pow, self.space, f2, f3)
         f1, f2, f3 = [iobj.W_IntObject(i) for i in (10, 5, 0)]
         self.space.raises_w(self.space.w_ValueError,
-                            iobj.pow__Int_Int_Int,
-                            self.space, f1, f2, f3)
+                            f1.descr_pow, self.space, f2, f3)
 
     def test_pow_iin(self):
+        space = self.space
         x = 10
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.pow__Int_Int_None(self.space, f1, f2, self.space.w_None)
+        v = f1.descr_pow(space, f2, space.w_None)
         assert v.intval == x ** y
         f1, f2 = [iobj.W_IntObject(i) for i in (10, 20)]
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.pow__Int_Int_None, self.space, f1, f2, self.space.w_None))
+        v = f1.descr_pow(space, f2, space.w_None)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(pow(10, 20)))
 
     def test_neg(self):
+        space = self.space
         x = 42
         f1 = iobj.W_IntObject(x)
-        v = iobj.neg__Int(self.space, f1)
+        v = f1.descr_neg(space)
         assert v.intval == -x
         x = -sys.maxint-1
         f1 = iobj.W_IntObject(x)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.neg__Int, self.space, f1))
+        v = f1.descr_neg(space)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(-x))
 
     def test_pos(self):
         x = 42
         f1 = iobj.W_IntObject(x)
-        v = iobj.pos__Int(self.space, f1)
+        v = f1.descr_pos(self.space)
         assert v.intval == +x
         x = -42
         f1 = iobj.W_IntObject(x)
-        v = iobj.pos__Int(self.space, f1)
+        v = f1.descr_pos(self.space)
         assert v.intval == +x
 
     def test_abs(self):
+        space = self.space
         x = 42
         f1 = iobj.W_IntObject(x)
-        v = iobj.abs__Int(self.space, f1)
+        v = f1.descr_abs(space)
         assert v.intval == abs(x)
         x = -42
         f1 = iobj.W_IntObject(x)
-        v = iobj.abs__Int(self.space, f1)
+        v = f1.descr_abs(space)
         assert v.intval == abs(x)
         x = -sys.maxint-1
         f1 = iobj.W_IntObject(x)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.abs__Int, self.space, f1))
+        v = f1.descr_abs(space)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(abs(x)))
 
     def test_invert(self):
         x = 42
         f1 = iobj.W_IntObject(x)
-        v = iobj.invert__Int(self.space, f1)
+        v = f1.descr_invert(self.space)
         assert v.intval == ~x
 
     def test_lshift(self):
+        space = self.space
         x = 12345678
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.lshift__Int_Int(self.space, f1, f2)
+        v = f1.descr_lshift(space, f2)
         assert v.intval == x << y
         y = self._longshiftresult(x)
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        assert self.space.w_OverflowError == (
-                          self._unwrap_nonimpl(iobj.lshift__Int_Int, self.space, f1, f2))
+        v = f1.descr_lshift(space, f2)
+        assert space.isinstance_w(v, space.w_long)
+        assert space.bigint_w(v).eq(rbigint.fromlong(x << y))
 
     def test_rshift(self):
         x = 12345678
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.rshift__Int_Int(self.space, f1, f2)
+        v = f1.descr_rshift(self.space, f2)
         assert v.intval == x >> y
 
     def test_and(self):
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.and__Int_Int(self.space, f1, f2)
+        v = f1.descr_and(self.space, f2)
         assert v.intval == x & y
 
     def test_xor(self):
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.xor__Int_Int(self.space, f1, f2)
+        v = f1.descr_xor(self.space, f2)
         assert v.intval == x ^ y
 
     def test_or(self):
         y = 2
         f1 = iobj.W_IntObject(x)
         f2 = iobj.W_IntObject(y)
-        v = iobj.or__Int_Int(self.space, f1, f2)
+        v = f1.descr_or(self.space, f2)
         assert v.intval == x | y
 
     def test_int(self):
     def test_oct(self):
         x = 012345
         f1 = iobj.W_IntObject(x)
-        result = iobj.oct__Int(self.space, f1)
+        result = f1.descr_oct(self.space)
         assert self.space.unwrap(result) == oct(x)
 
     def test_hex(self):
         x = 0x12345
         f1 = iobj.W_IntObject(x)
-        result = iobj.hex__Int(self.space, f1)
+        result = f1.descr_hex(self.space)
         assert self.space.unwrap(result) == hex(x)
 
 class AppTestInt:
         b = A(5).real
         assert type(b) is int
 
+    def test_coerce(self):
+        assert 3 .__coerce__(4) == (3, 4)
+        assert 3 .__coerce__(4L) == NotImplemented
+
 
 class AppTestIntOptimizedAdd(AppTestInt):
     spaceconfig = {"objspace.std.optimized_int_add": True}

File pypy/objspace/std/test/test_longobject.py

 
         assert int(long(3)) == long(3)
         assert int(A(13)) == 42
+
+    def test_coerce(self):
+        assert 3L.__coerce__(4L) == (3L, 4L)
+        assert 3L.__coerce__(4) == (3, 4)
+        assert 3L.__coerce__(object()) == NotImplemented