blob: b5fc60a67943e140e6f8e133391bb663c44beacf [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _unicodeobjects:
4
5Unicode Objects and Codecs
6--------------------------
7
8.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
9
10Unicode Objects
11^^^^^^^^^^^^^^^
12
Victor Stinner77c38622010-05-14 15:58:55 +000013Unicode Type
14""""""""""""
15
Georg Brandl54a3faa2008-01-20 09:30:57 +000016These are the basic Unicode object types used for the Unicode implementation in
17Python:
18
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Georg Brandl60203b42010-10-06 10:11:56 +000020.. c:type:: Py_UNICODE
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
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 Brandl60203b42010-10-06 10:11:56 +000024 for :c:type:`Py_UNICODE` and store Unicode values internally as UCS2. It is also
Georg Brandl54a3faa2008-01-20 09:30:57 +000025 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 Brandl60203b42010-10-06 10:11:56 +000027 :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 Brandl54a3faa2008-01-20 09:30:57 +000033
34Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
35this in mind when writing extensions or interfaces.
36
37
Georg Brandl60203b42010-10-06 10:11:56 +000038.. c:type:: PyUnicodeObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000039
Georg Brandl60203b42010-10-06 10:11:56 +000040 This subtype of :c:type:`PyObject` represents a Python Unicode object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000041
42
Georg Brandl60203b42010-10-06 10:11:56 +000043.. c:var:: PyTypeObject PyUnicode_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
Georg Brandl60203b42010-10-06 10:11:56 +000045 This instance of :c:type:`PyTypeObject` represents the Python Unicode type. It
Georg Brandl54a3faa2008-01-20 09:30:57 +000046 is exposed to Python code as ``str``.
47
48The following APIs are really C macros and can be used to do fast checks and to
49access internal read-only data of Unicode objects:
50
51
Georg Brandl60203b42010-10-06 10:11:56 +000052.. c:function:: int PyUnicode_Check(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
54 Return true if the object *o* is a Unicode object or an instance of a Unicode
55 subtype.
56
57
Georg Brandl60203b42010-10-06 10:11:56 +000058.. c:function:: int PyUnicode_CheckExact(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000059
60 Return true if the object *o* is a Unicode object, but not an instance of a
61 subtype.
62
63
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
Georg Brandl60203b42010-10-06 10:11:56 +000066 Return the size of the object. *o* has to be a :c:type:`PyUnicodeObject` (not
Georg Brandl54a3faa2008-01-20 09:30:57 +000067 checked).
68
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 Return the size of the object's internal buffer in bytes. *o* has to be a
Georg Brandl60203b42010-10-06 10:11:56 +000073 :c:type:`PyUnicodeObject` (not checked).
Georg Brandl54a3faa2008-01-20 09:30:57 +000074
75
Georg Brandl60203b42010-10-06 10:11:56 +000076.. c:function:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000077
Georg Brandl60203b42010-10-06 10:11:56 +000078 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 Brandl54a3faa2008-01-20 09:30:57 +000080
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: const char* PyUnicode_AS_DATA(PyObject *o)
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84 Return a pointer to the internal buffer of the object. *o* has to be a
Georg Brandl60203b42010-10-06 10:11:56 +000085 :c:type:`PyUnicodeObject` (not checked).
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
Christian Heimesa156e092008-02-16 07:38:31 +000087
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: int PyUnicode_ClearFreeList()
Christian Heimesa156e092008-02-16 07:38:31 +000089
90 Clear the free list. Return the total number of freed items.
91
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +000092
Victor Stinner77c38622010-05-14 15:58:55 +000093Unicode Character Properties
94""""""""""""""""""""""""""""
95
Georg Brandl54a3faa2008-01-20 09:30:57 +000096Unicode provides many different character properties. The most often needed ones
97are available through these macros which are mapped to C functions depending on
98the Python configuration.
99
Georg Brandl54a3faa2008-01-20 09:30:57 +0000100
Georg Brandl60203b42010-10-06 10:11:56 +0000101.. c:function:: int Py_UNICODE_ISSPACE(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000102
103 Return 1 or 0 depending on whether *ch* is a whitespace character.
104
105
Georg Brandl60203b42010-10-06 10:11:56 +0000106.. c:function:: int Py_UNICODE_ISLOWER(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107
108 Return 1 or 0 depending on whether *ch* is a lowercase character.
109
110
Georg Brandl60203b42010-10-06 10:11:56 +0000111.. c:function:: int Py_UNICODE_ISUPPER(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
113 Return 1 or 0 depending on whether *ch* is an uppercase character.
114
115
Georg Brandl60203b42010-10-06 10:11:56 +0000116.. c:function:: int Py_UNICODE_ISTITLE(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000117
118 Return 1 or 0 depending on whether *ch* is a titlecase character.
119
120
Georg Brandl60203b42010-10-06 10:11:56 +0000121.. c:function:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
123 Return 1 or 0 depending on whether *ch* is a linebreak character.
124
125
Georg Brandl60203b42010-10-06 10:11:56 +0000126.. c:function:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
128 Return 1 or 0 depending on whether *ch* is a decimal character.
129
130
Georg Brandl60203b42010-10-06 10:11:56 +0000131.. c:function:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000132
133 Return 1 or 0 depending on whether *ch* is a digit character.
134
135
Georg Brandl60203b42010-10-06 10:11:56 +0000136.. c:function:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000137
138 Return 1 or 0 depending on whether *ch* is a numeric character.
139
140
Georg Brandl60203b42010-10-06 10:11:56 +0000141.. c:function:: int Py_UNICODE_ISALPHA(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000142
143 Return 1 or 0 depending on whether *ch* is an alphabetic character.
144
145
Georg Brandl60203b42010-10-06 10:11:56 +0000146.. c:function:: int Py_UNICODE_ISALNUM(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000147
148 Return 1 or 0 depending on whether *ch* is an alphanumeric character.
149
Georg Brandl559e5d72008-06-11 18:37:52 +0000150
Georg Brandl60203b42010-10-06 10:11:56 +0000151.. c:function:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch)
Georg Brandl559e5d72008-06-11 18:37:52 +0000152
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 Brandl54a3faa2008-01-20 09:30:57 +0000162These APIs can be used for fast direct character conversions:
163
164
Georg Brandl60203b42010-10-06 10:11:56 +0000165.. c:function:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000166
167 Return the character *ch* converted to lower case.
168
169
Georg Brandl60203b42010-10-06 10:11:56 +0000170.. c:function:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000171
172 Return the character *ch* converted to upper case.
173
174
Georg Brandl60203b42010-10-06 10:11:56 +0000175.. c:function:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000176
177 Return the character *ch* converted to title case.
178
179
Georg Brandl60203b42010-10-06 10:11:56 +0000180.. c:function:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000181
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 Brandl60203b42010-10-06 10:11:56 +0000186.. c:function:: int Py_UNICODE_TODIGIT(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000187
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 Brandl60203b42010-10-06 10:11:56 +0000192.. c:function:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000193
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 Stinner77c38622010-05-14 15:58:55 +0000197
Ezio Melotti8c9375b2011-08-22 20:03:25 +0300198These APIs can be used to work with surrogates:
199
200.. c:macro:: Py_UNICODE_IS_SURROGATE(ch)
201
202 Check if *ch* is a surrogate (``0xD800 <= ch <= 0xDFFF``).
203
204.. c:macro:: Py_UNICODE_IS_HIGH_SURROGATE(ch)
205
206 Check if *ch* is an high surrogate (``0xD800 <= ch <= 0xDBFF``).
207
208.. c:macro:: Py_UNICODE_IS_LOW_SURROGATE(ch)
209
210 Check if *ch* is a low surrogate (``0xDC00 <= ch <= 0xDFFF``).
211
212.. c:macro:: Py_UNICODE_JOIN_SURROGATES(high, low)
213
214 Join two surrogate characters and return a single Py_UCS4 value.
215 *high* and *low* are respectively the leading and trailing surrogates in a
216 surrogate pair.
217
218
Victor Stinner77c38622010-05-14 15:58:55 +0000219Plain Py_UNICODE
220""""""""""""""""
221
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222To create Unicode objects and access their basic sequence properties, use these
223APIs:
224
Georg Brandl54a3faa2008-01-20 09:30:57 +0000225
Georg Brandl60203b42010-10-06 10:11:56 +0000226.. c:function:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000227
Georg Brandl418cc732010-10-17 10:53:54 +0000228 Create a Unicode object from the Py_UNICODE buffer *u* of the given size. *u*
Georg Brandl54a3faa2008-01-20 09:30:57 +0000229 may be *NULL* which causes the contents to be undefined. It is the user's
230 responsibility to fill in the needed data. The buffer is copied into the new
231 object. If the buffer is not *NULL*, the return value might be a shared object.
232 Therefore, modification of the resulting Unicode object is only allowed when *u*
233 is *NULL*.
234
235
Georg Brandl60203b42010-10-06 10:11:56 +0000236.. c:function:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000237
Georg Brandl418cc732010-10-17 10:53:54 +0000238 Create a Unicode object from the char buffer *u*. The bytes will be interpreted
Georg Brandl54a3faa2008-01-20 09:30:57 +0000239 as being UTF-8 encoded. *u* may also be *NULL* which
240 causes the contents to be undefined. It is the user's responsibility to fill in
241 the needed data. The buffer is copied into the new object. If the buffer is not
242 *NULL*, the return value might be a shared object. Therefore, modification of
243 the resulting Unicode object is only allowed when *u* is *NULL*.
244
245
Georg Brandl60203b42010-10-06 10:11:56 +0000246.. c:function:: PyObject *PyUnicode_FromString(const char *u)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000247
248 Create a Unicode object from an UTF-8 encoded null-terminated char buffer
249 *u*.
250
251
Georg Brandl60203b42010-10-06 10:11:56 +0000252.. c:function:: PyObject* PyUnicode_FromFormat(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000253
Georg Brandl60203b42010-10-06 10:11:56 +0000254 Take a C :c:func:`printf`\ -style *format* string and a variable number of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000255 arguments, calculate the size of the resulting Python unicode string and return
256 a string with the values formatted into it. The variable arguments must be C
257 types and must correspond exactly to the format characters in the *format*
Victor Stinner1205f272010-09-11 00:54:47 +0000258 ASCII-encoded string. The following format characters are allowed:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000259
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000260 .. % This should be exactly the same as the table in PyErr_Format.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000261 .. % The descriptions for %zd and %zu are wrong, but the truth is complicated
262 .. % because not all compilers support the %z width modifier -- we fake it
263 .. % when necessary via interpolating PY_FORMAT_SIZE_T.
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000264 .. % Similar comments apply to the %ll width modifier and
265 .. % PY_FORMAT_LONG_LONG.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000266
267 +-------------------+---------------------+--------------------------------+
268 | Format Characters | Type | Comment |
269 +===================+=====================+================================+
270 | :attr:`%%` | *n/a* | The literal % character. |
271 +-------------------+---------------------+--------------------------------+
272 | :attr:`%c` | int | A single character, |
273 | | | represented as an C int. |
274 +-------------------+---------------------+--------------------------------+
275 | :attr:`%d` | int | Exactly equivalent to |
276 | | | ``printf("%d")``. |
277 +-------------------+---------------------+--------------------------------+
278 | :attr:`%u` | unsigned int | Exactly equivalent to |
279 | | | ``printf("%u")``. |
280 +-------------------+---------------------+--------------------------------+
281 | :attr:`%ld` | long | Exactly equivalent to |
282 | | | ``printf("%ld")``. |
283 +-------------------+---------------------+--------------------------------+
Victor Stinner0fbe2262011-03-02 00:10:34 +0000284 | :attr:`%li` | long | Exactly equivalent to |
285 | | | ``printf("%li")``. |
286 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000287 | :attr:`%lu` | unsigned long | Exactly equivalent to |
288 | | | ``printf("%lu")``. |
289 +-------------------+---------------------+--------------------------------+
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000290 | :attr:`%lld` | long long | Exactly equivalent to |
291 | | | ``printf("%lld")``. |
292 +-------------------+---------------------+--------------------------------+
Victor Stinner0fbe2262011-03-02 00:10:34 +0000293 | :attr:`%lli` | long long | Exactly equivalent to |
294 | | | ``printf("%lli")``. |
295 +-------------------+---------------------+--------------------------------+
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000296 | :attr:`%llu` | unsigned long long | Exactly equivalent to |
297 | | | ``printf("%llu")``. |
298 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000299 | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
300 | | | ``printf("%zd")``. |
301 +-------------------+---------------------+--------------------------------+
Victor Stinner0fbe2262011-03-02 00:10:34 +0000302 | :attr:`%zi` | Py_ssize_t | Exactly equivalent to |
303 | | | ``printf("%zi")``. |
304 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000305 | :attr:`%zu` | size_t | Exactly equivalent to |
306 | | | ``printf("%zu")``. |
307 +-------------------+---------------------+--------------------------------+
308 | :attr:`%i` | int | Exactly equivalent to |
309 | | | ``printf("%i")``. |
310 +-------------------+---------------------+--------------------------------+
311 | :attr:`%x` | int | Exactly equivalent to |
312 | | | ``printf("%x")``. |
313 +-------------------+---------------------+--------------------------------+
314 | :attr:`%s` | char\* | A null-terminated C character |
315 | | | array. |
316 +-------------------+---------------------+--------------------------------+
317 | :attr:`%p` | void\* | The hex representation of a C |
318 | | | pointer. Mostly equivalent to |
319 | | | ``printf("%p")`` except that |
320 | | | it is guaranteed to start with |
321 | | | the literal ``0x`` regardless |
322 | | | of what the platform's |
323 | | | ``printf`` yields. |
324 +-------------------+---------------------+--------------------------------+
Georg Brandl559e5d72008-06-11 18:37:52 +0000325 | :attr:`%A` | PyObject\* | The result of calling |
326 | | | :func:`ascii`. |
327 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000328 | :attr:`%U` | PyObject\* | A unicode object. |
329 +-------------------+---------------------+--------------------------------+
330 | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be |
331 | | | *NULL*) and a null-terminated |
332 | | | C character array as a second |
333 | | | parameter (which will be used, |
334 | | | if the first parameter is |
335 | | | *NULL*). |
336 +-------------------+---------------------+--------------------------------+
337 | :attr:`%S` | PyObject\* | The result of calling |
Georg Brandl60203b42010-10-06 10:11:56 +0000338 | | | :c:func:`PyObject_Str`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000339 +-------------------+---------------------+--------------------------------+
340 | :attr:`%R` | PyObject\* | The result of calling |
Georg Brandl60203b42010-10-06 10:11:56 +0000341 | | | :c:func:`PyObject_Repr`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000342 +-------------------+---------------------+--------------------------------+
343
344 An unrecognized format character causes all the rest of the format string to be
345 copied as-is to the result string, and any extra arguments discarded.
346
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000347 .. note::
348
349 The `"%lld"` and `"%llu"` format specifiers are only available
Georg Brandlef871f62010-03-12 10:06:40 +0000350 when :const:`HAVE_LONG_LONG` is defined.
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000351
352 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000353 Support for ``"%lld"`` and ``"%llu"`` added.
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000354
Victor Stinner0fbe2262011-03-02 00:10:34 +0000355 .. versionchanged:: 3.3
356 Support for ``"%li"``, ``"%lli"`` and ``"%zi"`` added.
357
Georg Brandl54a3faa2008-01-20 09:30:57 +0000358
Georg Brandl60203b42010-10-06 10:11:56 +0000359.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000360
Georg Brandl60203b42010-10-06 10:11:56 +0000361 Identical to :c:func:`PyUnicode_FromFormat` except that it takes exactly two
Georg Brandl54a3faa2008-01-20 09:30:57 +0000362 arguments.
363
Alexander Belopolsky942af5a2010-12-04 03:38:46 +0000364.. c:function:: PyObject* PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t size)
365
366 Create a Unicode object by replacing all decimal digits in
Ezio Melottic1f05772011-04-14 07:50:25 +0300367 :c:type:`Py_UNICODE` buffer of the given *size* by ASCII digits 0--9
Alexander Belopolsky942af5a2010-12-04 03:38:46 +0000368 according to their decimal value. Return *NULL* if an exception
369 occurs.
370
Georg Brandl54a3faa2008-01-20 09:30:57 +0000371
Georg Brandl60203b42010-10-06 10:11:56 +0000372.. c:function:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000373
Georg Brandl60203b42010-10-06 10:11:56 +0000374 Return a read-only pointer to the Unicode object's internal :c:type:`Py_UNICODE`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000375 buffer, *NULL* if *unicode* is not a Unicode object.
376
377
Georg Brandl60203b42010-10-06 10:11:56 +0000378.. c:function:: Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode)
Victor Stinnere4ea9942010-09-03 16:23:29 +0000379
Ezio Melottic1f05772011-04-14 07:50:25 +0300380 Create a copy of a Unicode string ending with a nul character. Return *NULL*
Victor Stinnere4ea9942010-09-03 16:23:29 +0000381 and raise a :exc:`MemoryError` exception on memory allocation failure,
Georg Brandl60203b42010-10-06 10:11:56 +0000382 otherwise return a new allocated buffer (use :c:func:`PyMem_Free` to free the
Victor Stinnere4ea9942010-09-03 16:23:29 +0000383 buffer).
384
Victor Stinner2b19f352010-09-03 22:13:42 +0000385 .. versionadded:: 3.2
386
Victor Stinnere4ea9942010-09-03 16:23:29 +0000387
Georg Brandl60203b42010-10-06 10:11:56 +0000388.. c:function:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000389
390 Return the length of the Unicode object.
391
392
Georg Brandl60203b42010-10-06 10:11:56 +0000393.. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000394
395 Coerce an encoded object *obj* to an Unicode object and return a reference with
396 incremented refcount.
397
Georg Brandl952867a2010-06-27 10:17:12 +0000398 :class:`bytes`, :class:`bytearray` and other char buffer compatible objects
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300399 are decoded according to the given *encoding* and using the error handling
400 defined by *errors*. Both can be *NULL* to have the interface use the default
Georg Brandl952867a2010-06-27 10:17:12 +0000401 values (see the next section for details).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000402
403 All other objects, including Unicode objects, cause a :exc:`TypeError` to be
404 set.
405
406 The API returns *NULL* if there was an error. The caller is responsible for
407 decref'ing the returned objects.
408
409
Georg Brandl60203b42010-10-06 10:11:56 +0000410.. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000411
412 Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
413 throughout the interpreter whenever coercion to Unicode is needed.
414
Georg Brandl60203b42010-10-06 10:11:56 +0000415If the platform supports :c:type:`wchar_t` and provides a header file wchar.h,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000416Python can interface directly to this type using the following functions.
Georg Brandl60203b42010-10-06 10:11:56 +0000417Support is optimized if Python's own :c:type:`Py_UNICODE` type is identical to
418the system's :c:type:`wchar_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000419
Georg Brandl54a3faa2008-01-20 09:30:57 +0000420
Victor Stinner77c38622010-05-14 15:58:55 +0000421File System Encoding
422""""""""""""""""""""
423
424To encode and decode file names and other environment strings,
Georg Brandl60203b42010-10-06 10:11:56 +0000425:c:data:`Py_FileSystemEncoding` should be used as the encoding, and
Victor Stinner77c38622010-05-14 15:58:55 +0000426``"surrogateescape"`` should be used as the error handler (:pep:`383`). To
427encode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl60203b42010-10-06 10:11:56 +0000428used, passing :c:func:`PyUnicode_FSConverter` as the conversion function:
Victor Stinner77c38622010-05-14 15:58:55 +0000429
Georg Brandl60203b42010-10-06 10:11:56 +0000430.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
Victor Stinner77c38622010-05-14 15:58:55 +0000431
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000432 ParseTuple converter: encode :class:`str` objects to :class:`bytes` using
Georg Brandl60203b42010-10-06 10:11:56 +0000433 :c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
434 *result* must be a :c:type:`PyBytesObject*` which must be released when it is
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000435 no longer used.
Victor Stinner77c38622010-05-14 15:58:55 +0000436
437 .. versionadded:: 3.1
438
Georg Brandl67b21b72010-08-17 15:07:14 +0000439
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000440To decode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl60203b42010-10-06 10:11:56 +0000441used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000442
Georg Brandl60203b42010-10-06 10:11:56 +0000443.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000444
445 ParseTuple converter: decode :class:`bytes` objects to :class:`str` using
Georg Brandl60203b42010-10-06 10:11:56 +0000446 :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str` objects are output
447 as-is. *result* must be a :c:type:`PyUnicodeObject*` which must be released
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000448 when it is no longer used.
449
450 .. versionadded:: 3.2
451
Georg Brandl67b21b72010-08-17 15:07:14 +0000452
Georg Brandl60203b42010-10-06 10:11:56 +0000453.. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
Victor Stinner77c38622010-05-14 15:58:55 +0000454
Victor Stinner62165d62010-10-09 10:34:37 +0000455 Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the
456 ``'surrogateescape'`` error handler, or ``'strict'`` on Windows.
457
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000458 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
459 locale encoding.
Victor Stinner62165d62010-10-09 10:34:37 +0000460
461 .. versionchanged:: 3.2
462 Use ``'strict'`` error handler on Windows.
463
464
465.. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
466
Georg Brandl60203b42010-10-06 10:11:56 +0000467 Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding`
Victor Stinner62165d62010-10-09 10:34:37 +0000468 and the ``'surrogateescape'`` error handler, or ``'strict'`` on Windows.
Victor Stinner77c38622010-05-14 15:58:55 +0000469
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000470 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
471 locale encoding.
Victor Stinner77c38622010-05-14 15:58:55 +0000472
Georg Brandl60203b42010-10-06 10:11:56 +0000473 Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
Victor Stinner77c38622010-05-14 15:58:55 +0000474
Victor Stinner62165d62010-10-09 10:34:37 +0000475 .. versionchanged:: 3.2
476 Use ``'strict'`` error handler on Windows.
Victor Stinner77c38622010-05-14 15:58:55 +0000477
478
Georg Brandl60203b42010-10-06 10:11:56 +0000479.. c:function:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
Victor Stinnerae6265f2010-05-15 16:27:27 +0000480
Georg Brandl60203b42010-10-06 10:11:56 +0000481 Encode a Unicode object to :c:data:`Py_FileSystemDefaultEncoding` with the
Victor Stinner62165d62010-10-09 10:34:37 +0000482 ``'surrogateescape'`` error handler, or ``'strict'`` on Windows, and return
483 :class:`bytes`.
Victor Stinnerae6265f2010-05-15 16:27:27 +0000484
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000485 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
486 locale encoding.
Victor Stinnerae6265f2010-05-15 16:27:27 +0000487
488 .. versionadded:: 3.2
489
490
Victor Stinner77c38622010-05-14 15:58:55 +0000491wchar_t Support
492"""""""""""""""
493
Ezio Melottic1f05772011-04-14 07:50:25 +0300494:c:type:`wchar_t` support for platforms which support it:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000495
Georg Brandl60203b42010-10-06 10:11:56 +0000496.. c:function:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000497
Ezio Melottic1f05772011-04-14 07:50:25 +0300498 Create a Unicode object from the :c:type:`wchar_t` buffer *w* of the given *size*.
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300499 Passing -1 as the *size* indicates that the function must itself compute the length,
Martin v. Löwis790465f2008-04-05 20:41:37 +0000500 using wcslen.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000501 Return *NULL* on failure.
502
503
Georg Brandl60203b42010-10-06 10:11:56 +0000504.. c:function:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000505
Georg Brandl60203b42010-10-06 10:11:56 +0000506 Copy the Unicode object contents into the :c:type:`wchar_t` buffer *w*. At most
507 *size* :c:type:`wchar_t` characters are copied (excluding a possibly trailing
508 0-termination character). Return the number of :c:type:`wchar_t` characters
509 copied or -1 in case of an error. Note that the resulting :c:type:`wchar_t`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000510 string may or may not be 0-terminated. It is the responsibility of the caller
Georg Brandl60203b42010-10-06 10:11:56 +0000511 to make sure that the :c:type:`wchar_t` string is 0-terminated in case this is
Georg Brandl54a3faa2008-01-20 09:30:57 +0000512 required by the application.
513
514
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000515.. c:function:: wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)
Victor Stinner137c34c2010-09-29 10:25:54 +0000516
517 Convert the Unicode object to a wide character string. The output string
518 always ends with a nul character. If *size* is not *NULL*, write the number
Victor Stinner1c24bd02010-10-02 11:03:13 +0000519 of wide characters (excluding the trailing 0-termination character) into
520 *\*size*.
Victor Stinner137c34c2010-09-29 10:25:54 +0000521
Georg Brandl60203b42010-10-06 10:11:56 +0000522 Returns a buffer allocated by :c:func:`PyMem_Alloc` (use :c:func:`PyMem_Free`
Victor Stinner137c34c2010-09-29 10:25:54 +0000523 to free it) on success. On error, returns *NULL*, *\*size* is undefined and
524 raises a :exc:`MemoryError`.
525
526 .. versionadded:: 3.2
527
528
Georg Brandl54a3faa2008-01-20 09:30:57 +0000529.. _builtincodecs:
530
531Built-in Codecs
532^^^^^^^^^^^^^^^
533
Georg Brandl22b34312009-07-26 14:54:51 +0000534Python provides a set of built-in codecs which are written in C for speed. All of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000535these codecs are directly usable via the following functions.
536
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300537Many of the following APIs take two arguments encoding and errors, and they
538have the same semantics as the ones of the built-in :func:`str` string object
539constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000540
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000541Setting encoding to *NULL* causes the default encoding to be used
542which is ASCII. The file system calls should use
Georg Brandl60203b42010-10-06 10:11:56 +0000543:c:func:`PyUnicode_FSConverter` for encoding file names. This uses the
544variable :c:data:`Py_FileSystemDefaultEncoding` internally. This
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300545variable should be treated as read-only: on some systems, it will be a
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000546pointer to a static string, on others, it will change at run-time
547(such as when the application invokes setlocale).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000548
549Error handling is set by errors which may also be set to *NULL* meaning to use
550the default handling defined for the codec. Default error handling for all
Georg Brandl22b34312009-07-26 14:54:51 +0000551built-in codecs is "strict" (:exc:`ValueError` is raised).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000552
553The codecs all use a similar interface. Only deviation from the following
554generic ones are documented for simplicity.
555
Georg Brandl54a3faa2008-01-20 09:30:57 +0000556
Victor Stinner77c38622010-05-14 15:58:55 +0000557Generic Codecs
558""""""""""""""
559
560These are the generic codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000561
562
Georg Brandl60203b42010-10-06 10:11:56 +0000563.. c:function:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000564
565 Create a Unicode object by decoding *size* bytes of the encoded string *s*.
566 *encoding* and *errors* have the same meaning as the parameters of the same name
Georg Brandl22b34312009-07-26 14:54:51 +0000567 in the :func:`unicode` built-in function. The codec to be used is looked up
Georg Brandl54a3faa2008-01-20 09:30:57 +0000568 using the Python codec registry. Return *NULL* if an exception was raised by
569 the codec.
570
571
Georg Brandl60203b42010-10-06 10:11:56 +0000572.. c:function:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000573
Ezio Melottic1f05772011-04-14 07:50:25 +0300574 Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* and return a Python
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000575 bytes object. *encoding* and *errors* have the same meaning as the
576 parameters of the same name in the Unicode :meth:`encode` method. The codec
577 to be used is looked up using the Python codec registry. Return *NULL* if an
578 exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000579
580
Georg Brandl60203b42010-10-06 10:11:56 +0000581.. c:function:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000582
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000583 Encode a Unicode object and return the result as Python bytes object.
584 *encoding* and *errors* have the same meaning as the parameters of the same
585 name in the Unicode :meth:`encode` method. The codec to be used is looked up
586 using the Python codec registry. Return *NULL* if an exception was raised by
587 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000588
Georg Brandl54a3faa2008-01-20 09:30:57 +0000589
Victor Stinner77c38622010-05-14 15:58:55 +0000590UTF-8 Codecs
591""""""""""""
592
593These are the UTF-8 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000594
595
Georg Brandl60203b42010-10-06 10:11:56 +0000596.. c:function:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000597
598 Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
599 *s*. Return *NULL* if an exception was raised by the codec.
600
601
Georg Brandl60203b42010-10-06 10:11:56 +0000602.. c:function:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000603
Georg Brandl60203b42010-10-06 10:11:56 +0000604 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF8`. If
Georg Brandl54a3faa2008-01-20 09:30:57 +0000605 *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
606 treated as an error. Those bytes will not be decoded and the number of bytes
607 that have been decoded will be stored in *consumed*.
608
609
Georg Brandl60203b42010-10-06 10:11:56 +0000610.. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000611
Ezio Melottic1f05772011-04-14 07:50:25 +0300612 Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* using UTF-8 and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000613 return a Python bytes object. Return *NULL* if an exception was raised by
614 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000615
616
Georg Brandl60203b42010-10-06 10:11:56 +0000617.. c:function:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000618
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000619 Encode a Unicode object using UTF-8 and return the result as Python bytes
620 object. Error handling is "strict". Return *NULL* if an exception was
621 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000622
Georg Brandl54a3faa2008-01-20 09:30:57 +0000623
Victor Stinner77c38622010-05-14 15:58:55 +0000624UTF-32 Codecs
625"""""""""""""
626
627These are the UTF-32 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000628
629
Georg Brandl60203b42010-10-06 10:11:56 +0000630.. c:function:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000631
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300632 Decode *size* bytes from a UTF-32 encoded buffer string and return the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000633 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
634 handling. It defaults to "strict".
635
636 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
637 order::
638
639 *byteorder == -1: little endian
640 *byteorder == 0: native order
641 *byteorder == 1: big endian
642
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000643 If ``*byteorder`` is zero, and the first four bytes of the input data are a
644 byte order mark (BOM), the decoder switches to this byte order and the BOM is
645 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
646 ``1``, any byte order mark is copied to the output.
647
648 After completion, *\*byteorder* is set to the current byte order at the end
649 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000650
651 In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.
652
653 If *byteorder* is *NULL*, the codec starts in native order mode.
654
655 Return *NULL* if an exception was raised by the codec.
656
657
Georg Brandl60203b42010-10-06 10:11:56 +0000658.. c:function:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000659
Georg Brandl60203b42010-10-06 10:11:56 +0000660 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF32`. If
661 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF32Stateful` will not treat
Georg Brandl54a3faa2008-01-20 09:30:57 +0000662 trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
663 by four) as an error. Those bytes will not be decoded and the number of bytes
664 that have been decoded will be stored in *consumed*.
665
666
Georg Brandl60203b42010-10-06 10:11:56 +0000667.. c:function:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000668
669 Return a Python bytes object holding the UTF-32 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000670 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000671
672 byteorder == -1: little endian
673 byteorder == 0: native byte order (writes a BOM mark)
674 byteorder == 1: big endian
675
676 If byteorder is ``0``, the output string will always start with the Unicode BOM
677 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
678
679 If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output
680 as a single codepoint.
681
682 Return *NULL* if an exception was raised by the codec.
683
684
Georg Brandl60203b42010-10-06 10:11:56 +0000685.. c:function:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000686
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000687 Return a Python byte string using the UTF-32 encoding in native byte
688 order. The string always starts with a BOM mark. Error handling is "strict".
689 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000690
691
Victor Stinner77c38622010-05-14 15:58:55 +0000692UTF-16 Codecs
693"""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000694
Victor Stinner77c38622010-05-14 15:58:55 +0000695These are the UTF-16 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000696
697
Georg Brandl60203b42010-10-06 10:11:56 +0000698.. c:function:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000699
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300700 Decode *size* bytes from a UTF-16 encoded buffer string and return the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000701 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
702 handling. It defaults to "strict".
703
704 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
705 order::
706
707 *byteorder == -1: little endian
708 *byteorder == 0: native order
709 *byteorder == 1: big endian
710
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000711 If ``*byteorder`` is zero, and the first two bytes of the input data are a
712 byte order mark (BOM), the decoder switches to this byte order and the BOM is
713 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
714 ``1``, any byte order mark is copied to the output (where it will result in
715 either a ``\ufeff`` or a ``\ufffe`` character).
716
717 After completion, *\*byteorder* is set to the current byte order at the end
718 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000719
720 If *byteorder* is *NULL*, the codec starts in native order mode.
721
722 Return *NULL* if an exception was raised by the codec.
723
724
Georg Brandl60203b42010-10-06 10:11:56 +0000725.. c:function:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000726
Georg Brandl60203b42010-10-06 10:11:56 +0000727 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF16`. If
728 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF16Stateful` will not treat
Georg Brandl54a3faa2008-01-20 09:30:57 +0000729 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
730 split surrogate pair) as an error. Those bytes will not be decoded and the
731 number of bytes that have been decoded will be stored in *consumed*.
732
733
Georg Brandl60203b42010-10-06 10:11:56 +0000734.. c:function:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000735
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000736 Return a Python bytes object holding the UTF-16 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000737 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000738
739 byteorder == -1: little endian
740 byteorder == 0: native byte order (writes a BOM mark)
741 byteorder == 1: big endian
742
743 If byteorder is ``0``, the output string will always start with the Unicode BOM
744 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
745
Georg Brandl60203b42010-10-06 10:11:56 +0000746 If *Py_UNICODE_WIDE* is defined, a single :c:type:`Py_UNICODE` value may get
747 represented as a surrogate pair. If it is not defined, each :c:type:`Py_UNICODE`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000748 values is interpreted as an UCS-2 character.
749
750 Return *NULL* if an exception was raised by the codec.
751
752
Georg Brandl60203b42010-10-06 10:11:56 +0000753.. c:function:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000754
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000755 Return a Python byte string using the UTF-16 encoding in native byte
756 order. The string always starts with a BOM mark. Error handling is "strict".
757 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000758
Georg Brandl54a3faa2008-01-20 09:30:57 +0000759
Georg Brandl8477f822010-08-02 20:05:19 +0000760UTF-7 Codecs
761""""""""""""
762
763These are the UTF-7 codec APIs:
764
765
Georg Brandl60203b42010-10-06 10:11:56 +0000766.. c:function:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl8477f822010-08-02 20:05:19 +0000767
768 Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string
769 *s*. Return *NULL* if an exception was raised by the codec.
770
771
Georg Brandl60203b42010-10-06 10:11:56 +0000772.. c:function:: PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
Georg Brandl8477f822010-08-02 20:05:19 +0000773
Georg Brandl60203b42010-10-06 10:11:56 +0000774 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF7`. If
Georg Brandl8477f822010-08-02 20:05:19 +0000775 *consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not
776 be treated as an error. Those bytes will not be decoded and the number of
777 bytes that have been decoded will be stored in *consumed*.
778
779
Georg Brandl60203b42010-10-06 10:11:56 +0000780.. c:function:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors)
Georg Brandl8477f822010-08-02 20:05:19 +0000781
Georg Brandl60203b42010-10-06 10:11:56 +0000782 Encode the :c:type:`Py_UNICODE` buffer of the given size using UTF-7 and
Georg Brandl8477f822010-08-02 20:05:19 +0000783 return a Python bytes object. Return *NULL* if an exception was raised by
784 the codec.
785
786 If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise
787 special meaning) will be encoded in base-64. If *base64WhiteSpace* is
788 nonzero, whitespace will be encoded in base-64. Both are set to zero for the
789 Python "utf-7" codec.
790
791
Victor Stinner77c38622010-05-14 15:58:55 +0000792Unicode-Escape Codecs
793"""""""""""""""""""""
794
795These are the "Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000796
797
Georg Brandl60203b42010-10-06 10:11:56 +0000798.. c:function:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000799
800 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
801 string *s*. Return *NULL* if an exception was raised by the codec.
802
803
Georg Brandl60203b42010-10-06 10:11:56 +0000804.. c:function:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000805
Ezio Melottic1f05772011-04-14 07:50:25 +0300806 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Unicode-Escape and
Georg Brandl54a3faa2008-01-20 09:30:57 +0000807 return a Python string object. Return *NULL* if an exception was raised by the
808 codec.
809
810
Georg Brandl60203b42010-10-06 10:11:56 +0000811.. c:function:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000812
813 Encode a Unicode object using Unicode-Escape and return the result as Python
814 string object. Error handling is "strict". Return *NULL* if an exception was
815 raised by the codec.
816
Georg Brandl54a3faa2008-01-20 09:30:57 +0000817
Victor Stinner77c38622010-05-14 15:58:55 +0000818Raw-Unicode-Escape Codecs
819"""""""""""""""""""""""""
820
821These are the "Raw Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000822
823
Georg Brandl60203b42010-10-06 10:11:56 +0000824.. c:function:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000825
826 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
827 encoded string *s*. Return *NULL* if an exception was raised by the codec.
828
829
Georg Brandl60203b42010-10-06 10:11:56 +0000830.. c:function:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000831
Ezio Melottic1f05772011-04-14 07:50:25 +0300832 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Raw-Unicode-Escape
Georg Brandl54a3faa2008-01-20 09:30:57 +0000833 and return a Python string object. Return *NULL* if an exception was raised by
834 the codec.
835
836
Georg Brandl60203b42010-10-06 10:11:56 +0000837.. c:function:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000838
839 Encode a Unicode object using Raw-Unicode-Escape and return the result as
840 Python string object. Error handling is "strict". Return *NULL* if an exception
841 was raised by the codec.
842
Victor Stinner77c38622010-05-14 15:58:55 +0000843
844Latin-1 Codecs
845""""""""""""""
846
Georg Brandl54a3faa2008-01-20 09:30:57 +0000847These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
848ordinals and only these are accepted by the codecs during encoding.
849
Georg Brandl54a3faa2008-01-20 09:30:57 +0000850
Georg Brandl60203b42010-10-06 10:11:56 +0000851.. c:function:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000852
853 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
854 *s*. Return *NULL* if an exception was raised by the codec.
855
856
Georg Brandl60203b42010-10-06 10:11:56 +0000857.. c:function:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000858
Ezio Melottic1f05772011-04-14 07:50:25 +0300859 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Latin-1 and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000860 return a Python bytes object. Return *NULL* if an exception was raised by
861 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000862
863
Georg Brandl60203b42010-10-06 10:11:56 +0000864.. c:function:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000865
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000866 Encode a Unicode object using Latin-1 and return the result as Python bytes
867 object. Error handling is "strict". Return *NULL* if an exception was
868 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000869
Victor Stinner77c38622010-05-14 15:58:55 +0000870
871ASCII Codecs
872""""""""""""
873
Georg Brandl54a3faa2008-01-20 09:30:57 +0000874These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
875codes generate errors.
876
Georg Brandl54a3faa2008-01-20 09:30:57 +0000877
Georg Brandl60203b42010-10-06 10:11:56 +0000878.. c:function:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000879
880 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
881 *s*. Return *NULL* if an exception was raised by the codec.
882
883
Georg Brandl60203b42010-10-06 10:11:56 +0000884.. c:function:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000885
Ezio Melottic1f05772011-04-14 07:50:25 +0300886 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using ASCII and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000887 return a Python bytes object. Return *NULL* if an exception was raised by
888 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000889
890
Georg Brandl60203b42010-10-06 10:11:56 +0000891.. c:function:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000892
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000893 Encode a Unicode object using ASCII and return the result as Python bytes
894 object. Error handling is "strict". Return *NULL* if an exception was
895 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000896
Georg Brandl54a3faa2008-01-20 09:30:57 +0000897
Victor Stinner77c38622010-05-14 15:58:55 +0000898Character Map Codecs
899""""""""""""""""""""
900
Georg Brandl54a3faa2008-01-20 09:30:57 +0000901This codec is special in that it can be used to implement many different codecs
902(and this is in fact what was done to obtain most of the standard codecs
903included in the :mod:`encodings` package). The codec uses mapping to encode and
904decode characters.
905
906Decoding mappings must map single string characters to single Unicode
907characters, integers (which are then interpreted as Unicode ordinals) or None
908(meaning "undefined mapping" and causing an error).
909
910Encoding mappings must map single Unicode characters to single string
911characters, integers (which are then interpreted as Latin-1 ordinals) or None
912(meaning "undefined mapping" and causing an error).
913
914The mapping objects provided must only support the __getitem__ mapping
915interface.
916
917If a character lookup fails with a LookupError, the character is copied as-is
918meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
919resp. Because of this, mappings only need to contain those mappings which map
920characters to different code points.
921
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300922These are the mapping codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000923
Georg Brandl60203b42010-10-06 10:11:56 +0000924.. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000925
926 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
927 the given *mapping* object. Return *NULL* if an exception was raised by the
928 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
929 dictionary mapping byte or a unicode string, which is treated as a lookup table.
930 Byte values greater that the length of the string and U+FFFE "characters" are
931 treated as "undefined mapping".
932
933
Georg Brandl60203b42010-10-06 10:11:56 +0000934.. c:function:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000935
Ezio Melottic1f05772011-04-14 07:50:25 +0300936 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using the given
Georg Brandl54a3faa2008-01-20 09:30:57 +0000937 *mapping* object and return a Python string object. Return *NULL* if an
938 exception was raised by the codec.
939
940
Georg Brandl60203b42010-10-06 10:11:56 +0000941.. c:function:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000942
943 Encode a Unicode object using the given *mapping* object and return the result
944 as Python string object. Error handling is "strict". Return *NULL* if an
945 exception was raised by the codec.
946
947The following codec API is special in that maps Unicode to Unicode.
948
949
Georg Brandl60203b42010-10-06 10:11:56 +0000950.. c:function:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000951
Ezio Melottic1f05772011-04-14 07:50:25 +0300952 Translate a :c:type:`Py_UNICODE` buffer of the given *size* by applying a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000953 character mapping *table* to it and return the resulting Unicode object. Return
954 *NULL* when an exception was raised by the codec.
955
956 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
957 integers or None (causing deletion of the character).
958
959 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
960 and sequences work well. Unmapped character ordinals (ones which cause a
961 :exc:`LookupError`) are left untouched and are copied as-is.
962
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000963
Victor Stinner77c38622010-05-14 15:58:55 +0000964
965MBCS codecs for Windows
966"""""""""""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000967
Georg Brandl54a3faa2008-01-20 09:30:57 +0000968These are the MBCS codec APIs. They are currently only available on Windows and
969use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
970DBCS) is a class of encodings, not just one. The target encoding is defined by
971the user settings on the machine running the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000972
Georg Brandl60203b42010-10-06 10:11:56 +0000973.. c:function:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000974
975 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
976 Return *NULL* if an exception was raised by the codec.
977
978
Georg Brandl60203b42010-10-06 10:11:56 +0000979.. c:function:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000980
Georg Brandl60203b42010-10-06 10:11:56 +0000981 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeMBCS`. If
982 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeMBCSStateful` will not decode
Georg Brandl54a3faa2008-01-20 09:30:57 +0000983 trailing lead byte and the number of bytes that have been decoded will be stored
984 in *consumed*.
985
986
Georg Brandl60203b42010-10-06 10:11:56 +0000987.. c:function:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000988
Ezio Melottic1f05772011-04-14 07:50:25 +0300989 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using MBCS and return
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000990 a Python bytes object. Return *NULL* if an exception was raised by the
991 codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000992
993
Georg Brandl60203b42010-10-06 10:11:56 +0000994.. c:function:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000995
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000996 Encode a Unicode object using MBCS and return the result as Python bytes
997 object. Error handling is "strict". Return *NULL* if an exception was
998 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000999
Martin v. Löwisc15bdef2009-05-29 14:47:46 +00001000
Victor Stinner77c38622010-05-14 15:58:55 +00001001Methods & Slots
1002"""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +00001003
1004
1005.. _unicodemethodsandslots:
1006
1007Methods and Slot Functions
1008^^^^^^^^^^^^^^^^^^^^^^^^^^
1009
1010The following APIs are capable of handling Unicode objects and strings on input
1011(we refer to them as strings in the descriptions) and return Unicode objects or
1012integers as appropriate.
1013
1014They all return *NULL* or ``-1`` if an exception occurs.
1015
1016
Georg Brandl60203b42010-10-06 10:11:56 +00001017.. c:function:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001018
1019 Concat two strings giving a new Unicode string.
1020
1021
Georg Brandl60203b42010-10-06 10:11:56 +00001022.. c:function:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001023
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001024 Split a string giving a list of Unicode strings. If *sep* is *NULL*, splitting
Georg Brandl54a3faa2008-01-20 09:30:57 +00001025 will be done at all whitespace substrings. Otherwise, splits occur at the given
1026 separator. At most *maxsplit* splits will be done. If negative, no limit is
1027 set. Separators are not included in the resulting list.
1028
1029
Georg Brandl60203b42010-10-06 10:11:56 +00001030.. c:function:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001031
1032 Split a Unicode string at line breaks, returning a list of Unicode strings.
1033 CRLF is considered to be one line break. If *keepend* is 0, the Line break
1034 characters are not included in the resulting strings.
1035
1036
Georg Brandl60203b42010-10-06 10:11:56 +00001037.. c:function:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001038
1039 Translate a string by applying a character mapping table to it and return the
1040 resulting Unicode object.
1041
1042 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
1043 or None (causing deletion of the character).
1044
1045 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
1046 and sequences work well. Unmapped character ordinals (ones which cause a
1047 :exc:`LookupError`) are left untouched and are copied as-is.
1048
1049 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
1050 use the default error handling.
1051
1052
Georg Brandl60203b42010-10-06 10:11:56 +00001053.. c:function:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001054
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001055 Join a sequence of strings using the given *separator* and return the resulting
Georg Brandl54a3faa2008-01-20 09:30:57 +00001056 Unicode string.
1057
1058
Georg Brandl60203b42010-10-06 10:11:56 +00001059.. c:function:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001060
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001061 Return 1 if *substr* matches ``str[start:end]`` at the given tail end
Georg Brandl54a3faa2008-01-20 09:30:57 +00001062 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
1063 0 otherwise. Return ``-1`` if an error occurred.
1064
1065
Georg Brandl60203b42010-10-06 10:11:56 +00001066.. c:function:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001067
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001068 Return the first position of *substr* in ``str[start:end]`` using the given
Georg Brandl54a3faa2008-01-20 09:30:57 +00001069 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
1070 backward search). The return value is the index of the first match; a value of
1071 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
1072 occurred and an exception has been set.
1073
1074
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001075.. c:function:: Py_ssize_t PyUnicode_FindChar(PyObject *str, Py_UCS4 ch, Py_ssize_t start, Py_ssize_t end, int direction)
1076
1077 Return the first position of the character *ch* in ``str[start:end]`` using
1078 the given *direction* (*direction* == 1 means to do a forward search,
1079 *direction* == -1 a backward search). The return value is the index of the
1080 first match; a value of ``-1`` indicates that no match was found, and ``-2``
1081 indicates that an error occurred and an exception has been set.
1082
Georg Brandlee12f442011-09-28 21:51:06 +02001083 .. versionadded:: 3.3
1084
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001085
Georg Brandl60203b42010-10-06 10:11:56 +00001086.. c:function:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001087
1088 Return the number of non-overlapping occurrences of *substr* in
1089 ``str[start:end]``. Return ``-1`` if an error occurred.
1090
1091
Georg Brandl60203b42010-10-06 10:11:56 +00001092.. c:function:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001093
1094 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
1095 return the resulting Unicode object. *maxcount* == -1 means replace all
1096 occurrences.
1097
1098
Georg Brandl60203b42010-10-06 10:11:56 +00001099.. c:function:: int PyUnicode_Compare(PyObject *left, PyObject *right)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001100
1101 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1102 respectively.
1103
1104
Georg Brandl60203b42010-10-06 10:11:56 +00001105.. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string)
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001106
1107 Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less
Victor Stinner80e788a2010-12-28 23:39:51 +00001108 than, equal, and greater than, respectively. It is best to pass only
1109 ASCII-encoded strings, but the function interprets the input string as
1110 ISO-8859-1 if it contains non-ASCII characters".
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001111
1112
Georg Brandl60203b42010-10-06 10:11:56 +00001113.. c:function:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001114
1115 Rich compare two unicode strings and return one of the following:
1116
1117 * ``NULL`` in case an exception was raised
1118 * :const:`Py_True` or :const:`Py_False` for successful comparisons
1119 * :const:`Py_NotImplemented` in case the type combination is unknown
1120
1121 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
1122 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
1123 with a :exc:`UnicodeDecodeError`.
1124
1125 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
1126 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
1127
1128
Georg Brandl60203b42010-10-06 10:11:56 +00001129.. c:function:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001130
1131 Return a new string object from *format* and *args*; this is analogous to
1132 ``format % args``. The *args* argument must be a tuple.
1133
1134
Georg Brandl60203b42010-10-06 10:11:56 +00001135.. c:function:: int PyUnicode_Contains(PyObject *container, PyObject *element)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001136
1137 Check whether *element* is contained in *container* and return true or false
1138 accordingly.
1139
1140 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1141 there was an error.
1142
1143
Georg Brandl60203b42010-10-06 10:11:56 +00001144.. c:function:: void PyUnicode_InternInPlace(PyObject **string)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001145
1146 Intern the argument *\*string* in place. The argument must be the address of a
1147 pointer variable pointing to a Python unicode string object. If there is an
1148 existing interned string that is the same as *\*string*, it sets *\*string* to
1149 it (decrementing the reference count of the old string object and incrementing
1150 the reference count of the interned string object), otherwise it leaves
1151 *\*string* alone and interns it (incrementing its reference count).
1152 (Clarification: even though there is a lot of talk about reference counts, think
1153 of this function as reference-count-neutral; you own the object after the call
1154 if and only if you owned it before the call.)
1155
1156
Georg Brandl60203b42010-10-06 10:11:56 +00001157.. c:function:: PyObject* PyUnicode_InternFromString(const char *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001158
Georg Brandl60203b42010-10-06 10:11:56 +00001159 A combination of :c:func:`PyUnicode_FromString` and
1160 :c:func:`PyUnicode_InternInPlace`, returning either a new unicode string object
Georg Brandl54a3faa2008-01-20 09:30:57 +00001161 that has been interned, or a new ("owned") reference to an earlier interned
1162 string object with the same value.
1163