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.

mir.ndslice.connect.cpython

Utilities for Python Buffer Protocol.
License:
Authors:
Ilya Yaroshenko
enum int pythonBufferFlags(SliceKind kind, T);
Construct flags for PyObject_GetBuffer(). If T is not const or immutable then the flags require writable buffer. If slice kind is Contiguous  then the flags require c_contiguous buffer.
Parameters:
kind slice kind
T record type
Returns:
flags for Py_buffer request.
nothrow @nogc @trusted PythonBufferErrorCode fromPythonBuffer(T, size_t N, SliceKind kind)(ref Slice!(T*, N, kind) slice, ref const Py_buffer view)
if (N <= PyBuf_max_ndim);
Fills the slice (structure) from the python view. The view should be created by PyObject_GetBuffer() that was called with pythonBufferFlags.
Parameters:
Slice!(T*, N, kind) slice output ndslice
Py_buffer view Py_buffer requested
Returns:
one of the input_buffer_* PythonBufferErrorCode on failure and success otherwise.
Examples:
import mir.ndslice.slice: Slice;
auto bar(ref const Py_buffer view)
{
    Slice!(const(double)*, 2) mat;
    if (auto error = mat.fromPythonBuffer(view))
    {
        // has null pointer
    }
    return mat;
}
nothrow @nogc @trusted PythonBufferErrorCode toPythonBuffer(T, size_t N, SliceKind kind)(Slice!(T*, N, kind) slice, ref Py_buffer view, int flags, ref Structure!N structureBuffer)
if (N <= PyBuf_max_ndim);
Fills the python view (structure) from the slice.
Parameters:
Slice!(T*, N, kind) slice input ndslice
Py_buffer view 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.
int flags requester flags
Structure!N structureBuffer Single chunk of memory with the same alignment and size as Structure . The buffer is used to store shape and strides for the view.
Returns:
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;
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.
success
cannot_create_format_string
cannot_create_writable_buffer
cannot_create_f_contiguous_buffer
cannot_create_c_contiguous_buffer
cannot_create_any_contiguous_buffer
cannot_create_a_buffer_without_strides
input_buffer_ndim_mismatch
input_buffer_itemsize_mismatch
input_buffer_format_mismatch
input_buffer_strides_mismatch
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)
Returns python format (type) string. For example, "O" for PyObject and "B" for ubyte.