Exemplo n.º 1
0
    def descr_int(self, space):  # TODO
        index = self.nbits - 1
        bigval = self.bigval
        wordpos = index / SHIFT
        if wordpos > bigval.numdigits(
        ):  # msb must be zero, number is positive
            return self.descr_uint(space)

        bitpos = index - wordpos * SHIFT
        word = bigval.digit(wordpos)
        msb = (word >> bitpos) & 1
        if not msb:
            return newlong(space, bigval)

        # calculate self.nbits's index
        bitpos += 1
        if bitpos == SHIFT:
            wordpos += 1
            bitpos = 0

        # manually assemble (1 << (index+1))
        shift = rbigint([NULLDIGIT] * wordpos + [_store_digit(1 << bitpos)], 1,
                        wordpos + 1)

        res = bigval.sub(shift)
        return newlong(space, res)
Exemplo n.º 2
0
def _ensure_baseint(space, w_intvalue):
    from pypy.objspace.std.longobject import (W_LongObject,
                                              W_AbstractLongObject, newlong)
    if isinstance(w_intvalue, W_IntObject):
        if type(w_intvalue) is not W_IntObject:
            w_intvalue = wrapint(space, w_intvalue.intval)
        return w_intvalue
    elif isinstance(w_intvalue, W_AbstractLongObject):
        if type(w_intvalue) is not W_LongObject:
            w_intvalue = newlong(space, w_intvalue.asbigint())
        return w_intvalue
    else:
        # shouldn't happen
        raise oefmt(space.w_RuntimeError, "internal error in int.__new__()")
Exemplo n.º 3
0
def unmarshal_Long(space, u, tc):
    from rpython.rlib.rbigint import rbigint
    lng = u.get_int()
    if lng < 0:
        negative = True
        lng = -lng
    else:
        negative = False
    digits = [u.get_short() for i in range(lng)]
    result = rbigint.from_list_n_bits(digits, 15)
    if lng and not result.tobool():
        raise_exception(space, 'bad marshal data')
    if negative:
        result = result.neg()
    w_long = newlong(space, result)
    return w_long
Exemplo n.º 4
0
def unmarshal_Long(space, u, tc):
    from pypy.rlib.rbigint import rbigint
    lng = u.get_int()
    if lng < 0:
        negative = True
        lng = -lng
    else:
        negative = False
    SHIFT = 15
    result = rbigint.fromint(0)
    for i in range(lng):
        shift = i * SHIFT
        result = result.or_(rbigint.fromint(u.get_short()).lshift(shift))
    if lng and not result.tobool():
        raise_exception(space, 'bad marshal data')
    if negative:
        result = result.neg()
    w_long = newlong(space, result)
    return w_long
Exemplo n.º 5
0
def unmarshal_Long(space, u, tc):
    from pypy.rlib.rbigint import rbigint
    lng = u.get_int()
    if lng < 0:
        negative = True
        lng = -lng
    else:
        negative = False
    SHIFT = 15
    result = rbigint.fromint(0)
    for i in range(lng):
        shift = i * SHIFT
        result = result.or_(rbigint.fromint(u.get_short()).lshift(shift))
    if lng and not result.tobool():
        raise_exception(space, 'bad marshal data')
    if negative:
        result = result.neg()
    w_long = newlong(space, result)
    return w_long
Exemplo n.º 6
0
    def descr_round(self, space, w_ndigits=None):
        """Rounding an Integral returns itself.
        Rounding with an ndigits argument also returns an integer.
        """
        # To round an integer m to the nearest 10**n (n positive), we
        # make use of the divmod_near operation, defined by:
        #
        # divmod_near(a, b) = (q, r)
        #
        # where q is the nearest integer to the quotient a / b (the
        # nearest even integer in the case of a tie) and r == a - q * b.
        # Hence q * b = a - r is the nearest multiple of b to a,
        # preferring even multiples in the case of a tie.
        #
        # So the nearest multiple of 10**n to m is:
        #
        # m - divmod_near(m, 10**n)[1]

        # XXX: since divmod_near is pure python we can probably remove
        # the longs used here. or this could at least likely be more
        # efficient for W_IntObject
        from pypy.objspace.std.longobject import newlong

        if w_ndigits is None:
            return self.int(space)

        ndigits = space.bigint_w(space.index(w_ndigits))
        # if ndigits >= 0 then no rounding is necessary; return self
        # unchanged
        if ndigits.ge(rbigint.fromint(0)):
            return self.int(space)

        # result = self - divmod_near(self, 10 ** -ndigits)[1]
        right = rbigint.fromint(10).pow(ndigits.neg())
        w_tuple = divmod_near(space, self, newlong(space, right))
        _, w_r = space.fixedview(w_tuple, 2)
        return space.sub(self, w_r)
Exemplo n.º 7
0
    def descr_round(self, space, w_ndigits=None):
        """Rounding an Integral returns itself.
        Rounding with an ndigits argument also returns an integer.
        """
        # To round an integer m to the nearest 10**n (n positive), we
        # make use of the divmod_near operation, defined by:
        #
        # divmod_near(a, b) = (q, r)
        #
        # where q is the nearest integer to the quotient a / b (the
        # nearest even integer in the case of a tie) and r == a - q * b.
        # Hence q * b = a - r is the nearest multiple of b to a,
        # preferring even multiples in the case of a tie.
        #
        # So the nearest multiple of 10**n to m is:
        #
        # m - divmod_near(m, 10**n)[1]

        # XXX: since divmod_near is pure python we can probably remove
        # the longs used here. or this could at least likely be more
        # efficient for W_IntObject
        from pypy.objspace.std.longobject import newlong

        if w_ndigits is None:
            return self.int(space)

        ndigits = space.bigint_w(space.index(w_ndigits))
        # if ndigits >= 0 then no rounding is necessary; return self
        # unchanged
        if ndigits.ge(rbigint.fromint(0)):
            return self.int(space)

        # result = self - divmod_near(self, 10 ** -ndigits)[1]
        right = rbigint.fromint(10).pow(ndigits.neg())
        w_tuple = divmod_near(space, self, newlong(space, right))
        _, w_r = space.fixedview(w_tuple, 2)
        return space.sub(self, w_r)
Exemplo n.º 8
0
 def newlong_from_rbigint(self, val):
     return newlong(self, val)
Exemplo n.º 9
0
 def newlong_from_rbigint(self, val):
     return newlong(self, val)
Exemplo n.º 10
0
    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, base=0):
    parser.rewind()
    try:
        bigint = string_to_bigint(None, base=base, parser=parser)
    except ParseStringError, e:
        raise OperationError(space.w_ValueError,
                             space.wrap(e.msg))
    from pypy.objspace.std.longobject import newlong
    return newlong(space, bigint)

def descr__new__(space, w_inttype, w_x=0, w_s='', w_symbolic=False, w_base=gateway.NoneNotWrapped):
    """
    This is the constructor for creating an integer type. The values passed in
    the parameters are not Python types but PyPy types. This little detail
    can trip up any person new to hacking on PyPy.
    """
    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:
Exemplo n.º 11
0
 def descr_uint(self, space):
     return newlong(space, self.bigval)
Exemplo n.º 12
0
 def newlong_from_rbigint(self, val):
     try:
         return self.newint(val.toint())
     except OverflowError:
         return newlong(self, val)