def test_frombytes(self): bigint = rbigint.frombytes('', byteorder='big', signed=True) assert bigint.tolong() == 0 s = "\xFF\x12\x34\x56" bigint = rbigint.frombytes(s, byteorder="big", signed=False) assert bigint.tolong() == 0xFF123456 bigint = rbigint.frombytes(s, byteorder="little", signed=False) assert bigint.tolong() == 0x563412FF s = "\xFF\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\xFF" bigint = rbigint.frombytes(s, byteorder="big", signed=False) assert s == bigint.tobytes(16, byteorder="big", signed=False) py.test.raises(InvalidEndiannessError, bigint.frombytes, '\xFF', 'foo', signed=True)
def test_frombytes(self): bigint = rbigint.frombytes('', byteorder='big', signed=True) assert bigint.tolong() == 0 s = "\xFF\x12\x34\x56" bigint = rbigint.frombytes(s, byteorder="big", signed=False) assert bigint.tolong() == 0xFF123456 bigint = rbigint.frombytes(s, byteorder="little", signed=False) assert bigint.tolong() == 0x563412FF s = "\xFF\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\xFF" bigint = rbigint.frombytes(s, byteorder="big", signed=False) assert s == bigint.tobytes(16, byteorder="big", signed=False) py.test.raises(InvalidEndiannessError, bigint.frombytes, '\xFF', 'foo', signed=True) bigint = rbigint.frombytes('\x82', byteorder='big', signed=True) assert bigint.tolong() == -126
def decode_long(space, string, byteorder='little', signed=1): from rpython.rlib.rbigint import rbigint, InvalidEndiannessError try: result = rbigint.frombytes(string, byteorder, bool(signed)) except InvalidEndiannessError: raise oefmt(space.w_ValueError, "invalid byteorder argument") return space.newlong_from_rbigint(result)
def _integer_bytes_to_integer(bytes, signed, big_endian, start=0, w_end=None): if w_end is None: end = len(bytes) else: end = w_end.value if not (0 <= start < len(bytes)): raise SchemeException( "integer-bytes->integer: start position not in byte string") if not (0 <= end <= len(bytes)): raise SchemeException( "integer-bytes->integer: end position not in byte string") if end < start: raise SchemeException( "integer-bytes->integer: end position less than start position") length = end - start if length not in (2, 4, 8): raise SchemeException( "integer-bytes->integer: byte string must have length 2, 4, or 8") if start != 0 or end != len(bytes): bytes = bytes[start:end] byteorder = "little" if big_endian is values.w_false else "big" is_signed = signed is not values.w_false big = rbigint.frombytes(bytes, byteorder, is_signed) try: result = values.W_Fixnum(big.toint()) except OverflowError: result = values.W_Bignum(big) return result
def integer_bytes_to_integer(bstr, signed, big_endian, w_start, w_end): bytes = bstr.as_bytes_list() start = w_start.value if w_end is None: end = len(bytes) else: end = w_end.value if not (0 <= start < len(bytes)): raise SchemeException( "integer-bytes->integer: start position not in byte string") if not (0 <= end <= len(bytes)): raise SchemeException( "integer-bytes->integer: end position not in byte string") if end < start: raise SchemeException( "integer-bytes->integer: end position less than start position") length = end - start if length not in (2, 4, 8): raise SchemeException( "integer-bytes->integer: byte string must have length 2, 4, or 8") if start != 0 or end != len(bytes): bytes = bytes[start:end] byteorder = "little" if big_endian is values.w_false else "big" is_signed = signed is not values.w_false big = rbigint.frombytes(bytes, byteorder, is_signed) try: result = values.W_Fixnum(big.toint()) except OverflowError: result = values.W_Bignum(big) return result
def decode_long(space, string, byteorder='little', signed=1): from rpython.rlib.rbigint import rbigint, InvalidEndiannessError try: result = rbigint.frombytes(string, byteorder, bool(signed)) except InvalidEndiannessError: raise oefmt(space.w_ValueError, "invalid byteorder argument") return space.newlong_from_rbigint(result)
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed): little_endian = rffi.cast(lltype.Signed, little_endian) signed = rffi.cast(lltype.Signed, signed) s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, bytes), rffi.cast(lltype.Signed, n)) if little_endian: byteorder = 'little' else: byteorder = 'big' result = rbigint.frombytes(s, byteorder, signed != 0) return space.newlong_from_rbigint(result)
def _PyLong_FromByteArray(space, bytes, n, little_endian, signed): little_endian = rffi.cast(lltype.Signed, little_endian) signed = rffi.cast(lltype.Signed, signed) s = rffi.charpsize2str(rffi.cast(rffi.CCHARP, bytes), rffi.cast(lltype.Signed, n)) if little_endian: byteorder = 'little' else: byteorder = 'big' result = rbigint.frombytes(s, byteorder, signed != 0) return space.newlong_from_rbigint(result)
def integer_bytes_to_integer(bstr, signed, big_endian): # XXX Currently does not make use of the signed or big endian parameter bytes = bstr.value if len(bytes) not in (2, 4, 8): raise SchemeException( "integer-bytes->integer: byte string must have length 2, 4, or 8") byteorder = "little" if big_endian is values.w_false else "big" is_signed = signed is not values.w_false big = rbigint.frombytes(bytes, byteorder, is_signed) try: result = values.W_Fixnum(big.toint()) except OverflowError: result = values.W_Bignum(big) return result
def integer_bytes_to_integer(bstr, signed, big_endian): # XXX Currently does not make use of the signed or big endian parameter bytes = bstr.value if len(bytes) not in (2, 4, 8): raise SchemeException( "integer-bytes->integer: byte string must have length 2, 4, or 8") byteorder = "little" if big_endian is values.w_false else "big" is_signed = signed is not values.w_false big = rbigint.frombytes(bytes, byteorder, is_signed) try: result = values.W_Fixnum(big.toint()) except OverflowError: result = values.W_Bignum(big) return result
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 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 = 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: w_obj = space.newlong_from_rbigint(bigint) else: w_obj = space.newint(as_int) if not space.is_w(w_inttype, space.w_int): # That's what from_bytes() does in CPython 3.5.2 too w_obj = space.call_function(w_inttype, w_obj) return w_obj
def getrbigint(self): from rpython.rlib.rbigint import rbigint return rbigint.frombytes(self._bytes(), 'little', False)
def test_frombytes_tobytes_hypothesis(self, s, big, signed): # check the roundtrip from binary strings to bigints and back byteorder = 'big' if big else 'little' bigint = rbigint.frombytes(s, byteorder=byteorder, signed=signed) t = bigint.tobytes(len(s), byteorder=byteorder, signed=signed) assert s == t