This pre-PEP proposes enhancing the buffer protocol in Python 3000 to implement the array interface (protocol).
This Wiki will serve as a place to develop the PEP until it is assigned a number and committed to the Python development tree.
This PEP proposes re-designing the buffer API (PyBufferProcs function pointers) to improve the way Python allows memory sharing in Python 3.0
In particular, it is proposed that the multiple-segment and character buffer portions of the buffer API are eliminated and additional function pointers are provided to allow sharing any multi-dimensional character of the memory and what the memory contains.
The buffer protocol allows different Python types to exchange a pointer to a sequence of internal buffers. This functionality is extremely useful for sharing large segments of memory between different high-level objects, but it's too limited and has issues.
- There is the little (never?) used "sequence-of-segments" option (bf_getsegcount)
- There is the apparently redundant character-buffer option (bf_getcharbuffer)
- There is no way for a consumer to tell the buffer-API-exporting object it is "finished" with its view of the memory and therefore no way for the expoerting object to be sure that it is safe to reallocate the pointer to the memory that it owns (the array object reallocating its memory after sharing it with the buffer object which held the original pointer led to the infamous buffer-object problem).
- Memory is just a pointer with a length. There is no way to describe what's "in" the memory (float, int, C-structure, etc.)
There is no shape information provided for the memory. But, several array-like Python types could make use of a standard way to describe the shape-interpretation of the memory (!wxPython, GTK, pyQT, CVXOPT, PyVox, Audio and Video Libraries, ctypes, NumPy)
- Get rid of the char-buffer and multiple-segment sections of the buffer-protocol.
- Add a new function to the protocol that should be called when the consumer object is "done" with the view.
- Add a new function to allow the protocol to describe what is in memory (unifying what is currently done now in struct and array)
- Add a new function to allow the protocol to share shape information
All that is needed is to create a Python "memory_view" object that can contain all the information needed and be returned when the buffer protocol is called --- when it is garbage-collected, the "bp_release_view" function is called on the exporting object.
This "memory_view" is essentially the old Numeric C-structure (including the fact that the data-format is described by another C-structure).
This object is what the buffer protocol should return.