blob: 35006547c6469020a91e42118a85daa7f62a22ff [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
198Plain Py_UNICODE
199""""""""""""""""
200
Georg Brandl54a3faa2008-01-20 09:30:57 +0000201To create Unicode objects and access their basic sequence properties, use these
202APIs:
203
Georg Brandl54a3faa2008-01-20 09:30:57 +0000204
Georg Brandl60203b42010-10-06 10:11:56 +0000205.. c:function:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000206
Georg Brandl418cc732010-10-17 10:53:54 +0000207 Create a Unicode object from the Py_UNICODE buffer *u* of the given size. *u*
Georg Brandl54a3faa2008-01-20 09:30:57 +0000208 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 Brandl60203b42010-10-06 10:11:56 +0000215.. c:function:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000216
Georg Brandl418cc732010-10-17 10:53:54 +0000217 Create a Unicode object from the char buffer *u*. The bytes will be interpreted
Georg Brandl54a3faa2008-01-20 09:30:57 +0000218 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 Brandl60203b42010-10-06 10:11:56 +0000225.. c:function:: PyObject *PyUnicode_FromString(const char *u)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000226
227 Create a Unicode object from an UTF-8 encoded null-terminated char buffer
228 *u*.
229
230
Georg Brandl60203b42010-10-06 10:11:56 +0000231.. c:function:: PyObject* PyUnicode_FromFormat(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000232
Georg Brandl60203b42010-10-06 10:11:56 +0000233 Take a C :c:func:`printf`\ -style *format* string and a variable number of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000234 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 Stinner1205f272010-09-11 00:54:47 +0000237 ASCII-encoded string. The following format characters are allowed:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000238
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000239 .. % This should be exactly the same as the table in PyErr_Format.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000240 .. % 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 Dickinson6ce4a9a2009-11-16 17:00:11 +0000243 .. % Similar comments apply to the %ll width modifier and
244 .. % PY_FORMAT_LONG_LONG.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000245
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 +-------------------+---------------------+--------------------------------+
263 | :attr:`%lu` | unsigned long | Exactly equivalent to |
264 | | | ``printf("%lu")``. |
265 +-------------------+---------------------+--------------------------------+
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000266 | :attr:`%lld` | long long | Exactly equivalent to |
267 | | | ``printf("%lld")``. |
268 +-------------------+---------------------+--------------------------------+
269 | :attr:`%llu` | unsigned long long | Exactly equivalent to |
270 | | | ``printf("%llu")``. |
271 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000272 | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
273 | | | ``printf("%zd")``. |
274 +-------------------+---------------------+--------------------------------+
275 | :attr:`%zu` | size_t | Exactly equivalent to |
276 | | | ``printf("%zu")``. |
277 +-------------------+---------------------+--------------------------------+
278 | :attr:`%i` | int | Exactly equivalent to |
279 | | | ``printf("%i")``. |
280 +-------------------+---------------------+--------------------------------+
281 | :attr:`%x` | int | Exactly equivalent to |
282 | | | ``printf("%x")``. |
283 +-------------------+---------------------+--------------------------------+
284 | :attr:`%s` | char\* | A null-terminated C character |
285 | | | array. |
286 +-------------------+---------------------+--------------------------------+
287 | :attr:`%p` | void\* | The hex representation of a C |
288 | | | pointer. Mostly equivalent to |
289 | | | ``printf("%p")`` except that |
290 | | | it is guaranteed to start with |
291 | | | the literal ``0x`` regardless |
292 | | | of what the platform's |
293 | | | ``printf`` yields. |
294 +-------------------+---------------------+--------------------------------+
Georg Brandl559e5d72008-06-11 18:37:52 +0000295 | :attr:`%A` | PyObject\* | The result of calling |
296 | | | :func:`ascii`. |
297 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000298 | :attr:`%U` | PyObject\* | A unicode object. |
299 +-------------------+---------------------+--------------------------------+
300 | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be |
301 | | | *NULL*) and a null-terminated |
302 | | | C character array as a second |
303 | | | parameter (which will be used, |
304 | | | if the first parameter is |
305 | | | *NULL*). |
306 +-------------------+---------------------+--------------------------------+
307 | :attr:`%S` | PyObject\* | The result of calling |
Georg Brandl60203b42010-10-06 10:11:56 +0000308 | | | :c:func:`PyObject_Str`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309 +-------------------+---------------------+--------------------------------+
310 | :attr:`%R` | PyObject\* | The result of calling |
Georg Brandl60203b42010-10-06 10:11:56 +0000311 | | | :c:func:`PyObject_Repr`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000312 +-------------------+---------------------+--------------------------------+
313
314 An unrecognized format character causes all the rest of the format string to be
315 copied as-is to the result string, and any extra arguments discarded.
316
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000317 .. note::
318
319 The `"%lld"` and `"%llu"` format specifiers are only available
Georg Brandlef871f62010-03-12 10:06:40 +0000320 when :const:`HAVE_LONG_LONG` is defined.
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000321
322 .. versionchanged:: 3.2
Georg Brandl67b21b72010-08-17 15:07:14 +0000323 Support for ``"%lld"`` and ``"%llu"`` added.
Mark Dickinson6ce4a9a2009-11-16 17:00:11 +0000324
Georg Brandl54a3faa2008-01-20 09:30:57 +0000325
Georg Brandl60203b42010-10-06 10:11:56 +0000326.. c:function:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000327
Georg Brandl60203b42010-10-06 10:11:56 +0000328 Identical to :c:func:`PyUnicode_FromFormat` except that it takes exactly two
Georg Brandl54a3faa2008-01-20 09:30:57 +0000329 arguments.
330
Alexander Belopolsky942af5a2010-12-04 03:38:46 +0000331.. c:function:: PyObject* PyUnicode_TransformDecimalToASCII(Py_UNICODE *s, Py_ssize_t size)
332
333 Create a Unicode object by replacing all decimal digits in
Ezio Melottic1f05772011-04-14 07:50:25 +0300334 :c:type:`Py_UNICODE` buffer of the given *size* by ASCII digits 0--9
Alexander Belopolsky942af5a2010-12-04 03:38:46 +0000335 according to their decimal value. Return *NULL* if an exception
336 occurs.
337
Georg Brandl54a3faa2008-01-20 09:30:57 +0000338
Georg Brandl60203b42010-10-06 10:11:56 +0000339.. c:function:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000340
Victor Stinner6fbd5252011-12-18 19:22:31 +0100341 Return a read-only pointer to the Unicode object's internal
342 :c:type:`Py_UNICODE` buffer, *NULL* if *unicode* is not a Unicode object.
343 Note that the resulting :c:type:`Py_UNICODE*` string may contain embedded
344 null characters, which would cause the string to be truncated when used in
345 most C functions.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000346
347
Georg Brandl60203b42010-10-06 10:11:56 +0000348.. c:function:: Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode)
Victor Stinnere4ea9942010-09-03 16:23:29 +0000349
Ezio Melottic1f05772011-04-14 07:50:25 +0300350 Create a copy of a Unicode string ending with a nul character. Return *NULL*
Victor Stinnere4ea9942010-09-03 16:23:29 +0000351 and raise a :exc:`MemoryError` exception on memory allocation failure,
Victor Stinner6fbd5252011-12-18 19:22:31 +0100352 otherwise return a new allocated buffer (use :c:func:`PyMem_Free` to free
353 the buffer). Note that the resulting :c:type:`Py_UNICODE*` string may contain
354 embedded null characters, which would cause the string to be truncated when
355 used in most C functions.
Victor Stinnere4ea9942010-09-03 16:23:29 +0000356
Victor Stinner2b19f352010-09-03 22:13:42 +0000357 .. versionadded:: 3.2
358
Victor Stinnere4ea9942010-09-03 16:23:29 +0000359
Georg Brandl60203b42010-10-06 10:11:56 +0000360.. c:function:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000361
362 Return the length of the Unicode object.
363
364
Georg Brandl60203b42010-10-06 10:11:56 +0000365.. c:function:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000366
367 Coerce an encoded object *obj* to an Unicode object and return a reference with
368 incremented refcount.
369
Georg Brandl952867a2010-06-27 10:17:12 +0000370 :class:`bytes`, :class:`bytearray` and other char buffer compatible objects
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300371 are decoded according to the given *encoding* and using the error handling
372 defined by *errors*. Both can be *NULL* to have the interface use the default
Georg Brandl952867a2010-06-27 10:17:12 +0000373 values (see the next section for details).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000374
375 All other objects, including Unicode objects, cause a :exc:`TypeError` to be
376 set.
377
378 The API returns *NULL* if there was an error. The caller is responsible for
379 decref'ing the returned objects.
380
381
Georg Brandl60203b42010-10-06 10:11:56 +0000382.. c:function:: PyObject* PyUnicode_FromObject(PyObject *obj)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000383
384 Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
385 throughout the interpreter whenever coercion to Unicode is needed.
386
Georg Brandl60203b42010-10-06 10:11:56 +0000387If the platform supports :c:type:`wchar_t` and provides a header file wchar.h,
Georg Brandl54a3faa2008-01-20 09:30:57 +0000388Python can interface directly to this type using the following functions.
Georg Brandl60203b42010-10-06 10:11:56 +0000389Support is optimized if Python's own :c:type:`Py_UNICODE` type is identical to
390the system's :c:type:`wchar_t`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000391
Georg Brandl54a3faa2008-01-20 09:30:57 +0000392
Victor Stinner77c38622010-05-14 15:58:55 +0000393File System Encoding
394""""""""""""""""""""
395
396To encode and decode file names and other environment strings,
Georg Brandl60203b42010-10-06 10:11:56 +0000397:c:data:`Py_FileSystemEncoding` should be used as the encoding, and
Victor Stinner77c38622010-05-14 15:58:55 +0000398``"surrogateescape"`` should be used as the error handler (:pep:`383`). To
399encode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl60203b42010-10-06 10:11:56 +0000400used, passing :c:func:`PyUnicode_FSConverter` as the conversion function:
Victor Stinner77c38622010-05-14 15:58:55 +0000401
Georg Brandl60203b42010-10-06 10:11:56 +0000402.. c:function:: int PyUnicode_FSConverter(PyObject* obj, void* result)
Victor Stinner77c38622010-05-14 15:58:55 +0000403
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000404 ParseTuple converter: encode :class:`str` objects to :class:`bytes` using
Georg Brandl60203b42010-10-06 10:11:56 +0000405 :c:func:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
406 *result* must be a :c:type:`PyBytesObject*` which must be released when it is
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000407 no longer used.
Victor Stinner77c38622010-05-14 15:58:55 +0000408
409 .. versionadded:: 3.1
410
Georg Brandl67b21b72010-08-17 15:07:14 +0000411
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000412To decode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl60203b42010-10-06 10:11:56 +0000413used, passing :c:func:`PyUnicode_FSDecoder` as the conversion function:
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000414
Georg Brandl60203b42010-10-06 10:11:56 +0000415.. c:function:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000416
417 ParseTuple converter: decode :class:`bytes` objects to :class:`str` using
Georg Brandl60203b42010-10-06 10:11:56 +0000418 :c:func:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str` objects are output
419 as-is. *result* must be a :c:type:`PyUnicodeObject*` which must be released
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000420 when it is no longer used.
421
422 .. versionadded:: 3.2
423
Georg Brandl67b21b72010-08-17 15:07:14 +0000424
Georg Brandl60203b42010-10-06 10:11:56 +0000425.. c:function:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
Victor Stinner77c38622010-05-14 15:58:55 +0000426
Victor Stinner62165d62010-10-09 10:34:37 +0000427 Decode a string using :c:data:`Py_FileSystemDefaultEncoding` and the
428 ``'surrogateescape'`` error handler, or ``'strict'`` on Windows.
429
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000430 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
431 locale encoding.
Victor Stinner62165d62010-10-09 10:34:37 +0000432
433 .. versionchanged:: 3.2
434 Use ``'strict'`` error handler on Windows.
435
436
437.. c:function:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
438
Georg Brandl60203b42010-10-06 10:11:56 +0000439 Decode a null-terminated string using :c:data:`Py_FileSystemDefaultEncoding`
Victor Stinner62165d62010-10-09 10:34:37 +0000440 and the ``'surrogateescape'`` error handler, or ``'strict'`` on Windows.
Victor Stinner77c38622010-05-14 15:58:55 +0000441
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000442 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
443 locale encoding.
Victor Stinner77c38622010-05-14 15:58:55 +0000444
Georg Brandl60203b42010-10-06 10:11:56 +0000445 Use :c:func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
Victor Stinner77c38622010-05-14 15:58:55 +0000446
Victor Stinner62165d62010-10-09 10:34:37 +0000447 .. versionchanged:: 3.2
448 Use ``'strict'`` error handler on Windows.
Victor Stinner77c38622010-05-14 15:58:55 +0000449
450
Georg Brandl60203b42010-10-06 10:11:56 +0000451.. c:function:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
Victor Stinnerae6265f2010-05-15 16:27:27 +0000452
Georg Brandl60203b42010-10-06 10:11:56 +0000453 Encode a Unicode object to :c:data:`Py_FileSystemDefaultEncoding` with the
Victor Stinner62165d62010-10-09 10:34:37 +0000454 ``'surrogateescape'`` error handler, or ``'strict'`` on Windows, and return
Victor Stinner6fbd5252011-12-18 19:22:31 +0100455 :class:`bytes`. Note that the resulting :class:`bytes` object may contain
456 null bytes.
Victor Stinnerae6265f2010-05-15 16:27:27 +0000457
Victor Stinnerf3170cc2010-10-15 12:04:23 +0000458 If :c:data:`Py_FileSystemDefaultEncoding` is not set, fall back to the
459 locale encoding.
Victor Stinnerae6265f2010-05-15 16:27:27 +0000460
461 .. versionadded:: 3.2
462
463
Victor Stinner77c38622010-05-14 15:58:55 +0000464wchar_t Support
465"""""""""""""""
466
Ezio Melottic1f05772011-04-14 07:50:25 +0300467:c:type:`wchar_t` support for platforms which support it:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000468
Georg Brandl60203b42010-10-06 10:11:56 +0000469.. c:function:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000470
Ezio Melottic1f05772011-04-14 07:50:25 +0300471 Create a Unicode object from the :c:type:`wchar_t` buffer *w* of the given *size*.
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300472 Passing -1 as the *size* indicates that the function must itself compute the length,
Martin v. Löwis790465f2008-04-05 20:41:37 +0000473 using wcslen.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000474 Return *NULL* on failure.
475
476
Georg Brandl60203b42010-10-06 10:11:56 +0000477.. c:function:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000478
Georg Brandl60203b42010-10-06 10:11:56 +0000479 Copy the Unicode object contents into the :c:type:`wchar_t` buffer *w*. At most
480 *size* :c:type:`wchar_t` characters are copied (excluding a possibly trailing
481 0-termination character). Return the number of :c:type:`wchar_t` characters
482 copied or -1 in case of an error. Note that the resulting :c:type:`wchar_t`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000483 string may or may not be 0-terminated. It is the responsibility of the caller
Georg Brandl60203b42010-10-06 10:11:56 +0000484 to make sure that the :c:type:`wchar_t` string is 0-terminated in case this is
Victor Stinner6fbd5252011-12-18 19:22:31 +0100485 required by the application. Also, note that the :c:type:`wchar_t*` string
486 might contain null characters, which would cause the string to be truncated
487 when used with most C functions.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000488
489
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000490.. c:function:: wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)
Victor Stinner137c34c2010-09-29 10:25:54 +0000491
492 Convert the Unicode object to a wide character string. The output string
493 always ends with a nul character. If *size* is not *NULL*, write the number
Victor Stinner1c24bd02010-10-02 11:03:13 +0000494 of wide characters (excluding the trailing 0-termination character) into
495 *\*size*.
Victor Stinner137c34c2010-09-29 10:25:54 +0000496
Victor Stinner6fbd5252011-12-18 19:22:31 +0100497 Returns a buffer allocated by :c:func:`PyMem_Alloc` (use
498 :c:func:`PyMem_Free` to free it) on success. On error, returns *NULL*,
499 *\*size* is undefined and raises a :exc:`MemoryError`. Note that the
500 resulting :c:type:`wchar_t*` string might contain null characters, which
501 would cause the string to be truncated when used with most C functions.
Victor Stinner137c34c2010-09-29 10:25:54 +0000502
503 .. versionadded:: 3.2
504
505
Georg Brandl54a3faa2008-01-20 09:30:57 +0000506.. _builtincodecs:
507
508Built-in Codecs
509^^^^^^^^^^^^^^^
510
Georg Brandl22b34312009-07-26 14:54:51 +0000511Python provides a set of built-in codecs which are written in C for speed. All of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000512these codecs are directly usable via the following functions.
513
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300514Many of the following APIs take two arguments encoding and errors, and they
515have the same semantics as the ones of the built-in :func:`str` string object
516constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000517
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000518Setting encoding to *NULL* causes the default encoding to be used
519which is ASCII. The file system calls should use
Georg Brandl60203b42010-10-06 10:11:56 +0000520:c:func:`PyUnicode_FSConverter` for encoding file names. This uses the
521variable :c:data:`Py_FileSystemDefaultEncoding` internally. This
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300522variable should be treated as read-only: on some systems, it will be a
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000523pointer to a static string, on others, it will change at run-time
524(such as when the application invokes setlocale).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000525
526Error handling is set by errors which may also be set to *NULL* meaning to use
527the default handling defined for the codec. Default error handling for all
Georg Brandl22b34312009-07-26 14:54:51 +0000528built-in codecs is "strict" (:exc:`ValueError` is raised).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000529
530The codecs all use a similar interface. Only deviation from the following
531generic ones are documented for simplicity.
532
Georg Brandl54a3faa2008-01-20 09:30:57 +0000533
Victor Stinner77c38622010-05-14 15:58:55 +0000534Generic Codecs
535""""""""""""""
536
537These are the generic codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000538
539
Georg Brandl60203b42010-10-06 10:11:56 +0000540.. 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 +0000541
542 Create a Unicode object by decoding *size* bytes of the encoded string *s*.
543 *encoding* and *errors* have the same meaning as the parameters of the same name
Georg Brandl22b34312009-07-26 14:54:51 +0000544 in the :func:`unicode` built-in function. The codec to be used is looked up
Georg Brandl54a3faa2008-01-20 09:30:57 +0000545 using the Python codec registry. Return *NULL* if an exception was raised by
546 the codec.
547
548
Georg Brandl60203b42010-10-06 10:11:56 +0000549.. 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 +0000550
Ezio Melottic1f05772011-04-14 07:50:25 +0300551 Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* and return a Python
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000552 bytes object. *encoding* and *errors* have the same meaning as the
553 parameters of the same name in the Unicode :meth:`encode` method. The codec
554 to be used is looked up using the Python codec registry. Return *NULL* if an
555 exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000556
557
Georg Brandl60203b42010-10-06 10:11:56 +0000558.. c:function:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000559
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000560 Encode a Unicode object and return the result as Python bytes object.
561 *encoding* and *errors* have the same meaning as the parameters of the same
562 name in the Unicode :meth:`encode` method. The codec to be used is looked up
563 using the Python codec registry. Return *NULL* if an exception was raised by
564 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000565
Georg Brandl54a3faa2008-01-20 09:30:57 +0000566
Victor Stinner77c38622010-05-14 15:58:55 +0000567UTF-8 Codecs
568""""""""""""
569
570These are the UTF-8 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000571
572
Georg Brandl60203b42010-10-06 10:11:56 +0000573.. c:function:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000574
575 Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
576 *s*. Return *NULL* if an exception was raised by the codec.
577
578
Georg Brandl60203b42010-10-06 10:11:56 +0000579.. 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 +0000580
Georg Brandl60203b42010-10-06 10:11:56 +0000581 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF8`. If
Georg Brandl54a3faa2008-01-20 09:30:57 +0000582 *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
583 treated as an error. Those bytes will not be decoded and the number of bytes
584 that have been decoded will be stored in *consumed*.
585
586
Georg Brandl60203b42010-10-06 10:11:56 +0000587.. c:function:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000588
Ezio Melottic1f05772011-04-14 07:50:25 +0300589 Encode the :c:type:`Py_UNICODE` buffer *s* of the given *size* using UTF-8 and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000590 return a Python bytes object. Return *NULL* if an exception was raised by
591 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000592
593
Georg Brandl60203b42010-10-06 10:11:56 +0000594.. c:function:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000595
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000596 Encode a Unicode object using UTF-8 and return the result as Python bytes
597 object. Error handling is "strict". Return *NULL* if an exception was
598 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000599
Georg Brandl54a3faa2008-01-20 09:30:57 +0000600
Victor Stinner77c38622010-05-14 15:58:55 +0000601UTF-32 Codecs
602"""""""""""""
603
604These are the UTF-32 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000605
606
Georg Brandl60203b42010-10-06 10:11:56 +0000607.. c:function:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000608
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300609 Decode *size* bytes from a UTF-32 encoded buffer string and return the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000610 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
611 handling. It defaults to "strict".
612
613 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
614 order::
615
616 *byteorder == -1: little endian
617 *byteorder == 0: native order
618 *byteorder == 1: big endian
619
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000620 If ``*byteorder`` is zero, and the first four bytes of the input data are a
621 byte order mark (BOM), the decoder switches to this byte order and the BOM is
622 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
623 ``1``, any byte order mark is copied to the output.
624
625 After completion, *\*byteorder* is set to the current byte order at the end
626 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000627
628 In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.
629
630 If *byteorder* is *NULL*, the codec starts in native order mode.
631
632 Return *NULL* if an exception was raised by the codec.
633
634
Georg Brandl60203b42010-10-06 10:11:56 +0000635.. 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 +0000636
Georg Brandl60203b42010-10-06 10:11:56 +0000637 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF32`. If
638 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF32Stateful` will not treat
Georg Brandl54a3faa2008-01-20 09:30:57 +0000639 trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
640 by four) as an error. Those bytes will not be decoded and the number of bytes
641 that have been decoded will be stored in *consumed*.
642
643
Georg Brandl60203b42010-10-06 10:11:56 +0000644.. 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 +0000645
646 Return a Python bytes object holding the UTF-32 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000647 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000648
649 byteorder == -1: little endian
650 byteorder == 0: native byte order (writes a BOM mark)
651 byteorder == 1: big endian
652
653 If byteorder is ``0``, the output string will always start with the Unicode BOM
654 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
655
656 If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output
657 as a single codepoint.
658
659 Return *NULL* if an exception was raised by the codec.
660
661
Georg Brandl60203b42010-10-06 10:11:56 +0000662.. c:function:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000663
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000664 Return a Python byte string using the UTF-32 encoding in native byte
665 order. The string always starts with a BOM mark. Error handling is "strict".
666 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000667
668
Victor Stinner77c38622010-05-14 15:58:55 +0000669UTF-16 Codecs
670"""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000671
Victor Stinner77c38622010-05-14 15:58:55 +0000672These are the UTF-16 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000673
674
Georg Brandl60203b42010-10-06 10:11:56 +0000675.. c:function:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000676
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300677 Decode *size* bytes from a UTF-16 encoded buffer string and return the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000678 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
679 handling. It defaults to "strict".
680
681 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
682 order::
683
684 *byteorder == -1: little endian
685 *byteorder == 0: native order
686 *byteorder == 1: big endian
687
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000688 If ``*byteorder`` is zero, and the first two bytes of the input data are a
689 byte order mark (BOM), the decoder switches to this byte order and the BOM is
690 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
691 ``1``, any byte order mark is copied to the output (where it will result in
692 either a ``\ufeff`` or a ``\ufffe`` character).
693
694 After completion, *\*byteorder* is set to the current byte order at the end
695 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000696
697 If *byteorder* is *NULL*, the codec starts in native order mode.
698
699 Return *NULL* if an exception was raised by the codec.
700
701
Georg Brandl60203b42010-10-06 10:11:56 +0000702.. 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 +0000703
Georg Brandl60203b42010-10-06 10:11:56 +0000704 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF16`. If
705 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeUTF16Stateful` will not treat
Georg Brandl54a3faa2008-01-20 09:30:57 +0000706 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
707 split surrogate pair) as an error. Those bytes will not be decoded and the
708 number of bytes that have been decoded will be stored in *consumed*.
709
710
Georg Brandl60203b42010-10-06 10:11:56 +0000711.. 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 +0000712
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000713 Return a Python bytes object holding the UTF-16 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000714 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000715
716 byteorder == -1: little endian
717 byteorder == 0: native byte order (writes a BOM mark)
718 byteorder == 1: big endian
719
720 If byteorder is ``0``, the output string will always start with the Unicode BOM
721 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
722
Georg Brandl60203b42010-10-06 10:11:56 +0000723 If *Py_UNICODE_WIDE* is defined, a single :c:type:`Py_UNICODE` value may get
724 represented as a surrogate pair. If it is not defined, each :c:type:`Py_UNICODE`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000725 values is interpreted as an UCS-2 character.
726
727 Return *NULL* if an exception was raised by the codec.
728
729
Georg Brandl60203b42010-10-06 10:11:56 +0000730.. c:function:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000731
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000732 Return a Python byte string using the UTF-16 encoding in native byte
733 order. The string always starts with a BOM mark. Error handling is "strict".
734 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000735
Georg Brandl54a3faa2008-01-20 09:30:57 +0000736
Georg Brandl8477f822010-08-02 20:05:19 +0000737UTF-7 Codecs
738""""""""""""
739
740These are the UTF-7 codec APIs:
741
742
Georg Brandl60203b42010-10-06 10:11:56 +0000743.. c:function:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl8477f822010-08-02 20:05:19 +0000744
745 Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string
746 *s*. Return *NULL* if an exception was raised by the codec.
747
748
Georg Brandl60203b42010-10-06 10:11:56 +0000749.. 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 +0000750
Georg Brandl60203b42010-10-06 10:11:56 +0000751 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeUTF7`. If
Georg Brandl8477f822010-08-02 20:05:19 +0000752 *consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not
753 be treated as an error. Those bytes will not be decoded and the number of
754 bytes that have been decoded will be stored in *consumed*.
755
756
Georg Brandl60203b42010-10-06 10:11:56 +0000757.. 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 +0000758
Georg Brandl60203b42010-10-06 10:11:56 +0000759 Encode the :c:type:`Py_UNICODE` buffer of the given size using UTF-7 and
Georg Brandl8477f822010-08-02 20:05:19 +0000760 return a Python bytes object. Return *NULL* if an exception was raised by
761 the codec.
762
763 If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise
764 special meaning) will be encoded in base-64. If *base64WhiteSpace* is
765 nonzero, whitespace will be encoded in base-64. Both are set to zero for the
766 Python "utf-7" codec.
767
768
Victor Stinner77c38622010-05-14 15:58:55 +0000769Unicode-Escape Codecs
770"""""""""""""""""""""
771
772These are the "Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000773
774
Georg Brandl60203b42010-10-06 10:11:56 +0000775.. c:function:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000776
777 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
778 string *s*. Return *NULL* if an exception was raised by the codec.
779
780
Georg Brandl60203b42010-10-06 10:11:56 +0000781.. c:function:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000782
Ezio Melottic1f05772011-04-14 07:50:25 +0300783 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Unicode-Escape and
Georg Brandl54a3faa2008-01-20 09:30:57 +0000784 return a Python string object. Return *NULL* if an exception was raised by the
785 codec.
786
787
Georg Brandl60203b42010-10-06 10:11:56 +0000788.. c:function:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000789
790 Encode a Unicode object using Unicode-Escape and return the result as Python
791 string object. Error handling is "strict". Return *NULL* if an exception was
792 raised by the codec.
793
Georg Brandl54a3faa2008-01-20 09:30:57 +0000794
Victor Stinner77c38622010-05-14 15:58:55 +0000795Raw-Unicode-Escape Codecs
796"""""""""""""""""""""""""
797
798These are the "Raw Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000799
800
Georg Brandl60203b42010-10-06 10:11:56 +0000801.. c:function:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000802
803 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
804 encoded string *s*. Return *NULL* if an exception was raised by the codec.
805
806
Georg Brandl60203b42010-10-06 10:11:56 +0000807.. c:function:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000808
Ezio Melottic1f05772011-04-14 07:50:25 +0300809 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Raw-Unicode-Escape
Georg Brandl54a3faa2008-01-20 09:30:57 +0000810 and return a Python string object. Return *NULL* if an exception was raised by
811 the codec.
812
813
Georg Brandl60203b42010-10-06 10:11:56 +0000814.. c:function:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000815
816 Encode a Unicode object using Raw-Unicode-Escape and return the result as
817 Python string object. Error handling is "strict". Return *NULL* if an exception
818 was raised by the codec.
819
Victor Stinner77c38622010-05-14 15:58:55 +0000820
821Latin-1 Codecs
822""""""""""""""
823
Georg Brandl54a3faa2008-01-20 09:30:57 +0000824These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
825ordinals and only these are accepted by the codecs during encoding.
826
Georg Brandl54a3faa2008-01-20 09:30:57 +0000827
Georg Brandl60203b42010-10-06 10:11:56 +0000828.. c:function:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000829
830 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
831 *s*. Return *NULL* if an exception was raised by the codec.
832
833
Georg Brandl60203b42010-10-06 10:11:56 +0000834.. c:function:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000835
Ezio Melottic1f05772011-04-14 07:50:25 +0300836 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using Latin-1 and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000837 return a Python bytes object. Return *NULL* if an exception was raised by
838 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000839
840
Georg Brandl60203b42010-10-06 10:11:56 +0000841.. c:function:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000842
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000843 Encode a Unicode object using Latin-1 and return the result as Python bytes
844 object. Error handling is "strict". Return *NULL* if an exception was
845 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000846
Victor Stinner77c38622010-05-14 15:58:55 +0000847
848ASCII Codecs
849""""""""""""
850
Georg Brandl54a3faa2008-01-20 09:30:57 +0000851These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
852codes generate errors.
853
Georg Brandl54a3faa2008-01-20 09:30:57 +0000854
Georg Brandl60203b42010-10-06 10:11:56 +0000855.. c:function:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000856
857 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
858 *s*. Return *NULL* if an exception was raised by the codec.
859
860
Georg Brandl60203b42010-10-06 10:11:56 +0000861.. c:function:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000862
Ezio Melottic1f05772011-04-14 07:50:25 +0300863 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using ASCII and
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000864 return a Python bytes object. Return *NULL* if an exception was raised by
865 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000866
867
Georg Brandl60203b42010-10-06 10:11:56 +0000868.. c:function:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000869
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000870 Encode a Unicode object using ASCII and return the result as Python bytes
871 object. Error handling is "strict". Return *NULL* if an exception was
872 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000873
Georg Brandl54a3faa2008-01-20 09:30:57 +0000874
Victor Stinner77c38622010-05-14 15:58:55 +0000875Character Map Codecs
876""""""""""""""""""""
877
Georg Brandl54a3faa2008-01-20 09:30:57 +0000878This codec is special in that it can be used to implement many different codecs
879(and this is in fact what was done to obtain most of the standard codecs
880included in the :mod:`encodings` package). The codec uses mapping to encode and
881decode characters.
882
883Decoding mappings must map single string characters to single Unicode
884characters, integers (which are then interpreted as Unicode ordinals) or None
885(meaning "undefined mapping" and causing an error).
886
887Encoding mappings must map single Unicode characters to single string
888characters, integers (which are then interpreted as Latin-1 ordinals) or None
889(meaning "undefined mapping" and causing an error).
890
891The mapping objects provided must only support the __getitem__ mapping
892interface.
893
894If a character lookup fails with a LookupError, the character is copied as-is
895meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
896resp. Because of this, mappings only need to contain those mappings which map
897characters to different code points.
898
Ezio Melotti95cd91c2011-04-14 07:43:53 +0300899These are the mapping codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000900
Georg Brandl60203b42010-10-06 10:11:56 +0000901.. c:function:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000902
903 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
904 the given *mapping* object. Return *NULL* if an exception was raised by the
905 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
906 dictionary mapping byte or a unicode string, which is treated as a lookup table.
907 Byte values greater that the length of the string and U+FFFE "characters" are
908 treated as "undefined mapping".
909
910
Georg Brandl60203b42010-10-06 10:11:56 +0000911.. 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 +0000912
Ezio Melottic1f05772011-04-14 07:50:25 +0300913 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using the given
Georg Brandl54a3faa2008-01-20 09:30:57 +0000914 *mapping* object and return a Python string object. Return *NULL* if an
915 exception was raised by the codec.
916
917
Georg Brandl60203b42010-10-06 10:11:56 +0000918.. c:function:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000919
920 Encode a Unicode object using the given *mapping* object and return the result
921 as Python string object. Error handling is "strict". Return *NULL* if an
922 exception was raised by the codec.
923
924The following codec API is special in that maps Unicode to Unicode.
925
926
Georg Brandl60203b42010-10-06 10:11:56 +0000927.. 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 +0000928
Ezio Melottic1f05772011-04-14 07:50:25 +0300929 Translate a :c:type:`Py_UNICODE` buffer of the given *size* by applying a
Georg Brandl54a3faa2008-01-20 09:30:57 +0000930 character mapping *table* to it and return the resulting Unicode object. Return
931 *NULL* when an exception was raised by the codec.
932
933 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
934 integers or None (causing deletion of the character).
935
936 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
937 and sequences work well. Unmapped character ordinals (ones which cause a
938 :exc:`LookupError`) are left untouched and are copied as-is.
939
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000940
Victor Stinner77c38622010-05-14 15:58:55 +0000941
942MBCS codecs for Windows
943"""""""""""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000944
Georg Brandl54a3faa2008-01-20 09:30:57 +0000945These are the MBCS codec APIs. They are currently only available on Windows and
946use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
947DBCS) is a class of encodings, not just one. The target encoding is defined by
948the user settings on the machine running the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000949
Georg Brandl60203b42010-10-06 10:11:56 +0000950.. c:function:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000951
952 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
953 Return *NULL* if an exception was raised by the codec.
954
955
Georg Brandl60203b42010-10-06 10:11:56 +0000956.. c:function:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000957
Georg Brandl60203b42010-10-06 10:11:56 +0000958 If *consumed* is *NULL*, behave like :c:func:`PyUnicode_DecodeMBCS`. If
959 *consumed* is not *NULL*, :c:func:`PyUnicode_DecodeMBCSStateful` will not decode
Georg Brandl54a3faa2008-01-20 09:30:57 +0000960 trailing lead byte and the number of bytes that have been decoded will be stored
961 in *consumed*.
962
963
Georg Brandl60203b42010-10-06 10:11:56 +0000964.. c:function:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000965
Ezio Melottic1f05772011-04-14 07:50:25 +0300966 Encode the :c:type:`Py_UNICODE` buffer of the given *size* using MBCS and return
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000967 a Python bytes object. Return *NULL* if an exception was raised by the
968 codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000969
970
Georg Brandl60203b42010-10-06 10:11:56 +0000971.. c:function:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000972
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000973 Encode a Unicode object using MBCS and return the result as Python bytes
974 object. Error handling is "strict". Return *NULL* if an exception was
975 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000976
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000977
Victor Stinner77c38622010-05-14 15:58:55 +0000978Methods & Slots
979"""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000980
981
982.. _unicodemethodsandslots:
983
984Methods and Slot Functions
985^^^^^^^^^^^^^^^^^^^^^^^^^^
986
987The following APIs are capable of handling Unicode objects and strings on input
988(we refer to them as strings in the descriptions) and return Unicode objects or
989integers as appropriate.
990
991They all return *NULL* or ``-1`` if an exception occurs.
992
993
Georg Brandl60203b42010-10-06 10:11:56 +0000994.. c:function:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000995
996 Concat two strings giving a new Unicode string.
997
998
Georg Brandl60203b42010-10-06 10:11:56 +0000999.. c:function:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001000
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001001 Split a string giving a list of Unicode strings. If *sep* is *NULL*, splitting
Georg Brandl54a3faa2008-01-20 09:30:57 +00001002 will be done at all whitespace substrings. Otherwise, splits occur at the given
1003 separator. At most *maxsplit* splits will be done. If negative, no limit is
1004 set. Separators are not included in the resulting list.
1005
1006
Georg Brandl60203b42010-10-06 10:11:56 +00001007.. c:function:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001008
1009 Split a Unicode string at line breaks, returning a list of Unicode strings.
1010 CRLF is considered to be one line break. If *keepend* is 0, the Line break
1011 characters are not included in the resulting strings.
1012
1013
Georg Brandl60203b42010-10-06 10:11:56 +00001014.. c:function:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001015
1016 Translate a string by applying a character mapping table to it and return the
1017 resulting Unicode object.
1018
1019 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
1020 or None (causing deletion of the character).
1021
1022 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
1023 and sequences work well. Unmapped character ordinals (ones which cause a
1024 :exc:`LookupError`) are left untouched and are copied as-is.
1025
1026 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
1027 use the default error handling.
1028
1029
Georg Brandl60203b42010-10-06 10:11:56 +00001030.. c:function:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001031
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001032 Join a sequence of strings using the given *separator* and return the resulting
Georg Brandl54a3faa2008-01-20 09:30:57 +00001033 Unicode string.
1034
1035
Georg Brandl60203b42010-10-06 10:11:56 +00001036.. 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 +00001037
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001038 Return 1 if *substr* matches ``str[start:end]`` at the given tail end
Georg Brandl54a3faa2008-01-20 09:30:57 +00001039 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
1040 0 otherwise. Return ``-1`` if an error occurred.
1041
1042
Georg Brandl60203b42010-10-06 10:11:56 +00001043.. 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 +00001044
Ezio Melotti95cd91c2011-04-14 07:43:53 +03001045 Return the first position of *substr* in ``str[start:end]`` using the given
Georg Brandl54a3faa2008-01-20 09:30:57 +00001046 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
1047 backward search). The return value is the index of the first match; a value of
1048 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
1049 occurred and an exception has been set.
1050
1051
Georg Brandl60203b42010-10-06 10:11:56 +00001052.. 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 +00001053
1054 Return the number of non-overlapping occurrences of *substr* in
1055 ``str[start:end]``. Return ``-1`` if an error occurred.
1056
1057
Georg Brandl60203b42010-10-06 10:11:56 +00001058.. c:function:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001059
1060 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
1061 return the resulting Unicode object. *maxcount* == -1 means replace all
1062 occurrences.
1063
1064
Georg Brandl60203b42010-10-06 10:11:56 +00001065.. c:function:: int PyUnicode_Compare(PyObject *left, PyObject *right)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001066
1067 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1068 respectively.
1069
1070
Georg Brandl60203b42010-10-06 10:11:56 +00001071.. c:function:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string)
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001072
1073 Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less
Victor Stinner80e788a2010-12-28 23:39:51 +00001074 than, equal, and greater than, respectively. It is best to pass only
1075 ASCII-encoded strings, but the function interprets the input string as
1076 ISO-8859-1 if it contains non-ASCII characters".
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001077
1078
Georg Brandl60203b42010-10-06 10:11:56 +00001079.. c:function:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001080
1081 Rich compare two unicode strings and return one of the following:
1082
1083 * ``NULL`` in case an exception was raised
1084 * :const:`Py_True` or :const:`Py_False` for successful comparisons
1085 * :const:`Py_NotImplemented` in case the type combination is unknown
1086
1087 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
1088 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
1089 with a :exc:`UnicodeDecodeError`.
1090
1091 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
1092 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
1093
1094
Georg Brandl60203b42010-10-06 10:11:56 +00001095.. c:function:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001096
1097 Return a new string object from *format* and *args*; this is analogous to
1098 ``format % args``. The *args* argument must be a tuple.
1099
1100
Georg Brandl60203b42010-10-06 10:11:56 +00001101.. c:function:: int PyUnicode_Contains(PyObject *container, PyObject *element)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001102
1103 Check whether *element* is contained in *container* and return true or false
1104 accordingly.
1105
1106 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1107 there was an error.
1108
1109
Georg Brandl60203b42010-10-06 10:11:56 +00001110.. c:function:: void PyUnicode_InternInPlace(PyObject **string)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001111
1112 Intern the argument *\*string* in place. The argument must be the address of a
1113 pointer variable pointing to a Python unicode string object. If there is an
1114 existing interned string that is the same as *\*string*, it sets *\*string* to
1115 it (decrementing the reference count of the old string object and incrementing
1116 the reference count of the interned string object), otherwise it leaves
1117 *\*string* alone and interns it (incrementing its reference count).
1118 (Clarification: even though there is a lot of talk about reference counts, think
1119 of this function as reference-count-neutral; you own the object after the call
1120 if and only if you owned it before the call.)
1121
1122
Georg Brandl60203b42010-10-06 10:11:56 +00001123.. c:function:: PyObject* PyUnicode_InternFromString(const char *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +00001124
Georg Brandl60203b42010-10-06 10:11:56 +00001125 A combination of :c:func:`PyUnicode_FromString` and
1126 :c:func:`PyUnicode_InternInPlace`, returning either a new unicode string object
Georg Brandl54a3faa2008-01-20 09:30:57 +00001127 that has been interned, or a new ("owned") reference to an earlier interned
1128 string object with the same value.
1129