Esempio n. 1
0
    def descr__new__(space, w_floattype, w_x):
        def _string_to_float(space, w_source, string):
            try:
                return rfloat.string_to_float(string)
            except ParseStringError as e:
                raise wrap_parsestringerror(space, e, w_source)

        w_value = w_x     # 'x' is the keyword argument name in CPython
        if space.lookup(w_value, "__float__") is not None:
            w_obj = space.float(w_value)
            if space.is_w(w_floattype, space.w_float):
                return w_obj
            value = space.float_w(w_obj)
        elif space.isinstance_w(w_value, space.w_unicode):
            from unicodeobject import unicode_to_decimal_w
            value = _string_to_float(space, w_value,
                                     unicode_to_decimal_w(space, w_value))
        else:
            try:
                value = space.charbuf_w(w_value)
            except OperationError as e:
                if e.match(space, space.w_TypeError):
                    raise oefmt(
                        space.w_TypeError,
                        "float() argument must be a string or a number")
                raise
            value = _string_to_float(space, w_value, value)
        w_obj = space.allocate_instance(W_FloatObject, w_floattype)
        W_FloatObject.__init__(w_obj, value)
        return w_obj
Esempio n. 2
0
    def descr__new__(space, w_floattype, w_x, __posonly__):
        def _string_to_float(space, w_source, string):
            try:
                string = _remove_underscores(string)
            except ValueError:
                pass
            else:
                try:
                    return rfloat.string_to_float(string)
                except ParseStringError as e:
                    pass
            raise oefmt(space.w_ValueError,
                        "could not convert string to float: %R", w_source)

        w_value = w_x  # 'x' is the keyword argument name in CPython
        if space.lookup(w_value, "__float__") is not None:
            w_obj = space.float(w_value)
            w_obj_type = space.type(w_obj)
            if not space.is_w(w_obj_type, space.w_float):
                space.warn(
                    space.newtext(
                        "%s.__float__ returned non-float (type %s).  "
                        "The ability to return an instance of a strict subclass "
                        "of float is deprecated, and may be removed "
                        "in a future version of Python." %
                        (space.type(w_value).name, w_obj_type.name)),
                    space.w_DeprecationWarning)
            elif space.is_w(w_floattype, space.w_float):
                return w_obj
            value = space.float_w(w_obj)
        elif space.isinstance_w(w_value, space.w_unicode):
            from unicodeobject import unicode_to_decimal_w
            value = _string_to_float(space, w_value,
                                     unicode_to_decimal_w(space, w_value))
        else:
            try:
                value = space.charbuf_w(w_value)
            except OperationError as e:
                if e.match(space, space.w_TypeError):
                    raise oefmt(
                        space.w_TypeError,
                        "float() argument must be a string or a "
                        "number, not '%T'", w_value)
                raise
            value = _string_to_float(space, w_value, value)
        w_obj = space.allocate_instance(W_FloatObject, w_floattype)
        W_FloatObject.__init__(w_obj, value)
        return w_obj
Esempio n. 3
0
def descr__new__(space, w_longtype, w_x=0, w_base=NoneNotWrapped):
    from pypy.objspace.std.longobject import W_LongObject
    w_value = w_x     # 'x' is the keyword argument name in CPython
    if w_base is None:
        # check for easy cases
        if isinstance(w_value, W_LongObject):
            pass
        elif space.is_true(space.isinstance(w_value, space.w_str)):
            try:
                w_value = string_to_w_long(space, space.str_w(w_value))
            except ParseStringError, e:
                raise OperationError(space.w_ValueError,
                                     space.wrap(e.msg))
        elif space.is_true(space.isinstance(w_value, space.w_unicode)):
            try:
                from unicodeobject import unicode_to_decimal_w
                w_value = string_to_w_long(space, unicode_to_decimal_w(space, w_value))
            except ParseStringError, e:
                raise OperationError(space.w_ValueError,
                                     space.wrap(e.msg))
Esempio n. 4
0
            if space.is_true(space.isinstance(w_obj, space.w_long)):
                assert isinstance(w_obj, W_LongObject)  # XXX this could fail!
                # XXX find a way to do that even if w_obj is not a W_LongObject
                w_value = w_obj
            elif space.is_true(space.isinstance(w_obj, space.w_int)):
                intval = space.int_w(w_obj)
                w_value = W_LongObject.fromint(space, intval)
            else:
                raise OperationError(space.w_ValueError,
                                    space.wrap("value can't be converted to long"))
    else:
        base = space.int_w(w_base)

        if space.is_true(space.isinstance(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("long() can't convert non-string "
                                                "with explicit base"))
        try:
            w_value = string_to_w_long(space, s, base)
        except ParseStringError, e:
            raise OperationError(space.w_ValueError,
                                 space.wrap(e.msg))

    w_obj = space.allocate_instance(W_LongObject, w_longtype)
    W_LongObject.__init__(w_obj, w_value.num)