def string_to_int_or_long(space, string, base=10): w_longval = None value = 0 try: value = string_to_int(string, base) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg))
def descr__new__(space, w_inttype, w_x=0, w_base=NoneNotWrapped): 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: # check for easy cases if isinstance(w_value, W_IntObject): value = w_value.intval elif space.is_true(space.isinstance(w_value, space.w_str)): try: value = string_to_int(space.str_w(w_value)) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) except ParseStringOverflowError, e: w_longval = retry_to_w_long(space, e.parser)
value = w_value.intval elif space.is_true(space.isinstance(w_value, space.w_str)): try: value = string_to_int(space.str_w(w_value)) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) except ParseStringOverflowError, e: w_longval = retry_to_w_long(space, e.parser) 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 pypy.objspace.std.unicodeobject import unicode_to_decimal_w string = unicode_to_decimal_w(space, w_value) try: value = string_to_int(string) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) except ParseStringOverflowError, e: w_longval = retry_to_w_long(space, e.parser) else: # otherwise, use the __int__() method w_obj = space.int(w_value) # 'int(x)' should return whatever x.__int__() returned 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 try: value = space.int_w(w_obj)
elif space.is_true(space.isinstance(w_value, space.w_str)): try: value = string_to_int(space.str_w(w_value)) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) except ParseStringOverflowError, e: w_longval = retry_to_w_long(space, e.parser) 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 pypy.objspace.std.unicodeobject import unicode_to_decimal_w string = unicode_to_decimal_w(space, w_value) try: value = string_to_int(string) except ParseStringError, e: raise OperationError(space.w_ValueError, space.wrap(e.msg)) except ParseStringOverflowError, e: w_longval = retry_to_w_long(space, e.parser) else: # otherwise, use the __int__() method w_obj = space.int(w_value) # 'int(x)' should return whatever x.__int__() returned 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 try: