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 | |
| 21 | .. index:: single: BytesType (in module types) |
| 22 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 23 | This instance of :c:type:`PyTypeObject` represents the Python bytes type; it |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 24 | is the same object as ``bytes`` in the Python layer. . |
| 25 | |
| 26 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 27 | .. c:function:: int PyBytes_Check(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 28 | |
| 29 | Return true if the object *o* is a bytes object or an instance of a subtype |
| 30 | of the bytes type. |
| 31 | |
| 32 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 33 | .. c:function:: int PyBytes_CheckExact(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 34 | |
| 35 | Return true if the object *o* is a bytes object, but not an instance of a |
| 36 | subtype of the bytes type. |
| 37 | |
| 38 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 39 | .. c:function:: PyObject* PyBytes_FromString(const char *v) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 40 | |
| 41 | Return a new bytes object with a copy of the string *v* as value on success, |
| 42 | and *NULL* on failure. The parameter *v* must not be *NULL*; it will not be |
| 43 | checked. |
| 44 | |
| 45 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 46 | .. c:function:: PyObject* PyBytes_FromStringAndSize(const char *v, Py_ssize_t len) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 47 | |
| 48 | Return a new bytes object with a copy of the string *v* as value and length |
| 49 | *len* on success, and *NULL* on failure. If *v* is *NULL*, the contents of |
| 50 | the bytes object are uninitialized. |
| 51 | |
| 52 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 53 | .. c:function:: PyObject* PyBytes_FromFormat(const char *format, ...) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 54 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 55 | 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] | 56 | arguments, calculate the size of the resulting Python bytes object and return |
| 57 | a bytes object with the values formatted into it. The variable arguments |
| 58 | must be C types and must correspond exactly to the format characters in the |
| 59 | *format* string. The following format characters are allowed: |
| 60 | |
| 61 | .. % XXX: This should be exactly the same as the table in PyErr_Format. |
| 62 | .. % One should just refer to the other. |
| 63 | .. % XXX: The descriptions for %zd and %zu are wrong, but the truth is complicated |
| 64 | .. % because not all compilers support the %z width modifier -- we fake it |
| 65 | .. % when necessary via interpolating PY_FORMAT_SIZE_T. |
| 66 | |
| 67 | +-------------------+---------------+--------------------------------+ |
| 68 | | Format Characters | Type | Comment | |
| 69 | +===================+===============+================================+ |
| 70 | | :attr:`%%` | *n/a* | The literal % character. | |
| 71 | +-------------------+---------------+--------------------------------+ |
| 72 | | :attr:`%c` | int | A single character, | |
| 73 | | | | represented as an C int. | |
| 74 | +-------------------+---------------+--------------------------------+ |
| 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 |
| 112 | copied as-is to the result string, and any extra arguments discarded. |
| 113 | |
| 114 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 115 | .. c:function:: PyObject* PyBytes_FromFormatV(const char *format, va_list vargs) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 116 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 117 | Identical to :c:func:`PyBytes_FromFormat` except that it takes exactly two |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 118 | arguments. |
| 119 | |
| 120 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 121 | .. c:function:: PyObject* PyBytes_FromObject(PyObject *o) |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 122 | |
| 123 | Return the bytes representation of object *o* that implements the buffer |
| 124 | protocol. |
| 125 | |
| 126 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 127 | .. c:function:: Py_ssize_t PyBytes_Size(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 128 | |
| 129 | Return the length of the bytes in bytes object *o*. |
| 130 | |
| 131 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 132 | .. c:function:: Py_ssize_t PyBytes_GET_SIZE(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 133 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 134 | Macro form of :c:func:`PyBytes_Size` but without error checking. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 135 | |
| 136 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 137 | .. c:function:: char* PyBytes_AsString(PyObject *o) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 138 | |
| 139 | Return a NUL-terminated representation of the contents of *o*. The pointer |
| 140 | refers to the internal buffer of *o*, not a copy. The data must not be |
| 141 | modified in any way, unless the string was just created using |
| 142 | ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 143 | *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] | 144 | and raises :exc:`TypeError`. |
| 145 | |
| 146 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 147 | .. c:function:: char* PyBytes_AS_STRING(PyObject *string) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 148 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 149 | Macro form of :c:func:`PyBytes_AsString` but without error checking. |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 150 | |
| 151 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 152 | .. 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] | 153 | |
| 154 | Return a NUL-terminated representation of the contents of the object *obj* |
| 155 | through the output variables *buffer* and *length*. |
| 156 | |
| 157 | If *length* is *NULL*, the resulting buffer may not contain NUL characters; |
| 158 | if it does, the function returns ``-1`` and a :exc:`TypeError` is raised. |
| 159 | |
| 160 | The buffer refers to an internal string buffer of *obj*, not a copy. The data |
| 161 | must not be modified in any way, unless the string was just created using |
| 162 | ``PyBytes_FromStringAndSize(NULL, size)``. It must not be deallocated. If |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 163 | *string* is not a string object at all, :c:func:`PyBytes_AsStringAndSize` |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 164 | returns ``-1`` and raises :exc:`TypeError`. |
| 165 | |
| 166 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 167 | .. c:function:: void PyBytes_Concat(PyObject **bytes, PyObject *newpart) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 168 | |
| 169 | Create a new bytes object in *\*bytes* containing the contents of *newpart* |
| 170 | appended to *bytes*; the caller will own the new reference. The reference to |
| 171 | the old value of *bytes* will be stolen. If the new string cannot be |
| 172 | created, the old reference to *bytes* will still be discarded and the value |
| 173 | of *\*bytes* will be set to *NULL*; the appropriate exception will be set. |
| 174 | |
| 175 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 176 | .. c:function:: void PyBytes_ConcatAndDel(PyObject **bytes, PyObject *newpart) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 177 | |
| 178 | Create a new string object in *\*bytes* containing the contents of *newpart* |
| 179 | appended to *bytes*. This version decrements the reference count of |
| 180 | *newpart*. |
| 181 | |
| 182 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame^] | 183 | .. c:function:: int _PyBytes_Resize(PyObject **bytes, Py_ssize_t newsize) |
Benjamin Peterson | dae32c1 | 2008-05-26 15:01:55 +0000 | [diff] [blame] | 184 | |
| 185 | A way to resize a bytes object even though it is "immutable". Only use this |
| 186 | to build up a brand new bytes object; don't use this if the bytes may already |
| 187 | be known in other parts of the code. It is an error to call this function if |
| 188 | the refcount on the input bytes object is not one. Pass the address of an |
| 189 | existing bytes object as an lvalue (it may be written into), and the new size |
| 190 | desired. On success, *\*bytes* holds the resized bytes object and ``0`` is |
| 191 | returned; the address in *\*bytes* may differ from its input value. If the |
| 192 | reallocation fails, the original bytes object at *\*bytes* is deallocated, |
| 193 | *\*bytes* is set to *NULL*, a memory exception is set, and ``-1`` is |
| 194 | returned. |