blob: a757b880592979fe8f2496f32b95868273a71b1a [file] [log] [blame]
Georg Brandl0c77a822008-06-10 16:37:50 +00001/* ByteArray object interface */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00002
Georg Brandl0c77a822008-06-10 16:37:50 +00003#ifndef Py_BYTEARRAYOBJECT_H
4#define Py_BYTEARRAYOBJECT_H
Christian Heimes2c9c7a52008-05-26 13:42:13 +00005#ifdef __cplusplus
6extern "C" {
7#endif
8
9#include <stdarg.h>
10
11/* Type PyByteArrayObject represents a mutable array of bytes.
12 * The Python API is that of a sequence;
13 * the bytes are mapped to ints in [0, 256).
14 * Bytes are not characters; they may be used to encode characters.
15 * The only way to go between bytes and str/unicode is via encoding
16 * and decoding.
17 * For the convenience of C programmers, the bytes type is considered
18 * to contain a char pointer, not an unsigned char pointer.
19 */
20
21/* Object layout */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000022#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000023typedef struct {
24 PyObject_VAR_HEAD
Benjamin Peterson9185a5a2013-10-05 16:28:04 -040025 Py_ssize_t ob_alloc; /* How many bytes allocated in ob_bytes */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +020026 char *ob_bytes; /* Physical backing buffer */
27 char *ob_start; /* Logical start inside ob_bytes */
Christian Heimes2c9c7a52008-05-26 13:42:13 +000028 /* XXX(nnorwitz): should ob_exports be Py_ssize_t? */
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +020029 int ob_exports; /* How many buffer exports */
Christian Heimes2c9c7a52008-05-26 13:42:13 +000030} PyByteArrayObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000031#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000032
33/* Type object */
34PyAPI_DATA(PyTypeObject) PyByteArray_Type;
35PyAPI_DATA(PyTypeObject) PyByteArrayIter_Type;
36
37/* Type check macros */
38#define PyByteArray_Check(self) PyObject_TypeCheck(self, &PyByteArray_Type)
39#define PyByteArray_CheckExact(self) (Py_TYPE(self) == &PyByteArray_Type)
40
41/* Direct API functions */
42PyAPI_FUNC(PyObject *) PyByteArray_FromObject(PyObject *);
43PyAPI_FUNC(PyObject *) PyByteArray_Concat(PyObject *, PyObject *);
44PyAPI_FUNC(PyObject *) PyByteArray_FromStringAndSize(const char *, Py_ssize_t);
45PyAPI_FUNC(Py_ssize_t) PyByteArray_Size(PyObject *);
46PyAPI_FUNC(char *) PyByteArray_AsString(PyObject *);
47PyAPI_FUNC(int) PyByteArray_Resize(PyObject *, Py_ssize_t);
48
49/* Macros, trading safety for speed */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000050#ifndef Py_LIMITED_API
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000051#define PyByteArray_AS_STRING(self) \
52 (assert(PyByteArray_Check(self)), \
Antoine Pitrou5df8a8a2013-10-05 21:12:18 +020053 Py_SIZE(self) ? ((PyByteArrayObject *)(self))->ob_start : _PyByteArray_empty_string)
54#define PyByteArray_GET_SIZE(self) (assert(PyByteArray_Check(self)), Py_SIZE(self))
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055
Antoine Pitrou2ff627a2010-01-17 16:15:29 +000056PyAPI_DATA(char) _PyByteArray_empty_string[];
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000057#endif
Antoine Pitroufc8d6f42010-01-17 12:38:54 +000058
Christian Heimes2c9c7a52008-05-26 13:42:13 +000059#ifdef __cplusplus
60}
61#endif
Georg Brandl0c77a822008-06-10 16:37:50 +000062#endif /* !Py_BYTEARRAYOBJECT_H */