blob: 12ec80ca5e21c1459df900454aacb7a0e6539117 [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
65 +-------------------+---------------+--------------------------------+
66 | Format Characters | Type | Comment |
67 +===================+===============+================================+
68 | :attr:`%%` | *n/a* | The literal % character. |
69 +-------------------+---------------+--------------------------------+
70 | :attr:`%c` | int | A single character, |
71 | | | represented as an C int. |
72 +-------------------+---------------+--------------------------------+
73 | :attr:`%d` | int | Exactly equivalent to |
74 | | | ``printf("%d")``. |
75 +-------------------+---------------+--------------------------------+
76 | :attr:`%u` | unsigned int | Exactly equivalent to |
77 | | | ``printf("%u")``. |
78 +-------------------+---------------+--------------------------------+
79 | :attr:`%ld` | long | Exactly equivalent to |
80 | | | ``printf("%ld")``. |
81 +-------------------+---------------+--------------------------------+
82 | :attr:`%lu` | unsigned long | Exactly equivalent to |
83 | | | ``printf("%lu")``. |
84 +-------------------+---------------+--------------------------------+
85 | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
86 | | | ``printf("%zd")``. |
87 +-------------------+---------------+--------------------------------+
88 | :attr:`%zu` | size_t | Exactly equivalent to |
89 | | | ``printf("%zu")``. |
90 +-------------------+---------------+--------------------------------+
91 | :attr:`%i` | int | Exactly equivalent to |
92 | | | ``printf("%i")``. |
93 +-------------------+---------------+--------------------------------+
94 | :attr:`%x` | int | Exactly equivalent to |
95 | | | ``printf("%x")``. |
96 +-------------------+---------------+--------------------------------+
97 | :attr:`%s` | char\* | A null-terminated C character |
98 | | | array. |
99 +-------------------+---------------+--------------------------------+
100 | :attr:`%p` | void\* | The hex representation of a C |
101 | | | pointer. Mostly equivalent to |
102 | | | ``printf("%p")`` except that |
103 | | | it is guaranteed to start with |
104 | | | the literal ``0x`` regardless |
105 | | | of what the platform's |
106 | | | ``printf`` yields. |
107 +-------------------+---------------+--------------------------------+
108
109 An unrecognized format character causes all the rest of the format string to be
110 copied as-is to the result string, and any extra arguments discarded.
111
112
Georg Brandl60203b42010-10-06 10:11:56 +0000113.. c:function:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000114
Georg Brandl60203b42010-10-06 10:11:56 +0000115 Identical to :c:func:`PyBytes_FromFormat` except that it takes exactly two
Benjamin Petersondae32c12008-05-26 15:01:55 +0000116 arguments.
117
118
Georg Brandl60203b42010-10-06 10:11:56 +0000119.. c:function:: PyObject* PyBytes_FromObject(PyObject *o)
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000120
121 Return the bytes representation of object *o* that implements the buffer
122 protocol.
123
124
Georg Brandl60203b42010-10-06 10:11:56 +0000125.. c:function:: Py_ssize_t PyBytes_Size(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000126
127 Return the length of the bytes in bytes object *o*.
128
129
Georg Brandl60203b42010-10-06 10:11:56 +0000130.. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000131
Georg Brandl60203b42010-10-06 10:11:56 +0000132 Macro form of :c:func:`PyBytes_Size` but without error checking.
Benjamin Petersondae32c12008-05-26 15:01:55 +0000133
134
Georg Brandl60203b42010-10-06 10:11:56 +0000135.. c:function:: char* PyBytes_AsString(PyObject *o)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000136
137 Return a NUL-terminated representation of the contents of *o*. The pointer
138 refers to the internal buffer of *o*, not a copy. The data must not be
139 modified in any way, unless the string was just created using
140 ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
Georg Brandl60203b42010-10-06 10:11:56 +0000141 *o* is not a string object at all, :c:func:`PyBytes_AsString` returns *NULL*
Benjamin Petersondae32c12008-05-26 15:01:55 +0000142 and raises :exc:`TypeError`.
143
144
Georg Brandl60203b42010-10-06 10:11:56 +0000145.. c:function:: char* PyBytes_AS_STRING(PyObject *string)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000146
Georg Brandl60203b42010-10-06 10:11:56 +0000147 Macro form of :c:func:`PyBytes_AsString` but without error checking.
Benjamin Petersondae32c12008-05-26 15:01:55 +0000148
149
Georg Brandl60203b42010-10-06 10:11:56 +0000150.. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000151
152 Return a NUL-terminated representation of the contents of the object *obj*
153 through the output variables *buffer* and *length*.
154
155 If *length* is *NULL*, the resulting buffer may not contain NUL characters;
156 if it does, the function returns ``-1`` and a :exc:`TypeError` is raised.
157
158 The buffer refers to an internal string buffer of *obj*, not a copy. The data
159 must not be modified in any way, unless the string was just created using
160 ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If
Georg Brandl60203b42010-10-06 10:11:56 +0000161 *string* is not a string object at all, :c:func:`PyBytes_AsStringAndSize`
Benjamin Petersondae32c12008-05-26 15:01:55 +0000162 returns ``-1`` and raises :exc:`TypeError`.
163
164
Georg Brandl60203b42010-10-06 10:11:56 +0000165.. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000166
167 Create a new bytes object in *\*bytes* containing the contents of *newpart*
168 appended to *bytes*; the caller will own the new reference. The reference to
169 the old value of *bytes* will be stolen. If the new string cannot be
170 created, the old reference to *bytes* will still be discarded and the value
171 of *\*bytes* will be set to *NULL*; the appropriate exception will be set.
172
173
Georg Brandl60203b42010-10-06 10:11:56 +0000174.. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000175
176 Create a new string object in *\*bytes* containing the contents of *newpart*
177 appended to *bytes*. This version decrements the reference count of
178 *newpart*.
179
180
Georg Brandl60203b42010-10-06 10:11:56 +0000181.. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
Benjamin Petersondae32c12008-05-26 15:01:55 +0000182
183 A way to resize a bytes object even though it is "immutable". Only use this
184 to build up a brand new bytes object; don't use this if the bytes may already
185 be known in other parts of the code. It is an error to call this function if
186 the refcount on the input bytes object is not one. Pass the address of an
187 existing bytes object as an lvalue (it may be written into), and the new size
188 desired. On success, *\*bytes* holds the resized bytes object and ``0`` is
189 returned; the address in *\*bytes* may differ from its input value. If the
190 reallocation fails, the original bytes object at *\*bytes* is deallocated,
191 *\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is
192 returned.