Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _unicodeobjects: |
| 4 | |
| 5 | Unicode Objects and Codecs |
| 6 | -------------------------- |
| 7 | |
| 8 | .. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com> |
| 9 | |
| 10 | Unicode Objects |
| 11 | ^^^^^^^^^^^^^^^ |
| 12 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 13 | Unicode Type |
| 14 | """""""""""" |
| 15 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 16 | These are the basic Unicode object types used for the Unicode implementation in |
| 17 | Python: |
| 18 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 | .. c:type:: Py_UNICODE |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
| 22 | This type represents the storage type which is used by Python internally as |
| 23 | basis for holding Unicode ordinals. Python's default builds use a 16-bit type |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 24 | for :c:type:`Py_UNICODE` and store Unicode values internally as UCS2. It is also |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 25 | possible to build a UCS4 version of Python (most recent Linux distributions come |
| 26 | with UCS4 builds of Python). These builds then use a 32-bit type for |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 27 | :c:type:`Py_UNICODE` and store Unicode data internally as UCS4. On platforms |
| 28 | where :c:type:`wchar_t` is available and compatible with the chosen Python |
| 29 | Unicode build variant, :c:type:`Py_UNICODE` is a typedef alias for |
| 30 | :c:type:`wchar_t` to enhance native platform compatibility. On all other |
| 31 | platforms, :c:type:`Py_UNICODE` is a typedef alias for either :c:type:`unsigned |
| 32 | short` (UCS2) or :c:type:`unsigned long` (UCS4). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 33 | |
| 34 | Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep |
| 35 | this in mind when writing extensions or interfaces. |
| 36 | |
| 37 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 38 | .. c:type:: PyUnicodeObject |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 39 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 40 | This subtype of :c:type:`PyObject` represents a Python Unicode object. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 41 | |
| 42 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 43 | .. c:var:: PyTypeObject PyUnicode_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 45 | This instance of :c:type:`PyTypeObject` represents the Python Unicode type. It |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 46 | is exposed to Python code as ``str``. |
| 47 | |
| 48 | The following APIs are really C macros and can be used to do fast checks and to |
| 49 | access internal read-only data of Unicode objects: |
| 50 | |
| 51 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 52 | .. c:function:: int PyUnicode_Check(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | |
| 54 | Return true if the object *o* is a Unicode object or an instance of a Unicode |
| 55 | subtype. |
| 56 | |
| 57 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 58 | .. c:function:: int PyUnicode_CheckExact(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 59 | |
| 60 | Return true if the object *o* is a Unicode object, but not an instance of a |
| 61 | subtype. |
| 62 | |
| 63 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 64 | .. c:function:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 | Return the size of the object. *o* has to be a :c:type:`PyUnicodeObject` (not |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 67 | checked). |
| 68 | |
| 69 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 | |
| 72 | Return the size of the object's internal buffer in bytes. *o* has to be a |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 73 | :c:type:`PyUnicodeObject` (not checked). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 74 | |
| 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 78 | Return a pointer to the internal :c:type:`Py_UNICODE` buffer of the object. *o* |
| 79 | has to be a :c:type:`PyUnicodeObject` (not checked). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 80 | |
| 81 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 82 | .. c:function:: const char* PyUnicode_AS_DATA(PyObject *o) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 83 | |
| 84 | Return a pointer to the internal buffer of the object. *o* has to be a |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | :c:type:`PyUnicodeObject` (not checked). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 87 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: int PyUnicode_ClearFreeList() |
Christian Heimes | a156e09 | 2008-02-16 07:38:31 +0000 | [diff] [blame] | 89 | |
| 90 | Clear the free list. Return the total number of freed items. |
| 91 | |
Alexandre Vassalotti | 6d3dfc3 | 2009-07-29 19:54:39 +0000 | [diff] [blame] | 92 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 93 | Unicode Character Properties |
| 94 | """""""""""""""""""""""""""" |
| 95 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 96 | Unicode provides many different character properties. The most often needed ones |
| 97 | are available through these macros which are mapped to C functions depending on |
| 98 | the Python configuration. |
| 99 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 100 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 101 | .. c:function:: int Py_UNICODE_ISSPACE(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 102 | |
| 103 | Return 1 or 0 depending on whether *ch* is a whitespace character. |
| 104 | |
| 105 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 106 | .. c:function:: int Py_UNICODE_ISLOWER(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 107 | |
| 108 | Return 1 or 0 depending on whether *ch* is a lowercase character. |
| 109 | |
| 110 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 111 | .. c:function:: int Py_UNICODE_ISUPPER(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 112 | |
| 113 | Return 1 or 0 depending on whether *ch* is an uppercase character. |
| 114 | |
| 115 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 116 | .. c:function:: int Py_UNICODE_ISTITLE(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 117 | |
| 118 | Return 1 or 0 depending on whether *ch* is a titlecase character. |
| 119 | |
| 120 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 121 | .. c:function:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 122 | |
| 123 | Return 1 or 0 depending on whether *ch* is a linebreak character. |
| 124 | |
| 125 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 126 | .. c:function:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 | |
| 128 | Return 1 or 0 depending on whether *ch* is a decimal character. |
| 129 | |
| 130 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 131 | .. c:function:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 132 | |
| 133 | Return 1 or 0 depending on whether *ch* is a digit character. |
| 134 | |
| 135 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 136 | .. c:function:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 137 | |
| 138 | Return 1 or 0 depending on whether *ch* is a numeric character. |
| 139 | |
| 140 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 141 | .. c:function:: int Py_UNICODE_ISALPHA(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 142 | |
| 143 | Return 1 or 0 depending on whether *ch* is an alphabetic character. |
| 144 | |
| 145 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 146 | .. c:function:: int Py_UNICODE_ISALNUM(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 147 | |
| 148 | Return 1 or 0 depending on whether *ch* is an alphanumeric character. |
| 149 | |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 150 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 151 | .. c:function:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch) |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 152 | |
| 153 | Return 1 or 0 depending on whether *ch* is a printable character. |
| 154 | Nonprintable characters are those characters defined in the Unicode character |
| 155 | database as "Other" or "Separator", excepting the ASCII space (0x20) which is |
| 156 | considered printable. (Note that printable characters in this context are |
| 157 | those which should not be escaped when :func:`repr` is invoked on a string. |
| 158 | It has no bearing on the handling of strings written to :data:`sys.stdout` or |
| 159 | :data:`sys.stderr`.) |
| 160 | |
| 161 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 162 | These APIs can be used for fast direct character conversions: |
| 163 | |
| 164 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 165 | .. c:function:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 166 | |
| 167 | Return the character *ch* converted to lower case. |
| 168 | |
| 169 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 170 | .. c:function:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 171 | |
| 172 | Return the character *ch* converted to upper case. |
| 173 | |
| 174 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 175 | .. c:function:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 176 | |
| 177 | Return the character *ch* converted to title case. |
| 178 | |
| 179 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 180 | .. c:function:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 181 | |
| 182 | Return the character *ch* converted to a decimal positive integer. Return |
| 183 | ``-1`` if this is not possible. This macro does not raise exceptions. |
| 184 | |
| 185 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 186 | .. c:function:: int Py_UNICODE_TODIGIT(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 187 | |
| 188 | Return the character *ch* converted to a single digit integer. Return ``-1`` if |
| 189 | this is not possible. This macro does not raise exceptions. |
| 190 | |
| 191 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 192 | .. c:function:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 193 | |
| 194 | Return the character *ch* converted to a double. Return ``-1.0`` if this is not |
| 195 | possible. This macro does not raise exceptions. |
| 196 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 197 | |
| 198 | Plain Py_UNICODE |
| 199 | """""""""""""""" |
| 200 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 201 | To create Unicode objects and access their basic sequence properties, use these |
| 202 | APIs: |
| 203 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 204 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 205 | .. c:function:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 206 | |
Georg Brandl | 418cc73 | 2010-10-17 10:53:54 +0000 | [diff] [blame] | 207 | Create a Unicode object from the Py_UNICODE buffer *u* of the given size. *u* |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 208 | may be *NULL* which causes the contents to be undefined. It is the user's |
| 209 | responsibility to fill in the needed data. The buffer is copied into the new |
| 210 | object. If the buffer is not *NULL*, the return value might be a shared object. |
| 211 | Therefore, modification of the resulting Unicode object is only allowed when *u* |
| 212 | is *NULL*. |
| 213 | |
| 214 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 215 | .. c:function:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 216 | |
Georg Brandl | 418cc73 | 2010-10-17 10:53:54 +0000 | [diff] [blame] | 217 | Create a Unicode object from the char buffer *u*. The bytes will be interpreted |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 218 | as being UTF-8 encoded. *u* may also be *NULL* which |
| 219 | causes the contents to be undefined. It is the user's responsibility to fill in |
| 220 | the needed data. The buffer is copied into the new object. If the buffer is not |
| 221 | *NULL*, the return value might be a shared object. Therefore, modification of |
| 222 | the resulting Unicode object is only allowed when *u* is *NULL*. |
| 223 | |
| 224 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 225 | .. c:function:: PyObject *PyUnicode_FromString(const char *u) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 226 | |
| 227 | Create a Unicode object from an UTF-8 encoded null-terminated char buffer |
| 228 | *u*. |
| 229 | |
| 230 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 231 | .. c:function:: PyObject* PyUnicode_FromFormat(const char *format, ...) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 232 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 233 | Take a C :c:func:`printf`\ -style *format* string and a variable number of |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 234 | arguments, calculate the size of the resulting Python unicode string and return |
| 235 | a string with the values formatted into it. The variable arguments must be C |
| 236 | types and must correspond exactly to the format characters in the *format* |
Victor Stinner | 1205f27 | 2010-09-11 00:54:47 +0000 | [diff] [blame] | 237 | ASCII-encoded string. The following format characters are allowed: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 238 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 239 | .. % This should be exactly the same as the table in PyErr_Format. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 240 | .. % The descriptions for %zd and %zu are wrong, but the truth is complicated |
| 241 | .. % because not all compilers support the %z width modifier -- we fake it |
| 242 | .. % when necessary via interpolating PY_FORMAT_SIZE_T. |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 243 | .. % Similar comments apply to the %ll width modifier and |
| 244 | .. % PY_FORMAT_LONG_LONG. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 245 | |
| 246 | +-------------------+---------------------+--------------------------------+ |
| 247 | | Format Characters | Type | Comment | |
| 248 | +===================+=====================+================================+ |
| 249 | | :attr:`%%` | *n/a* | The literal % character. | |
| 250 | +-------------------+---------------------+--------------------------------+ |
| 251 | | :attr:`%c` | int | A single character, | |
| 252 | | | | represented as an C int. | |
| 253 | +-------------------+---------------------+--------------------------------+ |
| 254 | | :attr:`%d` | int | Exactly equivalent to | |
| 255 | | | | ``printf("%d")``. | |
| 256 | +-------------------+---------------------+--------------------------------+ |
| 257 | | :attr:`%u` | unsigned int | Exactly equivalent to | |
| 258 | | | | ``printf("%u")``. | |
| 259 | +-------------------+---------------------+--------------------------------+ |
| 260 | | :attr:`%ld` | long | Exactly equivalent to | |
| 261 | | | | ``printf("%ld")``. | |
| 262 | +-------------------+---------------------+--------------------------------+ |
Victor Stinner | 0fbe226 | 2011-03-02 00:10:34 +0000 | [diff] [blame] | 263 | | :attr:`%li` | long | Exactly equivalent to | |
| 264 | | | | ``printf("%li")``. | |
| 265 | +-------------------+---------------------+--------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 266 | | :attr:`%lu` | unsigned long | Exactly equivalent to | |
| 267 | | | | ``printf("%lu")``. | |
| 268 | +-------------------+---------------------+--------------------------------+ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 269 | | :attr:`%lld` | long long | Exactly equivalent to | |
| 270 | | | | ``printf("%lld")``. | |
| 271 | +-------------------+---------------------+--------------------------------+ |
Victor Stinner | 0fbe226 | 2011-03-02 00:10:34 +0000 | [diff] [blame] | 272 | | :attr:`%lli` | long long | Exactly equivalent to | |
| 273 | | | | ``printf("%lli")``. | |
| 274 | +-------------------+---------------------+--------------------------------+ |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 275 | | :attr:`%llu` | unsigned long long | Exactly equivalent to | |
| 276 | | | | ``printf("%llu")``. | |
| 277 | +-------------------+---------------------+--------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 278 | | :attr:`%zd` | Py_ssize_t | Exactly equivalent to | |
| 279 | | | | ``printf("%zd")``. | |
| 280 | +-------------------+---------------------+--------------------------------+ |
Victor Stinner | 0fbe226 | 2011-03-02 00:10:34 +0000 | [diff] [blame] | 281 | | :attr:`%zi` | Py_ssize_t | Exactly equivalent to | |
| 282 | | | | ``printf("%zi")``. | |
| 283 | +-------------------+---------------------+--------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 284 | | :attr:`%zu` | size_t | Exactly equivalent to | |
| 285 | | | | ``printf("%zu")``. | |
| 286 | +-------------------+---------------------+--------------------------------+ |
| 287 | | :attr:`%i` | int | Exactly equivalent to | |
| 288 | | | | ``printf("%i")``. | |
| 289 | +-------------------+---------------------+--------------------------------+ |
| 290 | | :attr:`%x` | int | Exactly equivalent to | |
| 291 | | | | ``printf("%x")``. | |
| 292 | +-------------------+---------------------+--------------------------------+ |
| 293 | | :attr:`%s` | char\* | A null-terminated C character | |
| 294 | | | | array. | |
| 295 | +-------------------+---------------------+--------------------------------+ |
| 296 | | :attr:`%p` | void\* | The hex representation of a C | |
| 297 | | | | pointer. Mostly equivalent to | |
| 298 | | | | ``printf("%p")`` except that | |
| 299 | | | | it is guaranteed to start with | |
| 300 | | | | the literal ``0x`` regardless | |
| 301 | | | | of what the platform's | |
| 302 | | | | ``printf`` yields. | |
| 303 | +-------------------+---------------------+--------------------------------+ |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 304 | | :attr:`%A` | PyObject\* | The result of calling | |
| 305 | | | | :func:`ascii`. | |
| 306 | +-------------------+---------------------+--------------------------------+ |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 307 | | :attr:`%U` | PyObject\* | A unicode object. | |
| 308 | +-------------------+---------------------+--------------------------------+ |
| 309 | | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be | |
| 310 | | | | *NULL*) and a null-terminated | |
| 311 | | | | C character array as a second | |
| 312 | | | | parameter (which will be used, | |
| 313 | | | | if the first parameter is | |
| 314 | | | | *NULL*). | |
| 315 | +-------------------+---------------------+--------------------------------+ |
| 316 | | :attr:`%S` | PyObject\* | The result of calling | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 317 | | | | :c:func:`PyObject_Str`. | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 318 | +-------------------+---------------------+--------------------------------+ |
| 319 | | :attr:`%R` | PyObject\* | The result of calling | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 320 | | | | :c:func:`PyObject_Repr`. | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 321 | +-------------------+---------------------+--------------------------------+ |
| 322 | |
| 323 | An unrecognized format character causes all the rest of the format string to be |
| 324 | copied as-is to the result string, and any extra arguments discarded. |
| 325 | |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 326 | .. note:: |
| 327 | |
| 328 | The `"%lld"` and `"%llu"` format specifiers are only available |
Georg Brandl | ef871f6 | 2010-03-12 10:06:40 +0000 | [diff] [blame] | 329 | when :const:`HAVE_LONG_LONG` is defined. |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 330 | |
| 331 | .. versionchanged:: 3.2 |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 332 | Support for ``"%lld"`` and ``"%llu"`` added. |
Mark Dickinson | 6ce4a9a | 2009-11-16 17:00:11 +0000 | [diff] [blame] | 333 | |
Victor Stinner | 0fbe226 | 2011-03-02 00:10:34 +0000 | [diff] [blame] | 334 | .. versionchanged:: 3.3 |
| 335 | Support for ``"%li"``, ``"%lli"`` and ``"%zi"`` added. |
| 336 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 337 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 338 | .. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 339 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 340 | Identical to :c:func:`PyUnicode_FromFormat` except that it takes exactly two |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 341 | arguments. |
| 342 | |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 343 | .. c:function:: PyObject* PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t size) |
| 344 | |
| 345 | Create a Unicode object by replacing all decimal digits in |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 346 | :c:type:`Py_UNICODE` buffer of the given *size* by ASCII digits 0--9 |
Alexander Belopolsky | 942af5a | 2010-12-04 03:38:46 +0000 | [diff] [blame] | 347 | according to their decimal value. Return *NULL* if an exception |
| 348 | occurs. |
| 349 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 350 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 351 | .. c:function:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 352 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 353 | Return a read-only pointer to the Unicode object's internal :c:type:`Py_UNICODE` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 354 | buffer, *NULL* if *unicode* is not a Unicode object. |
| 355 | |
| 356 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 357 | .. c:function:: Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode) |
Victor Stinner | e4ea994 | 2010-09-03 16:23:29 +0000 | [diff] [blame] | 358 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 359 | Create a copy of a Unicode string ending with a nul character. Return *NULL* |
Victor Stinner | e4ea994 | 2010-09-03 16:23:29 +0000 | [diff] [blame] | 360 | and raise a :exc:`MemoryError` exception on memory allocation failure, |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 361 | otherwise return a new allocated buffer (use :c:func:`PyMem_Free` to free the |
Victor Stinner | e4ea994 | 2010-09-03 16:23:29 +0000 | [diff] [blame] | 362 | buffer). |
| 363 | |
Victor Stinner | 2b19f35 | 2010-09-03 22:13:42 +0000 | [diff] [blame] | 364 | .. versionadded:: 3.2 |
| 365 | |
Victor Stinner | e4ea994 | 2010-09-03 16:23:29 +0000 | [diff] [blame] | 366 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 367 | .. c:function:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 368 | |
| 369 | Return the length of the Unicode object. |
| 370 | |
| 371 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 372 | .. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 373 | |
| 374 | Coerce an encoded object *obj* to an Unicode object and return a reference with |
| 375 | incremented refcount. |
| 376 | |
Georg Brandl | 952867a | 2010-06-27 10:17:12 +0000 | [diff] [blame] | 377 | :class:`bytes`, :class:`bytearray` and other char buffer compatible objects |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 378 | are decoded according to the given *encoding* and using the error handling |
| 379 | defined by *errors*. Both can be *NULL* to have the interface use the default |
Georg Brandl | 952867a | 2010-06-27 10:17:12 +0000 | [diff] [blame] | 380 | values (see the next section for details). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 381 | |
| 382 | All other objects, including Unicode objects, cause a :exc:`TypeError` to be |
| 383 | set. |
| 384 | |
| 385 | The API returns *NULL* if there was an error. The caller is responsible for |
| 386 | decref'ing the returned objects. |
| 387 | |
| 388 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 389 | .. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 390 | |
| 391 | Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used |
| 392 | throughout the interpreter whenever coercion to Unicode is needed. |
| 393 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 394 | If the platform supports :c:type:`wchar_t` and provides a header file wchar.h, |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 395 | Python can interface directly to this type using the following functions. |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 396 | Support is optimized if Python's own :c:type:`Py_UNICODE` type is identical to |
| 397 | the system's :c:type:`wchar_t`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 398 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 399 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 400 | File System Encoding |
| 401 | """""""""""""""""""" |
| 402 | |
| 403 | To encode and decode file names and other environment strings, |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 404 | :c:data:`Py_FileSystemEncoding` should be used as the encoding, and |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 405 | ``"surrogateescape"`` should be used as the error handler (:pep:`383`). To |
| 406 | encode file names during argument parsing, the ``"O&"`` converter should be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 407 | used, passing :c:func:`PyUnicode_FSConverter` as the conversion function: |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 408 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 409 | .. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result) |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 410 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 411 | ParseTuple converter: encode :class:`str` objects to :class:`bytes` using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 412 | :c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is. |
| 413 | *result* must be a :c:type:`PyBytesObject*` which must be released when it is |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 414 | no longer used. |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 415 | |
| 416 | .. versionadded:: 3.1 |
| 417 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 418 | |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 419 | To decode file names during argument parsing, the ``"O&"`` converter should be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 420 | used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function: |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 421 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 422 | .. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result) |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 423 | |
| 424 | ParseTuple converter: decode :class:`bytes` objects to :class:`str` using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 425 | :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str` objects are output |
| 426 | as-is. *result* must be a :c:type:`PyUnicodeObject*` which must be released |
Victor Stinner | 47fcb5b | 2010-08-13 23:59:58 +0000 | [diff] [blame] | 427 | when it is no longer used. |
| 428 | |
| 429 | .. versionadded:: 3.2 |
| 430 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 431 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 432 | .. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size) |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 433 | |
Victor Stinner | 62165d6 | 2010-10-09 10:34:37 +0000 | [diff] [blame] | 434 | Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the |
| 435 | ``'surrogateescape'`` error handler, or ``'strict'`` on Windows. |
| 436 | |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 437 | If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the |
| 438 | locale encoding. |
Victor Stinner | 62165d6 | 2010-10-09 10:34:37 +0000 | [diff] [blame] | 439 | |
| 440 | .. versionchanged:: 3.2 |
| 441 | Use ``'strict'`` error handler on Windows. |
| 442 | |
| 443 | |
| 444 | .. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s) |
| 445 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 446 | Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding` |
Victor Stinner | 62165d6 | 2010-10-09 10:34:37 +0000 | [diff] [blame] | 447 | and the ``'surrogateescape'`` error handler, or ``'strict'`` on Windows. |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 448 | |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 449 | If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the |
| 450 | locale encoding. |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 451 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 452 | Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length. |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 453 | |
Victor Stinner | 62165d6 | 2010-10-09 10:34:37 +0000 | [diff] [blame] | 454 | .. versionchanged:: 3.2 |
| 455 | Use ``'strict'`` error handler on Windows. |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 456 | |
| 457 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 458 | .. c:function:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode) |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 459 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 460 | Encode a Unicode object to :c:data:`Py_FileSystemDefaultEncoding` with the |
Victor Stinner | 62165d6 | 2010-10-09 10:34:37 +0000 | [diff] [blame] | 461 | ``'surrogateescape'`` error handler, or ``'strict'`` on Windows, and return |
| 462 | :class:`bytes`. |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 463 | |
Victor Stinner | f3170cc | 2010-10-15 12:04:23 +0000 | [diff] [blame] | 464 | If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the |
| 465 | locale encoding. |
Victor Stinner | ae6265f | 2010-05-15 16:27:27 +0000 | [diff] [blame] | 466 | |
| 467 | .. versionadded:: 3.2 |
| 468 | |
| 469 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 470 | wchar_t Support |
| 471 | """"""""""""""" |
| 472 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 473 | :c:type:`wchar_t` support for platforms which support it: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 474 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 475 | .. c:function:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 476 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 477 | Create a Unicode object from the :c:type:`wchar_t` buffer *w* of the given *size*. |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 478 | Passing -1 as the *size* indicates that the function must itself compute the length, |
Martin v. Löwis | 790465f | 2008-04-05 20:41:37 +0000 | [diff] [blame] | 479 | using wcslen. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 480 | Return *NULL* on failure. |
| 481 | |
| 482 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 483 | .. c:function:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 484 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 485 | Copy the Unicode object contents into the :c:type:`wchar_t` buffer *w*. At most |
| 486 | *size* :c:type:`wchar_t` characters are copied (excluding a possibly trailing |
| 487 | 0-termination character). Return the number of :c:type:`wchar_t` characters |
| 488 | copied or -1 in case of an error. Note that the resulting :c:type:`wchar_t` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 489 | string may or may not be 0-terminated. It is the responsibility of the caller |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 490 | to make sure that the :c:type:`wchar_t` string is 0-terminated in case this is |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 491 | required by the application. |
| 492 | |
| 493 | |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 494 | .. c:function:: wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size) |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 495 | |
| 496 | Convert the Unicode object to a wide character string. The output string |
| 497 | always ends with a nul character. If *size* is not *NULL*, write the number |
Victor Stinner | 1c24bd0 | 2010-10-02 11:03:13 +0000 | [diff] [blame] | 498 | of wide characters (excluding the trailing 0-termination character) into |
| 499 | *\*size*. |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 500 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 501 | Returns a buffer allocated by :c:func:`PyMem_Alloc` (use :c:func:`PyMem_Free` |
Victor Stinner | 137c34c | 2010-09-29 10:25:54 +0000 | [diff] [blame] | 502 | to free it) on success. On error, returns *NULL*, *\*size* is undefined and |
| 503 | raises a :exc:`MemoryError`. |
| 504 | |
| 505 | .. versionadded:: 3.2 |
| 506 | |
| 507 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 508 | .. _builtincodecs: |
| 509 | |
| 510 | Built-in Codecs |
| 511 | ^^^^^^^^^^^^^^^ |
| 512 | |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 513 | Python provides a set of built-in codecs which are written in C for speed. All of |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 514 | these codecs are directly usable via the following functions. |
| 515 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 516 | Many of the following APIs take two arguments encoding and errors, and they |
| 517 | have the same semantics as the ones of the built-in :func:`str` string object |
| 518 | constructor. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 519 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 520 | Setting encoding to *NULL* causes the default encoding to be used |
| 521 | which is ASCII. The file system calls should use |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 522 | :c:func:`PyUnicode_FSConverter` for encoding file names. This uses the |
| 523 | variable :c:data:`Py_FileSystemDefaultEncoding` internally. This |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 524 | variable should be treated as read-only: on some systems, it will be a |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 525 | pointer to a static string, on others, it will change at run-time |
| 526 | (such as when the application invokes setlocale). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 527 | |
| 528 | Error handling is set by errors which may also be set to *NULL* meaning to use |
| 529 | the default handling defined for the codec. Default error handling for all |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 530 | built-in codecs is "strict" (:exc:`ValueError` is raised). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 531 | |
| 532 | The codecs all use a similar interface. Only deviation from the following |
| 533 | generic ones are documented for simplicity. |
| 534 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 535 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 536 | Generic Codecs |
| 537 | """""""""""""" |
| 538 | |
| 539 | These are the generic codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 540 | |
| 541 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 542 | .. c:function:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 543 | |
| 544 | Create a Unicode object by decoding *size* bytes of the encoded string *s*. |
| 545 | *encoding* and *errors* have the same meaning as the parameters of the same name |
Georg Brandl | 22b3431 | 2009-07-26 14:54:51 +0000 | [diff] [blame] | 546 | in the :func:`unicode` built-in function. The codec to be used is looked up |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 547 | using the Python codec registry. Return *NULL* if an exception was raised by |
| 548 | the codec. |
| 549 | |
| 550 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 551 | .. c:function:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 552 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 553 | Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* and return a Python |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 554 | bytes object. *encoding* and *errors* have the same meaning as the |
| 555 | parameters of the same name in the Unicode :meth:`encode` method. The codec |
| 556 | to be used is looked up using the Python codec registry. Return *NULL* if an |
| 557 | exception was raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 558 | |
| 559 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 560 | .. c:function:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 561 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 562 | Encode a Unicode object and return the result as Python bytes object. |
| 563 | *encoding* and *errors* have the same meaning as the parameters of the same |
| 564 | name in the Unicode :meth:`encode` method. The codec to be used is looked up |
| 565 | using the Python codec registry. Return *NULL* if an exception was raised by |
| 566 | the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 567 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 568 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 569 | UTF-8 Codecs |
| 570 | """""""""""" |
| 571 | |
| 572 | These are the UTF-8 codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 573 | |
| 574 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 575 | .. c:function:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 576 | |
| 577 | Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string |
| 578 | *s*. Return *NULL* if an exception was raised by the codec. |
| 579 | |
| 580 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 581 | .. c:function:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 582 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 583 | If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF8`. If |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 584 | *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be |
| 585 | treated as an error. Those bytes will not be decoded and the number of bytes |
| 586 | that have been decoded will be stored in *consumed*. |
| 587 | |
| 588 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 589 | .. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 590 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 591 | Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* using UTF-8 and |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 592 | return a Python bytes object. Return *NULL* if an exception was raised by |
| 593 | the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 594 | |
| 595 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 596 | .. c:function:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 597 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 598 | Encode a Unicode object using UTF-8 and return the result as Python bytes |
| 599 | object. Error handling is "strict". Return *NULL* if an exception was |
| 600 | raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 601 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 602 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 603 | UTF-32 Codecs |
| 604 | """"""""""""" |
| 605 | |
| 606 | These are the UTF-32 codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 607 | |
| 608 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 609 | .. c:function:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 610 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 611 | Decode *size* bytes from a UTF-32 encoded buffer string and return the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 612 | corresponding Unicode object. *errors* (if non-*NULL*) defines the error |
| 613 | handling. It defaults to "strict". |
| 614 | |
| 615 | If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte |
| 616 | order:: |
| 617 | |
| 618 | *byteorder == -1: little endian |
| 619 | *byteorder == 0: native order |
| 620 | *byteorder == 1: big endian |
| 621 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 622 | If ``*byteorder`` is zero, and the first four bytes of the input data are a |
| 623 | byte order mark (BOM), the decoder switches to this byte order and the BOM is |
| 624 | not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or |
| 625 | ``1``, any byte order mark is copied to the output. |
| 626 | |
| 627 | After completion, *\*byteorder* is set to the current byte order at the end |
| 628 | of input data. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 629 | |
| 630 | In a narrow build codepoints outside the BMP will be decoded as surrogate pairs. |
| 631 | |
| 632 | If *byteorder* is *NULL*, the codec starts in native order mode. |
| 633 | |
| 634 | Return *NULL* if an exception was raised by the codec. |
| 635 | |
| 636 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 637 | .. c:function:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 638 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 639 | If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF32`. If |
| 640 | *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF32Stateful` will not treat |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 641 | trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible |
| 642 | by four) as an error. Those bytes will not be decoded and the number of bytes |
| 643 | that have been decoded will be stored in *consumed*. |
| 644 | |
| 645 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 646 | .. c:function:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 647 | |
| 648 | Return a Python bytes object holding the UTF-32 encoded value of the Unicode |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 649 | data in *s*. Output is written according to the following byte order:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 650 | |
| 651 | byteorder == -1: little endian |
| 652 | byteorder == 0: native byte order (writes a BOM mark) |
| 653 | byteorder == 1: big endian |
| 654 | |
| 655 | If byteorder is ``0``, the output string will always start with the Unicode BOM |
| 656 | mark (U+FEFF). In the other two modes, no BOM mark is prepended. |
| 657 | |
| 658 | If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output |
| 659 | as a single codepoint. |
| 660 | |
| 661 | Return *NULL* if an exception was raised by the codec. |
| 662 | |
| 663 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 664 | .. c:function:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 665 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 666 | Return a Python byte string using the UTF-32 encoding in native byte |
| 667 | order. The string always starts with a BOM mark. Error handling is "strict". |
| 668 | Return *NULL* if an exception was raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 669 | |
| 670 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 671 | UTF-16 Codecs |
| 672 | """"""""""""" |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 673 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 674 | These are the UTF-16 codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 675 | |
| 676 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 677 | .. c:function:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 678 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 679 | Decode *size* bytes from a UTF-16 encoded buffer string and return the |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 680 | corresponding Unicode object. *errors* (if non-*NULL*) defines the error |
| 681 | handling. It defaults to "strict". |
| 682 | |
| 683 | If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte |
| 684 | order:: |
| 685 | |
| 686 | *byteorder == -1: little endian |
| 687 | *byteorder == 0: native order |
| 688 | *byteorder == 1: big endian |
| 689 | |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 690 | If ``*byteorder`` is zero, and the first two bytes of the input data are a |
| 691 | byte order mark (BOM), the decoder switches to this byte order and the BOM is |
| 692 | not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or |
| 693 | ``1``, any byte order mark is copied to the output (where it will result in |
| 694 | either a ``\ufeff`` or a ``\ufffe`` character). |
| 695 | |
| 696 | After completion, *\*byteorder* is set to the current byte order at the end |
| 697 | of input data. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 698 | |
| 699 | If *byteorder* is *NULL*, the codec starts in native order mode. |
| 700 | |
| 701 | Return *NULL* if an exception was raised by the codec. |
| 702 | |
| 703 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 704 | .. c:function:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 705 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 706 | If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF16`. If |
| 707 | *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF16Stateful` will not treat |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 708 | trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a |
| 709 | split surrogate pair) as an error. Those bytes will not be decoded and the |
| 710 | number of bytes that have been decoded will be stored in *consumed*. |
| 711 | |
| 712 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 713 | .. c:function:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 714 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 715 | Return a Python bytes object holding the UTF-16 encoded value of the Unicode |
Benjamin Peterson | 4ac9ce4 | 2009-10-04 14:49:41 +0000 | [diff] [blame] | 716 | data in *s*. Output is written according to the following byte order:: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 717 | |
| 718 | byteorder == -1: little endian |
| 719 | byteorder == 0: native byte order (writes a BOM mark) |
| 720 | byteorder == 1: big endian |
| 721 | |
| 722 | If byteorder is ``0``, the output string will always start with the Unicode BOM |
| 723 | mark (U+FEFF). In the other two modes, no BOM mark is prepended. |
| 724 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 725 | If *Py_UNICODE_WIDE* is defined, a single :c:type:`Py_UNICODE` value may get |
| 726 | represented as a surrogate pair. If it is not defined, each :c:type:`Py_UNICODE` |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 727 | values is interpreted as an UCS-2 character. |
| 728 | |
| 729 | Return *NULL* if an exception was raised by the codec. |
| 730 | |
| 731 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 732 | .. c:function:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 733 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 734 | Return a Python byte string using the UTF-16 encoding in native byte |
| 735 | order. The string always starts with a BOM mark. Error handling is "strict". |
| 736 | Return *NULL* if an exception was raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 737 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 738 | |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 739 | UTF-7 Codecs |
| 740 | """""""""""" |
| 741 | |
| 742 | These are the UTF-7 codec APIs: |
| 743 | |
| 744 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 745 | .. c:function:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 746 | |
| 747 | Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string |
| 748 | *s*. Return *NULL* if an exception was raised by the codec. |
| 749 | |
| 750 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 751 | .. c:function:: PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed) |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 752 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 753 | If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF7`. If |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 754 | *consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not |
| 755 | be treated as an error. Those bytes will not be decoded and the number of |
| 756 | bytes that have been decoded will be stored in *consumed*. |
| 757 | |
| 758 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 759 | .. c:function:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors) |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 760 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 761 | Encode the :c:type:`Py_UNICODE` buffer of the given size using UTF-7 and |
Georg Brandl | 8477f82 | 2010-08-02 20:05:19 +0000 | [diff] [blame] | 762 | return a Python bytes object. Return *NULL* if an exception was raised by |
| 763 | the codec. |
| 764 | |
| 765 | If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise |
| 766 | special meaning) will be encoded in base-64. If *base64WhiteSpace* is |
| 767 | nonzero, whitespace will be encoded in base-64. Both are set to zero for the |
| 768 | Python "utf-7" codec. |
| 769 | |
| 770 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 771 | Unicode-Escape Codecs |
| 772 | """"""""""""""""""""" |
| 773 | |
| 774 | These are the "Unicode Escape" codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 775 | |
| 776 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 777 | .. c:function:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 778 | |
| 779 | Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded |
| 780 | string *s*. Return *NULL* if an exception was raised by the codec. |
| 781 | |
| 782 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 783 | .. c:function:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 784 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 785 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Unicode-Escape and |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 786 | return a Python string object. Return *NULL* if an exception was raised by the |
| 787 | codec. |
| 788 | |
| 789 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 790 | .. c:function:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 791 | |
| 792 | Encode a Unicode object using Unicode-Escape and return the result as Python |
| 793 | string object. Error handling is "strict". Return *NULL* if an exception was |
| 794 | raised by the codec. |
| 795 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 796 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 797 | Raw-Unicode-Escape Codecs |
| 798 | """"""""""""""""""""""""" |
| 799 | |
| 800 | These are the "Raw Unicode Escape" codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 801 | |
| 802 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 803 | .. c:function:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 804 | |
| 805 | Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape |
| 806 | encoded string *s*. Return *NULL* if an exception was raised by the codec. |
| 807 | |
| 808 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 809 | .. c:function:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 810 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 811 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Raw-Unicode-Escape |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 812 | and return a Python string object. Return *NULL* if an exception was raised by |
| 813 | the codec. |
| 814 | |
| 815 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 816 | .. c:function:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 817 | |
| 818 | Encode a Unicode object using Raw-Unicode-Escape and return the result as |
| 819 | Python string object. Error handling is "strict". Return *NULL* if an exception |
| 820 | was raised by the codec. |
| 821 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 822 | |
| 823 | Latin-1 Codecs |
| 824 | """""""""""""" |
| 825 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 826 | These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode |
| 827 | ordinals and only these are accepted by the codecs during encoding. |
| 828 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 829 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 830 | .. c:function:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 831 | |
| 832 | Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string |
| 833 | *s*. Return *NULL* if an exception was raised by the codec. |
| 834 | |
| 835 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 836 | .. c:function:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 837 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 838 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Latin-1 and |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 839 | return a Python bytes object. Return *NULL* if an exception was raised by |
| 840 | the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 841 | |
| 842 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 843 | .. c:function:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 844 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 845 | Encode a Unicode object using Latin-1 and return the result as Python bytes |
| 846 | object. Error handling is "strict". Return *NULL* if an exception was |
| 847 | raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 848 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 849 | |
| 850 | ASCII Codecs |
| 851 | """""""""""" |
| 852 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 853 | These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other |
| 854 | codes generate errors. |
| 855 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 856 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 857 | .. c:function:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 858 | |
| 859 | Create a Unicode object by decoding *size* bytes of the ASCII encoded string |
| 860 | *s*. Return *NULL* if an exception was raised by the codec. |
| 861 | |
| 862 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 863 | .. c:function:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 864 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 865 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using ASCII and |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 866 | return a Python bytes object. Return *NULL* if an exception was raised by |
| 867 | the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 868 | |
| 869 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 870 | .. c:function:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 871 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 872 | Encode a Unicode object using ASCII and return the result as Python bytes |
| 873 | object. Error handling is "strict". Return *NULL* if an exception was |
| 874 | raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 875 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 876 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 877 | Character Map Codecs |
| 878 | """""""""""""""""""" |
| 879 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 880 | This codec is special in that it can be used to implement many different codecs |
| 881 | (and this is in fact what was done to obtain most of the standard codecs |
| 882 | included in the :mod:`encodings` package). The codec uses mapping to encode and |
| 883 | decode characters. |
| 884 | |
| 885 | Decoding mappings must map single string characters to single Unicode |
| 886 | characters, integers (which are then interpreted as Unicode ordinals) or None |
| 887 | (meaning "undefined mapping" and causing an error). |
| 888 | |
| 889 | Encoding mappings must map single Unicode characters to single string |
| 890 | characters, integers (which are then interpreted as Latin-1 ordinals) or None |
| 891 | (meaning "undefined mapping" and causing an error). |
| 892 | |
| 893 | The mapping objects provided must only support the __getitem__ mapping |
| 894 | interface. |
| 895 | |
| 896 | If a character lookup fails with a LookupError, the character is copied as-is |
| 897 | meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal |
| 898 | resp. Because of this, mappings only need to contain those mappings which map |
| 899 | characters to different code points. |
| 900 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 901 | These are the mapping codec APIs: |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 902 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 903 | .. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 904 | |
| 905 | Create a Unicode object by decoding *size* bytes of the encoded string *s* using |
| 906 | the given *mapping* object. Return *NULL* if an exception was raised by the |
| 907 | codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a |
| 908 | dictionary mapping byte or a unicode string, which is treated as a lookup table. |
| 909 | Byte values greater that the length of the string and U+FFFE "characters" are |
| 910 | treated as "undefined mapping". |
| 911 | |
| 912 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 913 | .. c:function:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 914 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 915 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using the given |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 916 | *mapping* object and return a Python string object. Return *NULL* if an |
| 917 | exception was raised by the codec. |
| 918 | |
| 919 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 920 | .. c:function:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 921 | |
| 922 | Encode a Unicode object using the given *mapping* object and return the result |
| 923 | as Python string object. Error handling is "strict". Return *NULL* if an |
| 924 | exception was raised by the codec. |
| 925 | |
| 926 | The following codec API is special in that maps Unicode to Unicode. |
| 927 | |
| 928 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 929 | .. c:function:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 930 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 931 | Translate a :c:type:`Py_UNICODE` buffer of the given *size* by applying a |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 932 | character mapping *table* to it and return the resulting Unicode object. Return |
| 933 | *NULL* when an exception was raised by the codec. |
| 934 | |
| 935 | The *mapping* table must map Unicode ordinal integers to Unicode ordinal |
| 936 | integers or None (causing deletion of the character). |
| 937 | |
| 938 | Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries |
| 939 | and sequences work well. Unmapped character ordinals (ones which cause a |
| 940 | :exc:`LookupError`) are left untouched and are copied as-is. |
| 941 | |
Jeroen Ruigrok van der Werven | 47a7d70 | 2009-04-27 05:43:17 +0000 | [diff] [blame] | 942 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 943 | |
| 944 | MBCS codecs for Windows |
| 945 | """"""""""""""""""""""" |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 946 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 947 | These are the MBCS codec APIs. They are currently only available on Windows and |
| 948 | use the Win32 MBCS converters to implement the conversions. Note that MBCS (or |
| 949 | DBCS) is a class of encodings, not just one. The target encoding is defined by |
| 950 | the user settings on the machine running the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 951 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 952 | .. c:function:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 953 | |
| 954 | Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*. |
| 955 | Return *NULL* if an exception was raised by the codec. |
| 956 | |
| 957 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 958 | .. c:function:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 959 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 960 | If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeMBCS`. If |
| 961 | *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeMBCSStateful` will not decode |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 962 | trailing lead byte and the number of bytes that have been decoded will be stored |
| 963 | in *consumed*. |
| 964 | |
| 965 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 966 | .. c:function:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 967 | |
Ezio Melotti | c1f0577 | 2011-04-14 07:50:25 +0300 | [diff] [blame] | 968 | Encode the :c:type:`Py_UNICODE` buffer of the given *size* using MBCS and return |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 969 | a Python bytes object. Return *NULL* if an exception was raised by the |
| 970 | codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 971 | |
| 972 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 973 | .. c:function:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 974 | |
Benjamin Peterson | b6eba4f | 2009-01-13 23:14:04 +0000 | [diff] [blame] | 975 | Encode a Unicode object using MBCS and return the result as Python bytes |
| 976 | object. Error handling is "strict". Return *NULL* if an exception was |
| 977 | raised by the codec. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 978 | |
Martin v. Löwis | c15bdef | 2009-05-29 14:47:46 +0000 | [diff] [blame] | 979 | |
Victor Stinner | 77c3862 | 2010-05-14 15:58:55 +0000 | [diff] [blame] | 980 | Methods & Slots |
| 981 | """"""""""""""" |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 982 | |
| 983 | |
| 984 | .. _unicodemethodsandslots: |
| 985 | |
| 986 | Methods and Slot Functions |
| 987 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 988 | |
| 989 | The following APIs are capable of handling Unicode objects and strings on input |
| 990 | (we refer to them as strings in the descriptions) and return Unicode objects or |
| 991 | integers as appropriate. |
| 992 | |
| 993 | They all return *NULL* or ``-1`` if an exception occurs. |
| 994 | |
| 995 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 996 | .. c:function:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 997 | |
| 998 | Concat two strings giving a new Unicode string. |
| 999 | |
| 1000 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1001 | .. c:function:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1002 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 1003 | Split a string giving a list of Unicode strings. If *sep* is *NULL*, splitting |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1004 | will be done at all whitespace substrings. Otherwise, splits occur at the given |
| 1005 | separator. At most *maxsplit* splits will be done. If negative, no limit is |
| 1006 | set. Separators are not included in the resulting list. |
| 1007 | |
| 1008 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1009 | .. c:function:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1010 | |
| 1011 | Split a Unicode string at line breaks, returning a list of Unicode strings. |
| 1012 | CRLF is considered to be one line break. If *keepend* is 0, the Line break |
| 1013 | characters are not included in the resulting strings. |
| 1014 | |
| 1015 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1016 | .. c:function:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1017 | |
| 1018 | Translate a string by applying a character mapping table to it and return the |
| 1019 | resulting Unicode object. |
| 1020 | |
| 1021 | The mapping table must map Unicode ordinal integers to Unicode ordinal integers |
| 1022 | or None (causing deletion of the character). |
| 1023 | |
| 1024 | Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries |
| 1025 | and sequences work well. Unmapped character ordinals (ones which cause a |
| 1026 | :exc:`LookupError`) are left untouched and are copied as-is. |
| 1027 | |
| 1028 | *errors* has the usual meaning for codecs. It may be *NULL* which indicates to |
| 1029 | use the default error handling. |
| 1030 | |
| 1031 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1032 | .. c:function:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1033 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 1034 | Join a sequence of strings using the given *separator* and return the resulting |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1035 | Unicode string. |
| 1036 | |
| 1037 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1038 | .. c:function:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1039 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 1040 | Return 1 if *substr* matches ``str[start:end]`` at the given tail end |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1041 | (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match), |
| 1042 | 0 otherwise. Return ``-1`` if an error occurred. |
| 1043 | |
| 1044 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1045 | .. c:function:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1046 | |
Ezio Melotti | 95cd91c | 2011-04-14 07:43:53 +0300 | [diff] [blame] | 1047 | Return the first position of *substr* in ``str[start:end]`` using the given |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1048 | *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a |
| 1049 | backward search). The return value is the index of the first match; a value of |
| 1050 | ``-1`` indicates that no match was found, and ``-2`` indicates that an error |
| 1051 | occurred and an exception has been set. |
| 1052 | |
| 1053 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1054 | .. c:function:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1055 | |
| 1056 | Return the number of non-overlapping occurrences of *substr* in |
| 1057 | ``str[start:end]``. Return ``-1`` if an error occurred. |
| 1058 | |
| 1059 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1060 | .. c:function:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1061 | |
| 1062 | Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and |
| 1063 | return the resulting Unicode object. *maxcount* == -1 means replace all |
| 1064 | occurrences. |
| 1065 | |
| 1066 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1067 | .. c:function:: int PyUnicode_Compare(PyObject *left, PyObject *right) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1068 | |
| 1069 | Compare two strings and return -1, 0, 1 for less than, equal, and greater than, |
| 1070 | respectively. |
| 1071 | |
| 1072 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1073 | .. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string) |
Benjamin Peterson | c22ed14 | 2008-07-01 19:12:34 +0000 | [diff] [blame] | 1074 | |
| 1075 | Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less |
Victor Stinner | 80e788a | 2010-12-28 23:39:51 +0000 | [diff] [blame] | 1076 | than, equal, and greater than, respectively. It is best to pass only |
| 1077 | ASCII-encoded strings, but the function interprets the input string as |
| 1078 | ISO-8859-1 if it contains non-ASCII characters". |
Benjamin Peterson | c22ed14 | 2008-07-01 19:12:34 +0000 | [diff] [blame] | 1079 | |
| 1080 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1081 | .. c:function:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1082 | |
| 1083 | Rich compare two unicode strings and return one of the following: |
| 1084 | |
| 1085 | * ``NULL`` in case an exception was raised |
| 1086 | * :const:`Py_True` or :const:`Py_False` for successful comparisons |
| 1087 | * :const:`Py_NotImplemented` in case the type combination is unknown |
| 1088 | |
| 1089 | Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a |
| 1090 | :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails |
| 1091 | with a :exc:`UnicodeDecodeError`. |
| 1092 | |
| 1093 | Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`, |
| 1094 | :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`. |
| 1095 | |
| 1096 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1097 | .. c:function:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1098 | |
| 1099 | Return a new string object from *format* and *args*; this is analogous to |
| 1100 | ``format % args``. The *args* argument must be a tuple. |
| 1101 | |
| 1102 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1103 | .. c:function:: int PyUnicode_Contains(PyObject *container, PyObject *element) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1104 | |
| 1105 | Check whether *element* is contained in *container* and return true or false |
| 1106 | accordingly. |
| 1107 | |
| 1108 | *element* has to coerce to a one element Unicode string. ``-1`` is returned if |
| 1109 | there was an error. |
| 1110 | |
| 1111 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1112 | .. c:function:: void PyUnicode_InternInPlace(PyObject **string) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1113 | |
| 1114 | Intern the argument *\*string* in place. The argument must be the address of a |
| 1115 | pointer variable pointing to a Python unicode string object. If there is an |
| 1116 | existing interned string that is the same as *\*string*, it sets *\*string* to |
| 1117 | it (decrementing the reference count of the old string object and incrementing |
| 1118 | the reference count of the interned string object), otherwise it leaves |
| 1119 | *\*string* alone and interns it (incrementing its reference count). |
| 1120 | (Clarification: even though there is a lot of talk about reference counts, think |
| 1121 | of this function as reference-count-neutral; you own the object after the call |
| 1122 | if and only if you owned it before the call.) |
| 1123 | |
| 1124 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1125 | .. c:function:: PyObject* PyUnicode_InternFromString(const char *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1126 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 1127 | A combination of :c:func:`PyUnicode_FromString` and |
| 1128 | :c:func:`PyUnicode_InternInPlace`, returning either a new unicode string object |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1129 | that has been interned, or a new ("owned") reference to an earlier interned |
| 1130 | string object with the same value. |
| 1131 | |