Report a bug
If you spot a problem with this page, click here to create a Github issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


Utilities for Python Buffer Protocol.
Ilya Yaroshenko
enum int pythonBufferFlags(SliceKind kind, T);
Construct flags for PyObject_GetBuffer(). If T is not const or immutable then the flags requrie writable buffer. If slice kind is Contiguous  then the flags require c_contiguous buffer.
kindslice kind
Trecord type
flags for Py_buffer request.
nothrow @nogc @trusted PythonBufferErrorCode fromPythonBuffer(SliceKind kind, size_t[] packs, T)(ref Slice!(kind, packs, T*) slice, ref const Py_buffer view)
if (packs.length == 1 && (packs[0] <= PyBuf_max_ndim));
Fills the slice (structure) from the python view. The view should be created by PyObject_GetBuffer() that was called with pythonBufferFlags.
Slice!(kind, packs, T*) sliceoutput ndslice
Py_buffer viewPy_buffer requested
one of the input_buffer_* PythonBufferErrorCode on failure and success otherwise.
auto bar(ref const Py_buffer view)
    ContiguousMatrix!(const double) mat = void;
    if (auto error = mat.fromPythonBuffer(view))
        mat = mat.init; // has null pointer
    return mat;
nothrow @nogc @trusted PythonBufferErrorCode toPythonBuffer(SliceKind kind, size_t[] packs, T, size_t N)(Slice!(kind, packs, T*) slice, ref Py_buffer view, int flags, ref Structure!N structureBuffer)
if (packs == [N] && (N <= PyBuf_max_ndim));
Fills the python view (structure) from the slice.
Slice!(kind, packs, T*) sliceinput ndslice
Py_buffer viewoutput Py_buffer. Py_buffer.internal is initialized with null value, Py_buffer.obj is not initialized. Other Py_buffer fields are initialized accroding to the flags and slice.
int flagsrequester flags
Structure!N structureBufferSingle chunk of memory with the same alignment and size as Structure . The buffer is used to store shape and strides for the view.
one of the cannot_create_* PythonBufferErrorCode on failure and success otherwise.
Py_buffer bar(Slice!(Universal, [2], double*) slice)
    import core.stdc.stdlib;
    enum N = slice.N;

    auto structurePtr = cast(Structure!N*) Structure!N.sizeof.malloc;
    if (!structurePtr)
    Py_buffer view = void;

    if (auto error = slice.toPythonBuffer(view, PyBuf_records_ro, *structurePtr))
        view = view.init; // null buffer;
        assert(cast(sizediff_t*)&structurePtr.lengths == view.shape);
        assert(cast(sizediff_t*)&structurePtr.strides == view.strides);

    return view;
struct bufferinfo;

alias Py_buffer = bufferinfo;
void* buf;
void* obj;
sizediff_t len;
sizediff_t itemsize;
int readonly;
int ndim;
char* format;
sizediff_t* shape;
sizediff_t* strides;
sizediff_t* suboffsets;
void* internal;
enum PythonBufferErrorCode: int;
Error codes for ndslice - Py_buffer conversion.
enum int PyBuf_max_ndim;
enum int PyBuf_simple;
enum int PyBuf_writable;
enum int PyBuf_writeable;
enum int PyBuf_format;
enum int PyBuf_nd;
enum int PyBuf_strides;
enum int PyBuf_c_contiguous;
enum int PyBuf_f_contiguous;
enum int PyBuf_any_contiguous;
enum int PyBuf_indirect;
enum int PyBuf_contig;
enum int PyBuf_contig_ro;
enum int PyBuf_strided;
enum int PyBuf_strided_ro;
enum int PyBuf_records;
enum int PyBuf_records_ro;
template pythonBufferFormat(T)

enum auto pythonBufferFormat(T : short);

enum auto pythonBufferFormat(T : ushort);

enum auto pythonBufferFormat(T : int);

enum auto pythonBufferFormat(T : uint);

enum auto pythonBufferFormat(T : float);

enum auto pythonBufferFormat(T : double);

enum auto pythonBufferFormat(T : c_long_double);

enum auto pythonBufferFormat(T : long);

enum auto pythonBufferFormat(T : ulong);

enum auto pythonBufferFormat(T : ubyte);

enum auto pythonBufferFormat(T : byte);

enum auto pythonBufferFormat(T : char);

enum auto pythonBufferFormat(T : char*);

enum auto pythonBufferFormat(T : void*);

enum auto pythonBufferFormat(T : bool);

enum auto pythonBufferFormat(T : wchar*);

enum auto pythonBufferFormat(T : wchar);
Returns python format (type) string. For example, "O" for PyObject and "B" for ubyte.