Author: Philip Jenvey <[email protected]>
Branch: remove-intlong-smm
Changeset: r67334:9ea8988dbe08
Date: 2013-10-11 16:28 -0700
http://bitbucket.org/pypy/pypy/changeset/9ea8988dbe08/
Log: most of long's SMM removal
diff --git a/pypy/objspace/std/intobject.py b/pypy/objspace/std/intobject.py
--- a/pypy/objspace/std/intobject.py
+++ b/pypy/objspace/std/intobject.py
@@ -8,8 +8,8 @@
from rpython.rlib import jit
from rpython.rlib.rarithmetic import (
LONG_BIT, is_valid_int, ovfcheck, string_to_int, r_uint)
+from rpython.rlib.objectmodel import instantiate
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
@@ -186,7 +186,7 @@
return space.newtuple([w(z), w(m)])
@unwrap_spec(w_modulus=WrappedDefault(None))
- def descr_pow(self, space, w_exponent, w_modulus):
+ def descr_pow(self, space, w_exponent, w_modulus=None):
if not space.isinstance_w(w_exponent, space.w_int):
return space.w_NotImplemented
if space.is_none(w_modulus):
@@ -217,10 +217,11 @@
return space.pow(w_long1, w_exponent, w_modulus)
@unwrap_spec(w_modulus=WrappedDefault(None))
- def descr_rpow(self, space, w_base, w_modulus):
+ def descr_rpow(self, space, w_base, w_modulus=None):
if not space.isinstance_w(w_base, space.w_int):
return space.w_NotImplemented
- # XXX: this seems like trouble?
+ # XXX: this seems like trouble? very likely trouble with int
+ # subclasses implementing __pow__
return space.pow(w_base, self, w_modulus)
def descr_neg(self, space):
@@ -658,6 +659,7 @@
__gt__ = interpindirect2app(W_AbstractIntObject.descr_gt),
__ge__ = interpindirect2app(W_AbstractIntObject.descr_ge),
+ # XXX: rtruediv
__floordiv__ = interpindirect2app(W_AbstractIntObject.descr_floordiv),
__div__ = interpindirect2app(W_AbstractIntObject.descr_div),
__truediv__ = interpindirect2app(W_AbstractIntObject.descr_truediv),
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
@@ -3,23 +3,49 @@
import sys
from rpython.rlib.rbigint import rbigint
+from rpython.rlib.rstring import ParseStringError
+from rpython.tool.sourcetools import func_with_new_name
-from pypy.interpreter.error import OperationError
+from pypy.interpreter import typedef
+from pypy.interpreter.error import OperationError, operationerrfmt
+from pypy.interpreter.gateway import (
+ WrappedDefault, interp2app, interpindirect2app, unwrap_spec)
from pypy.objspace.std import model, newformat
from pypy.objspace.std.intobject import W_IntObject
-from pypy.objspace.std.longtype import W_AbstractLongObject, long_typedef
-from pypy.objspace.std.model import W_Object, registerimplementation
-from pypy.objspace.std.multimethod import FailedToImplementArgs
+from pypy.objspace.std.model import W_Object
from pypy.objspace.std.noneobject import W_NoneObject
-from pypy.objspace.std.register_all import register_all
+from pypy.objspace.std.stdtypedef import StdTypeDef
+
+
+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
class W_LongObject(W_AbstractLongObject):
"""This is a wrapper of rbigint."""
_immutable_fields_ = ['num']
- typedef = long_typedef
-
def __init__(self, l):
self.num = l # instance of rbigint
@@ -34,8 +60,8 @@
try:
return self.num.tofloat()
except OverflowError:
- raise OperationError(space.w_OverflowError,
- space.wrap("long int too large to convert to float"))
+ raise operationerrfmt(space.w_OverflowError,
+ "long int too large to convert to float")
def toint(self):
return self.num.toint()
@@ -83,12 +109,278 @@
try:
return space.newint(self.num.toint())
except OverflowError:
- return long__Long(space, self)
+ return self.descr_long(space)
def __repr__(self):
return '<W_LongObject(%d)>' % self.num.tolong()
-registerimplementation(W_LongObject)
+ def descr_conjugate(self, space):
+ return space.long(self)
+
+ def descr_get_numerator(self, space):
+ return space.long(self)
+
+ def descr_get_denominator(self, space):
+ return space.newlong(1)
+
+ def descr_get_real(self, space):
+ return space.long(self)
+
+ def descr_get_imag(self, space):
+ return space.newlong(0)
+
+ def descr_get_bit_length(self, space):
+ bigint = space.bigint_w(self)
+ try:
+ return space.wrap(bigint.bit_length())
+ except OverflowError:
+ raise OperationError(space.w_OverflowError,
+ space.wrap("too many digits in integer"))
+
+ def descr_long(self, space):
+ # long__Long is supposed to do nothing, unless it has a derived
+ # long object, where it should return an exact one.
+ if space.is_w(space.type(self), space.w_long):
+ return self
+ l = self.num
+ return W_LongObject(l)
+ descr_index = func_with_new_name(descr_long, 'descr_index')
+ descr_trunc = func_with_new_name(descr_long, 'descr_trunc')
+ descr_pos = func_with_new_name(descr_long, 'descr_pos')
+
+ def descr_float(self, space):
+ return space.newfloat(self.tofloat(space))
+
+ def descr_repr(self, space):
+ return space.wrap(self.num.repr())
+
+ def descr_str(self, space):
+ return space.wrap(self.num.str())
+
+ def descr_format(self, space, w_format_spec):
+ return newformat.run_formatter(space, w_format_spec,
+ "format_int_or_long", self,
+ newformat.LONG_KIND)
+
+ def descr_hash(self, space):
+ return space.wrap(self.num.hash())
+
+ def descr_coerce(self, space, w_other):
+ # XXX: consider stian's branch where he optimizes long + ints
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+ return space.newtuple([self, w_other])
+
+ def _make_descr_cmp(opname):
+ op = getattr(rbigint, opname)
+ def descr_impl(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+ return space.newbool(op(self.num, w_other.num))
+ return func_with_new_name(descr_impl, "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 _make_descr_binop(opname):
+ from rpython.tool.sourcetools import func_renamer
+ methname = opname + '_' if opname in ('and', 'or') else opname
+ op = getattr(rbigint, methname)
+
+ @func_renamer('descr_' + opname)
+ def descr_binop(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+ return W_LongObject(op(self.num, w_other.num))
+
+ @func_renamer('descr_r' + opname)
+ def descr_rbinop(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+ return W_LongObject(op(w_other.num, self.num))
+
+ 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')
+ descr_and, descr_rand = _make_descr_binop('and')
+ descr_or, descr_ror = _make_descr_binop('or')
+ descr_xor, descr_rxor = _make_descr_binop('xor')
+
+ def _make_descr_unaryop(opname):
+ from rpython.tool.sourcetools import func_renamer
+ op = getattr(rbigint, opname)
+ @func_renamer('descr_' + opname)
+ def descr_unaryop(self, space):
+ return W_LongObject(op(self.num))
+ return descr_unaryop
+
+ descr_neg = _make_descr_unaryop('neg')
+ descr_abs = _make_descr_unaryop('abs')
+ descr_invert = _make_descr_unaryop('invert')
+
+ def descr_oct(self, space):
+ return space.wrap(self.num.oct())
+
+ def descr_hex(self, space):
+ return space.wrap(self.num.hex())
+
+ def descr_nonzero(self, space):
+ return space.newbool(self.num.tobool())
+
+ def descr_lshift(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ # XXX need to replicate some of the logic, to get the errors right
+ if w_other.num.sign < 0:
+ raise operationerrfmt(space.w_ValueError, "negative shift count")
+ try:
+ shift = w_other.num.toint()
+ except OverflowError: # b too big
+ raise operationerrfmt(space.w_OverflowError,
+ "shift count too large")
+ return W_LongObject(self.num.lshift(shift))
+
+ def descr_rshift(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ # XXX need to replicate some of the logic, to get the errors right
+ if w_other.num.sign < 0:
+ raise operationerrfmt(space.w_ValueError, "negative shift count")
+ try:
+ shift = w_other.num.toint()
+ except OverflowError: # b too big # XXX maybe just return 0L instead?
+ raise operationerrfmt(space.w_OverflowError,
+ "shift count too large")
+ return newlong(space, self.num.rshift(shift))
+
+ # XXX: need rtruediv etc
+ def descr_truediv(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ try:
+ f = self.num.truediv(w_other.num)
+ except ZeroDivisionError:
+ raise operationerrfmt(space.w_ZeroDivisionError,
+ "long division or modulo by zero")
+ except OverflowError:
+ raise operationerrfmt(space.w_OverflowError,
+ "long/long too large for a float")
+ return space.newfloat(f)
+
+ def descr_floordiv(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ try:
+ z = self.num.floordiv(w_other.num)
+ except ZeroDivisionError:
+ raise operationerrfmt(space.w_ZeroDivisionError,
+ "long division or modulo by zero")
+ return newlong(space, z)
+
+ def descr_div(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ return self.floordiv(space, w_other)
+
+ def descr_mod(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ try:
+ z = self.num.mod(w_other.num)
+ except ZeroDivisionError:
+ raise operationerrfmt(space.w_ZeroDivisionError,
+ "long division or modulo by zero")
+ return newlong(space, z)
+
+ def descr_divmod(self, space, w_other):
+ if space.isinstance_w(w_other, space.w_int):
+ w_other = _delegate_Int2Long(space, w_other)
+ elif not space.isinstance_w(w_other, space.w_long):
+ return space.w_NotImplemented
+
+ try:
+ div, mod = self.num.divmod(w_other.num)
+ except ZeroDivisionError:
+ raise operationerrfmt(space.w_ZeroDivisionError,
+ "long division or modulo by zero")
+ return space.newtuple([newlong(space, div), newlong(space, mod)])
+
+ @unwrap_spec(w_modulus=WrappedDefault(None))
+ def descr_pow(self, space, w_exponent, w_modulus=None):
+ if space.isinstance_w(w_exponent, space.w_int):
+ w_exponent = _delegate_Int2Long(space, w_exponent)
+ elif not space.isinstance_w(w_exponent, space.w_long):
+ return space.w_NotImplemented
+ if space.isinstance_w(w_modulus, space.w_int):
+ w_modulus = _delegate_Int2Long(space, w_modulus)
+ elif space.is_none(w_modulus):
+ # XXX need to replicate some of the logic, to get the errors right
+ if w_exponent.num.sign < 0:
+ return space.pow(self.descr_float(space), w_exponent,
w_modulus)
+ return W_LongObject(self.num.pow(w_exponent.num, None))
+ elif not space.isinstance_w(w_modulus, space.w_long):
+ return space.w_NotImplemented
+
+ # XXX need to replicate some of the logic, to get the errors right
+ if w_exponent.num.sign < 0:
+ raise OperationError(
+ space.w_TypeError,
+ space.wrap(
+ "pow() 2nd argument "
+ "cannot be negative when 3rd argument specified"))
+ try:
+ return W_LongObject(self.num.pow(w_exponent.num, w_modulus.num))
+ except ValueError:
+ raise OperationError(space.w_ValueError,
+ space.wrap("pow 3rd argument cannot be 0"))
+
+ @unwrap_spec(w_modulus=WrappedDefault(None))
+ def descr_rpow(self, space, w_exponent, w_modulus=None):
+ if space.isinstance_w(w_exponent, space.w_int):
+ w_exponent = _delegate_Int2Long(space, w_exponent)
+ elif not space.isinstance_w(w_exponent, space.w_long):
+ return space.w_NotImplemented
+ ### XXX: these may needs all the checks above has. annoying
+ #if not space.isinstance_w(w_exponent, space.w_long):
+ # return space.w_NotImplemented
+ # XXX:
+ return space.pow(w_exponent, self, w_modulus)
+
+ def descr_getnewargs(self, space):
+ return space.newtuple([W_LongObject(self.num)])
+
def newlong(space, bigint):
"""Turn the bigint into a W_LongObject. If withsmalllong is enabled,
@@ -106,220 +398,10 @@
return W_LongObject(bigint)
-# bool-to-long
-def delegate_Bool2Long(space, w_bool):
- return W_LongObject(rbigint.frombool(space.is_true(w_bool)))
+def _delegate_Int2Long(space, w_intobj):
+ """int-to-long delegation"""
+ return W_LongObject.fromint(space, w_intobj.int_w(space))
-# int-to-long delegation
-def delegate_Int2Long(space, w_intobj):
- return W_LongObject.fromint(space, w_intobj.intval)
-
-
-# long__Long is supposed to do nothing, unless it has
-# a derived long object, where it should return
-# an exact one.
-def long__Long(space, w_long1):
- if space.is_w(space.type(w_long1), space.w_long):
- return w_long1
- l = w_long1.num
- return W_LongObject(l)
-trunc__Long = long__Long
-
-def long__Int(space, w_intobj):
- return space.newlong(w_intobj.intval)
-
-def index__Long(space, w_value):
- return long__Long(space, w_value)
-
-def float__Long(space, w_longobj):
- return space.newfloat(w_longobj.tofloat(space))
-
-def repr__Long(space, w_long):
- return space.wrap(w_long.num.repr())
-
-def str__Long(space, w_long):
- return space.wrap(w_long.num.str())
-
-def format__Long_ANY(space, w_long, w_format_spec):
- return newformat.run_formatter(space, w_format_spec, "format_int_or_long",
- w_long, newformat.LONG_KIND)
-
-
-def lt__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.lt(w_long2.num))
-def le__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.le(w_long2.num))
-def eq__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.eq(w_long2.num))
-def ne__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.ne(w_long2.num))
-def gt__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.gt(w_long2.num))
-def ge__Long_Long(space, w_long1, w_long2):
- return space.newbool(w_long1.num.ge(w_long2.num))
-
-def lt__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.lt(rbigint.fromint(w_int2.intval)))
-def le__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.le(rbigint.fromint(w_int2.intval)))
-def eq__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.eq(rbigint.fromint(w_int2.intval)))
-def ne__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.ne(rbigint.fromint(w_int2.intval)))
-def gt__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.gt(rbigint.fromint(w_int2.intval)))
-def ge__Long_Int(space, w_long1, w_int2):
- return space.newbool(w_long1.num.ge(rbigint.fromint(w_int2.intval)))
-
-def lt__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).lt(w_long2.num))
-def le__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).le(w_long2.num))
-def eq__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).eq(w_long2.num))
-def ne__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).ne(w_long2.num))
-def gt__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).gt(w_long2.num))
-def ge__Int_Long(space, w_int1, w_long2):
- return space.newbool(rbigint.fromint(w_int1.intval).ge(w_long2.num))
-
-
-def hash__Long(space, w_value):
- return space.wrap(w_value.num.hash())
-
-# coerce
-def coerce__Long_Long(space, w_long1, w_long2):
- return space.newtuple([w_long1, w_long2])
-
-
-def add__Long_Long(space, w_long1, w_long2):
- return W_LongObject(w_long1.num.add(w_long2.num))
-
-def sub__Long_Long(space, w_long1, w_long2):
- return W_LongObject(w_long1.num.sub(w_long2.num))
-
-def mul__Long_Long(space, w_long1, w_long2):
- return W_LongObject(w_long1.num.mul(w_long2.num))
-
-def truediv__Long_Long(space, w_long1, w_long2):
- try:
- f = w_long1.num.truediv(w_long2.num)
- except ZeroDivisionError:
- raise OperationError(space.w_ZeroDivisionError,
- space.wrap("long division or modulo by zero"))
- except OverflowError:
- raise OperationError(space.w_OverflowError,
- space.wrap("long/long too large for a float"))
- return space.newfloat(f)
-
-def floordiv__Long_Long(space, w_long1, w_long2):
- try:
- z = w_long1.num.floordiv(w_long2.num)
- except ZeroDivisionError:
- raise OperationError(space.w_ZeroDivisionError,
- space.wrap("long division or modulo by zero"))
- return newlong(space, z)
-
-def div__Long_Long(space, w_long1, w_long2):
- return floordiv__Long_Long(space, w_long1, w_long2)
-
-def mod__Long_Long(space, w_long1, w_long2):
- try:
- z = w_long1.num.mod(w_long2.num)
- except ZeroDivisionError:
- raise OperationError(space.w_ZeroDivisionError,
- space.wrap("long division or modulo by zero"))
- return newlong(space, z)
-
-def divmod__Long_Long(space, w_long1, w_long2):
- try:
- div, mod = w_long1.num.divmod(w_long2.num)
- except ZeroDivisionError:
- raise OperationError(space.w_ZeroDivisionError,
- space.wrap("long division or modulo by zero"))
- return space.newtuple([newlong(space, div), newlong(space, mod)])
-
-def pow__Long_Long_Long(space, w_long1, w_long2, w_long3):
- # XXX need to replicate some of the logic, to get the errors right
- if w_long2.num.sign < 0:
- raise OperationError(
- space.w_TypeError,
- space.wrap(
- "pow() 2nd argument "
- "cannot be negative when 3rd argument specified"))
- try:
- return W_LongObject(w_long1.num.pow(w_long2.num, w_long3.num))
- except ValueError:
- raise OperationError(space.w_ValueError,
- space.wrap("pow 3rd argument cannot be 0"))
-
-def pow__Long_Long_None(space, w_long1, w_long2, w_long3):
- # XXX need to replicate some of the logic, to get the errors right
- if w_long2.num.sign < 0:
- raise FailedToImplementArgs(
- space.w_ValueError,
- space.wrap("long pow() too negative"))
- return W_LongObject(w_long1.num.pow(w_long2.num, None))
-
-def neg__Long(space, w_long1):
- return W_LongObject(w_long1.num.neg())
-
-def pos__Long(space, w_long):
- return long__Long(space, w_long)
-
-def abs__Long(space, w_long):
- return W_LongObject(w_long.num.abs())
-
-def nonzero__Long(space, w_long):
- return space.newbool(w_long.num.tobool())
-
-def invert__Long(space, w_long):
- return W_LongObject(w_long.num.invert())
-
-def lshift__Long_Long(space, w_long1, w_long2):
- # XXX need to replicate some of the logic, to get the errors right
- if w_long2.num.sign < 0:
- raise OperationError(space.w_ValueError,
- space.wrap("negative shift count"))
- try:
- shift = w_long2.num.toint()
- except OverflowError: # b too big
- raise OperationError(space.w_OverflowError,
- space.wrap("shift count too large"))
- return W_LongObject(w_long1.num.lshift(shift))
-
-def rshift__Long_Long(space, w_long1, w_long2):
- # XXX need to replicate some of the logic, to get the errors right
- if w_long2.num.sign < 0:
- raise OperationError(space.w_ValueError,
- space.wrap("negative shift count"))
- try:
- shift = w_long2.num.toint()
- except OverflowError: # b too big # XXX maybe just return 0L instead?
- raise OperationError(space.w_OverflowError,
- space.wrap("shift count too large"))
- return newlong(space, w_long1.num.rshift(shift))
-
-def and__Long_Long(space, w_long1, w_long2):
- return newlong(space, w_long1.num.and_(w_long2.num))
-
-def xor__Long_Long(space, w_long1, w_long2):
- return W_LongObject(w_long1.num.xor(w_long2.num))
-
-def or__Long_Long(space, w_long1, w_long2):
- return W_LongObject(w_long1.num.or_(w_long2.num))
-
-def oct__Long(space, w_long1):
- return space.wrap(w_long1.num.oct())
-
-def hex__Long(space, w_long1):
- return space.wrap(w_long1.num.hex())
-
-def getnewargs__Long(space, w_long1):
- return space.newtuple([W_LongObject(w_long1.num)])
-
-register_all(vars())
# register implementations of ops that recover int op overflows
def recover_with_smalllong(space):
@@ -327,6 +409,7 @@
return (space.config.objspace.std.withsmalllong and
sys.maxint == 2147483647)
+# XXX:
# binary ops
for opname in ['add', 'sub', 'mul', 'div', 'floordiv', 'truediv', 'mod',
'divmod', 'lshift']:
@@ -335,8 +418,8 @@
if recover_with_smalllong(space) and %(opname)r != 'truediv':
from pypy.objspace.std.smalllongobject import %(opname)s_ovr
return %(opname)s_ovr(space, w_int1, w_int2)
- w_long1 = delegate_Int2Long(space, w_int1)
- w_long2 = delegate_Int2Long(space, w_int2)
+ w_long1 = _delegate_Int2Long(space, w_int1)
+ w_long2 = _delegate_Int2Long(space, w_int2)
return %(opname)s__Long_Long(space, w_long1, w_long2)
""" % {'opname': opname}, '', 'exec')
@@ -350,7 +433,7 @@
if recover_with_smalllong(space):
from pypy.objspace.std.smalllongobject import %(opname)s_ovr
return %(opname)s_ovr(space, w_int1)
- w_long1 = delegate_Int2Long(space, w_int1)
+ w_long1 = _delegate_Int2Long(space, w_int1)
return %(opname)s__Long(space, w_long1)
""" % {'opname': opname}
@@ -362,13 +445,13 @@
if recover_with_smalllong(space):
from pypy.objspace.std.smalllongobject import pow_ovr
return pow_ovr(space, w_int1, w_int2)
- w_long1 = delegate_Int2Long(space, w_int1)
- w_long2 = delegate_Int2Long(space, w_int2)
+ w_long1 = _delegate_Int2Long(space, w_int1)
+ w_long2 = _delegate_Int2Long(space, w_int2)
return pow__Long_Long_None(space, w_long1, w_long2, w_none3)
def pow_ovr__Int_Int_Long(space, w_int1, w_int2, w_long3):
- w_long1 = delegate_Int2Long(space, w_int1)
- w_long2 = delegate_Int2Long(space, w_int2)
+ w_long1 = _delegate_Int2Long(space, w_int1)
+ w_long2 = _delegate_Int2Long(space, w_int2)
return pow__Long_Long_Long(space, w_long1, w_long2, w_long3)
model.MM.pow.register(pow_ovr__Int_Int_None, W_IntObject, W_IntObject,
@@ -377,3 +460,155 @@
W_LongObject, order=1)
+@unwrap_spec(w_x=WrappedDefault(0))
+def descr__new__(space, w_longtype, w_x, w_base=None):
+ if space.config.objspace.std.withsmalllong:
+ from pypy.objspace.std.smalllongobject import W_SmallLongObject
+ else:
+ W_SmallLongObject = None
+
+ w_value = w_x # 'x' is the keyword argument name in CPython
+ if w_base is None:
+ # check for easy cases
+ if (W_SmallLongObject and type(w_value) is W_SmallLongObject
+ and space.is_w(w_longtype, space.w_long)):
+ return w_value
+ elif type(w_value) is W_LongObject:
+ return newbigint(space, w_longtype, w_value.num)
+ elif space.isinstance_w(w_value, space.w_str):
+ return string_to_w_long(space, w_longtype, space.str_w(w_value))
+ elif space.isinstance_w(w_value, space.w_unicode):
+ from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
+ return string_to_w_long(space, w_longtype,
+ unicode_to_decimal_w(space, w_value))
+ else:
+ # otherwise, use the __long__() or the __trunc__ methods
+ w_obj = w_value
+ if (space.lookup(w_obj, '__long__') is not None or
+ space.lookup(w_obj, '__int__') is not None):
+ w_obj = space.long(w_obj)
+ else:
+ w_obj = space.trunc(w_obj)
+ # :-( blame CPython 2.7
+ if space.lookup(w_obj, '__long__') is not None:
+ w_obj = space.long(w_obj)
+ else:
+ w_obj = space.int(w_obj)
+ bigint = space.bigint_w(w_obj)
+ return newbigint(space, w_longtype, bigint)
+ 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:
+ msg = "long() can't convert non-string with explicit base"
+ raise operationerrfmt(space.w_TypeError, msg)
+ return string_to_w_long(space, w_longtype, s, base)
+
+
+def string_to_w_long(space, w_longtype, s, base=10):
+ try:
+ bigint = rbigint.fromstr(s, base)
+ except ParseStringError as e:
+ raise operationerrfmt(space.w_ValueError, e.msg)
+ return newbigint(space, w_longtype, bigint)
+string_to_w_long._dont_inline_ = True
+
+
+def newbigint(space, w_longtype, bigint):
+ """Turn the bigint into a W_LongObject. If withsmalllong is enabled,
+ check if the bigint would fit in a smalllong, and return a
+ W_SmallLongObject instead if it does. Similar to newlong() in
+ longobject.py, but takes an explicit w_longtype argument.
+ """
+ if (space.config.objspace.std.withsmalllong
+ and space.is_w(w_longtype, space.w_long)):
+ try:
+ z = bigint.tolonglong()
+ except OverflowError:
+ pass
+ else:
+ from pypy.objspace.std.smalllongobject import W_SmallLongObject
+ return W_SmallLongObject(z)
+ w_obj = space.allocate_instance(W_LongObject, w_longtype)
+ W_LongObject.__init__(w_obj, bigint)
+ return w_obj
+
+
+W_LongObject.typedef = StdTypeDef("long",
+ __doc__ = """long(x[, base]) -> integer
+
+Convert a string or number to a long 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.""",
+ __new__ = interp2app(descr__new__),
+ conjugate = interp2app(W_LongObject.descr_conjugate),
+ numerator = typedef.GetSetProperty(W_LongObject.descr_get_numerator),
+ denominator = typedef.GetSetProperty(W_LongObject.descr_get_denominator),
+ real = typedef.GetSetProperty(W_LongObject.descr_get_real),
+ imag = typedef.GetSetProperty(W_LongObject.descr_get_imag),
+ bit_length = interp2app(W_LongObject.descr_get_bit_length),
+
+ # XXX: likely need indirect everything for SmallLong
+ __int__ = interpindirect2app(W_AbstractLongObject.int),
+ __long__ = interp2app(W_LongObject.descr_long),
+ __trunc__ = interp2app(W_LongObject.descr_trunc),
+ __index__ = interp2app(W_LongObject.descr_index),
+ __float__ = interp2app(W_LongObject.descr_float),
+ __repr__ = interp2app(W_LongObject.descr_repr),
+ __str__ = interp2app(W_LongObject.descr_str),
+ __format__ = interp2app(W_LongObject.descr_format),
+
+ __hash__ = interp2app(W_LongObject.descr_hash),
+ __coerce__ = interp2app(W_LongObject.descr_coerce),
+
+ __lt__ = interp2app(W_LongObject.descr_lt),
+ __le__ = interp2app(W_LongObject.descr_le),
+ __eq__ = interp2app(W_LongObject.descr_eq),
+ __ne__ = interp2app(W_LongObject.descr_ne),
+ __gt__ = interp2app(W_LongObject.descr_gt),
+ __ge__ = interp2app(W_LongObject.descr_ge),
+
+ __add__ = interp2app(W_LongObject.descr_add),
+ __radd__ = interp2app(W_LongObject.descr_radd),
+ __sub__ = interp2app(W_LongObject.descr_sub),
+ __rsub__ = interp2app(W_LongObject.descr_rsub),
+ __mul__ = interp2app(W_LongObject.descr_mul),
+ __rmul__ = interp2app(W_LongObject.descr_rmul),
+
+ __and__ = interp2app(W_LongObject.descr_and),
+ __rand__ = interp2app(W_LongObject.descr_rand),
+ __or__ = interp2app(W_LongObject.descr_or),
+ __ror__ = interp2app(W_LongObject.descr_ror),
+ __xor__ = interp2app(W_LongObject.descr_xor),
+ __rxor__ = interp2app(W_LongObject.descr_rxor),
+
+ __neg__ = interp2app(W_LongObject.descr_neg),
+ __pos__ = interp2app(W_LongObject.descr_pos),
+ __abs__ = interp2app(W_LongObject.descr_abs),
+ __nonzero__ = interp2app(W_LongObject.descr_nonzero),
+ __invert__ = interp2app(W_LongObject.descr_invert),
+ __oct__ = interp2app(W_LongObject.descr_oct),
+ __hex__ = interp2app(W_LongObject.descr_hex),
+
+ __lshift__ = interp2app(W_LongObject.descr_lshift),
+ __rshift__ = interp2app(W_LongObject.descr_rshift),
+
+ __truediv__ = interp2app(W_LongObject.descr_truediv),
+ __floordiv__ = interp2app(W_LongObject.descr_floordiv),
+ __div__ = interp2app(W_LongObject.descr_div),
+ __mod__ = interp2app(W_LongObject.descr_mod),
+ __divmod__ = interp2app(W_LongObject.descr_divmod),
+
+ __pow__ = interp2app(W_LongObject.descr_pow),
+ __rpow__ = interp2app(W_LongObject.descr_rpow),
+
+ __getnewargs__ = interp2app(W_LongObject.descr_getnewargs),
+)
diff --git a/pypy/objspace/std/longtype.py b/pypy/objspace/std/longtype.py
deleted file mode 100644
--- a/pypy/objspace/std/longtype.py
+++ /dev/null
@@ -1,159 +0,0 @@
-from pypy.interpreter.error import OperationError
-from pypy.interpreter import typedef
-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 rpython.rlib.rstring import 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
- if space.config.objspace.std.withsmalllong:
- from pypy.objspace.std.smalllongobject import W_SmallLongObject
- else:
- W_SmallLongObject = None
-
- w_value = w_x # 'x' is the keyword argument name in CPython
- if w_base is None:
- # check for easy cases
- if (W_SmallLongObject and type(w_value) is W_SmallLongObject
- and space.is_w(w_longtype, space.w_long)):
- return w_value
- elif type(w_value) is W_LongObject:
- return newbigint(space, w_longtype, w_value.num)
- elif space.isinstance_w(w_value, space.w_str):
- return string_to_w_long(space, w_longtype, space.str_w(w_value))
- elif space.isinstance_w(w_value, space.w_unicode):
- from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
- return string_to_w_long(space, w_longtype,
- unicode_to_decimal_w(space, w_value))
- else:
- # otherwise, use the __long__() or the __trunc__ methods
- w_obj = w_value
- if (space.lookup(w_obj, '__long__') is not None or
- space.lookup(w_obj, '__int__') is not None):
- w_obj = space.long(w_obj)
- else:
- w_obj = space.trunc(w_obj)
- # :-( blame CPython 2.7
- if space.lookup(w_obj, '__long__') is not None:
- w_obj = space.long(w_obj)
- else:
- w_obj = space.int(w_obj)
- bigint = space.bigint_w(w_obj)
- return newbigint(space, w_longtype, bigint)
- 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:
- raise OperationError(space.w_TypeError,
- space.wrap("long() can't convert
non-string "
- "with explicit base"))
- return string_to_w_long(space, w_longtype, s, base)
-
-
-def string_to_w_long(space, w_longtype, s, base=10):
- try:
- bigint = rbigint.fromstr(s, base)
- except ParseStringError, e:
- raise OperationError(space.w_ValueError,
- space.wrap(e.msg))
- return newbigint(space, w_longtype, bigint)
-string_to_w_long._dont_inline_ = True
-
-def newbigint(space, w_longtype, bigint):
- """Turn the bigint into a W_LongObject. If withsmalllong is enabled,
- check if the bigint would fit in a smalllong, and return a
- W_SmallLongObject instead if it does. Similar to newlong() in
- longobject.py, but takes an explicit w_longtype argument.
- """
- if (space.config.objspace.std.withsmalllong
- and space.is_w(w_longtype, space.w_long)):
- try:
- z = bigint.tolonglong()
- except OverflowError:
- pass
- else:
- from pypy.objspace.std.smalllongobject import W_SmallLongObject
- return W_SmallLongObject(z)
- from pypy.objspace.std.longobject import W_LongObject
- w_obj = space.allocate_instance(W_LongObject, w_longtype)
- W_LongObject.__init__(w_obj, bigint)
- return w_obj
-
-def descr_get_numerator(space, w_obj):
- return space.long(w_obj)
-
-def descr_get_denominator(space, w_obj):
- return space.newlong(1)
-
-def descr_get_real(space, w_obj):
- return space.long(w_obj)
-
-def descr_get_imag(space, w_obj):
- return space.newlong(0)
-
-def bit_length(space, w_obj):
- bigint = space.bigint_w(w_obj)
- try:
- return space.wrap(bigint.bit_length())
- except OverflowError:
- raise OperationError(space.w_OverflowError,
- space.wrap("too many digits in integer"))
-
-# ____________________________________________________________
-
-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
-
-Convert a string or number to a long 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.''',
- __new__ = interp2app(descr__new__),
- conjugate = interp2app(descr_conjugate),
- numerator = typedef.GetSetProperty(descr_get_numerator),
- denominator = typedef.GetSetProperty(descr_get_denominator),
- 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/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -39,7 +39,7 @@
from pypy.objspace.std.bytearraytype import bytearray_typedef
from pypy.objspace.std.typeobject import type_typedef
from pypy.objspace.std.slicetype import slice_typedef
- from pypy.objspace.std.longtype import long_typedef
+ #from pypy.objspace.std.longtype import long_typedef
from pypy.objspace.std.unicodetype import unicode_typedef
from pypy.objspace.std.nonetype import none_typedef
self.pythontypes = [value for key, value in result.__dict__.items()
@@ -81,6 +81,7 @@
self.pythontypes.append(iterobject.W_AbstractSeqIterObject.typedef)
self.pythontypes.append(intobject.W_IntObject.typedef)
self.pythontypes.append(boolobject.W_BoolObject.typedef)
+ self.pythontypes.append(longobject.W_LongObject.typedef)
# the set of implementation types
self.typeorder = {
@@ -107,6 +108,7 @@
if option.startswith("with") and option in option_to_typename:
for classname in option_to_typename[option]:
modname = classname[:classname.index('.')]
+ if modname == 'smalllongobject': continue # XXX:
classname = classname[classname.index('.')+1:]
d = {}
exec "from pypy.objspace.std.%s import %s" % (
@@ -136,7 +138,7 @@
self.typeorder[boolobject.W_BoolObject] += [
# (intobject.W_IntObject, boolobject.delegate_Bool2IntObject),
(floatobject.W_FloatObject, floatobject.delegate_Bool2Float),
- (longobject.W_LongObject, longobject.delegate_Bool2Long),
+# (longobject.W_LongObject, longobject.delegate_Bool2Long),
(complexobject.W_ComplexObject,
complexobject.delegate_Bool2Complex),
]
self.typeorder[intobject.W_IntObject] += [
@@ -144,7 +146,7 @@
(longobject.W_LongObject, longobject.delegate_Int2Long),
(complexobject.W_ComplexObject,
complexobject.delegate_Int2Complex),
]
- if config.objspace.std.withsmalllong:
+ if False and config.objspace.std.withsmalllong:
from pypy.objspace.std import smalllongobject
self.typeorder[boolobject.W_BoolObject] += [
(smalllongobject.W_SmallLongObject,
smalllongobject.delegate_Bool2SmallLong),
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit