Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
Antoine Pitrou | f7ba2fa | 2010-09-28 23:39:41 +0000 | [diff] [blame] | 3 | Old Buffer Protocol |
| 4 | ------------------- |
Antoine Pitrou | a0b6873 | 2010-09-28 21:52:30 +0000 | [diff] [blame] | 5 | |
| 6 | .. deprecated:: 3.0 |
| 7 | |
| 8 | These functions were part of the "old buffer protocol" API in Python 2. |
Antoine Pitrou | f7ba2fa | 2010-09-28 23:39:41 +0000 | [diff] [blame] | 9 | In Python 3, this protocol doesn't exist anymore but the functions are still |
| 10 | exposed to ease porting 2.x code. They act as a compatibility wrapper |
| 11 | around the :ref:`new buffer protocol <bufferobjects>`, but they don't give |
| 12 | you control over the lifetime of the resources acquired when a buffer is |
| 13 | exported. |
Antoine Pitrou | a0b6873 | 2010-09-28 21:52:30 +0000 | [diff] [blame] | 14 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | Therefore, it is recommended that you call :c:func:`PyObject_GetBuffer` |
Antoine Pitrou | a0b6873 | 2010-09-28 21:52:30 +0000 | [diff] [blame] | 16 | (or the ``y*`` or ``w*`` :ref:`format codes <arg-parsing>` with the |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 17 | :c:func:`PyArg_ParseTuple` family of functions) to get a buffer view over |
| 18 | an object, and :c:func:`PyBuffer_Release` when the buffer view can be released. |
Antoine Pitrou | a0b6873 | 2010-09-28 21:52:30 +0000 | [diff] [blame] | 19 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | .. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 22 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 23 | Returns a pointer to a read-only memory location usable as character-based |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 24 | input. The *obj* argument must support the single-segment character buffer |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 25 | interface. On success, returns ``0``, sets *buffer* to the memory location |
| 26 | and *buffer_len* to the buffer length. Returns ``-1`` and sets a |
| 27 | :exc:`TypeError` on error. |
| 28 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 29 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 30 | .. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 31 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 32 | Returns a pointer to a read-only memory location containing arbitrary data. |
| 33 | The *obj* argument must support the single-segment readable buffer |
| 34 | interface. On success, returns ``0``, sets *buffer* to the memory location |
| 35 | and *buffer_len* to the buffer length. Returns ``-1`` and sets a |
| 36 | :exc:`TypeError` on error. |
| 37 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 39 | .. c:function:: int PyObject_CheckReadBuffer(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
| 41 | Returns ``1`` if *o* supports the single-segment readable buffer interface. |
| 42 | Otherwise returns ``0``. |
| 43 | |
| 44 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 45 | .. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 46 | |
| 47 | Returns a pointer to a writable memory location. The *obj* argument must |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 48 | support the single-segment, character buffer interface. On success, |
| 49 | returns ``0``, sets *buffer* to the memory location and *buffer_len* to the |
| 50 | buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error. |
| 51 | |