Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1 | |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 2 | /* Bytes object interface */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3 | |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 4 | #ifndef Py_BYTESOBJECT_H |
| 5 | #define Py_BYTESOBJECT_H |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
| 10 | #include <stdarg.h> |
| 11 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 12 | /* |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 13 | Type PyBytesObject represents a byte string. An extra zero byte is |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 14 | reserved at the end to ensure it is zero-terminated, but a size is |
| 15 | present so strings with null bytes in them can be represented. This |
| 16 | is an immutable object type. |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 17 | |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 18 | There are functions to create new bytes objects, to test |
| 19 | an object for bytes-ness, and to get the |
| 20 | byte string value. The latter function returns a null pointer |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 21 | if the object is not of the proper type. |
| 22 | There is a variant that takes an explicit size as well as a |
| 23 | variant that assumes a zero-terminated string. Note that none of the |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 24 | functions should be applied to NULL pointer. |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 27 | PyAPI_DATA(PyTypeObject) PyBytes_Type; |
| 28 | PyAPI_DATA(PyTypeObject) PyBytesIter_Type; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 29 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 30 | #define PyBytes_Check(op) \ |
| 31 | PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS) |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 32 | #define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 33 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 34 | PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t); |
| 35 | PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 36 | PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 37 | PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list) |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 38 | Py_GCC_ATTRIBUTE((format(printf, 1, 0))); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 39 | PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...) |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 40 | Py_GCC_ATTRIBUTE((format(printf, 1, 2))); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 41 | PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *); |
| 42 | PyAPI_FUNC(char *) PyBytes_AsString(PyObject *); |
| 43 | PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int); |
| 44 | PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *); |
| 45 | PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 46 | PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t, |
Serhiy Storchaka | 598ceae | 2017-11-28 17:56:10 +0200 | [diff] [blame] | 47 | const char *, Py_ssize_t, |
| 48 | const char *); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 49 | |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 50 | /* 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 Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 53 | cause an exception). */ |
| 54 | PyAPI_FUNC(int) PyBytes_AsStringAndSize( |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 55 | PyObject *obj, /* bytes object */ |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 56 | char **s, /* pointer to buffer variable */ |
Serhiy Storchaka | 2ad9382 | 2020-12-03 12:46:16 +0200 | [diff] [blame] | 57 | Py_ssize_t *len /* pointer to length variable or NULL */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 58 | ); |
| 59 | |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 60 | #ifndef Py_LIMITED_API |
Victor Stinner | 98921ae | 2020-02-12 23:54:31 +0100 | [diff] [blame] | 61 | # define Py_CPYTHON_BYTESOBJECT_H |
| 62 | # include "cpython/bytesobject.h" |
| 63 | # undef Py_CPYTHON_BYTESOBJECT_H |
| 64 | #endif |
Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 65 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 66 | #ifdef __cplusplus |
| 67 | } |
| 68 | #endif |
Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 69 | #endif /* !Py_BYTESOBJECT_H */ |