toPythonBuffer

Fills the python view (structure) from the slice.

nothrow @nogc @trusted
toPythonBuffer
(
T
size_t N
SliceKind kind
)
(
Slice!(T*, N, kind) slice
,,
int flags
,)

Parameters

slice Slice!(T*, N, kind)

input ndslice

view Py_buffer

output Py_buffer. Py_buffer.internal is initialized with null value, Py_buffer.obj is not initialized. Other Py_buffer fields are initialized according to the flags and slice.

flags int

requester flags

structureBuffer Structure!N

Single chunk of memory with the same alignment and size as Structure. The buffer is used to store shape and strides for the view.

Return Value

one of the cannot_create_* PythonBufferErrorCode on failure and success otherwise.

Examples

import mir.ndslice.slice : Slice, Structure, Universal, Contiguous, SliceKind;
Py_buffer bar(SliceKind kind)(Slice!(double*, 2, kind) slice)
{
    import core.stdc.stdlib;
    enum N = 2;

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

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

    return view;
}

alias barUni = bar!Universal;
alias barCon = bar!Contiguous;

Meta