def __setitem__(self, key, value):
        """Set value in the correct index of the parent bytearray."""
        # Handle integer keys (lookup).
        try:
            int(key)
        except TypeError:
            key_is_int = False
        else:
            key_is_int = True
            if key < 0:
                key += len(self)
            key = slice(key, key + 1)

        # Calculate slices.
        start = key.start or 0
        if start < 0:
            start += len(self)
        if start >= len(self):
            raise IndexError('{0} index out of range'.format(self.__class__.__name__))
        stop = key.stop or (start if key.stop == 0 else len(self))
        if stop < 0:
            stop += len(self)
        start += self.slice.start
        stop += self.slice.start
        stop = min(stop, self.slice.stop)

        # Catch invalid length.
        original_length = len(self.pointee)
        if not key_is_int and len(self.pointee[start:stop]) != len(value):
            raise TypeError("length of '{0}' object cannot be changed".format(self.__class__.__name__))

        # Handle slices.
        if key_is_int:
            self.pointee[start] = value
        else:
            self.pointee[start:stop] = value

        # Catch bugs.
        if len(self.pointee) != original_length:
            raise RuntimeError('Bug in {0} found! Please report this.'.format(self.__class__.__name__))
def __setitem__ ( self , key , value ) : try : int ( key ) except TypeError : key_is_int = False else : key_is_int = True if key < 0 : key += len ( self ) key = slice ( key , key + 1 ) start = key . start or 0 if start < 0 : start += len ( self ) if start >= len ( self ) : raise IndexError ( '{0} index out of range' . format ( self . __class__ . __name__ ) ) stop = key . stop or ( start if key . stop == 0 else len ( self ) ) if stop < 0 : stop += len ( self ) start += self . slice . start stop += self . slice . start stop = min ( stop , self . slice . stop ) original_length = len ( self . pointee ) if not key_is_int and len ( self . pointee [ start : stop ] ) != len ( value ) : raise TypeError ( "length of '{0}' object cannot be changed" . format ( self . __class__ . __name__ ) ) if key_is_int : self . pointee [ start ] = value else : self . pointee [ start : stop ] = value if len ( self . pointee ) != original_length : raise RuntimeError ( 'Bug in {0} found! Please report this.' . format ( self . __class__ . __name__ ) )
Set value in the correct index of the parent bytearray.