pybuffer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Public Py_buffer API */
  2. #ifndef Py_BUFFER_H
  3. #define Py_BUFFER_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030b0000
  8. /* === New Buffer API ============================================
  9. * Limited API and stable ABI since Python 3.11
  10. *
  11. * Py_buffer struct layout and size is now part of the stable abi3. The
  12. * struct layout and size must not be changed in any way, as it would
  13. * break the ABI.
  14. *
  15. */
  16. typedef struct {
  17. void *buf;
  18. PyObject *obj; /* owned reference */
  19. Py_ssize_t len;
  20. Py_ssize_t itemsize; /* This is Py_ssize_t so it can be
  21. pointed to by strides in simple case.*/
  22. int readonly;
  23. int ndim;
  24. char *format;
  25. Py_ssize_t *shape;
  26. Py_ssize_t *strides;
  27. Py_ssize_t *suboffsets;
  28. void *internal;
  29. } Py_buffer;
  30. typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);
  31. typedef void (*releasebufferproc)(PyObject *, Py_buffer *);
  32. /* Return 1 if the getbuffer function is available, otherwise return 0. */
  33. PyAPI_FUNC(int) PyObject_CheckBuffer(PyObject *obj);
  34. /* This is a C-API version of the getbuffer function call. It checks
  35. to make sure object has the required function pointer and issues the
  36. call.
  37. Returns -1 and raises an error on failure and returns 0 on success. */
  38. PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
  39. int flags);
  40. /* Get the memory area pointed to by the indices for the buffer given.
  41. Note that view->ndim is the assumed size of indices. */
  42. PyAPI_FUNC(void *) PyBuffer_GetPointer(const Py_buffer *view, const Py_ssize_t *indices);
  43. /* Return the implied itemsize of the data-format area from a
  44. struct-style description. */
  45. PyAPI_FUNC(Py_ssize_t) PyBuffer_SizeFromFormat(const char *format);
  46. /* Implementation in memoryobject.c */
  47. PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, const Py_buffer *view,
  48. Py_ssize_t len, char order);
  49. PyAPI_FUNC(int) PyBuffer_FromContiguous(const Py_buffer *view, const void *buf,
  50. Py_ssize_t len, char order);
  51. /* Copy len bytes of data from the contiguous chunk of memory
  52. pointed to by buf into the buffer exported by obj. Return
  53. 0 on success and return -1 and raise a PyBuffer_Error on
  54. error (i.e. the object does not have a buffer interface or
  55. it is not working).
  56. If fort is 'F', then if the object is multi-dimensional,
  57. then the data will be copied into the array in
  58. Fortran-style (first dimension varies the fastest). If
  59. fort is 'C', then the data will be copied into the array
  60. in C-style (last dimension varies the fastest). If fort
  61. is 'A', then it does not matter and the copy will be made
  62. in whatever way is more efficient. */
  63. PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
  64. /* Copy the data from the src buffer to the buffer of destination. */
  65. PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
  66. /*Fill the strides array with byte-strides of a contiguous
  67. (Fortran-style if fort is 'F' or C-style otherwise)
  68. array of the given shape with the given number of bytes
  69. per element. */
  70. PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
  71. Py_ssize_t *shape,
  72. Py_ssize_t *strides,
  73. int itemsize,
  74. char fort);
  75. /* Fills in a buffer-info structure correctly for an exporter
  76. that can only share a contiguous chunk of memory of
  77. "unsigned bytes" of the given length.
  78. Returns 0 on success and -1 (with raising an error) on error. */
  79. PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
  80. Py_ssize_t len, int readonly,
  81. int flags);
  82. /* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
  83. PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
  84. /* Maximum number of dimensions */
  85. #define PyBUF_MAX_NDIM 64
  86. /* Flags for getting buffers. Keep these in sync with inspect.BufferFlags. */
  87. #define PyBUF_SIMPLE 0
  88. #define PyBUF_WRITABLE 0x0001
  89. #ifndef Py_LIMITED_API
  90. /* we used to include an E, backwards compatible alias */
  91. #define PyBUF_WRITEABLE PyBUF_WRITABLE
  92. #endif
  93. #define PyBUF_FORMAT 0x0004
  94. #define PyBUF_ND 0x0008
  95. #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
  96. #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
  97. #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
  98. #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
  99. #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
  100. #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE)
  101. #define PyBUF_CONTIG_RO (PyBUF_ND)
  102. #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE)
  103. #define PyBUF_STRIDED_RO (PyBUF_STRIDES)
  104. #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT)
  105. #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT)
  106. #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT)
  107. #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT)
  108. #define PyBUF_READ 0x100
  109. #define PyBUF_WRITE 0x200
  110. #endif /* !Py_LIMITED_API || Py_LIMITED_API >= 3.11 */
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #endif /* Py_BUFFER_H */