python_buffer.pxd 4.64 KB
Newer Older
1
# Please see the Python header files (object.h/abstract.h) for docs
Stefan Behnel's avatar
Stefan Behnel committed
2

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
cdef extern from "Python.h":

    cdef enum:
        PyBUF_SIMPLE,
        PyBUF_WRITABLE,
        PyBUF_WRITEABLE, # backwards compatability
        PyBUF_FORMAT,
        PyBUF_ND,
        PyBUF_STRIDES,
        PyBUF_C_CONTIGUOUS,
        PyBUF_F_CONTIGUOUS,
        PyBUF_ANY_CONTIGUOUS,
        PyBUF_INDIRECT,
        PyBUF_CONTIG,
        PyBUF_CONTIG_RO,
        PyBUF_STRIDED,
        PyBUF_STRIDED_RO,
        PyBUF_RECORDS,
        PyBUF_RECORDS_RO,
        PyBUF_FULL,
        PyBUF_FULL_RO,
        PyBUF_READ,
        PyBUF_WRITE,
        PyBUF_SHADOW

Stefan Behnel's avatar
Stefan Behnel committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    bint PyObject_CheckBuffer(object obj)
    # Return 1 if obj supports the buffer interface otherwise 0.

    int PyObject_GetBuffer(object obj, Py_buffer *view, int flags) except -1
    # Export obj into a Py_buffer, view. These arguments must never be
    # NULL. The flags argument is a bit field indicating what kind of
    # buffer the caller is prepared to deal with and therefore what
    # kind of buffer the exporter is allowed to return. The buffer
    # interface allows for complicated memory sharing possibilities,
    # but some caller may not be able to handle all the complexity but
    # may want to see if the exporter will let them take a simpler
    # view to its memory.

    # Some exporters may not be able to share memory in every possible
    # way and may need to raise errors to signal to some consumers
    # that something is just not possible. These errors should be a
    # BufferError unless there is another error that is actually
    # causing the problem. The exporter can use flags information to
    # simplify how much of the Py_buffer structure is filled in with
    # non-default values and/or raise an error if the object can’t
    # support a simpler view of its memory.

    # 0 is returned on success and -1 on error.

    void PyBuffer_Release(object obj, object view)
    # Release the buffer view over obj. This should be called when the
    # buffer is no longer being used as it may free memory from it.

56
    void* PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices)
Stefan Behnel's avatar
Stefan Behnel committed
57 58
    # ??

59
    int PyBuffer_SizeFromFormat(char *) # actually const char
Stefan Behnel's avatar
Stefan Behnel committed
60 61 62
    # Return the implied ~Py_buffer.itemsize from the struct-stype
    # ~Py_buffer.format

63
    int PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort)
Stefan Behnel's avatar
Stefan Behnel committed
64 65
    # ??

66
    int PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort)
Stefan Behnel's avatar
Stefan Behnel committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    # ??

    int PyObject_CopyToObject(object obj, void *buf, Py_ssize_t len, char fortran) except -1
    # Copy len bytes of data pointed to by the contiguous chunk of
    # memory pointed to by buf into the buffer exported by obj. The
    # buffer must of course be writable. Return 0 on success and
    # return -1 and raise an error on failure. If the object does not
    # have a writable buffer, then an error is raised. If fortran is
    # 'F', then if the object is multi-dimensional, then the data will
    # be copied into the array in Fortran-style (first dimension
    # varies the fastest). If fortran is 'C', then the data will be
    # copied into the array in C-style (last dimension varies the
    # fastest). If fortran is 'A', then it does not matter and the
    # copy will be made in whatever way is more efficient.

82
    int PyObject_CopyData(object dest, object src)
Stefan Behnel's avatar
Stefan Behnel committed
83 84 85 86 87 88 89
    # Copy the data from the src buffer to the buffer of destination

    bint PyBuffer_IsContiguous(Py_buffer *view, char fort)
    # Return 1 if the memory defined by the view is C-style (fortran
    # is 'C') or Fortran-style (fortran is 'F') contiguous or either
    # one (fortran is 'A'). Return 0 otherwise.

90 91 92 93 94
    void PyBuffer_FillContiguousStrides(int ndims, 
                                        Py_ssize_t *shape, 
                                        Py_ssize_t *strides,
                                        int itemsize,
                                        char fort)
Stefan Behnel's avatar
Stefan Behnel committed
95 96 97 98
    # Fill the strides array with byte-strides of a contiguous
    # (Fortran-style if fort is 'F' or C-style otherwise) array of the
    # given shape with the given number of bytes per element.

99 100
    int PyBuffer_FillInfo(Py_buffer *view, void *buf,
                          Py_ssize_t len, int readonly,
Stefan Behnel's avatar
Stefan Behnel committed
101 102 103 104 105
                          int flags) except -1
    # Fill in a buffer-info structure, view, correctly for an exporter
    # that can only share a contiguous chunk of memory of “unsigned
    # bytes” of the given length. Return 0 on success and -1 (with
    # raising an error) on error.
106

Stefan Behnel's avatar
Stefan Behnel committed
107 108 109
    object PyObject_Format(object obj, object format_spec)
    # Takes an arbitrary object and returns the result of calling
    # obj.__format__(format_spec).