Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1 | |
| 2 | /* Memory object interface */ |
| 3 | |
| 4 | #ifndef Py_MEMORYOBJECT_H |
| 5 | #define Py_MEMORYOBJECT_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 10 | typedef struct { |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 11 | PyObject_HEAD |
| 12 | PyObject *base; |
| 13 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 14 | } PyMemoryViewObject; |
| 15 | |
| 16 | |
| 17 | PyAPI_DATA(PyTypeObject) PyMemoryView_Type; |
| 18 | |
| 19 | #define PyMemory_Check(op) (Py_Type(op) == &PyMemoryView_Type) |
| 20 | #define PyMemoryView(op) (((PyMemoryViewObject *)(op))->view) |
| 21 | |
| 22 | #define Py_END_OF_MEMORY (-1) |
| 23 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 24 | PyAPI_FUNC(PyObject *) PyMemoryView_GetContiguous(PyObject *base, |
| 25 | int buffertype, |
| 26 | char fort); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 27 | |
| 28 | /* Return a contiguous chunk of memory representing the buffer |
| 29 | from an object in a memory view object. If a copy is made then the |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 30 | base object for the memory view will be a *new* bytes object. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 31 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 32 | Otherwise, the base-object will be the object itself and no |
| 33 | data-copying will be done. |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 34 | |
Travis E. Oliphant | fe9bed0 | 2007-10-12 23:27:53 +0000 | [diff] [blame] | 35 | The buffertype argument can be PyBUF_READ, PyBUF_WRITE, |
| 36 | PyBUF_SHADOW to determine whether the returned buffer |
| 37 | should be READONLY, WRITABLE, or set to update the |
| 38 | original buffer if a copy must be made. If buffertype is |
| 39 | PyBUF_WRITE and the buffer is not contiguous an error will |
| 40 | be raised. In this circumstance, the user can use |
| 41 | PyBUF_SHADOW to ensure that a a writable temporary |
| 42 | contiguous buffer is returned. The contents of this |
| 43 | contiguous buffer will be copied back into the original |
| 44 | object after the memoryview object is deleted as long as |
| 45 | the original object is writable and allows setting an |
| 46 | exclusive write lock. If this is not allowed by the |
| 47 | original object, then a BufferError is raised. |
| 48 | |
| 49 | If the object is multi-dimensional and if fortran is 'F', |
| 50 | the first dimension of the underlying array will vary the |
| 51 | fastest in the buffer. If fortran is 'C', then the last |
| 52 | dimension will vary the fastest (C-style contiguous). If |
| 53 | fortran is 'A', then it does not matter and you will get |
| 54 | whatever the object decides is more efficient. |
| 55 | |
| 56 | A new reference is returned that must be DECREF'd when finished. |
| 57 | */ |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 58 | |
| 59 | PyAPI_FUNC(PyObject *) PyMemoryView_FromObject(PyObject *base); |
| 60 | |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 61 | PyAPI_FUNC(PyObject *) PyMemoryView_FromMemory(Py_buffer *info); |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 62 | /* create new if bufptr is NULL |
| 63 | will be a new bytesobject in base */ |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | } |
| 67 | #endif |
| 68 | #endif /* !Py_MEMORYOBJECT_H */ |