Since version 1.5, C Python API define a buffer interface which allow an object to expose its internal data as a set of memory segments. This interface has been widely used by C coded extension but was mostly inaccessible from Python.
The mem package contain various tools to handle objects which define a single segment buffer interface: the object storage memory is contained into a contigous memory area.
Download mem 0.3 (24 Ko)This module provide various low level functions to access objects raw memory, with the single-segment buffer API. It defines the following functions:
Return the lowest index in dest where pattern is found, such as the pattern is contained in the range [start, end]. Optional arguments start and end are interpreted as in the slice notation. The third argument step can be used to specify alignment: (index-start)=k*step with k a natural number. Raise a ValueError if pattern is not found.
Like index but return the highest index available.
Byte per byte comparison of the memory areas a and b. The objects a and b must support the buffer API. Return -1 if a<b, 1 if a>b and 0 if a==b.
Copy src buffer into dest buffer at start location. An IndexError is raised if src is too long for dest buffer.
Return the length in byte of the buffer.
Return the lowest index where a and b have a different byte. Return -1 if the two buffers have all bytes in common.
This equivalent to diff but do the comparizon from right to left and return the hightest index. The returned index corresponds to the buffer a, this may be important if the two buffers haven't the same length.
Return the lower index where x and y have a common byte. Return -1 if the two buffers have no bytes in common.
This equivalent to match but do the comparizon from right to left and return the hightest index. The returned index corresponds to the buffer a, this may be important if the two buffers haven't the same length.
The chunk module define a new object type which can be used to allocate raw segments of memory. Theses objects support the buffer API.
Create a new chunk object by joining the buffers of the arguments list.
chunks objects are exposed as a mutable sequence of length one strings. They support all the methods required by the sequence protocol: len(), slicing, concatenation, repetition, iteration, indexing...
chunks objects support also the pickle protocol.
The following method are defined by chunk objects:
Resize the memory segment to n bytes. If n is greater than the current segment size, the extra bytes are fill with pattern.
This module define a new kind of objects which can be used to keep reference to a slice of raw memory from objects which define the buffer interface (strings, chunks, mmap ...). This object define itself the single-segment buffer API, so that it can be used instead of the target object on any operation which need an object with the buffer API.
return a new memslice object. The base argument must be filled with an object which define the the single-segment buffer API. start and stop argument are interpreted as in slice notation. They provide offset values in bytes.
The object define the following attributes:
return the target object. Note that the equality: x=SubBuffer(b); x.base==b isn't guaranteed, some optimizations may occur in future version, for sample if the target object b is a SubBuffer itself.
the start of the subbuffer
the end of the subbuffer
The object also support the len(s) method.These method return the length of the accessible buffer. This is not necessairely equal to s.stop-s.length.
memslice object also handle slicing. This operation return a new memslice object.
memslice objects support also the pickle protocol, the base object must then support pickling itself.
This module is a rewrite of Python buffer() object which can be usable with relocatable and/or writable buffer segments. Another difference is that it use the common (start,stop) convention, instead of a more specific (start,length).
The module defines an array like adaptator to access a string or any object which define the buffer interface. The wrapped object must also define a .resize(n) method if the emulated sequence need to be able to change it's size. An array is a sequence of monotonic data values.
Return a new array object. format must be a string as struct module format specifier, for now only scalar values are allowed. buffer is the wrapped object, it defaults to a memory chunk.
StrArray object support all the sequence protocol except the non-inplace form of the * and + operators. Note also that some other parts of the protocol are dependent of the wrapped objects: for sample you can't use mutable methods of the sequence protocol on a read-only buffer. Slicing behavior is also dependent of the wrapped object: you will get slice-by-copy on most buffer(str,chunks...) but some will provide slicing-by-view(memslice).
Create a new StrArray sublcass which wrap a memory chunk. If value is provided it must be a sequence which will be used to initialize the array.
Array object add support for the non-inplace version of * and +.
This module is mostly a demo.
This class provide a file like object to access a string or any object which define the buffer interface. The wrapped object must also define a .resize(n) method if the emulated file need to be able to change it's size.
Return a new StrIO object. data is the wrapped buffer, it default to a memory chunk.
Create a new StrIO object which wrap a memory chunk. If value is provided it will be copied into the memory chunk.
This module is mostly a demo.