Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _bytesobjects: |
| 4 | |
| 5 | Bytes Objects |
| 6 | ------------- |
| 7 | |
| 8 | These functions raise :exc:`TypeError` when expecting a bytes parameter and are |
| 9 | called with a non-bytes parameter. |
| 10 | |
| 11 | .. index:: object: bytes |
| 12 | |
| 13 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 14 | .. c:type:: PyBytesObject |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 15 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 16 | This subtype of :c:type:`PyObject` represents a Python bytes object. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 17 | |
| 18 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 19 | .. c:var:: PyTypeObject PyBytes_Type |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 20 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 21 | This instance of :c:type:`PyTypeObject` represents the Python bytes type; it |
Georg Brandl | 2aff335 | 2010-10-17 10:59:41 +0000 | [diff] [blame] | 22 | is the same object as :class:`bytes` in the Python layer. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 23 | |
| 24 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 25 | .. c:function:: int PyBytes_Check(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 31 | .. c:function:: int PyBytes_CheckExact(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 37 | .. c:function:: PyObject* PyBytes_FromString(const char *v) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 44 | .. c:function:: PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 51 | .. c:function:: PyObject* PyBytes_FromFormat(const char *format, ...) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 53 | Take a C :c:func:`printf`\ -style *format* string and a variable number of |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 113 | .. c:function:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 114 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | Identical to :c:func:`PyBytes_FromFormat` except that it takes exactly two |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 116 | arguments. |
| 117 | |
| 118 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 119 | .. c:function:: PyObject* PyBytes_FromObject(PyObject *o) |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 120 | |
| 121 | Return the bytes representation of object *o* that implements the buffer |
| 122 | protocol. |
| 123 | |
| 124 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 125 | .. c:function:: Py_ssize_t PyBytes_Size(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 126 | |
| 127 | Return the length of the bytes in bytes object *o*. |
| 128 | |
| 129 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 130 | .. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 131 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 132 | Macro form of :c:func:`PyBytes_Size` but without error checking. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 133 | |
| 134 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 135 | .. c:function:: char* PyBytes_AsString(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 141 | *o* is not a string object at all, :c:func:`PyBytes_AsString` returns *NULL* |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 142 | and raises :exc:`TypeError`. |
| 143 | |
| 144 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 145 | .. c:function:: char* PyBytes_AS_STRING(PyObject *string) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 146 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 147 | Macro form of :c:func:`PyBytes_AsString` but without error checking. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 148 | |
| 149 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 150 | .. c:function:: int PyBytes_AsStringAndSize(PyObject *obj, char **buffer, Py_ssize_t *length) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 161 | *string* is not a string object at all, :c:func:`PyBytes_AsStringAndSize` |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 162 | returns ``-1`` and raises :exc:`TypeError`. |
| 163 | |
| 164 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 165 | .. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 174 | .. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 181 | .. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 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. |