コード例 #1
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)
コード例 #2
0
ファイル: longtype.py プロジェクト: charred/pypy
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)
コード例 #3
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
コード例 #4
0
ファイル: inttype.py プロジェクト: charred/pypy
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
コード例 #5
0
    def descr__new__(space, w_complextype, w_real, w_imag=None):
        # if w_real is already a complex number and there is no second
        # argument, return it.  Note that we cannot return w_real if
        # it is an instance of a *subclass* of complex, or if w_complextype
        # is itself a subclass of complex.
        noarg2 = w_imag is None
        if (noarg2 and space.is_w(w_complextype, space.w_complex)
                and space.is_w(space.type(w_real), space.w_complex)):
            return w_real

        if space.isinstance_w(w_real, space.w_text):
            # a string argument
            if not noarg2:
                raise oefmt(
                    space.w_TypeError, "complex() can't take second"
                    " arg if first is a string")
            unistr = unicode_to_decimal_w(space, w_real)
            try:
                unistr = _remove_underscores(unistr)
            except ValueError:
                raise oefmt(space.w_ValueError,
                            "complex() arg is a malformed string")
            try:
                realstr, imagstr = _split_complex(unistr)
            except ValueError:
                raise oefmt(space.w_ValueError,
                            "complex() arg is a malformed string")
            try:
                realval = string_to_float(realstr)
                imagval = string_to_float(imagstr)
            except ParseStringError:
                raise oefmt(space.w_ValueError,
                            "complex() arg is a malformed string")

        else:
            # non-string arguments
            realval, imagval = unpackcomplex(space, w_real)

            # now take w_imag into account
            if not noarg2:
                # complex(x, y) == x+y*j, even if 'y' is already a complex.
                realval2, imagval2 = unpackcomplex(space,
                                                   w_imag,
                                                   firstarg=False)

                # try to preserve the signs of zeroes of realval and realval2
                if imagval2 != 0.0:
                    realval -= imagval2

                if imagval != 0.0:
                    imagval += realval2
                else:
                    imagval = realval2
        # done
        w_obj = space.allocate_instance(W_ComplexObject, w_complextype)
        W_ComplexObject.__init__(w_obj, realval, imagval)
        return w_obj
コード例 #6
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.lookup(w_value, '__long__') is not None
              or space.lookup(w_value, '__int__') is not None):
            w_obj = space.long(w_value)
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.lookup(w_value, '__trunc__') is not None:
            w_obj = space.trunc(w_value)
            # :-(  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)
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.isinstance_w(w_value, space.w_str):
            return _string_to_w_long(space, w_longtype, w_value,
                                     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, w_value,
                                     unicode_to_decimal_w(space, w_value))
        else:
            try:
                buf = space.charbuf_w(w_value)
            except OperationError, e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(
                    space.w_TypeError,
                    "long() argument must be a string or a number, "
                    "not '%T'", w_value)
            else:
                return _string_to_w_long(space, w_longtype, w_value, buf)
コード例 #7
0
ファイル: longobject.py プロジェクト: abhinavthomas/pypy
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.lookup(w_value, '__long__') is not None or
              space.lookup(w_value, '__int__') is not None):
            w_obj = space.long(w_value)
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.lookup(w_value, '__trunc__') is not None:
            w_obj = space.trunc(w_value)
            # :-(  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)
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.isinstance_w(w_value, space.w_str):
            return _string_to_w_long(space, w_longtype, w_value,
                                     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, w_value,
                                     unicode_to_decimal_w(space, w_value))
        else:
            try:
                buf = space.charbuf_w(w_value)
            except OperationError, e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError,
                            "long() argument must be a string or a number, "
                            "not '%T'", w_value)
            else:
                return _string_to_w_long(space, w_longtype, w_value, buf)
