blob: abd8744108f522b408c42f583e6e3d61abd2bcba [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
14.. ctype:: PyBytesObject
15
16 This subtype of :ctype:`PyObject` represents a Python bytes object.
17
18
19.. cvar:: PyTypeObject PyBytes_Type
20
Benjamin Petersondae32c12008-05-26 15:01:55 +000021 This instance of :ctype:`PyTypeObject` represents the Python bytes type; it
Georg Brandlab32fec2010-11-26 08:49:15 +000022 is the same object as :class:`bytes` in the Python layer.
Benjamin Petersondae32c12008-05-26 15:01:55 +000023
24
25.. cfunction:: int PyBytes_Check(PyObject *o)
26
27 Return true if the object *o* is a bytes object or an instance of a subtype
28 of the bytes type.
29
30
31.. cfunction:: int PyBytes_CheckExact(PyObject *o)
32
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
37.. cfunction:: PyObject* PyBytes_FromString(const char *v)
38
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
44.. cfunction:: PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len)
45
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
51.. cfunction:: PyObject* PyBytes_FromFormat(const char *format, ...)
52
53 Take a C :cfunc:`printf`\ -style *format* string and a variable number of
54 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
113.. cfunction:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs)
114
115 Identical to :func:`PyBytes_FromFormat` except that it takes exactly two
116 arguments.
117
118
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000119.. cfunction:: PyObject* PyBytes_FromObject(PyObject *o)
120
121 Return the bytes representation of object *o* that implements the buffer
122 protocol.
123
124
Benjamin Petersondae32c12008-05-26 15:01:55 +0000125.. cfunction:: Py_ssize_t PyBytes_Size(PyObject *o)
126
127 Return the length of the bytes in bytes object *o*.
128
129
130.. cfunction:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o)
131
132 Macro form of :cfunc:`PyBytes_Size` but without error checking.
133
134
135.. cfunction:: char* PyBytes_AsString(PyObject *o)
136
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
141 *o* is not a string object at all, :cfunc:`PyBytes_AsString` returns *NULL*
142 and raises :exc:`TypeError`.
143
144
145.. cfunction:: char* PyBytes_AS_STRING(PyObject *string)
146
147 Macro form of :cfunc:`PyBytes_AsString` but without error checking.
148
149
150.. cfunction:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length)
151
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
161 *string* is not a string object at all, :cfunc:`PyBytes_AsStringAndSize`
162 returns ``-1`` and raises :exc:`TypeError`.
163
164
165.. cfunction:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart)
166
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
174.. cfunction:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart)
175
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
181.. cfunction:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize)
182
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.