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.
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(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.
Parameters:
Slice!(kind, packs, T*) slice output ndslice
Py_buffer view Py_buffer requested
Returns:
one of the input_buffer_* PythonBufferErrorCode on failure and success otherwise.
Examples:
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.
Parameters:
Slice!(kind, packs, T*) 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:
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)
        assert(0);
    Py_buffer view = void;

    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;
}
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)

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.