blob: c5228c6f43838732ea7ffbf31a9f021a57d103fe [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _abstract-buffer:
4
Antoine Pitrou789be0c2009-04-02 21:18:34 +00005
6Old Buffer Protocol
7===================
8
9This section describes the legacy buffer protocol, which has been introduced
10in Python 1.6. It is still supported but deprecated in the Python 2.x series.
Ezio Melotti510ff542012-05-03 19:21:40 +030011Python 3 introduces a new buffer protocol which fixes weaknesses and
Jeroen Ruigrok van der Werven27d51f12009-04-25 20:43:30 +000012shortcomings of the protocol, and has been backported to Python 2.6. See
13:ref:`bufferobjects` for more information.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:function:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
Georg Brandlf6842722008-01-19 22:08:21 +000017
Georg Brandl907a7202008-02-22 12:31:45 +000018 Returns a pointer to a read-only memory location usable as character-based
Georg Brandlf6842722008-01-19 22:08:21 +000019 input. The *obj* argument must support the single-segment character buffer
Jeroen Ruigrok van der Werven27d51f12009-04-25 20:43:30 +000020 interface. On success, returns ``0``, sets *buffer* to the memory location
21 and *buffer_len* to the buffer length. Returns ``-1`` and sets a
22 :exc:`TypeError` on error.
Georg Brandlf6842722008-01-19 22:08:21 +000023
24 .. versionadded:: 1.6
25
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000026 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010027 This function used an :c:type:`int *` type for *buffer_len*. This might
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000028 require changes in your code for properly supporting 64-bit systems.
29
Georg Brandlf6842722008-01-19 22:08:21 +000030
Sandro Tosi98ed08f2012-01-14 16:42:02 +010031.. c:function:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
Georg Brandlf6842722008-01-19 22:08:21 +000032
Jeroen Ruigrok van der Werven27d51f12009-04-25 20:43:30 +000033 Returns a pointer to a read-only memory location containing arbitrary data.
34 The *obj* argument must support the single-segment readable buffer
35 interface. On success, returns ``0``, sets *buffer* to the memory location
36 and *buffer_len* to the buffer length. Returns ``-1`` and sets a
37 :exc:`TypeError` on error.
Georg Brandlf6842722008-01-19 22:08:21 +000038
39 .. versionadded:: 1.6
40
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000041 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010042 This function used an :c:type:`int *` type for *buffer_len*. This might
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000043 require changes in your code for properly supporting 64-bit systems.
44
Georg Brandlf6842722008-01-19 22:08:21 +000045
Sandro Tosi98ed08f2012-01-14 16:42:02 +010046.. c:function:: int PyObject_CheckReadBuffer(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000047
48 Returns ``1`` if *o* supports the single-segment readable buffer interface.
49 Otherwise returns ``0``.
50
51 .. versionadded:: 2.2
52
53
Sandro Tosi98ed08f2012-01-14 16:42:02 +010054.. c:function:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
Georg Brandlf6842722008-01-19 22:08:21 +000055
56 Returns a pointer to a writeable memory location. The *obj* argument must
Jeroen Ruigrok van der Werven27d51f12009-04-25 20:43:30 +000057 support the single-segment, character buffer interface. On success,
58 returns ``0``, sets *buffer* to the memory location and *buffer_len* to the
59 buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
Georg Brandlf6842722008-01-19 22:08:21 +000060
61 .. versionadded:: 1.6
62
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000063 .. versionchanged:: 2.5
Sandro Tosi98ed08f2012-01-14 16:42:02 +010064 This function used an :c:type:`int *` type for *buffer_len*. This might
Jeroen Ruigrok van der Werven9594c2c2009-04-25 20:44:58 +000065 require changes in your code for properly supporting 64-bit systems.
66