blob: 5526e999d65529775bd4aed37a612405667fd70d [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
13These are the basic Unicode object types used for the Unicode implementation in
14Python:
15
16.. % --- Unicode Type -------------------------------------------------------
17
18
19.. ctype:: Py_UNICODE
20
21 This type represents the storage type which is used by Python internally as
22 basis for holding Unicode ordinals. Python's default builds use a 16-bit type
23 for :ctype:`Py_UNICODE` and store Unicode values internally as UCS2. It is also
24 possible to build a UCS4 version of Python (most recent Linux distributions come
25 with UCS4 builds of Python). These builds then use a 32-bit type for
26 :ctype:`Py_UNICODE` and store Unicode data internally as UCS4. On platforms
27 where :ctype:`wchar_t` is available and compatible with the chosen Python
28 Unicode build variant, :ctype:`Py_UNICODE` is a typedef alias for
29 :ctype:`wchar_t` to enhance native platform compatibility. On all other
30 platforms, :ctype:`Py_UNICODE` is a typedef alias for either :ctype:`unsigned
31 short` (UCS2) or :ctype:`unsigned long` (UCS4).
32
33Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep
34this in mind when writing extensions or interfaces.
35
36
37.. ctype:: PyUnicodeObject
38
39 This subtype of :ctype:`PyObject` represents a Python Unicode object.
40
41
42.. cvar:: PyTypeObject PyUnicode_Type
43
44 This instance of :ctype:`PyTypeObject` represents the Python Unicode type. It
45 is exposed to Python code as ``str``.
46
47The following APIs are really C macros and can be used to do fast checks and to
48access internal read-only data of Unicode objects:
49
50
51.. cfunction:: int PyUnicode_Check(PyObject *o)
52
53 Return true if the object *o* is a Unicode object or an instance of a Unicode
54 subtype.
55
56
57.. cfunction:: int PyUnicode_CheckExact(PyObject *o)
58
59 Return true if the object *o* is a Unicode object, but not an instance of a
60 subtype.
61
62
63.. cfunction:: Py_ssize_t PyUnicode_GET_SIZE(PyObject *o)
64
65 Return the size of the object. *o* has to be a :ctype:`PyUnicodeObject` (not
66 checked).
67
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000068 .. versionchanged:: 2.5
69 This function returned an :ctype:`int` type. This might require changes
70 in your code for properly supporting 64-bit systems.
71
Georg Brandl54a3faa2008-01-20 09:30:57 +000072
73.. cfunction:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o)
74
75 Return the size of the object's internal buffer in bytes. *o* has to be a
76 :ctype:`PyUnicodeObject` (not checked).
77
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +000078 .. versionchanged:: 2.5
79 This function returned an :ctype:`int` type. This might require changes
80 in your code for properly supporting 64-bit systems.
81
Georg Brandl54a3faa2008-01-20 09:30:57 +000082
83.. cfunction:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o)
84
85 Return a pointer to the internal :ctype:`Py_UNICODE` buffer of the object. *o*
86 has to be a :ctype:`PyUnicodeObject` (not checked).
87
88
89.. cfunction:: const char* PyUnicode_AS_DATA(PyObject *o)
90
91 Return a pointer to the internal buffer of the object. *o* has to be a
92 :ctype:`PyUnicodeObject` (not checked).
93
Christian Heimesa156e092008-02-16 07:38:31 +000094
95.. cfunction:: int PyUnicode_ClearFreeList(void)
96
97 Clear the free list. Return the total number of freed items.
98
Georg Brandl54a3faa2008-01-20 09:30:57 +000099Unicode provides many different character properties. The most often needed ones
100are available through these macros which are mapped to C functions depending on
101the Python configuration.
102
103.. % --- Unicode character properties ---------------------------------------
104
105
106.. cfunction:: int Py_UNICODE_ISSPACE(Py_UNICODE ch)
107
108 Return 1 or 0 depending on whether *ch* is a whitespace character.
109
110
111.. cfunction:: int Py_UNICODE_ISLOWER(Py_UNICODE ch)
112
113 Return 1 or 0 depending on whether *ch* is a lowercase character.
114
115
116.. cfunction:: int Py_UNICODE_ISUPPER(Py_UNICODE ch)
117
118 Return 1 or 0 depending on whether *ch* is an uppercase character.
119
120
121.. cfunction:: int Py_UNICODE_ISTITLE(Py_UNICODE ch)
122
123 Return 1 or 0 depending on whether *ch* is a titlecase character.
124
125
126.. cfunction:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch)
127
128 Return 1 or 0 depending on whether *ch* is a linebreak character.
129
130
131.. cfunction:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch)
132
133 Return 1 or 0 depending on whether *ch* is a decimal character.
134
135
136.. cfunction:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch)
137
138 Return 1 or 0 depending on whether *ch* is a digit character.
139
140
141.. cfunction:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch)
142
143 Return 1 or 0 depending on whether *ch* is a numeric character.
144
145
146.. cfunction:: int Py_UNICODE_ISALPHA(Py_UNICODE ch)
147
148 Return 1 or 0 depending on whether *ch* is an alphabetic character.
149
150
151.. cfunction:: int Py_UNICODE_ISALNUM(Py_UNICODE ch)
152
153 Return 1 or 0 depending on whether *ch* is an alphanumeric character.
154
Georg Brandl559e5d72008-06-11 18:37:52 +0000155
156.. cfunction:: int Py_UNICODE_ISPRINTABLE(Py_UNICODE ch)
157
158 Return 1 or 0 depending on whether *ch* is a printable character.
159 Nonprintable characters are those characters defined in the Unicode character
160 database as "Other" or "Separator", excepting the ASCII space (0x20) which is
161 considered printable. (Note that printable characters in this context are
162 those which should not be escaped when :func:`repr` is invoked on a string.
163 It has no bearing on the handling of strings written to :data:`sys.stdout` or
164 :data:`sys.stderr`.)
165
166
Georg Brandl54a3faa2008-01-20 09:30:57 +0000167These APIs can be used for fast direct character conversions:
168
169
170.. cfunction:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch)
171
172 Return the character *ch* converted to lower case.
173
174
175.. cfunction:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch)
176
177 Return the character *ch* converted to upper case.
178
179
180.. cfunction:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch)
181
182 Return the character *ch* converted to title case.
183
184
185.. cfunction:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch)
186
187 Return the character *ch* converted to a decimal positive integer. Return
188 ``-1`` if this is not possible. This macro does not raise exceptions.
189
190
191.. cfunction:: int Py_UNICODE_TODIGIT(Py_UNICODE ch)
192
193 Return the character *ch* converted to a single digit integer. Return ``-1`` if
194 this is not possible. This macro does not raise exceptions.
195
196
197.. cfunction:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch)
198
199 Return the character *ch* converted to a double. Return ``-1.0`` if this is not
200 possible. This macro does not raise exceptions.
201
202To create Unicode objects and access their basic sequence properties, use these
203APIs:
204
205.. % --- Plain Py_UNICODE ---------------------------------------------------
206
207
208.. cfunction:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size)
209
210 Create a Unicode Object from the Py_UNICODE buffer *u* of the given size. *u*
211 may be *NULL* which causes the contents to be undefined. It is the user's
212 responsibility to fill in the needed data. The buffer is copied into the new
213 object. If the buffer is not *NULL*, the return value might be a shared object.
214 Therefore, modification of the resulting Unicode object is only allowed when *u*
215 is *NULL*.
216
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000217 .. versionchanged:: 2.5
218 This function used an :ctype:`int` type for *size*. This might require
219 changes in your code for properly supporting 64-bit systems.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000220
221.. cfunction:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size)
222
223 Create a Unicode Object from the char buffer *u*. The bytes will be interpreted
224 as being UTF-8 encoded. *u* may also be *NULL* which
225 causes the contents to be undefined. It is the user's responsibility to fill in
226 the needed data. The buffer is copied into the new object. If the buffer is not
227 *NULL*, the return value might be a shared object. Therefore, modification of
228 the resulting Unicode object is only allowed when *u* is *NULL*.
229
230
231.. cfunction:: PyObject *PyUnicode_FromString(const char *u)
232
233 Create a Unicode object from an UTF-8 encoded null-terminated char buffer
234 *u*.
235
236
237.. cfunction:: PyObject* PyUnicode_FromFormat(const char *format, ...)
238
239 Take a C :cfunc:`printf`\ -style *format* string and a variable number of
240 arguments, calculate the size of the resulting Python unicode string and return
241 a string with the values formatted into it. The variable arguments must be C
242 types and must correspond exactly to the format characters in the *format*
243 string. The following format characters are allowed:
244
245 .. % The descriptions for %zd and %zu are wrong, but the truth is complicated
246 .. % because not all compilers support the %z width modifier -- we fake it
247 .. % when necessary via interpolating PY_FORMAT_SIZE_T.
248
249 +-------------------+---------------------+--------------------------------+
250 | Format Characters | Type | Comment |
251 +===================+=====================+================================+
252 | :attr:`%%` | *n/a* | The literal % character. |
253 +-------------------+---------------------+--------------------------------+
254 | :attr:`%c` | int | A single character, |
255 | | | represented as an C int. |
256 +-------------------+---------------------+--------------------------------+
257 | :attr:`%d` | int | Exactly equivalent to |
258 | | | ``printf("%d")``. |
259 +-------------------+---------------------+--------------------------------+
260 | :attr:`%u` | unsigned int | Exactly equivalent to |
261 | | | ``printf("%u")``. |
262 +-------------------+---------------------+--------------------------------+
263 | :attr:`%ld` | long | Exactly equivalent to |
264 | | | ``printf("%ld")``. |
265 +-------------------+---------------------+--------------------------------+
266 | :attr:`%lu` | unsigned long | Exactly equivalent to |
267 | | | ``printf("%lu")``. |
268 +-------------------+---------------------+--------------------------------+
269 | :attr:`%zd` | Py_ssize_t | Exactly equivalent to |
270 | | | ``printf("%zd")``. |
271 +-------------------+---------------------+--------------------------------+
272 | :attr:`%zu` | size_t | Exactly equivalent to |
273 | | | ``printf("%zu")``. |
274 +-------------------+---------------------+--------------------------------+
275 | :attr:`%i` | int | Exactly equivalent to |
276 | | | ``printf("%i")``. |
277 +-------------------+---------------------+--------------------------------+
278 | :attr:`%x` | int | Exactly equivalent to |
279 | | | ``printf("%x")``. |
280 +-------------------+---------------------+--------------------------------+
281 | :attr:`%s` | char\* | A null-terminated C character |
282 | | | array. |
283 +-------------------+---------------------+--------------------------------+
284 | :attr:`%p` | void\* | The hex representation of a C |
285 | | | pointer. Mostly equivalent to |
286 | | | ``printf("%p")`` except that |
287 | | | it is guaranteed to start with |
288 | | | the literal ``0x`` regardless |
289 | | | of what the platform's |
290 | | | ``printf`` yields. |
291 +-------------------+---------------------+--------------------------------+
Georg Brandl559e5d72008-06-11 18:37:52 +0000292 | :attr:`%A` | PyObject\* | The result of calling |
293 | | | :func:`ascii`. |
294 +-------------------+---------------------+--------------------------------+
Georg Brandl54a3faa2008-01-20 09:30:57 +0000295 | :attr:`%U` | PyObject\* | A unicode object. |
296 +-------------------+---------------------+--------------------------------+
297 | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be |
298 | | | *NULL*) and a null-terminated |
299 | | | C character array as a second |
300 | | | parameter (which will be used, |
301 | | | if the first parameter is |
302 | | | *NULL*). |
303 +-------------------+---------------------+--------------------------------+
304 | :attr:`%S` | PyObject\* | The result of calling |
Benjamin Petersone8662062009-03-08 23:51:13 +0000305 | | | :func:`PyObject_Str`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000306 +-------------------+---------------------+--------------------------------+
307 | :attr:`%R` | PyObject\* | The result of calling |
308 | | | :func:`PyObject_Repr`. |
309 +-------------------+---------------------+--------------------------------+
310
311 An unrecognized format character causes all the rest of the format string to be
312 copied as-is to the result string, and any extra arguments discarded.
313
314
315.. cfunction:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
316
317 Identical to :func:`PyUnicode_FromFormat` except that it takes exactly two
318 arguments.
319
320
321.. cfunction:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
322
323 Return a read-only pointer to the Unicode object's internal :ctype:`Py_UNICODE`
324 buffer, *NULL* if *unicode* is not a Unicode object.
325
326
327.. cfunction:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
328
329 Return the length of the Unicode object.
330
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000331 .. versionchanged:: 2.5
332 This function returned an :ctype:`int` type. This might require changes
333 in your code for properly supporting 64-bit systems.
334
Georg Brandl54a3faa2008-01-20 09:30:57 +0000335
336.. cfunction:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
337
338 Coerce an encoded object *obj* to an Unicode object and return a reference with
339 incremented refcount.
340
341 String and other char buffer compatible objects are decoded according to the
342 given encoding and using the error handling defined by errors. Both can be
343 *NULL* to have the interface use the default values (see the next section for
344 details).
345
346 All other objects, including Unicode objects, cause a :exc:`TypeError` to be
347 set.
348
349 The API returns *NULL* if there was an error. The caller is responsible for
350 decref'ing the returned objects.
351
352
353.. cfunction:: PyObject* PyUnicode_FromObject(PyObject *obj)
354
355 Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
356 throughout the interpreter whenever coercion to Unicode is needed.
357
358If the platform supports :ctype:`wchar_t` and provides a header file wchar.h,
359Python can interface directly to this type using the following functions.
360Support is optimized if Python's own :ctype:`Py_UNICODE` type is identical to
361the system's :ctype:`wchar_t`.
362
363.. % --- wchar_t support for platforms which support it ---------------------
364
365
366.. cfunction:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
367
368 Create a Unicode object from the :ctype:`wchar_t` buffer *w* of the given size.
Martin v. Löwis790465f2008-04-05 20:41:37 +0000369 Passing -1 as the size indicates that the function must itself compute the length,
370 using wcslen.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000371 Return *NULL* on failure.
372
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000373 .. versionchanged:: 2.5
374 This function used an :ctype:`int` type for *size*. This might require
375 changes in your code for properly supporting 64-bit systems.
376
Georg Brandl54a3faa2008-01-20 09:30:57 +0000377
378.. cfunction:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
379
380 Copy the Unicode object contents into the :ctype:`wchar_t` buffer *w*. At most
381 *size* :ctype:`wchar_t` characters are copied (excluding a possibly trailing
382 0-termination character). Return the number of :ctype:`wchar_t` characters
383 copied or -1 in case of an error. Note that the resulting :ctype:`wchar_t`
384 string may or may not be 0-terminated. It is the responsibility of the caller
385 to make sure that the :ctype:`wchar_t` string is 0-terminated in case this is
386 required by the application.
387
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000388 .. versionchanged:: 2.5
389 This function returned an :ctype:`int` type and used an :ctype:`int`
390 type for *size*. This might require changes in your code for properly
391 supporting 64-bit systems.
392
Georg Brandl54a3faa2008-01-20 09:30:57 +0000393
394.. _builtincodecs:
395
396Built-in Codecs
397^^^^^^^^^^^^^^^
398
399Python provides a set of builtin codecs which are written in C for speed. All of
400these codecs are directly usable via the following functions.
401
402Many of the following APIs take two arguments encoding and errors. These
403parameters encoding and errors have the same semantics as the ones of the
404builtin unicode() Unicode object constructor.
405
406Setting encoding to *NULL* causes the default encoding to be used which is
407ASCII. The file system calls should use :cdata:`Py_FileSystemDefaultEncoding`
408as the encoding for file names. This variable should be treated as read-only: On
409some systems, it will be a pointer to a static string, on others, it will change
410at run-time (such as when the application invokes setlocale).
411
412Error handling is set by errors which may also be set to *NULL* meaning to use
413the default handling defined for the codec. Default error handling for all
414builtin codecs is "strict" (:exc:`ValueError` is raised).
415
416The codecs all use a similar interface. Only deviation from the following
417generic ones are documented for simplicity.
418
419These are the generic codec APIs:
420
421.. % --- Generic Codecs -----------------------------------------------------
422
423
424.. cfunction:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
425
426 Create a Unicode object by decoding *size* bytes of the encoded string *s*.
427 *encoding* and *errors* have the same meaning as the parameters of the same name
428 in the :func:`unicode` builtin function. The codec to be used is looked up
429 using the Python codec registry. Return *NULL* if an exception was raised by
430 the codec.
431
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000432 .. versionchanged:: 2.5
433 This function used an :ctype:`int` type for *size*. This might require
434 changes in your code for properly supporting 64-bit systems.
435
Georg Brandl54a3faa2008-01-20 09:30:57 +0000436
437.. cfunction:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)
438
439 Encode the :ctype:`Py_UNICODE` buffer of the given size and return a Python
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000440 bytes object. *encoding* and *errors* have the same meaning as the
441 parameters of the same name in the Unicode :meth:`encode` method. The codec
442 to be used is looked up using the Python codec registry. Return *NULL* if an
443 exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000444
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000445 .. versionchanged:: 2.5
446 This function used an :ctype:`int` type for *size*. This might require
447 changes in your code for properly supporting 64-bit systems.
448
Georg Brandl54a3faa2008-01-20 09:30:57 +0000449
450.. cfunction:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
451
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000452 Encode a Unicode object and return the result as Python bytes object.
453 *encoding* and *errors* have the same meaning as the parameters of the same
454 name in the Unicode :meth:`encode` method. The codec to be used is looked up
455 using the Python codec registry. Return *NULL* if an exception was raised by
456 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000457
458These are the UTF-8 codec APIs:
459
460.. % --- UTF-8 Codecs -------------------------------------------------------
461
462
463.. cfunction:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
464
465 Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
466 *s*. Return *NULL* if an exception was raised by the codec.
467
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000468 .. versionchanged:: 2.5
469 This function used an :ctype:`int` type for *size*. This might require
470 changes in your code for properly supporting 64-bit systems.
471
Georg Brandl54a3faa2008-01-20 09:30:57 +0000472
473.. cfunction:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
474
475 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF8`. If
476 *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
477 treated as an error. Those bytes will not be decoded and the number of bytes
478 that have been decoded will be stored in *consumed*.
479
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000480 .. versionchanged:: 2.5
481 This function used an :ctype:`int` type for *size*. This might require
482 changes in your code for properly supporting 64-bit systems.
483
Georg Brandl54a3faa2008-01-20 09:30:57 +0000484
485.. cfunction:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
486
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000487 Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-8 and
488 return a Python bytes object. Return *NULL* if an exception was raised by
489 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000490
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000491 .. versionchanged:: 2.5
492 This function used an :ctype:`int` type for *size*. This might require
493 changes in your code for properly supporting 64-bit systems.
494
Georg Brandl54a3faa2008-01-20 09:30:57 +0000495
496.. cfunction:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
497
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000498 Encode a Unicode object using UTF-8 and return the result as Python bytes
499 object. Error handling is "strict". Return *NULL* if an exception was
500 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000501
502These are the UTF-32 codec APIs:
503
504.. % --- UTF-32 Codecs ------------------------------------------------------ */
505
506
507.. cfunction:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
508
509 Decode *length* bytes from a UTF-32 encoded buffer string and return the
510 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
511 handling. It defaults to "strict".
512
513 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
514 order::
515
516 *byteorder == -1: little endian
517 *byteorder == 0: native order
518 *byteorder == 1: big endian
519
520 and then switches if the first four bytes of the input data are a byte order mark
521 (BOM) and the specified byte order is native order. This BOM is not copied into
522 the resulting Unicode string. After completion, *\*byteorder* is set to the
523 current byte order at the end of input data.
524
525 In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.
526
527 If *byteorder* is *NULL*, the codec starts in native order mode.
528
529 Return *NULL* if an exception was raised by the codec.
530
531
532.. cfunction:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
533
534 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF32`. If
535 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF32Stateful` will not treat
536 trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
537 by four) as an error. Those bytes will not be decoded and the number of bytes
538 that have been decoded will be stored in *consumed*.
539
540
541.. cfunction:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
542
543 Return a Python bytes object holding the UTF-32 encoded value of the Unicode
544 data in *s*. If *byteorder* is not ``0``, output is written according to the
545 following byte order::
546
547 byteorder == -1: little endian
548 byteorder == 0: native byte order (writes a BOM mark)
549 byteorder == 1: big endian
550
551 If byteorder is ``0``, the output string will always start with the Unicode BOM
552 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
553
554 If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output
555 as a single codepoint.
556
557 Return *NULL* if an exception was raised by the codec.
558
559
560.. cfunction:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
561
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000562 Return a Python byte string using the UTF-32 encoding in native byte
563 order. The string always starts with a BOM mark. Error handling is "strict".
564 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000565
566
567These are the UTF-16 codec APIs:
568
569.. % --- UTF-16 Codecs ------------------------------------------------------ */
570
571
572.. cfunction:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
573
574 Decode *length* bytes from a UTF-16 encoded buffer string and return the
575 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
576 handling. It defaults to "strict".
577
578 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
579 order::
580
581 *byteorder == -1: little endian
582 *byteorder == 0: native order
583 *byteorder == 1: big endian
584
585 and then switches if the first two bytes of the input data are a byte order mark
586 (BOM) and the specified byte order is native order. This BOM is not copied into
587 the resulting Unicode string. After completion, *\*byteorder* is set to the
588 current byte order at the end of input data.
589
590 If *byteorder* is *NULL*, the codec starts in native order mode.
591
592 Return *NULL* if an exception was raised by the codec.
593
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000594 .. versionchanged:: 2.5
595 This function used an :ctype:`int` type for *size*. This might require
596 changes in your code for properly supporting 64-bit systems.
597
Georg Brandl54a3faa2008-01-20 09:30:57 +0000598
599.. cfunction:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
600
601 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF16`. If
602 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF16Stateful` will not treat
603 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
604 split surrogate pair) as an error. Those bytes will not be decoded and the
605 number of bytes that have been decoded will be stored in *consumed*.
606
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000607 .. versionchanged:: 2.5
608 This function used an :ctype:`int` type for *size* and an :ctype:`int *`
609 type for *consumed*. This might require changes in your code for
610 properly supporting 64-bit systems.
611
Georg Brandl54a3faa2008-01-20 09:30:57 +0000612
613.. cfunction:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
614
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000615 Return a Python bytes object holding the UTF-16 encoded value of the Unicode
Georg Brandl54a3faa2008-01-20 09:30:57 +0000616 data in *s*. If *byteorder* is not ``0``, output is written according to the
617 following byte order::
618
619 byteorder == -1: little endian
620 byteorder == 0: native byte order (writes a BOM mark)
621 byteorder == 1: big endian
622
623 If byteorder is ``0``, the output string will always start with the Unicode BOM
624 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
625
626 If *Py_UNICODE_WIDE* is defined, a single :ctype:`Py_UNICODE` value may get
627 represented as a surrogate pair. If it is not defined, each :ctype:`Py_UNICODE`
628 values is interpreted as an UCS-2 character.
629
630 Return *NULL* if an exception was raised by the codec.
631
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000632 .. versionchanged:: 2.5
633 This function used an :ctype:`int` type for *size*. This might require
634 changes in your code for properly supporting 64-bit systems.
635
Georg Brandl54a3faa2008-01-20 09:30:57 +0000636
637.. cfunction:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
638
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000639 Return a Python byte string using the UTF-16 encoding in native byte
640 order. The string always starts with a BOM mark. Error handling is "strict".
641 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000642
643These are the "Unicode Escape" codec APIs:
644
645.. % --- Unicode-Escape Codecs ----------------------------------------------
646
647
648.. cfunction:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
649
650 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
651 string *s*. Return *NULL* if an exception was raised by the codec.
652
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000653 .. versionchanged:: 2.5
654 This function used an :ctype:`int` type for *size*. This might require
655 changes in your code for properly supporting 64-bit systems.
656
Georg Brandl54a3faa2008-01-20 09:30:57 +0000657
658.. cfunction:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
659
660 Encode the :ctype:`Py_UNICODE` buffer of the given size using Unicode-Escape and
661 return a Python string object. Return *NULL* if an exception was raised by the
662 codec.
663
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000664 .. versionchanged:: 2.5
665 This function used an :ctype:`int` type for *size*. This might require
666 changes in your code for properly supporting 64-bit systems.
667
Georg Brandl54a3faa2008-01-20 09:30:57 +0000668
669.. cfunction:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
670
671 Encode a Unicode object using Unicode-Escape and return the result as Python
672 string object. Error handling is "strict". Return *NULL* if an exception was
673 raised by the codec.
674
675These are the "Raw Unicode Escape" codec APIs:
676
677.. % --- Raw-Unicode-Escape Codecs ------------------------------------------
678
679
680.. cfunction:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
681
682 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
683 encoded string *s*. Return *NULL* if an exception was raised by the codec.
684
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000685 .. versionchanged:: 2.5
686 This function used an :ctype:`int` type for *size*. This might require
687 changes in your code for properly supporting 64-bit systems.
688
Georg Brandl54a3faa2008-01-20 09:30:57 +0000689
690.. cfunction:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
691
692 Encode the :ctype:`Py_UNICODE` buffer of the given size using Raw-Unicode-Escape
693 and return a Python string object. Return *NULL* if an exception was raised by
694 the codec.
695
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000696 .. versionchanged:: 2.5
697 This function used an :ctype:`int` type for *size*. This might require
698 changes in your code for properly supporting 64-bit systems.
699
Georg Brandl54a3faa2008-01-20 09:30:57 +0000700
701.. cfunction:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
702
703 Encode a Unicode object using Raw-Unicode-Escape and return the result as
704 Python string object. Error handling is "strict". Return *NULL* if an exception
705 was raised by the codec.
706
707These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
708ordinals and only these are accepted by the codecs during encoding.
709
710.. % --- Latin-1 Codecs -----------------------------------------------------
711
712
713.. cfunction:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
714
715 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
716 *s*. Return *NULL* if an exception was raised by the codec.
717
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000718 .. versionchanged:: 2.5
719 This function used an :ctype:`int` type for *size*. This might require
720 changes in your code for properly supporting 64-bit systems.
721
Georg Brandl54a3faa2008-01-20 09:30:57 +0000722
723.. cfunction:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
724
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000725 Encode the :ctype:`Py_UNICODE` buffer of the given size using Latin-1 and
726 return a Python bytes object. Return *NULL* if an exception was raised by
727 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000728
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000729 .. versionchanged:: 2.5
730 This function used an :ctype:`int` type for *size*. This might require
731 changes in your code for properly supporting 64-bit systems.
732
Georg Brandl54a3faa2008-01-20 09:30:57 +0000733
734.. cfunction:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode)
735
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000736 Encode a Unicode object using Latin-1 and return the result as Python bytes
737 object. Error handling is "strict". Return *NULL* if an exception was
738 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000739
740These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
741codes generate errors.
742
743.. % --- ASCII Codecs -------------------------------------------------------
744
745
746.. cfunction:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
747
748 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
749 *s*. Return *NULL* if an exception was raised by the codec.
750
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000751 .. versionchanged:: 2.5
752 This function used an :ctype:`int` type for *size*. This might require
753 changes in your code for properly supporting 64-bit systems.
754
Georg Brandl54a3faa2008-01-20 09:30:57 +0000755
756.. cfunction:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
757
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000758 Encode the :ctype:`Py_UNICODE` buffer of the given size using ASCII and
759 return a Python bytes object. Return *NULL* if an exception was raised by
760 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000761
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000762 .. versionchanged:: 2.5
763 This function used an :ctype:`int` type for *size*. This might require
764 changes in your code for properly supporting 64-bit systems.
765
Georg Brandl54a3faa2008-01-20 09:30:57 +0000766
767.. cfunction:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
768
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000769 Encode a Unicode object using ASCII and return the result as Python bytes
770 object. Error handling is "strict". Return *NULL* if an exception was
771 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000772
773These are the mapping codec APIs:
774
775.. % --- Character Map Codecs -----------------------------------------------
776
777This codec is special in that it can be used to implement many different codecs
778(and this is in fact what was done to obtain most of the standard codecs
779included in the :mod:`encodings` package). The codec uses mapping to encode and
780decode characters.
781
782Decoding mappings must map single string characters to single Unicode
783characters, integers (which are then interpreted as Unicode ordinals) or None
784(meaning "undefined mapping" and causing an error).
785
786Encoding mappings must map single Unicode characters to single string
787characters, integers (which are then interpreted as Latin-1 ordinals) or None
788(meaning "undefined mapping" and causing an error).
789
790The mapping objects provided must only support the __getitem__ mapping
791interface.
792
793If a character lookup fails with a LookupError, the character is copied as-is
794meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
795resp. Because of this, mappings only need to contain those mappings which map
796characters to different code points.
797
798
799.. cfunction:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
800
801 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
802 the given *mapping* object. Return *NULL* if an exception was raised by the
803 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
804 dictionary mapping byte or a unicode string, which is treated as a lookup table.
805 Byte values greater that the length of the string and U+FFFE "characters" are
806 treated as "undefined mapping".
807
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000808 .. versionchanged:: 2.5
809 This function used an :ctype:`int` type for *size*. This might require
810 changes in your code for properly supporting 64-bit systems.
811
Georg Brandl54a3faa2008-01-20 09:30:57 +0000812
813.. cfunction:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
814
815 Encode the :ctype:`Py_UNICODE` buffer of the given size using the given
816 *mapping* object and return a Python string object. Return *NULL* if an
817 exception was raised by the codec.
818
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000819 .. versionchanged:: 2.5
820 This function used an :ctype:`int` type for *size*. This might require
821 changes in your code for properly supporting 64-bit systems.
822
Georg Brandl54a3faa2008-01-20 09:30:57 +0000823
824.. cfunction:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
825
826 Encode a Unicode object using the given *mapping* object and return the result
827 as Python string object. Error handling is "strict". Return *NULL* if an
828 exception was raised by the codec.
829
830The following codec API is special in that maps Unicode to Unicode.
831
832
833.. cfunction:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
834
835 Translate a :ctype:`Py_UNICODE` buffer of the given length by applying a
836 character mapping *table* to it and return the resulting Unicode object. Return
837 *NULL* when an exception was raised by the codec.
838
839 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
840 integers or None (causing deletion of the character).
841
842 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
843 and sequences work well. Unmapped character ordinals (ones which cause a
844 :exc:`LookupError`) are left untouched and are copied as-is.
845
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000846 .. versionchanged:: 2.5
847 This function used an :ctype:`int` type for *size*. This might require
848 changes in your code for properly supporting 64-bit systems.
849
Georg Brandl54a3faa2008-01-20 09:30:57 +0000850These are the MBCS codec APIs. They are currently only available on Windows and
851use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
852DBCS) is a class of encodings, not just one. The target encoding is defined by
853the user settings on the machine running the codec.
854
855.. % --- MBCS codecs for Windows --------------------------------------------
856
857
858.. cfunction:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
859
860 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
861 Return *NULL* if an exception was raised by the codec.
862
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000863 .. versionchanged:: 2.5
864 This function used an :ctype:`int` type for *size*. This might require
865 changes in your code for properly supporting 64-bit systems.
866
Georg Brandl54a3faa2008-01-20 09:30:57 +0000867
868.. cfunction:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
869
870 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeMBCS`. If
871 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeMBCSStateful` will not decode
872 trailing lead byte and the number of bytes that have been decoded will be stored
873 in *consumed*.
874
875
876.. cfunction:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
877
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000878 Encode the :ctype:`Py_UNICODE` buffer of the given size using MBCS and return
879 a Python bytes object. Return *NULL* if an exception was raised by the
880 codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000881
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000882 .. versionchanged:: 2.5
883 This function used an :ctype:`int` type for *size*. This might require
884 changes in your code for properly supporting 64-bit systems.
885
Georg Brandl54a3faa2008-01-20 09:30:57 +0000886
887.. cfunction:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
888
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000889 Encode a Unicode object using MBCS and return the result as Python bytes
890 object. Error handling is "strict". Return *NULL* if an exception was
891 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000892
893.. % --- Methods & Slots ----------------------------------------------------
894
895
896.. _unicodemethodsandslots:
897
898Methods and Slot Functions
899^^^^^^^^^^^^^^^^^^^^^^^^^^
900
901The following APIs are capable of handling Unicode objects and strings on input
902(we refer to them as strings in the descriptions) and return Unicode objects or
903integers as appropriate.
904
905They all return *NULL* or ``-1`` if an exception occurs.
906
907
908.. cfunction:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
909
910 Concat two strings giving a new Unicode string.
911
912
913.. cfunction:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
914
915 Split a string giving a list of Unicode strings. If sep is *NULL*, splitting
916 will be done at all whitespace substrings. Otherwise, splits occur at the given
917 separator. At most *maxsplit* splits will be done. If negative, no limit is
918 set. Separators are not included in the resulting list.
919
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000920 .. versionchanged:: 2.5
921 This function used an :ctype:`int` type for *maxsplit*. This might require
922 changes in your code for properly supporting 64-bit systems.
923
Georg Brandl54a3faa2008-01-20 09:30:57 +0000924
925.. cfunction:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
926
927 Split a Unicode string at line breaks, returning a list of Unicode strings.
928 CRLF is considered to be one line break. If *keepend* is 0, the Line break
929 characters are not included in the resulting strings.
930
931
932.. cfunction:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
933
934 Translate a string by applying a character mapping table to it and return the
935 resulting Unicode object.
936
937 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
938 or None (causing deletion of the character).
939
940 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
941 and sequences work well. Unmapped character ordinals (ones which cause a
942 :exc:`LookupError`) are left untouched and are copied as-is.
943
944 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
945 use the default error handling.
946
947
948.. cfunction:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
949
950 Join a sequence of strings using the given separator and return the resulting
951 Unicode string.
952
953
954.. cfunction:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
955
956 Return 1 if *substr* matches *str*[*start*:*end*] at the given tail end
957 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
958 0 otherwise. Return ``-1`` if an error occurred.
959
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000960 .. versionchanged:: 2.5
961 This function used an :ctype:`int` type for *start* and *end*. This
962 might require changes in your code for properly supporting 64-bit
963 systems.
964
Georg Brandl54a3faa2008-01-20 09:30:57 +0000965
966.. cfunction:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
967
968 Return the first position of *substr* in *str*[*start*:*end*] using the given
969 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
970 backward search). The return value is the index of the first match; a value of
971 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
972 occurred and an exception has been set.
973
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000974 .. versionchanged:: 2.5
975 This function used an :ctype:`int` type for *start* and *end*. This
976 might require changes in your code for properly supporting 64-bit
977 systems.
978
Georg Brandl54a3faa2008-01-20 09:30:57 +0000979
980.. cfunction:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
981
982 Return the number of non-overlapping occurrences of *substr* in
983 ``str[start:end]``. Return ``-1`` if an error occurred.
984
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000985 .. versionchanged:: 2.5
986 This function returned an :ctype:`int` type and used an :ctype:`int`
987 type for *start* and *end*. This might require changes in your code for
988 properly supporting 64-bit systems.
989
Georg Brandl54a3faa2008-01-20 09:30:57 +0000990
991.. cfunction:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
992
993 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
994 return the resulting Unicode object. *maxcount* == -1 means replace all
995 occurrences.
996
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000997 .. versionchanged:: 2.5
998 This function used an :ctype:`int` type for *maxcount*. This might
999 require changes in your code for properly supporting 64-bit systems.
1000
Georg Brandl54a3faa2008-01-20 09:30:57 +00001001
1002.. cfunction:: int PyUnicode_Compare(PyObject *left, PyObject *right)
1003
1004 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
1005 respectively.
1006
1007
Benjamin Petersonc22ed142008-07-01 19:12:34 +00001008.. cfunction:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string)
1009
1010 Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less
1011 than, equal, and greater than, respectively.
1012
1013
Georg Brandl54a3faa2008-01-20 09:30:57 +00001014.. cfunction:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
1015
1016 Rich compare two unicode strings and return one of the following:
1017
1018 * ``NULL`` in case an exception was raised
1019 * :const:`Py_True` or :const:`Py_False` for successful comparisons
1020 * :const:`Py_NotImplemented` in case the type combination is unknown
1021
1022 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
1023 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
1024 with a :exc:`UnicodeDecodeError`.
1025
1026 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
1027 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
1028
1029
1030.. cfunction:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
1031
1032 Return a new string object from *format* and *args*; this is analogous to
1033 ``format % args``. The *args* argument must be a tuple.
1034
1035
1036.. cfunction:: int PyUnicode_Contains(PyObject *container, PyObject *element)
1037
1038 Check whether *element* is contained in *container* and return true or false
1039 accordingly.
1040
1041 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1042 there was an error.
1043
1044
1045.. cfunction:: void PyUnicode_InternInPlace(PyObject **string)
1046
1047 Intern the argument *\*string* in place. The argument must be the address of a
1048 pointer variable pointing to a Python unicode string object. If there is an
1049 existing interned string that is the same as *\*string*, it sets *\*string* to
1050 it (decrementing the reference count of the old string object and incrementing
1051 the reference count of the interned string object), otherwise it leaves
1052 *\*string* alone and interns it (incrementing its reference count).
1053 (Clarification: even though there is a lot of talk about reference counts, think
1054 of this function as reference-count-neutral; you own the object after the call
1055 if and only if you owned it before the call.)
1056
1057
1058.. cfunction:: PyObject* PyUnicode_InternFromString(const char *v)
1059
1060 A combination of :cfunc:`PyUnicode_FromString` and
1061 :cfunc:`PyUnicode_InternInPlace`, returning either a new unicode string object
1062 that has been interned, or a new ("owned") reference to an earlier interned
1063 string object with the same value.
1064