1
.. include:: common.txt
5
:class:`pygame.BufferProxy`
6
===========================
8
.. currentmodule:: pygame
10
.. class:: BufferProxy
12
| :sl:`pygame object to export a surface buffer through an array protocol`
13
| :sg:`BufferProxy(<parent>) -> BufferProxy`
15
:class:`BufferProxy` is a Pygame support type, designed as the return value
16
of the :meth:`Surface.get_buffer` and :meth:`Surface.get_view` methods.
17
For all Python versions a :class:`BufferProxy` object exports a C struct
18
and Python level array interface on behalf of its parent object's buffer.
19
For CPython 2.6 and later a new buffer interface is also exported.
20
In Pygame, :class:`BufferProxy` is key to implementing the
21
:mod:`pygame.surfarray` module.
23
:class:`BufferProxy` instances can be created directly from Python code,
24
either for a parent that exports an interface, or from a Python ``dict``
25
describing an object's buffer layout. The dict entries are based on the
26
Python level array interface mapping. The following keys are recognized:
29
The length of each array dimension as a tuple of integers. The
30
length of the tuple is the number of dimensions in the array.
32
``"typestr"`` : string
33
The array element type as a length 3 string. The first character
34
gives byteorder, '<' for little-endian, '>' for big-endian, and
35
'\|' for not applicable. The second character is the element type,
36
'i' for signed integer, 'u' for unsigned integer, 'f' for floating
37
point, and 'V' for an chunk of bytes. The third character gives the
38
bytesize of the element, from '1' to '9' bytes. So, for example,
39
"<u4" is an unsigned 4 byte little-endian integer, such as a
40
32 bit pixel on a PC, while "\|V3" would represent a 24 bit pixel,
41
which has no integer equivalent.
44
The physical buffer start address and a read-only flag as a length
45
2 tuple. The address is an integer value, while the read-only flag
46
is a bool—``False`` for writable, ``True`` for read-only.
48
``"strides"`` : tuple : (optional)
49
Array stride information as a tuple of integers. It is required
50
only of non C-contiguous arrays. The tuple length must match
53
``"parent"`` : object : (optional)
54
The exporting object. It can be used to keep the parent object
55
alive while its buffer is visible.
57
``"before"`` : callable : (optional)
58
Callback invoked when the :class:`BufferProxy` instance
59
exports the buffer. The callback is given one argument, the
60
``"parent"`` object if given, otherwise ``None``.
61
The callback is useful for setting a lock on the parent.
63
``"after"`` : callable : (optional)
64
Callback invoked when an exported buffer is released.
65
The callback is passed on argument, the ``"parent"`` object if given,
66
otherwise None. The callback is useful for releasing a lock on the
69
The BufferProxy class supports subclassing, instance variables, and weak
72
New in Pygame 1.8.0, extended in Pygame 1.9.2
76
| :sl:`Return wrapped exporting object.`
77
| :sg:`parent -> Surface`
78
| :sg:`parent -> <parent>`
80
The :class:`Surface` which returned the :class:`BufferProxy` object or
81
the object passed to a :class:`BufferProxy` call.
85
| :sl:`The size, in bytes, of the exported buffer.`
88
The number of valid bytes of data exported. For discontinuous data,
89
that is data which is not a single block of memory, the bytes within
90
the gaps are excluded from the count. This property is equivalent to
91
the ``Py_buffer`` C struct ``len`` field.
95
| :sl:`A copy of the exported buffer as a single block of bytes.`
98
The buffer data as a ``str``/``bytes`` object.
99
Any gaps in the exported data are removed.
103
| :sl:`Write raw bytes to object buffer.`
104
| :sg:`write(buffer, offset=0)`
106
Overwrite bytes in the parent object's data. The data must be C or F
107
contiguous, otherwise a ValueError is raised. Argument `buffer` is a
108
``str``/``bytes`` object. An optional offset gives a
109
start position, in bytes, within the buffer where overwriting begins.
110
If the offset is negative or greater that or equal to the buffer proxy's
111
:attr:`length` value, an ``IndexException`` is raised.
112
If ``len(buffer) > proxy.length + offset``, a ``ValueError`` is raised.