blob: 39c241a2dcf5f7c6696b71dabfaa40054c8b29cd [file] [log] [blame]
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001
Serhiy Storchaka2ad93822020-12-03 12:46:16 +02002/* Bytes object interface */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003
Georg Brandl0c77a822008-06-10 16:37:50 +00004#ifndef Py_BYTESOBJECT_H
5#define Py_BYTESOBJECT_H
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00006#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include <stdarg.h>
11
Christian Heimes2c9c7a52008-05-26 13:42:13 +000012/*
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020013Type PyBytesObject represents a byte string. An extra zero byte is
Christian Heimes2c9c7a52008-05-26 13:42:13 +000014reserved at the end to ensure it is zero-terminated, but a size is
15present so strings with null bytes in them can be represented. This
16is an immutable object type.
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000017
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020018There are functions to create new bytes objects, to test
19an object for bytes-ness, and to get the
20byte string value. The latter function returns a null pointer
Christian Heimes2c9c7a52008-05-26 13:42:13 +000021if the object is not of the proper type.
22There is a variant that takes an explicit size as well as a
23variant that assumes a zero-terminated string. Note that none of the
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020024functions should be applied to NULL pointer.
Christian Heimes2c9c7a52008-05-26 13:42:13 +000025*/
26
Christian Heimes2c9c7a52008-05-26 13:42:13 +000027PyAPI_DATA(PyTypeObject) PyBytes_Type;
28PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000029
Christian Heimes2c9c7a52008-05-26 13:42:13 +000030#define PyBytes_Check(op) \
31 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
Dong-hee Nad905df72020-02-14 02:37:17 +090032#define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000033
Christian Heimes2c9c7a52008-05-26 13:42:13 +000034PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
35PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +000036PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000037PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020038 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000039PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020040 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000041PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
42PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
43PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
44PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
45PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000046PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020047 const char *, Py_ssize_t,
48 const char *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000049
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020050/* Provides access to the internal data buffer and size of a bytes object.
51 Passing NULL as len parameter will force the string buffer to be
52 0-terminated (passing a string with embedded NUL characters will
Christian Heimes2c9c7a52008-05-26 13:42:13 +000053 cause an exception). */
54PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020055 PyObject *obj, /* bytes object */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 char **s, /* pointer to buffer variable */
Serhiy Storchaka2ad93822020-12-03 12:46:16 +020057 Py_ssize_t *len /* pointer to length variable or NULL */
Christian Heimes2c9c7a52008-05-26 13:42:13 +000058 );
59
Victor Stinner00165072015-10-09 01:53:21 +020060#ifndef Py_LIMITED_API
Victor Stinner98921ae2020-02-12 23:54:31 +010061# define Py_CPYTHON_BYTESOBJECT_H
62# include "cpython/bytesobject.h"
63# undef Py_CPYTHON_BYTESOBJECT_H
64#endif
Victor Stinner00165072015-10-09 01:53:21 +020065
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000066#ifdef __cplusplus
67}
68#endif
Georg Brandl0c77a822008-06-10 16:37:50 +000069#endif /* !Py_BYTESOBJECT_H */