def _as_subint(space, w_inttype, w_value): 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 if type(w_value) is W_IntObject: w_obj = space.allocate_instance(W_IntObject, w_inttype) W_IntObject.__init__(w_obj, w_value.intval) return w_obj elif type(w_value) is W_LongObject: return newbigint(space, w_inttype, w_value.num) elif W_SmallLongObject and type(w_value) is W_SmallLongObject: return newbigint(space, w_inttype, space.bigint_w(w_value))
def _from_intlike(space, w_inttype, w_intlike): w_obj = space.int(w_intlike) if space.is_w(w_inttype, space.w_int): return w_obj from pypy.objspace.std.longobject import newbigint return newbigint(space, w_inttype, space.bigint_w(w_obj))
def _retry_to_w_long(space, parser, w_inttype, w_source): from pypy.objspace.std.longobject import newbigint parser.rewind() try: bigint = rbigint._from_numberstring_parser(parser) except ParseStringError as e: raise wrap_parsestringerror(space, e, w_source) return newbigint(space, w_inttype, bigint)
def _retry_to_w_long(space, parser, w_inttype, w_source): from pypy.objspace.std.longobject import newbigint parser.rewind() try: bigint = rbigint._from_numberstring_parser(parser) except ParseStringError as e: raise wrap_parsestringerror(space, e, w_source) return newbigint(space, w_inttype, bigint)
def descr_from_bytes(space, w_inttype, w_obj, byteorder, signed=False): """int.from_bytes(bytes, byteorder, *, signed=False) -> int Return the integer represented by the given array of bytes. The bytes argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol. The byteorder argument determines the byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. The signed keyword-only argument indicates whether two's complement is used to represent the integer. """ from pypy.objspace.std.bytesobject import makebytesdata_w bytes = ''.join(makebytesdata_w(space, w_obj)) try: bigint = rbigint.frombytes(bytes, byteorder=byteorder, signed=signed) except InvalidEndiannessError: raise oefmt(space.w_ValueError, "byteorder must be either 'little' or 'big'") try: as_int = bigint.toint() except OverflowError: from pypy.objspace.std.longobject import newbigint return newbigint(space, w_inttype, bigint) else: if space.is_w(w_inttype, space.w_int): # common case return wrapint(space, as_int) w_obj = space.allocate_instance(W_IntObject, w_inttype) W_IntObject.__init__(w_obj, as_int) return w_obj
def descr_from_bytes(space, w_inttype, w_obj, byteorder, signed=False): """int.from_bytes(bytes, byteorder, *, signed=False) -> int Return the integer represented by the given array of bytes. The bytes argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol. The byteorder argument determines the byte order used to represent the integer. If byteorder is 'big', the most significant byte is at the beginning of the byte array. If byteorder is 'little', the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder' as the byte order value. The signed keyword-only argument indicates whether two's complement is used to represent the integer. """ from pypy.objspace.std.bytesobject import makebytesdata_w bytes = "".join(makebytesdata_w(space, w_obj)) try: bigint = rbigint.frombytes(bytes, byteorder=byteorder, signed=signed) except InvalidEndiannessError: raise oefmt(space.w_ValueError, "byteorder must be either 'little' or 'big'") try: as_int = bigint.toint() except OverflowError: from pypy.objspace.std.longobject import newbigint return newbigint(space, w_inttype, bigint) else: if space.is_w(w_inttype, space.w_int): # common case return wrapint(space, as_int) w_obj = space.allocate_instance(W_IntObject, w_inttype) W_IntObject.__init__(w_obj, as_int) return w_obj
def _from_intlike(space, w_inttype, w_intlike): w_obj = space.int(w_intlike) if space.is_w(w_inttype, space.w_int): return w_obj from pypy.objspace.std.longobject import newbigint return newbigint(space, w_inttype, space.bigint_w(w_obj))
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)
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)