コード例 #8
0
ファイル: intobject.py プロジェクト: timfel/thesis-data
def _new_int(space, w_inttype, w_x, w_base=None):
    w_longval = None
    w_value = w_x     # 'x' is the keyword argument name in CPython
    value = 0
    if w_base is None:
        # check for easy cases
        if type(w_value) is W_IntObject:
            value = w_value.intval
        elif (space.lookup(w_value, '__int__') is not None or
              space.lookup(w_value, '__trunc__') is not None):
            # 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
            value = space.int_w(w_obj, allow_conversion=False)
        elif space.isinstance_w(w_value, space.w_str):
            value, w_longval = _string_to_int_or_long(space, w_value,
                                                      space.str_w(w_value))
        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, w_value, string)
        else:
            # If object supports the buffer interface
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError,
                            "int() argument must be a string or a number, "
                            "not '%T'", w_value)
            else:
                value, w_longval = _string_to_int_or_long(space, w_value, buf)
    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 as e:
                raise oefmt(space.w_TypeError,
                            "int() can't convert non-string with explicit "
                            "base")

        value, w_longval = _string_to_int_or_long(space, w_value, s, base)

    if w_longval is not None:
        if not space.is_w(w_inttype, space.w_int):
            raise oefmt(space.w_OverflowError,
                        "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
コード例 #9
0
            # 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,
コード例 #10
0
ファイル: test_unicodeobject.py プロジェクト: Qointum/pypy
 def test_unicode_to_decimal_w_wide(self, space):
     from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
     w_s = space.wrap(u'\U0001D7CF\U0001D7CE')  # 𝟏𝟎
     s2 = unicode_to_decimal_w(space, w_s)
     assert s2 == "10"
コード例 #11
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.lookup(w_value, '__long__') is not None
              or space.lookup(w_value, '__int__') is not None):
            w_obj = space.long(w_value)
            if (space.is_w(w_longtype, space.w_long)
                    and space.isinstance_w(w_obj, space.w_long)):
                return w_obj
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.lookup(w_value, '__trunc__') is not None:
            w_obj = space.trunc(w_value)
            if (space.is_w(w_longtype, space.w_long)
                    and space.isinstance_w(w_obj, space.w_long)):
                return 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)
            return newbigint(space, w_longtype, space.bigint_w(w_obj))
        elif space.isinstance_w(w_value, space.w_bytes):
            return _string_to_w_long(space, w_longtype, w_value,
                                     space.bytes_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, w_value,
                                     unicode_to_decimal_w(space, w_value))
        else:
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(
                    space.w_TypeError,
                    "larg() argumento tiene que ser palabra o número, "
                    "no '%T'", w_value)
            else:
                return _string_to_w_long(space, w_longtype, w_value, buf)
    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.bytes_w(w_value)
            except OperationError:
                raise oefmt(
                    space.w_TypeError,
                    "larg() no puede convertir no-palabra con base "
                    "explícito")
        return _string_to_w_long(space, w_longtype, w_value, s, base)
コード例 #12
0
def _new_baseint(space, w_value, w_base=None):
    if w_base is None:
        if space.is_w(space.type(w_value), space.w_int):
            assert isinstance(w_value, W_AbstractIntObject)
            return w_value
        elif space.lookup(w_value, '__int__') is not None:
            w_intvalue = space.int(w_value)
            return _ensure_baseint(space, w_intvalue)
        elif space.lookup(w_value, '__trunc__') is not None:
            w_obj = space.trunc(w_value)
            if not space.isinstance_w(w_obj, space.w_int):
                w_obj = space.int(w_obj)
            assert isinstance(w_obj, W_AbstractIntObject)
            return _ensure_baseint(space, w_obj)
        elif space.isinstance_w(w_value, space.w_unicode):
            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
            try:
                b = unicode_to_decimal_w(space, w_value)
            except Exception:
                raise oefmt(space.w_ValueError,
                            'invalid literal for int() with base 10: %R',
                            w_value)
            return _string_to_int_or_long(space, w_value, b)
        elif (space.isinstance_w(w_value, space.w_bytearray)
              or space.isinstance_w(w_value, space.w_bytes)):
            return _string_to_int_or_long(space, w_value,
                                          space.charbuf_w(w_value))
        else:
            # If object supports the buffer interface
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(
                    space.w_TypeError,
                    "int() argument must be a string, a bytes-like "
                    "object or a number, not '%T'", w_value)
            else:
                return _string_to_int_or_long(space, w_value, buf)
    else:
        try:
            base = space.getindex_w(w_base, None)
        except OperationError as e:
            if not e.match(space, space.w_OverflowError):
                raise
            base = 37  # this raises the right error in string_to_bigint()

        if space.isinstance_w(w_value, space.w_unicode):
            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
            try:
                s = unicode_to_decimal_w(space, w_value)
            except Exception:
                raise oefmt(space.w_ValueError,
                            'invalid literal for int() with base %d: %R', base,
                            w_value)
        elif (space.isinstance_w(w_value, space.w_bytes)
              or space.isinstance_w(w_value, space.w_bytearray)):
            s = space.charbuf_w(w_value)
        else:
            raise oefmt(space.w_TypeError,
                        "int() can't convert non-string with explicit base")

        return _string_to_int_or_long(space, w_value, s, base)
