blob: 23b712881af4e4243b549f4f410cd00d91b73043 [file] [log] [blame]
Benjamin Petersondae32c12008-05-26 15:01:55 +00001.. highlightlang:: c
2
3.. _bytesobjects:
4
5Bytes Objects
6-------------
7
8These functions raise :exc:`TypeError` when expecting a bytes parameter and are
9called with a non-bytes parameter.
10
11.. index:: object: bytes
12
13
Georg Brandl60203b42010-10-06 10:11:56 +000014.. c:type:: PyBytesObject
Benjamin Petersondae32c12008-05-26 15:01:55 +000015
Georg Brandl60203b42010-10-06 10:11:56 +000016 This subtype of :c:type:`PyObject` represents a Python bytes object.
Benjamin Petersondae32c12008-05-26 15:01:55 +000017
18
Georg Brandl60203b42010-10-06 10:11:56 +000019.. c:var:: PyTypeObject PyBytes_Type
Benjamin Petersondae32c12008-05-26 15:01:55 +000020
Georg Brandl60203b42010-10-06 10:11:56 +000021 This instance of :c:type:`PyTypeObject` represents the Python bytes type; it
Georg Brandl2aff3352010-10-17 10:59:41 +000022 is the same object as :class:`bytes` in the Python layer.
Benjamin Petersondae32c12008-05-26 15:01:55 +000023
24
Georg Brandl60203b42010-10-06 10:11:56 +000025.. c:function:: int PyBytes_Check(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +000026
27 Return true if the object *o* is a bytes object or an instance of a subtype
28 of the bytes type.
29
30
Georg Brandl60203b42010-10-06 10:11:56 +000031.. c:function:: int PyBytes_CheckExact(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +000032
33 Return true if the object *o* is a bytes object, but not an instance of a
34 subtype of the bytes type.
35
36
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: PyObject* PyBytes_FromString(const char *v)
Benjamin Petersondae32c12008-05-26 15:01:55 +000038
39 Return a new bytes object with a copy of the string *v* as value on success,
40 and *NULL* on failure. The parameter *v* must not be *NULL*; it will not be
41 checked.
42
43
Georg Brandl60203b42010-10-06 10:11:56 +000044.. c:function:: PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)
Benjamin Petersondae32c12008-05-26 15:01:55 +000045
46 Return a new bytes object with a copy of the string *v* as value and length
47 *len* on success, and *NULL* on failure. If *v* is *NULL*, the contents of
48 the bytes object are uninitialized.
49
50
Georg Brandl60203b42010-10-06 10:11:56 +000051.. c:function:: PyObject* PyBytes_FromFormat(const char *format, ...)
Benjamin Petersondae32c12008-05-26 15:01:55 +000052
Georg Brandl60203b42010-10-06 10:11:56 +000053 Take a C :c:func:`printf`\ -style *format* string and a variable number of
Benjamin Petersondae32c12008-05-26 15:01:55 +000054 arguments, calculate the size of the resulting Python bytes object and return
55 a bytes object with the values formatted into it. The variable arguments
56 must be C types and must correspond exactly to the format characters in the
57 *format* string. The following format characters are allowed:
58
59 .. % XXX: This should be exactly the same as the table in PyErr_Format.
60 .. % One should just refer to the other.
61 .. % XXX: The descriptions for %zd and %zu are wrong, but the truth is complicated
62 .. % because not all compilers support the %z width modifier -- we fake it
63 .. % when necessary via interpolating PY_FORMAT_SIZE_T.
64
Georg Brandl44ea77b2013-03-28 13:28:44 +010065 .. tabularcolumns:: |l|l|L|
66
Benjamin Petersondae32c12008-05-26 15:01:55 +000067 +-------------------+---------------+--------------------------------+
68 | Format Characters | Type | Comment |
69 +===================+===============+================================+
70 | :attr:`%%` | *n/a* | The literal % character. |
71 +-------------------+---------------+--------------------------------+
R David Murray0a560a12015-05-13 20:31:53 -040072 | :attr:`%c` | int | A single byte, |
73 | | | represented as a C int. |
Benjamin Petersondae32c12008-05-26 15:01:55 +000074 +-------------------+---------------+--------------------------------+
75 | :attr:`%d` | int | Exactly equivalent to |
76 | | | ``printf("%d")``. |
77 +-------------------+---------------+--------------------------------+
78 | :attr:`%u` | unsigned int | Exactly equivalent to |
79 | | | ``printf("%u")``. |
80 +-------------------+---------------+--------------------------------+
81 | :attr:`%ld` | long | Exactly equivalent to |
82 | | | ``printf("%ld")``. |
83 +-------------------+---------------+--------------------------------+
84 | :attr:`%lu` | unsigned long | Exactly equivalent to |
85 | | | ``printf("%lu")``. |
86 +-------------------+---------------+--------------------------------+
87 | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
88 | | | ``printf("%zd")``. |
89 +-------------------+---------------+--------------------------------+
90 | :attr:`%zu` | size_t | Exactly equivalent to |
91 | | | ``printf("%zu")``. |
92 +-------------------+---------------+--------------------------------+
93 | :attr:`%i` | int | Exactly equivalent to |
94 | | | ``printf("%i")``. |
95 +-------------------+---------------+--------------------------------+
96 | :attr:`%x` | int | Exactly equivalent to |
97 | | | ``printf("%x")``. |
98 +-------------------+---------------+--------------------------------+
99 | :attr:`%s` | char\* | A null-terminated C character |
100 | | | array. |
101 +-------------------+---------------+--------------------------------+
102 | :attr:`%p` | void\* | The hex representation of a C |
103 | | | pointer. Mostly equivalent to |
104 | | | ``printf("%p")`` except that |
105 | | | it is guaranteed to start with |
106 | | | the literal ``0x`` regardless |
107 | | | of what the platform's |
108 | | | ``printf`` yields. |
109 +-------------------+---------------+--------------------------------+
110
111 An unrecognized format character causes all the rest of the format string to be
R David Murray0a560a12015-05-13 20:31:53 -0400112 copied as-is to the result object, and any extra arguments discarded.
Benjamin Petersondae32c12008-05-26 15:01:55 +0000113
114
Georg Brandl60203b42010-10-06 10:11:56 +0000115.. c:function:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000116
Georg Brandl60203b42010-10-06 10:11:56 +0000117 Identical to :c:func:`PyBytes_FromFormat` except that it takes exactly two
Benjamin Petersondae32c12008-05-26 15:01:55 +0000118 arguments.
119
120
Georg Brandl60203b42010-10-06 10:11:56 +0000121.. c:function:: PyObject* PyBytes_FromObject(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000122
123 Return the bytes representation of object *o* that implements the buffer
124 protocol.
125
126
Georg Brandl60203b42010-10-06 10:11:56 +0000127.. c:function:: Py_ssize_t PyBytes_Size(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000128
129 Return the length of the bytes in bytes object *o*.
130
131
Georg Brandl60203b42010-10-06 10:11:56 +0000132.. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000133
Georg Brandl60203b42010-10-06 10:11:56 +0000134 Macro form of :c:func:`PyBytes_Size` but without error checking.
Benjamin Petersondae32c12008-05-26 15:01:55 +0000135
136
Georg Brandl60203b42010-10-06 10:11:56 +0000137.. c:function:: char* PyBytes_AsString(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000138
R David Murray0a560a12015-05-13 20:31:53 -0400139 Return a pointer to the contents of *o*. The pointer
140 refers to the internal buffer of *o*, which consists of ``len(o) + 1``
141 bytes. The last byte in the buffer is always null, regardless of
142 whether there are any other null bytes. The data must not be
143 modified in any way, unless the object was just created using
Benjamin Petersondae32c12008-05-26 15:01:55 +0000144 ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
R David Murray0a560a12015-05-13 20:31:53 -0400145 *o* is not a bytes object at all, :c:func:`PyBytes_AsString` returns *NULL*
Benjamin Petersondae32c12008-05-26 15:01:55 +0000146 and raises :exc:`TypeError`.
147
148
Georg Brandl60203b42010-10-06 10:11:56 +0000149.. c:function:: char* PyBytes_AS_STRING(PyObject *string)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000150
Georg Brandl60203b42010-10-06 10:11:56 +0000151 Macro form of :c:func:`PyBytes_AsString` but without error checking.
Benjamin Petersondae32c12008-05-26 15:01:55 +0000152
153
Georg Brandl60203b42010-10-06 10:11:56 +0000154.. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000155
R David Murray0a560a12015-05-13 20:31:53 -0400156 Return the null-terminated contents of the object *obj*
Benjamin Petersondae32c12008-05-26 15:01:55 +0000157 through the output variables *buffer* and *length*.
158
R David Murray0a560a12015-05-13 20:31:53 -0400159 If *length* is *NULL*, the bytes object
160 may not contain embedded null bytes;
Benjamin Petersondae32c12008-05-26 15:01:55 +0000161 if it does, the function returns ``-1`` and a :exc:`TypeError` is raised.
162
R David Murray0a560a12015-05-13 20:31:53 -0400163 The buffer refers to an internal buffer of *obj*, which includes an
164 additional null byte at the end (not counted in *length*). The data
165 must not be modified in any way, unless the object was just created using
Benjamin Petersondae32c12008-05-26 15:01:55 +0000166 ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
R David Murray0a560a12015-05-13 20:31:53 -0400167 *obj* is not a bytes object at all, :c:func:`PyBytes_AsStringAndSize`
Benjamin Petersondae32c12008-05-26 15:01:55 +0000168 returns ``-1`` and raises :exc:`TypeError`.
169
170
Georg Brandl60203b42010-10-06 10:11:56 +0000171.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000172
173 Create a new bytes object in *\*bytes* containing the contents of *newpart*
174 appended to *bytes*; the caller will own the new reference. The reference to
R David Murray0a560a12015-05-13 20:31:53 -0400175 the old value of *bytes* will be stolen. If the new object cannot be
Benjamin Petersondae32c12008-05-26 15:01:55 +0000176 created, the old reference to *bytes* will still be discarded and the value
177 of *\*bytes* will be set to *NULL*; the appropriate exception will be set.
178
179
Georg Brandl60203b42010-10-06 10:11:56 +0000180.. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000181
R David Murray0a560a12015-05-13 20:31:53 -0400182 Create a new bytes object in *\*bytes* containing the contents of *newpart*
Benjamin Petersondae32c12008-05-26 15:01:55 +0000183 appended to *bytes*. This version decrements the reference count of
184 *newpart*.
185
186
Georg Brandl60203b42010-10-06 10:11:56 +0000187.. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000188
189 A way to resize a bytes object even though it is "immutable". Only use this
190 to build up a brand new bytes object; don't use this if the bytes may already
191 be known in other parts of the code. It is an error to call this function if
192 the refcount on the input bytes object is not one. Pass the address of an
193 existing bytes object as an lvalue (it may be written into), and the new size
194 desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
195 returned; the address in *\*bytes* may differ from its input value. If the
196 reallocation fails, the original bytes object at *\*bytes* is deallocated,
197 *\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is
198 returned.