memoryobject.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Memory view object. In Python this is available as "memoryview". */
  2. #ifndef Py_MEMORYOBJECT_H
  3. #define Py_MEMORYOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. PyAPI_DATA(PyTypeObject) PyMemoryView_Type;
  8. #define PyMemoryView_Check(op) (Py_TYPE(op) == &PyMemoryView_Type)
  9. /* Get a pointer to the underlying Py_buffer of a memoryview object. */
  10. #define PyMemoryView_GET_BUFFER(op) (&((PyMemoryViewObject *)(op))->view)
  11. /* Get a pointer to the PyObject from which originates a memoryview object. */
  12. #define PyMemoryView_GET_BASE(op) (((PyMemoryViewObject *)(op))->view.obj)
  13. PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base,
  14. int buffertype,
  15. char fort);
  16. /* Return a contiguous chunk of memory representing the buffer
  17. from an object in a memory view object. If a copy is made then the
  18. base object for the memory view will be a *new* bytes object.
  19. Otherwise, the base-object will be the object itself and no
  20. data-copying will be done.
  21. The buffertype argument can be PyBUF_READ, PyBUF_WRITE,
  22. PyBUF_SHADOW to determine whether the returned buffer
  23. should be READONLY, WRITABLE, or set to update the
  24. original buffer if a copy must be made. If buffertype is
  25. PyBUF_WRITE and the buffer is not contiguous an error will
  26. be raised. In this circumstance, the user can use
  27. PyBUF_SHADOW to ensure that a writable temporary
  28. contiguous buffer is returned. The contents of this
  29. contiguous buffer will be copied back into the original
  30. object after the memoryview object is deleted as long as
  31. the original object is writable and allows setting an
  32. exclusive write lock. If this is not allowed by the
  33. original object, then a BufferError is raised.
  34. If the object is multi-dimensional and if fortran is 'F',
  35. the first dimension of the underlying array will vary the
  36. fastest in the buffer. If fortran is 'C', then the last
  37. dimension will vary the fastest (C-style contiguous). If
  38. fortran is 'A', then it does not matter and you will get
  39. whatever the object decides is more efficient.
  40. A new reference is returned that must be DECREF'd when finished.
  41. */
  42. PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base);
  43. PyAPI_FUNC(PyObject *) PyMemoryView_FromBuffer(Py_buffer *info);
  44. /* create new if bufptr is NULL
  45. will be a new bytesobject in base */
  46. /* The struct is declared here so that macros can work, but it shouldn't
  47. be considered public. Don't access those fields directly, use the macros
  48. and functions instead! */
  49. typedef struct {
  50. PyObject_HEAD
  51. PyObject *base;
  52. Py_buffer view;
  53. } PyMemoryViewObject;
  54. #ifdef __cplusplus
  55. }
  56. #endif
  57. #endif /* !Py_MEMORYOBJECT_H */