コード例 #13
0
ファイル: test_unicodeobject.py プロジェクト: Qointum/pypy
 def test_unicode_to_decimal_w(self, space):
     from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
     w_s = space.wrap(u"\N{EM SPACE}-3\N{EN SPACE}")
     s2 = unicode_to_decimal_w(space, w_s)
     assert s2 == " -3 "
コード例 #14
0
ファイル: test_unicodeobject.py プロジェクト: Qointum/pypy
 def test_unicode_to_decimal_w_wide(self, space):
     from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
     w_s = space.wrap(u'\U0001D7CF\U0001D7CE') # 𝟏𝟎
     s2 = unicode_to_decimal_w(space, w_s)
     assert s2 == "10"
コード例 #15
0
ファイル: intobject.py プロジェクト: sota/pypy-old
def _new_int(space, w_inttype, w_x, w_base=None):
    w_longval = None
    w_value = w_x  # 'x' is the keyword argument name in CPython
    value = 0
    if w_base is None:
        # check for easy cases
        if type(w_value) is W_IntObject:
            value = w_value.intval
        elif (space.lookup(w_value, '__int__') is not None
              or space.lookup(w_value, '__trunc__') is not None):
            # 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
            value = space.int_w(w_obj, allow_conversion=False)
        elif space.isinstance_w(w_value, space.w_str):
            value, w_longval = _string_to_int_or_long(space, w_value,
                                                      space.str_w(w_value))
        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, w_value, string)
        else:
            # If object supports the buffer interface
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(
                    space.w_TypeError,
                    "int() argument must be a string or a number, "
                    "not '%T'", w_value)
            else:
                value, w_longval = _string_to_int_or_long(space, w_value, buf)
    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 as e:
                raise oefmt(
                    space.w_TypeError,
                    "int() can't convert non-string with explicit "
                    "base")

        value, w_longval = _string_to_int_or_long(space, w_value, s, base)

    if w_longval is not None:
        if not space.is_w(w_inttype, space.w_int):
            raise oefmt(space.w_OverflowError,
                        "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
コード例 #16
0
ファイル: longobject.py プロジェクト: abhinavthomas/pypy
            try:
                buf = space.charbuf_w(w_value)
            except OperationError, e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError,
                            "long() argument must be a string or a number, "
                            "not '%T'", w_value)
            else:
                return _string_to_w_long(space, w_longtype, w_value, buf)
    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 oefmt(space.w_TypeError,
                            "long() can't convert non-string with explicit "
                            "base")
        return _string_to_w_long(space, w_longtype, w_value, s, base)


def _string_to_w_long(space, w_longtype, w_source, string, base=10):
    try:
        bigint = rbigint.fromstr(string, base)
    except ParseStringError as e:
        raise wrap_parsestringerror(space, e, w_source)
コード例 #17
0
ファイル: test_unicodeobject.py プロジェクト: Qointum/pypy
 def test_unicode_to_decimal_w(self, space):
     from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
     w_s = space.wrap(u"\N{EM SPACE}-3\N{EN SPACE}")
     s2 = unicode_to_decimal_w(space, w_s)
     assert s2 == " -3 "
