Beispiel #1
0
def _stringToBuffer(datastring):
    """_stringToBuffer(datastring)  allocates a buffer, copies datastring into
    it, and returns the buffer.
    """
    abuff = memory.new_memory(len(datastring))
    memory.writeable_buffer(abuff)[:] = datastring
    return abuff
Beispiel #2
0
def _stringToBuffer(datastring):
    """_stringToBuffer(datastring)  allocates a buffer, copies datastring into
    it, and returns the buffer.
    """
    abuff = memory.new_memory(len(datastring))
    memory.writeable_buffer(abuff)[:] = datastring
    return abuff
Beispiel #3
0
 def _buffer(self, begin=0, end=None):
     """_buffer(self) returns a buffer object for Memmap 'self'.
     """
     if self._mmap is None:
         raise RuntimeError("Memmap no longer valid.  (closed?)")
     if end is None:
         end = len(self)
     if self._readonly:
         return buffer(self._mmap, begin, end - begin)
     else:
         return memory.writeable_buffer(self._mmap, begin, end - begin)
Beispiel #4
0
 def _buffer(self, begin=0, end=None):
     """_buffer(self) returns a buffer object for Memmap 'self'.
     """
     if self._mmap is None:
         raise RuntimeError("Memmap no longer valid.  (closed?)")
     if end is None:
         end = len(self)
     if self._readonly:
         return buffer(self._mmap, begin, end-begin)
     else:
         return memory.writeable_buffer(self._mmap, begin, end-begin)
Beispiel #5
0
    def _setitem(self, offset, value):
        """_setitem(self, offset) sets 'offset' to result of "padding" 'value'.

        _setitem silently truncates inputs which are too long.

        >>> s=array([""])
        >>> s[0] = "this"
        >>> s
        CharArray([''])
        >>> s=array(["this","that"])
        >>> s[0] = "othe"
        >>> s
        CharArray(['othe', 'that'])
        >>> s[1] = "v"
        >>> s
        CharArray(['othe', 'v'])
        >>> s = array("")
        >>> s[0] = "this"
        >>> s
        CharArray([''])
        """
        bo = offset
        where = memory.writeable_buffer(self._data)
        where[bo : bo + self._itemsize] = self.pad(value)[0 : self._itemsize]
Beispiel #6
0
    def _setitem(self, offset, value):
        """_setitem(self, offset) sets 'offset' to result of "padding" 'value'.

        _setitem silently truncates inputs which are too long.

        >>> s=array([""])
        >>> s[0] = "this"
        >>> s
        CharArray([''])
        >>> s=array(["this","that"])
        >>> s[0] = "othe"
        >>> s
        CharArray(['othe', 'that'])
        >>> s[1] = "v"
        >>> s
        CharArray(['othe', 'v'])
        >>> s = array("")
        >>> s[0] = "this"
        >>> s
        CharArray([''])
        """
        bo = offset
        where = memory.writeable_buffer(self._data)
        where[bo:bo + self._itemsize] = self.pad(value)[0:self._itemsize]
Beispiel #7
0
    def insert(self, offset, size=None, buffer=None):
        """
        insert(self, offset, size) inserts a new slice of 'size'
        bytes, possibly between two adjacent slices, at byte 'offset'.
        It is not legal to insert into the middle of another slice, but
        pretty much everything else is legal.  The resulting
        MemmapSlice is returned.

        >>> m = open("memmap.tst",mode="w+",len=100)
        >>> n = m[0:50]
        >>> p = m[50:100]
        >>> q=m.insert(0, 200)
        >>> r=m.insert(50, 100)
        >>> s=m.insert(100, 300)
        >>> t=m.insert(45, 100)
        Traceback (most recent call last):
        ...
        IndexError: Slice overlaps prior slice of same file.
        >>> m.flush()
        >>> len(m)
        700
        >>> m.close()
        """
        self._chkIndex(offset, isSlice=1)
        self._chkOverlaps(offset, offset)
        if buffer is None:
            mem = memory.new_memory(size)
            mem = memory.writeable_buffer(mem)
        elif size is None or len(buffer) == size:
            mem = buffer
            size = len(buffer)
        else:
            raise ValueError("buffer and size don't match.")
        obj = MemmapSlice(mem, dirty=1)
        self._slices.append((offset, offset, obj))
        return obj
Beispiel #8
0
    def insert(self, offset, size=None, buffer=None):
        """
        insert(self, offset, size) inserts a new slice of 'size'
        bytes, possibly between two adjacent slices, at byte 'offset'.
        It is not legal to insert into the middle of another slice, but
        pretty much everything else is legal.  The resulting
        MemmapSlice is returned.

        >>> m = open("memmap.tst",mode="w+",len=100)
        >>> n = m[0:50]
        >>> p = m[50:100]
        >>> q=m.insert(0, 200)
        >>> r=m.insert(50, 100)
        >>> s=m.insert(100, 300)
        >>> t=m.insert(45, 100)
        Traceback (most recent call last):
        ...
        IndexError: Slice overlaps prior slice of same file.
        >>> m.flush()
        >>> len(m)
        700
        >>> m.close()
        """
        self._chkIndex(offset, isSlice=1)
        self._chkOverlaps(offset, offset)
        if buffer is None:
            mem = memory.new_memory(size)
            mem = memory.writeable_buffer(mem)
        elif size is None or len(buffer) == size:
            mem = buffer
            size = len(buffer)
        else:
            raise ValueError("buffer and size don't match.")
        obj = MemmapSlice(mem, dirty=1)
        self._slices.append((offset, offset, obj))
        return obj