blob: 50a125419268f794de8390b8763934091570ad42 [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
20.. ctype:: Py_UNICODE
21
22 This type represents the storage type which is used by Python internally as
23 basis for holding Unicode ordinals. Python's default builds use a 16-bit type
24 for :ctype:`Py_UNICODE` and store Unicode values internally as UCS2. It is also
25 possible to build a UCS4 version of Python (most recent Linux distributions come
26 with UCS4 builds of Python). These builds then use a 32-bit type for
27 :ctype:`Py_UNICODE` and store Unicode data internally as UCS4. On platforms
28 where :ctype:`wchar_t` is available and compatible with the chosen Python
29 Unicode build variant, :ctype:`Py_UNICODE` is a typedef alias for
30 :ctype:`wchar_t` to enhance native platform compatibility. On all other
31 platforms, :ctype:`Py_UNICODE` is a typedef alias for either :ctype:`unsigned
32 short` (UCS2) or :ctype:`unsigned long` (UCS4).
33
34Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
35this in mind when writing extensions or interfaces.
36
37
38.. ctype:: PyUnicodeObject
39
40 This subtype of :ctype:`PyObject` represents a Python Unicode object.
41
42
43.. cvar:: PyTypeObject PyUnicode_Type
44
45 This instance of :ctype:`PyTypeObject` represents the Python Unicode type. It
46 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
52.. cfunction:: int PyUnicode_Check(PyObject *o)
53
54 Return true if the object *o* is a Unicode object or an instance of a Unicode
55 subtype.
56
57
58.. cfunction:: int PyUnicode_CheckExact(PyObject *o)
59
60 Return true if the object *o* is a Unicode object, but not an instance of a
61 subtype.
62
63
64.. cfunction:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
65
66 Return the size of the object. *o* has to be a :ctype:`PyUnicodeObject` (not
67 checked).
68
69
70.. cfunction:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
71
72 Return the size of the object's internal buffer in bytes. *o* has to be a
73 :ctype:`PyUnicodeObject` (not checked).
74
75
76.. cfunction:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
77
78 Return a pointer to the internal :ctype:`Py_UNICODE` buffer of the object. *o*
79 has to be a :ctype:`PyUnicodeObject` (not checked).
80
81
82.. cfunction:: const char* PyUnicode_AS_DATA(PyObject *o)
83
84 Return a pointer to the internal buffer of the object. *o* has to be a
85 :ctype:`PyUnicodeObject` (not checked).
86
Christian Heimesa156e092008-02-16 07:38:31 +000087
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +000088.. cfunction:: 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
101.. cfunction:: int Py_UNICODE_ISSPACE(Py_UNICODE ch)
102
103 Return 1 or 0 depending on whether *ch* is a whitespace character.
104
105
106.. cfunction:: int Py_UNICODE_ISLOWER(Py_UNICODE ch)
107
108 Return 1 or 0 depending on whether *ch* is a lowercase character.
109
110
111.. cfunction:: int Py_UNICODE_ISUPPER(Py_UNICODE ch)
112
113 Return 1 or 0 depending on whether *ch* is an uppercase character.
114
115
116.. cfunction:: int Py_UNICODE_ISTITLE(Py_UNICODE ch)
117
118 Return 1 or 0 depending on whether *ch* is a titlecase character.
119
120
121.. cfunction:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch)
122
123 Return 1 or 0 depending on whether *ch* is a linebreak character.
124
125
126.. cfunction:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch)
127
128 Return 1 or 0 depending on whether *ch* is a decimal character.
129
130
131.. cfunction:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch)
132
133 Return 1 or 0 depending on whether *ch* is a digit character.
134
135
136.. cfunction:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch)
137
138 Return 1 or 0 depending on whether *ch* is a numeric character.
139
140
141.. cfunction:: int Py_UNICODE_ISALPHA(Py_UNICODE ch)
142
143 Return 1 or 0 depending on whether *ch* is an alphabetic character.
144
145
146.. cfunction:: int Py_UNICODE_ISALNUM(Py_UNICODE ch)
147
148 Return 1 or 0 depending on whether *ch* is an alphanumeric character.
149
Georg Brandl559e5d72008-06-11 18:37:52 +0000150
151.. cfunction:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch)
152
153 Return 1 or 0 depending on whether *ch* is a printable character.
154 Nonprintable characters are those characters defined in the Unicode character
155 database as "Other" or "Separator", excepting the ASCII space (0x20) which is
156 considered printable. (Note that printable characters in this context are
157 those which should not be escaped when :func:`repr` is invoked on a string.
158 It has no bearing on the handling of strings written to :data:`sys.stdout` or
159 :data:`sys.stderr`.)
160
161
Georg Brandl54a3faa2008-01-20 09:30:57 +0000162These APIs can be used for fast direct character conversions:
163
164
165.. cfunction:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch)
166
167 Return the character *ch* converted to lower case.
168
169
170.. cfunction:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)
171
172 Return the character *ch* converted to upper case.
173
174
175.. cfunction:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)
176
177 Return the character *ch* converted to title case.
178
179
180.. cfunction:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch)
181
182 Return the character *ch* converted to a decimal positive integer. Return
183 ``-1`` if this is not possible. This macro does not raise exceptions.
184
185
186.. cfunction:: int Py_UNICODE_TODIGIT(Py_UNICODE ch)
187
188 Return the character *ch* converted to a single digit integer. Return ``-1`` if
189 this is not possible. This macro does not raise exceptions.
190
191
192.. cfunction:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch)
193
194 Return the character *ch* converted to a double. Return ``-1.0`` if this is not
195 possible. This macro does not raise exceptions.
196
Victor 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
205.. cfunction:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
206
207 Create a Unicode Object from the Py_UNICODE buffer *u* of the given size. *u*
208 may be *NULL* which causes the contents to be undefined. It is the user's
209 responsibility to fill in the needed data. The buffer is copied into the new
210 object. If the buffer is not *NULL*, the return value might be a shared object.
211 Therefore, modification of the resulting Unicode object is only allowed when *u*
212 is *NULL*.
213
214
215.. cfunction:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
216
217 Create a Unicode Object from the char buffer *u*. The bytes will be interpreted
218 as being UTF-8 encoded. *u* may also be *NULL* which
219 causes the contents to be undefined. It is the user's responsibility to fill in
220 the needed data. The buffer is copied into the new object. If the buffer is not
221 *NULL*, the return value might be a shared object. Therefore, modification of
222 the resulting Unicode object is only allowed when *u* is *NULL*.
223
224
225.. cfunction:: PyObject *PyUnicode_FromString(const char *u)
226
227 Create a Unicode object from an UTF-8 encoded null-terminated char buffer
228 *u*.
229
230
231.. cfunction:: PyObject* PyUnicode_FromFormat(const char *format, ...)
232
233 Take a C :cfunc:`printf`\ -style *format* string and a variable number of
234 arguments, calculate the size of the resulting Python unicode string and return
235 a string with the values formatted into it. The variable arguments must be C
236 types and must correspond exactly to the format characters in the *format*
Victor 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 |
Victor Stinner6009ece2010-08-17 22:01:02 +0000308 | | | :cfunc:`PyObject_Str`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309 +-------------------+---------------------+--------------------------------+
310 | :attr:`%R` | PyObject\* | The result of calling |
Victor Stinner6009ece2010-08-17 22:01:02 +0000311 | | | :cfunc:`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
326.. cfunction:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
327
Victor Stinner6009ece2010-08-17 22:01:02 +0000328 Identical to :cfunc:`PyUnicode_FromFormat` except that it takes exactly two
Georg Brandl54a3faa2008-01-20 09:30:57 +0000329 arguments.
330
331
332.. cfunction:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
333
334 Return a read-only pointer to the Unicode object's internal :ctype:`Py_UNICODE`
335 buffer, *NULL* if *unicode* is not a Unicode object.
336
337
Victor Stinnere4ea9942010-09-03 16:23:29 +0000338.. cfunction:: Py_UNICODE* PyUnicode_AsUnicodeCopy(PyObject *unicode)
339
340 Create a copy of a unicode string ending with a nul character. Return *NULL*
341 and raise a :exc:`MemoryError` exception on memory allocation failure,
342 otherwise return a new allocated buffer (use :cfunc:`PyMem_Free` to free the
343 buffer).
344
Victor Stinner2b19f352010-09-03 22:13:42 +0000345 .. versionadded:: 3.2
346
Victor Stinnere4ea9942010-09-03 16:23:29 +0000347
Georg Brandl54a3faa2008-01-20 09:30:57 +0000348.. cfunction:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
349
350 Return the length of the Unicode object.
351
352
353.. cfunction:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
354
355 Coerce an encoded object *obj* to an Unicode object and return a reference with
356 incremented refcount.
357
Georg Brandl952867a2010-06-27 10:17:12 +0000358 :class:`bytes`, :class:`bytearray` and other char buffer compatible objects
359 are decoded according to the given encoding and using the error handling
360 defined by errors. Both can be *NULL* to have the interface use the default
361 values (see the next section for details).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000362
363 All other objects, including Unicode objects, cause a :exc:`TypeError` to be
364 set.
365
366 The API returns *NULL* if there was an error. The caller is responsible for
367 decref'ing the returned objects.
368
369
370.. cfunction:: PyObject* PyUnicode_FromObject(PyObject *obj)
371
372 Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
373 throughout the interpreter whenever coercion to Unicode is needed.
374
375If the platform supports :ctype:`wchar_t` and provides a header file wchar.h,
376Python can interface directly to this type using the following functions.
377Support is optimized if Python's own :ctype:`Py_UNICODE` type is identical to
378the system's :ctype:`wchar_t`.
379
Georg Brandl54a3faa2008-01-20 09:30:57 +0000380
Victor Stinner77c38622010-05-14 15:58:55 +0000381File System Encoding
382""""""""""""""""""""
383
384To encode and decode file names and other environment strings,
385:cdata:`Py_FileSystemEncoding` should be used as the encoding, and
386``"surrogateescape"`` should be used as the error handler (:pep:`383`). To
387encode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl6faee4e2010-09-21 14:48:28 +0000388used, passing :cfunc:`PyUnicode_FSConverter` as the conversion function:
Victor Stinner77c38622010-05-14 15:58:55 +0000389
390.. cfunction:: int PyUnicode_FSConverter(PyObject* obj, void* result)
391
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000392 ParseTuple converter: encode :class:`str` objects to :class:`bytes` using
393 :cfunc:`PyUnicode_EncodeFSDefault`; :class:`bytes` objects are output as-is.
394 *result* must be a :ctype:`PyBytesObject*` which must be released when it is
395 no longer used.
Victor Stinner77c38622010-05-14 15:58:55 +0000396
397 .. versionadded:: 3.1
398
Georg Brandl67b21b72010-08-17 15:07:14 +0000399
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000400To decode file names during argument parsing, the ``"O&"`` converter should be
Georg Brandl6faee4e2010-09-21 14:48:28 +0000401used, passing :cfunc:`PyUnicode_FSDecoder` as the conversion function:
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000402
403.. cfunction:: int PyUnicode_FSDecoder(PyObject* obj, void* result)
404
405 ParseTuple converter: decode :class:`bytes` objects to :class:`str` using
406 :cfunc:`PyUnicode_DecodeFSDefaultAndSize`; :class:`str` objects are output
407 as-is. *result* must be a :ctype:`PyUnicodeObject*` which must be released
408 when it is no longer used.
409
410 .. versionadded:: 3.2
411
Georg Brandl67b21b72010-08-17 15:07:14 +0000412
Victor Stinner77c38622010-05-14 15:58:55 +0000413.. cfunction:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
414
415 Decode a null-terminated string using :cdata:`Py_FileSystemDefaultEncoding`
416 and the ``"surrogateescape"`` error handler.
417
418 If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
419
Victor Stinner6009ece2010-08-17 22:01:02 +0000420 Use :cfunc:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
Victor Stinner77c38622010-05-14 15:58:55 +0000421
Victor Stinnerae6265f2010-05-15 16:27:27 +0000422
Victor Stinner77c38622010-05-14 15:58:55 +0000423.. cfunction:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
424
425 Decode a string using :cdata:`Py_FileSystemDefaultEncoding` and
426 the ``"surrogateescape"`` error handler.
427
428 If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
429
430
Victor Stinnerae6265f2010-05-15 16:27:27 +0000431.. cfunction:: PyObject* PyUnicode_EncodeFSDefault(PyObject *unicode)
432
433 Encode a Unicode object to :cdata:`Py_FileSystemDefaultEncoding` with the
Benjamin Petersonb4324512010-05-15 17:42:02 +0000434 ``'surrogateescape'`` error handler, and return :class:`bytes`.
Victor Stinnerae6265f2010-05-15 16:27:27 +0000435
436 If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
437
438 .. versionadded:: 3.2
439
440
Victor Stinner77c38622010-05-14 15:58:55 +0000441wchar_t Support
442"""""""""""""""
443
444wchar_t support for platforms which support it:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000445
446.. cfunction:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
447
448 Create a Unicode object from the :ctype:`wchar_t` buffer *w* of the given size.
Martin v. Löwis790465f2008-04-05 20:41:37 +0000449 Passing -1 as the size indicates that the function must itself compute the length,
450 using wcslen.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000451 Return *NULL* on failure.
452
453
454.. cfunction:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
455
456 Copy the Unicode object contents into the :ctype:`wchar_t` buffer *w*. At most
457 *size* :ctype:`wchar_t` characters are copied (excluding a possibly trailing
458 0-termination character). Return the number of :ctype:`wchar_t` characters
459 copied or -1 in case of an error. Note that the resulting :ctype:`wchar_t`
460 string may or may not be 0-terminated. It is the responsibility of the caller
461 to make sure that the :ctype:`wchar_t` string is 0-terminated in case this is
462 required by the application.
463
464
Victor Stinner137c34c2010-09-29 10:25:54 +0000465.. cfunction:: wchar_t* PyUnicode_AsWideCharString(PyUnicodeObject *unicode, Py_ssize_t *size)
466
467 Convert the Unicode object to a wide character string. The output string
468 always ends with a nul character. If *size* is not *NULL*, write the number
469 of wide characters (including the nul character) into *\*size*.
470
471 Returns a buffer allocated by :cfunc:`PyMem_Alloc` (use :cfunc:`PyMem_Free`
472 to free it) on success. On error, returns *NULL*, *\*size* is undefined and
473 raises a :exc:`MemoryError`.
474
475 .. versionadded:: 3.2
476
477
Georg Brandl54a3faa2008-01-20 09:30:57 +0000478.. _builtincodecs:
479
480Built-in Codecs
481^^^^^^^^^^^^^^^
482
Georg Brandl22b34312009-07-26 14:54:51 +0000483Python provides a set of built-in codecs which are written in C for speed. All of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000484these codecs are directly usable via the following functions.
485
486Many of the following APIs take two arguments encoding and errors. These
487parameters encoding and errors have the same semantics as the ones of the
Daniel Stutzbach98c07bd2010-09-03 18:31:07 +0000488built-in :func:`str` string object constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000489
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000490Setting encoding to *NULL* causes the default encoding to be used
491which is ASCII. The file system calls should use
492:cfunc:`PyUnicode_FSConverter` for encoding file names. This uses the
493variable :cdata:`Py_FileSystemDefaultEncoding` internally. This
494variable should be treated as read-only: On some systems, it will be a
495pointer to a static string, on others, it will change at run-time
496(such as when the application invokes setlocale).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000497
498Error handling is set by errors which may also be set to *NULL* meaning to use
499the default handling defined for the codec. Default error handling for all
Georg Brandl22b34312009-07-26 14:54:51 +0000500built-in codecs is "strict" (:exc:`ValueError` is raised).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000501
502The codecs all use a similar interface. Only deviation from the following
503generic ones are documented for simplicity.
504
Georg Brandl54a3faa2008-01-20 09:30:57 +0000505
Victor Stinner77c38622010-05-14 15:58:55 +0000506Generic Codecs
507""""""""""""""
508
509These are the generic codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000510
511
512.. cfunction:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
513
514 Create a Unicode object by decoding *size* bytes of the encoded string *s*.
515 *encoding* and *errors* have the same meaning as the parameters of the same name
Georg Brandl22b34312009-07-26 14:54:51 +0000516 in the :func:`unicode` built-in function. The codec to be used is looked up
Georg Brandl54a3faa2008-01-20 09:30:57 +0000517 using the Python codec registry. Return *NULL* if an exception was raised by
518 the codec.
519
520
521.. cfunction:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)
522
523 Encode the :ctype:`Py_UNICODE` buffer of the given size and return a Python
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000524 bytes object. *encoding* and *errors* have the same meaning as the
525 parameters of the same name in the Unicode :meth:`encode` method. The codec
526 to be used is looked up using the Python codec registry. Return *NULL* if an
527 exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000528
529
530.. cfunction:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
531
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000532 Encode a Unicode object and return the result as Python bytes object.
533 *encoding* and *errors* have the same meaning as the parameters of the same
534 name in the Unicode :meth:`encode` method. The codec to be used is looked up
535 using the Python codec registry. Return *NULL* if an exception was raised by
536 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000537
Georg Brandl54a3faa2008-01-20 09:30:57 +0000538
Victor Stinner77c38622010-05-14 15:58:55 +0000539UTF-8 Codecs
540""""""""""""
541
542These are the UTF-8 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000543
544
545.. cfunction:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
546
547 Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
548 *s*. Return *NULL* if an exception was raised by the codec.
549
550
551.. cfunction:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
552
553 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF8`. If
554 *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
555 treated as an error. Those bytes will not be decoded and the number of bytes
556 that have been decoded will be stored in *consumed*.
557
558
559.. cfunction:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
560
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000561 Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-8 and
562 return a Python bytes object. Return *NULL* if an exception was raised by
563 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000564
565
566.. cfunction:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
567
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000568 Encode a Unicode object using UTF-8 and return the result as Python bytes
569 object. Error handling is "strict". Return *NULL* if an exception was
570 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000571
Georg Brandl54a3faa2008-01-20 09:30:57 +0000572
Victor Stinner77c38622010-05-14 15:58:55 +0000573UTF-32 Codecs
574"""""""""""""
575
576These are the UTF-32 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000577
578
579.. cfunction:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
580
581 Decode *length* bytes from a UTF-32 encoded buffer string and return the
582 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
583 handling. It defaults to "strict".
584
585 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
586 order::
587
588 *byteorder == -1: little endian
589 *byteorder == 0: native order
590 *byteorder == 1: big endian
591
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000592 If ``*byteorder`` is zero, and the first four bytes of the input data are a
593 byte order mark (BOM), the decoder switches to this byte order and the BOM is
594 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
595 ``1``, any byte order mark is copied to the output.
596
597 After completion, *\*byteorder* is set to the current byte order at the end
598 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000599
600 In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.
601
602 If *byteorder* is *NULL*, the codec starts in native order mode.
603
604 Return *NULL* if an exception was raised by the codec.
605
606
607.. cfunction:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
608
609 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF32`. If
610 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF32Stateful` will not treat
611 trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
612 by four) as an error. Those bytes will not be decoded and the number of bytes
613 that have been decoded will be stored in *consumed*.
614
615
616.. cfunction:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
617
618 Return a Python bytes object holding the UTF-32 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000619 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000620
621 byteorder == -1: little endian
622 byteorder == 0: native byte order (writes a BOM mark)
623 byteorder == 1: big endian
624
625 If byteorder is ``0``, the output string will always start with the Unicode BOM
626 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
627
628 If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output
629 as a single codepoint.
630
631 Return *NULL* if an exception was raised by the codec.
632
633
634.. cfunction:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
635
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000636 Return a Python byte string using the UTF-32 encoding in native byte
637 order. The string always starts with a BOM mark. Error handling is "strict".
638 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000639
640
Victor Stinner77c38622010-05-14 15:58:55 +0000641UTF-16 Codecs
642"""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000643
Victor Stinner77c38622010-05-14 15:58:55 +0000644These are the UTF-16 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000645
646
647.. cfunction:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
648
649 Decode *length* bytes from a UTF-16 encoded buffer string and return the
650 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
651 handling. It defaults to "strict".
652
653 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
654 order::
655
656 *byteorder == -1: little endian
657 *byteorder == 0: native order
658 *byteorder == 1: big endian
659
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000660 If ``*byteorder`` is zero, and the first two bytes of the input data are a
661 byte order mark (BOM), the decoder switches to this byte order and the BOM is
662 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
663 ``1``, any byte order mark is copied to the output (where it will result in
664 either a ``\ufeff`` or a ``\ufffe`` character).
665
666 After completion, *\*byteorder* is set to the current byte order at the end
667 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000668
669 If *byteorder* is *NULL*, the codec starts in native order mode.
670
671 Return *NULL* if an exception was raised by the codec.
672
673
674.. cfunction:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
675
676 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF16`. If
677 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF16Stateful` will not treat
678 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
679 split surrogate pair) as an error. Those bytes will not be decoded and the
680 number of bytes that have been decoded will be stored in *consumed*.
681
682
683.. cfunction:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
684
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000685 Return a Python bytes object holding the UTF-16 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000686 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000687
688 byteorder == -1: little endian
689 byteorder == 0: native byte order (writes a BOM mark)
690 byteorder == 1: big endian
691
692 If byteorder is ``0``, the output string will always start with the Unicode BOM
693 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
694
695 If *Py_UNICODE_WIDE* is defined, a single :ctype:`Py_UNICODE` value may get
696 represented as a surrogate pair. If it is not defined, each :ctype:`Py_UNICODE`
697 values is interpreted as an UCS-2 character.
698
699 Return *NULL* if an exception was raised by the codec.
700
701
702.. cfunction:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
703
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000704 Return a Python byte string using the UTF-16 encoding in native byte
705 order. The string always starts with a BOM mark. Error handling is "strict".
706 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000707
Georg Brandl54a3faa2008-01-20 09:30:57 +0000708
Georg Brandl8477f822010-08-02 20:05:19 +0000709UTF-7 Codecs
710""""""""""""
711
712These are the UTF-7 codec APIs:
713
714
715.. cfunction:: PyObject* PyUnicode_DecodeUTF7(const char *s, Py_ssize_t size, const char *errors)
716
717 Create a Unicode object by decoding *size* bytes of the UTF-7 encoded string
718 *s*. Return *NULL* if an exception was raised by the codec.
719
720
Georg Brandl4d224092010-08-13 15:10:49 +0000721.. cfunction:: PyObject* PyUnicode_DecodeUTF7Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
Georg Brandl8477f822010-08-02 20:05:19 +0000722
723 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF7`. If
724 *consumed* is not *NULL*, trailing incomplete UTF-7 base-64 sections will not
725 be treated as an error. Those bytes will not be decoded and the number of
726 bytes that have been decoded will be stored in *consumed*.
727
728
729.. cfunction:: PyObject* PyUnicode_EncodeUTF7(const Py_UNICODE *s, Py_ssize_t size, int base64SetO, int base64WhiteSpace, const char *errors)
730
731 Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-7 and
732 return a Python bytes object. Return *NULL* if an exception was raised by
733 the codec.
734
735 If *base64SetO* is nonzero, "Set O" (punctuation that has no otherwise
736 special meaning) will be encoded in base-64. If *base64WhiteSpace* is
737 nonzero, whitespace will be encoded in base-64. Both are set to zero for the
738 Python "utf-7" codec.
739
740
Victor Stinner77c38622010-05-14 15:58:55 +0000741Unicode-Escape Codecs
742"""""""""""""""""""""
743
744These are the "Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000745
746
747.. cfunction:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
748
749 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
750 string *s*. Return *NULL* if an exception was raised by the codec.
751
752
753.. cfunction:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
754
755 Encode the :ctype:`Py_UNICODE` buffer of the given size using Unicode-Escape and
756 return a Python string object. Return *NULL* if an exception was raised by the
757 codec.
758
759
760.. cfunction:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
761
762 Encode a Unicode object using Unicode-Escape and return the result as Python
763 string object. Error handling is "strict". Return *NULL* if an exception was
764 raised by the codec.
765
Georg Brandl54a3faa2008-01-20 09:30:57 +0000766
Victor Stinner77c38622010-05-14 15:58:55 +0000767Raw-Unicode-Escape Codecs
768"""""""""""""""""""""""""
769
770These are the "Raw Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000771
772
773.. cfunction:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
774
775 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
776 encoded string *s*. Return *NULL* if an exception was raised by the codec.
777
778
779.. cfunction:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
780
781 Encode the :ctype:`Py_UNICODE` buffer of the given size using Raw-Unicode-Escape
782 and return a Python string object. Return *NULL* if an exception was raised by
783 the codec.
784
785
786.. cfunction:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
787
788 Encode a Unicode object using Raw-Unicode-Escape and return the result as
789 Python string object. Error handling is "strict". Return *NULL* if an exception
790 was raised by the codec.
791
Victor Stinner77c38622010-05-14 15:58:55 +0000792
793Latin-1 Codecs
794""""""""""""""
795
Georg Brandl54a3faa2008-01-20 09:30:57 +0000796These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
797ordinals and only these are accepted by the codecs during encoding.
798
Georg Brandl54a3faa2008-01-20 09:30:57 +0000799
800.. cfunction:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
801
802 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
803 *s*. Return *NULL* if an exception was raised by the codec.
804
805
806.. cfunction:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
807
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000808 Encode the :ctype:`Py_UNICODE` buffer of the given size using Latin-1 and
809 return a Python bytes object. Return *NULL* if an exception was raised by
810 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000811
812
813.. cfunction:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
814
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000815 Encode a Unicode object using Latin-1 and return the result as Python bytes
816 object. Error handling is "strict". Return *NULL* if an exception was
817 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000818
Victor Stinner77c38622010-05-14 15:58:55 +0000819
820ASCII Codecs
821""""""""""""
822
Georg Brandl54a3faa2008-01-20 09:30:57 +0000823These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
824codes generate errors.
825
Georg Brandl54a3faa2008-01-20 09:30:57 +0000826
827.. cfunction:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
828
829 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
830 *s*. Return *NULL* if an exception was raised by the codec.
831
832
833.. cfunction:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
834
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000835 Encode the :ctype:`Py_UNICODE` buffer of the given size using ASCII and
836 return a Python bytes object. Return *NULL* if an exception was raised by
837 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000838
839
840.. cfunction:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
841
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000842 Encode a Unicode object using ASCII and return the result as Python bytes
843 object. Error handling is "strict". Return *NULL* if an exception was
844 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000845
Georg Brandl54a3faa2008-01-20 09:30:57 +0000846
Victor Stinner77c38622010-05-14 15:58:55 +0000847Character Map Codecs
848""""""""""""""""""""
849
850These are the mapping codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000851
852This codec is special in that it can be used to implement many different codecs
853(and this is in fact what was done to obtain most of the standard codecs
854included in the :mod:`encodings` package). The codec uses mapping to encode and
855decode characters.
856
857Decoding mappings must map single string characters to single Unicode
858characters, integers (which are then interpreted as Unicode ordinals) or None
859(meaning "undefined mapping" and causing an error).
860
861Encoding mappings must map single Unicode characters to single string
862characters, integers (which are then interpreted as Latin-1 ordinals) or None
863(meaning "undefined mapping" and causing an error).
864
865The mapping objects provided must only support the __getitem__ mapping
866interface.
867
868If a character lookup fails with a LookupError, the character is copied as-is
869meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
870resp. Because of this, mappings only need to contain those mappings which map
871characters to different code points.
872
873
874.. cfunction:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
875
876 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
877 the given *mapping* object. Return *NULL* if an exception was raised by the
878 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
879 dictionary mapping byte or a unicode string, which is treated as a lookup table.
880 Byte values greater that the length of the string and U+FFFE "characters" are
881 treated as "undefined mapping".
882
883
884.. cfunction:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
885
886 Encode the :ctype:`Py_UNICODE` buffer of the given size using the given
887 *mapping* object and return a Python string object. Return *NULL* if an
888 exception was raised by the codec.
889
890
891.. cfunction:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
892
893 Encode a Unicode object using the given *mapping* object and return the result
894 as Python string object. Error handling is "strict". Return *NULL* if an
895 exception was raised by the codec.
896
897The following codec API is special in that maps Unicode to Unicode.
898
899
900.. cfunction:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
901
902 Translate a :ctype:`Py_UNICODE` buffer of the given length by applying a
903 character mapping *table* to it and return the resulting Unicode object. Return
904 *NULL* when an exception was raised by the codec.
905
906 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
907 integers or None (causing deletion of the character).
908
909 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
910 and sequences work well. Unmapped character ordinals (ones which cause a
911 :exc:`LookupError`) are left untouched and are copied as-is.
912
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000913
Georg Brandl54a3faa2008-01-20 09:30:57 +0000914These are the MBCS codec APIs. They are currently only available on Windows and
915use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
916DBCS) is a class of encodings, not just one. The target encoding is defined by
917the user settings on the machine running the codec.
918
Victor Stinner77c38622010-05-14 15:58:55 +0000919
920MBCS codecs for Windows
921"""""""""""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000922
923
924.. cfunction:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
925
926 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
927 Return *NULL* if an exception was raised by the codec.
928
929
930.. cfunction:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
931
932 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeMBCS`. If
933 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeMBCSStateful` will not decode
934 trailing lead byte and the number of bytes that have been decoded will be stored
935 in *consumed*.
936
937
938.. cfunction:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
939
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000940 Encode the :ctype:`Py_UNICODE` buffer of the given size using MBCS and return
941 a Python bytes object. Return *NULL* if an exception was raised by the
942 codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000943
944
945.. cfunction:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
946
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000947 Encode a Unicode object using MBCS and return the result as Python bytes
948 object. Error handling is "strict". Return *NULL* if an exception was
949 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000950
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000951
Victor Stinner77c38622010-05-14 15:58:55 +0000952Methods & Slots
953"""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000954
955
956.. _unicodemethodsandslots:
957
958Methods and Slot Functions
959^^^^^^^^^^^^^^^^^^^^^^^^^^
960
961The following APIs are capable of handling Unicode objects and strings on input
962(we refer to them as strings in the descriptions) and return Unicode objects or
963integers as appropriate.
964
965They all return *NULL* or ``-1`` if an exception occurs.
966
967
968.. cfunction:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
969
970 Concat two strings giving a new Unicode string.
971
972
973.. cfunction:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
974
975 Split a string giving a list of Unicode strings. If sep is *NULL*, splitting
976 will be done at all whitespace substrings. Otherwise, splits occur at the given
977 separator. At most *maxsplit* splits will be done. If negative, no limit is
978 set. Separators are not included in the resulting list.
979
980
981.. cfunction:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
982
983 Split a Unicode string at line breaks, returning a list of Unicode strings.
984 CRLF is considered to be one line break. If *keepend* is 0, the Line break
985 characters are not included in the resulting strings.
986
987
988.. cfunction:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
989
990 Translate a string by applying a character mapping table to it and return the
991 resulting Unicode object.
992
993 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
994 or None (causing deletion of the character).
995
996 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
997 and sequences work well. Unmapped character ordinals (ones which cause a
998 :exc:`LookupError`) are left untouched and are copied as-is.
999
1000 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
1001 use the default error handling.
1002
1003
1004.. cfunction:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
1005
1006 Join a sequence of strings using the given separator and return the resulting
1007 Unicode string.
1008
1009
1010.. cfunction:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
1011
1012 Return 1 if *substr* matches *str*[*start*:*end*] at the given tail end
1013 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
1014 0 otherwise. Return ``-1`` if an error occurred.
1015
1016
1017.. cfunction:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
1018
1019 Return the first position of *substr* in *str*[*start*:*end*] using the given
1020 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
1021 backward search). The return value is the index of the first match; a value of
1022 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
1023 occurred and an exception has been set.
1024
1025
1026.. cfunction:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
1027
1028 Return the number of non-overlapping occurrences of *substr* in
1029 ``str[start:end]``. Return ``-1`` if an error occurred.
1030
1031
1032.. cfunction:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
1033
1034 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
1035 return the resulting Unicode object. *maxcount* == -1 means replace all
1036 occurrences.
1037
1038
1039.. cfunction:: int PyUnicode_Compare(PyObject *left, PyObject *right)
1040
1041 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1042 respectively.
1043
1044
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001045.. cfunction:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string)
1046
1047 Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less
1048 than, equal, and greater than, respectively.
1049
1050
Georg Brandl54a3faa2008-01-20 09:30:57 +00001051.. cfunction:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
1052
1053 Rich compare two unicode strings and return one of the following:
1054
1055 * ``NULL`` in case an exception was raised
1056 * :const:`Py_True` or :const:`Py_False` for successful comparisons
1057 * :const:`Py_NotImplemented` in case the type combination is unknown
1058
1059 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
1060 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
1061 with a :exc:`UnicodeDecodeError`.
1062
1063 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
1064 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
1065
1066
1067.. cfunction:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
1068
1069 Return a new string object from *format* and *args*; this is analogous to
1070 ``format % args``. The *args* argument must be a tuple.
1071
1072
1073.. cfunction:: int PyUnicode_Contains(PyObject *container, PyObject *element)
1074
1075 Check whether *element* is contained in *container* and return true or false
1076 accordingly.
1077
1078 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1079 there was an error.
1080
1081
1082.. cfunction:: void PyUnicode_InternInPlace(PyObject **string)
1083
1084 Intern the argument *\*string* in place. The argument must be the address of a
1085 pointer variable pointing to a Python unicode string object. If there is an
1086 existing interned string that is the same as *\*string*, it sets *\*string* to
1087 it (decrementing the reference count of the old string object and incrementing
1088 the reference count of the interned string object), otherwise it leaves
1089 *\*string* alone and interns it (incrementing its reference count).
1090 (Clarification: even though there is a lot of talk about reference counts, think
1091 of this function as reference-count-neutral; you own the object after the call
1092 if and only if you owned it before the call.)
1093
1094
1095.. cfunction:: PyObject* PyUnicode_InternFromString(const char *v)
1096
1097 A combination of :cfunc:`PyUnicode_FromString` and
1098 :cfunc:`PyUnicode_InternInPlace`, returning either a new unicode string object
1099 that has been interned, or a new ("owned") reference to an earlier interned
1100 string object with the same value.
1101