| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
 | 2 |  | 
 | 3 | .. _bytearrayobjects: | 
 | 4 |  | 
 | 5 | Byte Array Objects | 
 | 6 | ------------------ | 
 | 7 |  | 
 | 8 | .. index:: object: bytearray | 
 | 9 |  | 
 | 10 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 11 | .. c:type:: PyByteArrayObject | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 12 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 13 |    This subtype of :c:type:`PyObject` represents a Python bytearray object. | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 14 |  | 
 | 15 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | .. c:var:: PyTypeObject PyByteArray_Type | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 17 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 |    This instance of :c:type:`PyTypeObject` represents the Python bytearray type; | 
| Georg Brandl | 2aff335 | 2010-10-17 10:59:41 +0000 | [diff] [blame] | 19 |    it is the same object as :class:`bytearray` in the Python layer. | 
 | 20 |  | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 21 |  | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 22 | Type check macros | 
 | 23 | ^^^^^^^^^^^^^^^^^ | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 24 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:function:: int PyByteArray_Check(PyObject *o) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 26 |  | 
 | 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | .. c:function:: int PyByteArray_CheckExact(PyObject *o) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 32 |  | 
 | 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 Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 37 | Direct API functions | 
 | 38 | ^^^^^^^^^^^^^^^^^^^^ | 
 | 39 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | .. c:function:: PyObject* PyByteArray_FromObject(PyObject *o) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 41 |  | 
 | 42 |    Return a new bytearray object from any object, *o*, that implements the | 
| Ezio Melotti | c228e96 | 2013-05-04 18:06:34 +0300 | [diff] [blame] | 43 |    :ref:`buffer protocol <bufferobjects>`. | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 44 |  | 
| Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 45 |    .. XXX expand about the buffer protocol, at least somewhere | 
 | 46 |  | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 47 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 48 | .. c:function:: PyObject* PyByteArray_FromStringAndSize(const char *string, Py_ssize_t len) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 49 |  | 
| Georg Brandl | f08a9dd | 2008-06-10 16:57:31 +0000 | [diff] [blame] | 50 |    Create a new bytearray object from *string* and its length, *len*.  On | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 51 |    failure, *NULL* is returned. | 
 | 52 |  | 
 | 53 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 54 | .. c:function:: PyObject* PyByteArray_Concat(PyObject *a, PyObject *b) | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 55 |  | 
 | 56 |    Concat bytearrays *a* and *b* and return a new bytearray with the result. | 
 | 57 |  | 
 | 58 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:function:: Py_ssize_t PyByteArray_Size(PyObject *bytearray) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 60 |  | 
 | 61 |    Return the size of *bytearray* after checking for a *NULL* pointer. | 
 | 62 |  | 
 | 63 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 64 | .. c:function:: char* PyByteArray_AsString(PyObject *bytearray) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 65 |  | 
 | 66 |    Return the contents of *bytearray* as a char array after checking for a | 
 | 67 |    *NULL* pointer. | 
 | 68 |  | 
 | 69 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: int PyByteArray_Resize(PyObject *bytearray, Py_ssize_t len) | 
| Benjamin Peterson | fbeb6b6 | 2008-05-26 16:04:49 +0000 | [diff] [blame] | 71 |  | 
 | 72 |    Resize the internal buffer of *bytearray* to *len*. | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 73 |  | 
 | 74 | Macros | 
 | 75 | ^^^^^^ | 
 | 76 |  | 
 | 77 | These macros trade safety for speed and they don't check pointers. | 
 | 78 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 79 | .. c:function:: char* PyByteArray_AS_STRING(PyObject *bytearray) | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 80 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 81 |    Macro version of :c:func:`PyByteArray_AsString`. | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 82 |  | 
 | 83 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 84 | .. c:function:: Py_ssize_t PyByteArray_GET_SIZE(PyObject *bytearray) | 
| Ezio Melotti | bb43a05 | 2009-09-20 07:19:57 +0000 | [diff] [blame] | 85 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 86 |    Macro version of :c:func:`PyByteArray_Size`. |