コード例 #18
0
def _new_int(space, w_inttype, w_x, w_base=None):
    from pypy.objspace.std.longobject import W_LongObject, newbigint
    if space.config.objspace.std.withsmalllong:
        from pypy.objspace.std.smalllongobject import W_SmallLongObject
    else:
        W_SmallLongObject = None

    w_longval = None
    w_value = w_x     # 'x' is the keyword argument name in CPython
    value = 0
    if w_base is None:
        # check for easy cases
        if type(w_value) is W_IntObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            value = w_value.intval
            w_obj = space.allocate_instance(W_IntObject, w_inttype)
            W_IntObject.__init__(w_obj, value)
            return w_obj
        elif type(w_value) is W_LongObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            return newbigint(space, w_inttype, w_value.num)
        elif W_SmallLongObject and type(w_value) is W_SmallLongObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            return newbigint(space, w_inttype, space.bigint_w(w_value))
        elif space.lookup(w_value, '__int__') is not None:
            return _from_intlike(space, w_inttype, w_value)
        elif space.lookup(w_value, '__trunc__') is not None:
            return _from_intlike(space, w_inttype, space.trunc(w_value))
        elif space.isinstance_w(w_value, space.w_unicode):
            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w
            return _string_to_int_or_long(space, w_inttype, w_value,
                                          unicode_to_decimal_w(space, w_value))
        elif (space.isinstance_w(w_value, space.w_bytearray) or
              space.isinstance_w(w_value, space.w_bytes)):
            return _string_to_int_or_long(space, w_inttype, w_value,
                                          space.bufferstr_w(w_value))
        else:
            # If object supports the buffer interface
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError,
                            "int() argument must be a string or a number, "
                            "not '%T'", w_value)
            else:
                return _string_to_int_or_long(space, w_inttype, w_value, buf)
    else:
        try:
            base = space.int_w(w_base)
        except OperationError, e:
            if not e.match(space, space.w_OverflowError):
                raise
            base = 37 # this raises the right error in string_to_bigint()

        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.bufferstr_w(w_value)
            except OperationError as e:
                raise oefmt(space.w_TypeError,
                            "int() can't convert non-string with explicit "
                            "base")

        return _string_to_int_or_long(space, w_inttype, w_value, s, base)
コード例 #19
0
ファイル: intobject.py プロジェクト: Qointum/pypy
def _new_int(space, w_inttype, w_x, w_base=None):
    from pypy.objspace.std.longobject import W_LongObject, newbigint

    if space.config.objspace.std.withsmalllong:
        from pypy.objspace.std.smalllongobject import W_SmallLongObject
    else:
        W_SmallLongObject = None

    w_longval = None
    w_value = w_x  # 'x' is the keyword argument name in CPython
    value = 0
    if w_base is None:
        # check for easy cases
        if type(w_value) is W_IntObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            value = w_value.intval
            w_obj = space.allocate_instance(W_IntObject, w_inttype)
            W_IntObject.__init__(w_obj, value)
            return w_obj
        elif type(w_value) is W_LongObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            return newbigint(space, w_inttype, w_value.num)
        elif W_SmallLongObject and type(w_value) is W_SmallLongObject:
            if space.is_w(w_inttype, space.w_int):
                return w_value
            return newbigint(space, w_inttype, space.bigint_w(w_value))
        elif space.lookup(w_value, "__int__") is not None:
            return _from_intlike(space, w_inttype, w_value)
        elif space.lookup(w_value, "__trunc__") is not None:
            return _from_intlike(space, w_inttype, space.trunc(w_value))
        elif space.isinstance_w(w_value, space.w_unicode):
            from pypy.objspace.std.unicodeobject import unicode_to_decimal_w

            return _string_to_int_or_long(space, w_inttype, w_value, unicode_to_decimal_w(space, w_value))
        elif space.isinstance_w(w_value, space.w_bytearray) or space.isinstance_w(w_value, space.w_bytes):
            return _string_to_int_or_long(space, w_inttype, w_value, space.bufferstr_w(w_value))
        else:
            # If object supports the buffer interface
            try:
                buf = space.charbuf_w(w_value)
            except OperationError as e:
                if not e.match(space, space.w_TypeError):
                    raise
                raise oefmt(space.w_TypeError, "int() argument must be a string or a number, " "not '%T'", w_value)
            else:
                return _string_to_int_or_long(space, w_inttype, w_value, buf)
    else:
        try:
            base = space.int_w(w_base)
        except OperationError, e:
            if not e.match(space, space.w_OverflowError):
                raise
            base = 37  # this raises the right error in string_to_bigint()

        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.bufferstr_w(w_value)
            except OperationError as e:
                raise oefmt(space.w_TypeError, "int() can't convert non-string with explicit " "base")

        return _string_to_int_or_long(space, w_inttype, w_value, s, base)