bytearray.rst 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
.. highlightlang:: c

.. _bytearrayobjects:

Byte Array Objects
------------------

.. index:: object: bytearray


11
.. c:type:: PyByteArrayObject
12

13
   This subtype of :c:type:`PyObject` represents a Python bytearray object.
14 15


16
.. c:var:: PyTypeObject PyByteArray_Type
17

18
   This instance of :c:type:`PyTypeObject` represents the Python bytearray type;
19 20
   it is the same object as :class:`bytearray` in the Python layer.

21

22 23
Type check macros
^^^^^^^^^^^^^^^^^
24

25
.. c:function:: int PyByteArray_Check(PyObject *o)
26 27 28 29 30

   Return true if the object *o* is a bytearray object or an instance of a
   subtype of the bytearray type.


31
.. c:function:: int PyByteArray_CheckExact(PyObject *o)
32 33 34 35 36

   Return true if the object *o* is a bytearray object, but not an instance of a
   subtype of the bytearray type.


37 38 39
Direct API functions
^^^^^^^^^^^^^^^^^^^^

40
.. c:function:: PyObject* PyByteArray_FromObject(PyObject *o)
41 42

   Return a new bytearray object from any object, *o*, that implements the
43
   :ref:`buffer protocol <bufferobjects>`.
44

Georg Brandl's avatar
Georg Brandl committed
45 46
   .. XXX expand about the buffer protocol, at least somewhere

47

48
.. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len)
49

Georg Brandl's avatar
Georg Brandl committed
50
   Create a new bytearray object from *string* and its length, *len*.  On
51 52 53
   failure, *NULL* is returned.


54
.. c:function:: PyObject* PyByteArray_Concat(PyObject *a, PyObject *b)
55

56
   Concat bytearrays *a* and *b* and return a new bytearray with the result.
57 58


59
.. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray)
60

61
   Return the size of *bytearray* after checking for a *NULL* pointer.
62 63


64
.. c:function:: char* PyByteArray_AsString(PyObject *bytearray)
65 66

   Return the contents of *bytearray* as a char array after checking for a
67 68
   *NULL* pointer.  The returned array always has an extra
   null byte appended.
69 70


71
.. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len)
72

73
   Resize the internal buffer of *bytearray* to *len*.
74

75 76
Macros
^^^^^^
77

78
These macros trade safety for speed and they don't check pointers.
79

80
.. c:function:: char* PyByteArray_AS_STRING(PyObject *bytearray)
81

82
   Macro version of :c:func:`PyByteArray_AsString`.
83 84


85
.. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray)
86

87
   Macro version of :c:func:`PyByteArray_Size`.