blob: 41b6e3c71be53d560cbf8f8d306ad41912e9d47d [file] [log] [blame]
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +00001.. highlightlang:: c
2
3.. _bytearrayobjects:
4
5Byte Array Objects
6------------------
7
8.. index:: object: bytearray
9
10
Georg Brandl60203b42010-10-06 10:11:56 +000011.. c:type:: PyByteArrayObject
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000012
Georg Brandl60203b42010-10-06 10:11:56 +000013 This subtype of :c:type:`PyObject` represents a Python bytearray object.
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000014
15
Georg Brandl60203b42010-10-06 10:11:56 +000016.. c:var:: PyTypeObject PyByteArray_Type
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000017
Georg Brandl60203b42010-10-06 10:11:56 +000018 This instance of :c:type:`PyTypeObject` represents the Python bytearray type;
Georg Brandl2aff3352010-10-17 10:59:41 +000019 it is the same object as :class:`bytearray` in the Python layer.
20
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000021
Ezio Melottibb43a052009-09-20 07:19:57 +000022Type check macros
23^^^^^^^^^^^^^^^^^
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000024
Georg Brandl60203b42010-10-06 10:11:56 +000025.. c:function:: int PyByteArray_Check(PyObject *o)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000026
27 Return true if the object *o* is a bytearray object or an instance of a
28 subtype of the bytearray type.
29
30
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:function:: int PyByteArray_CheckExact(PyObject *o)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000032
33 Return true if the object *o* is a bytearray object, but not an instance of a
34 subtype of the bytearray type.
35
36
Ezio Melottibb43a052009-09-20 07:19:57 +000037Direct API functions
38^^^^^^^^^^^^^^^^^^^^
39
Georg Brandl60203b42010-10-06 10:11:56 +000040.. c:function:: PyObject* PyByteArray_FromObject(PyObject *o)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000041
42 Return a new bytearray object from any object, *o*, that implements the
Ezio Melottic228e962013-05-04 18:06:34 +030043 :ref:`buffer protocol <bufferobjects>`.
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000044
Georg Brandlf08a9dd2008-06-10 16:57:31 +000045 .. XXX expand about the buffer protocol, at least somewhere
46
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000047
Georg Brandl60203b42010-10-06 10:11:56 +000048.. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000049
Georg Brandlf08a9dd2008-06-10 16:57:31 +000050 Create a new bytearray object from *string* and its length, *len*. On
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000051 failure, *NULL* is returned.
52
53
Georg Brandl60203b42010-10-06 10:11:56 +000054.. c:function:: PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)
Ezio Melottibb43a052009-09-20 07:19:57 +000055
56 Concat bytearrays *a* and *b* and return a new bytearray with the result.
57
58
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000060
61 Return the size of *bytearray* after checking for a *NULL* pointer.
62
63
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: char* PyByteArray_AsString(PyObject *bytearray)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000065
66 Return the contents of *bytearray* as a char array after checking for a
R David Murray0a560a12015-05-13 20:31:53 -040067 *NULL* pointer. The returned array always has an extra
68 null byte appended.
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000069
70
Georg Brandl60203b42010-10-06 10:11:56 +000071.. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
Benjamin Petersonfbeb6b62008-05-26 16:04:49 +000072
73 Resize the internal buffer of *bytearray* to *len*.
Ezio Melottibb43a052009-09-20 07:19:57 +000074
75Macros
76^^^^^^
77
78These macros trade safety for speed and they don't check pointers.
79
Georg Brandl60203b42010-10-06 10:11:56 +000080.. c:function:: char* PyByteArray_AS_STRING(PyObject *bytearray)
Ezio Melottibb43a052009-09-20 07:19:57 +000081
Georg Brandl60203b42010-10-06 10:11:56 +000082 Macro version of :c:func:`PyByteArray_AsString`.
Ezio Melottibb43a052009-09-20 07:19:57 +000083
84
Georg Brandl60203b42010-10-06 10:11:56 +000085.. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
Ezio Melottibb43a052009-09-20 07:19:57 +000086
Georg Brandl60203b42010-10-06 10:11:56 +000087 Macro version of :c:func:`PyByteArray_Size`.