def descr__new__(space, w_complextype, w_real=0.0, w_imag=None): from pypy.objspace.std.complexobject import W_ComplexObject # 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 = space.is_w(w_imag, space.w_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.is_true(space.isinstance(w_real, space.w_str)) or \ space.is_true(space.isinstance(w_real, space.w_unicode)): # a string argument if not noarg2: raise OperationError( space.w_TypeError, space.wrap("complex() can't take second arg" " if first is a string")) try: realstr, imagstr = _split_complex(space.str_w(w_real)) except ValueError: raise OperationError(space.w_ValueError, space.wrap(ERR_MALFORMED)) try: realval = interp_string_to_float(space, realstr) imagval = interp_string_to_float(space, imagstr) except ParseStringError: raise OperationError(space.w_ValueError, space.wrap(ERR_MALFORMED)) else: #check for overflow if abs(realval) == OVERFLOWED_FLOAT or abs( imagval) == OVERFLOWED_FLOAT: raise OperationError( space.w_ValueError, space.wrap("complex() literal too large to convert")) else: # non-string arguments # test for a '__complex__' method, and call it if found. # A bit of a hack to support old-style classes: don't use # space.lookup() (this is similar to CPython). try: w_method = space.getattr(w_real, space.wrap('__complex__')) except OperationError, e: if not e.match(space, space.w_AttributeError): raise else:
def descr__new__(space, w_complextype, w_real=0.0, w_imag=None): from pypy.objspace.std.complexobject import W_ComplexObject # 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 = space.is_w(w_imag, space.w_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.is_true(space.isinstance(w_real, space.w_str)) or \ space.is_true(space.isinstance(w_real, space.w_unicode)): # a string argument if not noarg2: raise OperationError(space.w_TypeError, space.wrap("complex() can't take second arg" " if first is a string")) try: realstr, imagstr = _split_complex(space.str_w(w_real)) except ValueError: raise OperationError(space.w_ValueError, space.wrap(ERR_MALFORMED)) try: realval = interp_string_to_float(space, realstr) imagval = interp_string_to_float(space, imagstr) except ParseStringError: raise OperationError(space.w_ValueError, space.wrap(ERR_MALFORMED)) else: #check for overflow if abs(realval) == OVERFLOWED_FLOAT or abs(imagval) == OVERFLOWED_FLOAT: raise OperationError(space.w_ValueError,space.wrap( "complex() literal too large to convert")) else: # non-string arguments # test for a '__complex__' method, and call it if found. # A bit of a hack to support old-style classes: don't use # space.lookup() (this is similar to CPython). try: w_method = space.getattr(w_real, space.wrap('__complex__')) except OperationError, e: if not e.match(space, space.w_AttributeError): raise else:
def descr__new__(space, w_floattype, w_x=0.0): from pypy.objspace.std.floatobject import W_FloatObject w_value = w_x # 'x' is the keyword argument name in CPython if space.is_true(space.isinstance(w_value, space.w_str)): strvalue = space.str_w(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))
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)) elif space.is_true(space.isinstance(w_value, space.w_unicode)): if space.config.objspace.std.withropeunicode: 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