def descr_extend(self, space, w_other): if isinstance(w_other, W_BytearrayObject): self._data += w_other.getdata() elif isinstance(w_other, W_BytesObject): # performance only self._data += w_other.bytes_w(space) else: self._data += makebytesdata_w(space, w_other)
def descr_setitem(self, space, w_index, w_other): if isinstance(w_index, W_SliceObject): oldsize = len(self.data) start, stop, step, slicelength = w_index.indices4(space, oldsize) sequence2 = makebytesdata_w(space, w_other) _setitem_slice_helper(space, self.data, start, step, slicelength, sequence2, empty_elem='\x00') else: idx = space.getindex_w(w_index, space.w_IndexError, "bytearray index") try: self.data[idx] = getbytevalue(space, w_other) except IndexError: raise oefmt(space.w_IndexError, "bytearray index out of range")
def descr_maketrans(space, w_type, w_from, w_to): """B.maketrans(frm, to) -> translation table Return a translation table (a bytes object of length 256) suitable for use in the bytes or bytearray translate method where each byte in frm is mapped to the byte at the same position in to. The bytes objects frm and to must be of the same length. """ from pypy.objspace.std.bytesobject import makebytesdata_w, wrapstr base_table = [chr(i) for i in range(256)] list_from = makebytesdata_w(space, w_from) list_to = makebytesdata_w(space, w_to) if len(list_from) != len(list_to): raise oefmt(space.w_ValueError, "maketrans arguments must have same length") for i in range(len(list_from)): pos_from = ord(list_from[i]) char_to = list_to[i] base_table[pos_from] = char_to return wrapstr(space, ''.join(base_table))
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 descr_setitem(self, space, w_index, w_other): if isinstance(w_index, W_SliceObject): sequence2 = makebytesdata_w(space, w_other) oldsize = self._len() start, stop, step, slicelength = w_index.indices4(space, oldsize) if start == 0 and step == 1 and len(sequence2) <= slicelength: self._delete_from_start(slicelength - len(sequence2)) slicelength = len(sequence2) if slicelength == 0: return data = self._data start += self._offset _setitem_slice_helper(space, data, start, step, slicelength, sequence2, empty_elem='\x00') else: idx = space.getindex_w(w_index, space.w_IndexError, "bytearray") newvalue = space.byte_w(w_other) self._data[self._fixindex(space, idx)] = newvalue
def descr_extend(self, space, w_other): if isinstance(w_other, W_BytearrayObject): self._data += w_other.getdata() else: self._inplace_add(makebytesdata_w(space, w_other))
def descr_extend(self, space, w_other): if isinstance(w_other, W_BytearrayObject): self.data += w_other.data else: self.data += makebytesdata_w(space, w_other) return self