blob: ef4e4ea606695c4e7a667347e921beb6301e3330 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
Antoine Pitroua0b68732010-09-28 21:52:30 +00003Old buffer API
4--------------
5
6.. deprecated:: 3.0
7
8These functions were part of the "old buffer protocol" API in Python 2.
9In Python 3, these functions are still exposed for ease of porting code.
10They act as a compatibility wrapper around the :ref:`new buffer API
11<bufferobjects>`, but they don't give you control over the lifetime of
12the resources acquired when a buffer is exported.
13
14Therefore, it is recommended that you call :cfunc:`PyObject_GetBuffer`
15(or the ``y*`` or ``w*`` :ref:`format codes <arg-parsing>` with the
16:cfunc:`PyArg_ParseTuple` family of functions) to get a buffer view over
17an object, and :cfunc:`PyBuffer_Release` when the buffer view can be released.
18
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20Buffer Protocol
21===============
22
23
24.. cfunction:: int PyObject_AsCharBuffer(PyObject *obj, const char **buffer, Py_ssize_t *buffer_len)
25
Christian Heimesc3f30c42008-02-22 16:37:40 +000026 Returns a pointer to a read-only memory location usable as character-based
Georg Brandl54a3faa2008-01-20 09:30:57 +000027 input. The *obj* argument must support the single-segment character buffer
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000028 interface. On success, returns ``0``, sets *buffer* to the memory location
29 and *buffer_len* to the buffer length. Returns ``-1`` and sets a
30 :exc:`TypeError` on error.
31
Georg Brandl54a3faa2008-01-20 09:30:57 +000032
33.. cfunction:: int PyObject_AsReadBuffer(PyObject *obj, const void **buffer, Py_ssize_t *buffer_len)
34
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000035 Returns a pointer to a read-only memory location containing arbitrary data.
36 The *obj* argument must support the single-segment readable buffer
37 interface. On success, returns ``0``, sets *buffer* to the memory location
38 and *buffer_len* to the buffer length. Returns ``-1`` and sets a
39 :exc:`TypeError` on error.
40
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
42.. cfunction:: int PyObject_CheckReadBuffer(PyObject *o)
43
44 Returns ``1`` if *o* supports the single-segment readable buffer interface.
45 Otherwise returns ``0``.
46
47
48.. cfunction:: int PyObject_AsWriteBuffer(PyObject *obj, void **buffer, Py_ssize_t *buffer_len)
49
50 Returns a pointer to a writable memory location. The *obj* argument must
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000051 support the single-segment, character buffer interface. On success,
52 returns ``0``, sets *buffer* to the memory location and *buffer_len* to the
53 buffer length. Returns ``-1`` and sets a :exc:`TypeError` on error.
54