Exemple #1
0
def descr__new__(space, w_floattype, w_x):
    from pypy.objspace.std.floatobject import W_FloatObject
    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 OperationError(
                    space.w_TypeError,
                    space.wrap(
                        "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
Exemple #2
0
def descr__new__(space, w_floattype, w_x):
    from pypy.objspace.std.floatobject import W_FloatObject
    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 OperationError(space.w_TypeError, space.wrap(
                    "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
Exemple #3
0
            from pypy.objspace.std.ropeunicodeobject import unicode_to_decimal_w
        else:
            from unicodeobject import unicode_to_decimal_w
        strvalue = unicode_to_decimal_w(space, w_value)
        try:
            if USE_NEW_S2F:
                value = interp_string_to_float(space, strvalue)
            else:
                value = string_to_float(strvalue)
        except ParseStringError, e:
            raise OperationError(space.w_ValueError,
                                 space.wrap(e.msg))
    else:
        w_obj = space.float(w_value)
        if space.is_w(w_floattype, space.w_float):
            return w_obj  # 'float(x)' should return
                          # whatever x.__float__() returned
        value = space.float_w(w_obj)
    w_obj = space.allocate_instance(W_FloatObject, w_floattype)
    W_FloatObject.__init__(w_obj, value)
    return w_obj

# ____________________________________________________________

float_typedef = StdTypeDef("float",
    __doc__ = '''float(x) -> floating point number

Convert a string or number to a floating point number, if possible.''',
    __new__ = newmethod(descr__new__),
    )