blob: 6e163d6ec7406846e912815e3cdfd032632c9818 [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*
237 string. The following format characters are allowed:
238
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 |
Benjamin Petersone8662062009-03-08 23:51:13 +0000308 | | | :func:`PyObject_Str`. |
Georg Brandl54a3faa2008-01-20 09:30:57 +0000309 +-------------------+---------------------+--------------------------------+
310 | :attr:`%R` | PyObject\* | The result of calling |
311 | | | :func:`PyObject_Repr`. |
312 +-------------------+---------------------+--------------------------------+
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
323 Support for `"%lld"` and `"%llu"` added.
324
325
Georg Brandl54a3faa2008-01-20 09:30:57 +0000326
327.. cfunction:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs)
328
329 Identical to :func:`PyUnicode_FromFormat` except that it takes exactly two
330 arguments.
331
332
333.. cfunction:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode)
334
335 Return a read-only pointer to the Unicode object's internal :ctype:`Py_UNICODE`
336 buffer, *NULL* if *unicode* is not a Unicode object.
337
338
339.. cfunction:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode)
340
341 Return the length of the Unicode object.
342
343
344.. cfunction:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors)
345
346 Coerce an encoded object *obj* to an Unicode object and return a reference with
347 incremented refcount.
348
349 String and other char buffer compatible objects are decoded according to the
350 given encoding and using the error handling defined by errors. Both can be
351 *NULL* to have the interface use the default values (see the next section for
352 details).
353
354 All other objects, including Unicode objects, cause a :exc:`TypeError` to be
355 set.
356
357 The API returns *NULL* if there was an error. The caller is responsible for
358 decref'ing the returned objects.
359
360
361.. cfunction:: PyObject* PyUnicode_FromObject(PyObject *obj)
362
363 Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used
364 throughout the interpreter whenever coercion to Unicode is needed.
365
366If the platform supports :ctype:`wchar_t` and provides a header file wchar.h,
367Python can interface directly to this type using the following functions.
368Support is optimized if Python's own :ctype:`Py_UNICODE` type is identical to
369the system's :ctype:`wchar_t`.
370
Georg Brandl54a3faa2008-01-20 09:30:57 +0000371
Victor Stinner77c38622010-05-14 15:58:55 +0000372File System Encoding
373""""""""""""""""""""
374
375To encode and decode file names and other environment strings,
376:cdata:`Py_FileSystemEncoding` should be used as the encoding, and
377``"surrogateescape"`` should be used as the error handler (:pep:`383`). To
378encode file names during argument parsing, the ``"O&"`` converter should be
379used, passsing :func:PyUnicode_FSConverter as the conversion function:
380
381.. cfunction:: int PyUnicode_FSConverter(PyObject* obj, void* result)
382
383 Convert *obj* into *result*, using :cdata:`Py_FileSystemDefaultEncoding`,
384 and the ``"surrogateescape"`` error handler. *result* must be a
385 ``PyObject*``, return a :func:`bytes` object which must be released if it
386 is no longer used.
387
388 .. versionadded:: 3.1
389
390.. cfunction:: PyObject* PyUnicode_DecodeFSDefaultAndSize(const char *s, Py_ssize_t size)
391
392 Decode a null-terminated string using :cdata:`Py_FileSystemDefaultEncoding`
393 and the ``"surrogateescape"`` error handler.
394
395 If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
396
397 Use :func:`PyUnicode_DecodeFSDefaultAndSize` if you know the string length.
398
399.. cfunction:: PyObject* PyUnicode_DecodeFSDefault(const char *s)
400
401 Decode a string using :cdata:`Py_FileSystemDefaultEncoding` and
402 the ``"surrogateescape"`` error handler.
403
404 If :cdata:`Py_FileSystemDefaultEncoding` is not set, fall back to UTF-8.
405
406
407wchar_t Support
408"""""""""""""""
409
410wchar_t support for platforms which support it:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000411
412.. cfunction:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size)
413
414 Create a Unicode object from the :ctype:`wchar_t` buffer *w* of the given size.
Martin v. Löwis790465f2008-04-05 20:41:37 +0000415 Passing -1 as the size indicates that the function must itself compute the length,
416 using wcslen.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000417 Return *NULL* on failure.
418
419
420.. cfunction:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size)
421
422 Copy the Unicode object contents into the :ctype:`wchar_t` buffer *w*. At most
423 *size* :ctype:`wchar_t` characters are copied (excluding a possibly trailing
424 0-termination character). Return the number of :ctype:`wchar_t` characters
425 copied or -1 in case of an error. Note that the resulting :ctype:`wchar_t`
426 string may or may not be 0-terminated. It is the responsibility of the caller
427 to make sure that the :ctype:`wchar_t` string is 0-terminated in case this is
428 required by the application.
429
430
431.. _builtincodecs:
432
433Built-in Codecs
434^^^^^^^^^^^^^^^
435
Georg Brandl22b34312009-07-26 14:54:51 +0000436Python provides a set of built-in codecs which are written in C for speed. All of
Georg Brandl54a3faa2008-01-20 09:30:57 +0000437these codecs are directly usable via the following functions.
438
439Many of the following APIs take two arguments encoding and errors. These
440parameters encoding and errors have the same semantics as the ones of the
Georg Brandl22b34312009-07-26 14:54:51 +0000441built-in :func:`unicode` Unicode object constructor.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000442
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000443Setting encoding to *NULL* causes the default encoding to be used
444which is ASCII. The file system calls should use
445:cfunc:`PyUnicode_FSConverter` for encoding file names. This uses the
446variable :cdata:`Py_FileSystemDefaultEncoding` internally. This
447variable should be treated as read-only: On some systems, it will be a
448pointer to a static string, on others, it will change at run-time
449(such as when the application invokes setlocale).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000450
451Error handling is set by errors which may also be set to *NULL* meaning to use
452the default handling defined for the codec. Default error handling for all
Georg Brandl22b34312009-07-26 14:54:51 +0000453built-in codecs is "strict" (:exc:`ValueError` is raised).
Georg Brandl54a3faa2008-01-20 09:30:57 +0000454
455The codecs all use a similar interface. Only deviation from the following
456generic ones are documented for simplicity.
457
Georg Brandl54a3faa2008-01-20 09:30:57 +0000458
Victor Stinner77c38622010-05-14 15:58:55 +0000459Generic Codecs
460""""""""""""""
461
462These are the generic codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000463
464
465.. cfunction:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors)
466
467 Create a Unicode object by decoding *size* bytes of the encoded string *s*.
468 *encoding* and *errors* have the same meaning as the parameters of the same name
Georg Brandl22b34312009-07-26 14:54:51 +0000469 in the :func:`unicode` built-in function. The codec to be used is looked up
Georg Brandl54a3faa2008-01-20 09:30:57 +0000470 using the Python codec registry. Return *NULL* if an exception was raised by
471 the codec.
472
473
474.. cfunction:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors)
475
476 Encode the :ctype:`Py_UNICODE` buffer of the given size and return a Python
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000477 bytes object. *encoding* and *errors* have the same meaning as the
478 parameters of the same name in the Unicode :meth:`encode` method. The codec
479 to be used is looked up using the Python codec registry. Return *NULL* if an
480 exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000481
482
483.. cfunction:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors)
484
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000485 Encode a Unicode object and return the result as Python bytes object.
486 *encoding* and *errors* have the same meaning as the parameters of the same
487 name in the Unicode :meth:`encode` method. The codec to be used is looked up
488 using the Python codec registry. Return *NULL* if an exception was raised by
489 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000490
Georg Brandl54a3faa2008-01-20 09:30:57 +0000491
Victor Stinner77c38622010-05-14 15:58:55 +0000492UTF-8 Codecs
493""""""""""""
494
495These are the UTF-8 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000496
497
498.. cfunction:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors)
499
500 Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string
501 *s*. Return *NULL* if an exception was raised by the codec.
502
503
504.. cfunction:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed)
505
506 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF8`. If
507 *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be
508 treated as an error. Those bytes will not be decoded and the number of bytes
509 that have been decoded will be stored in *consumed*.
510
511
512.. cfunction:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
513
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000514 Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-8 and
515 return a Python bytes object. Return *NULL* if an exception was raised by
516 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000517
518
519.. cfunction:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode)
520
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000521 Encode a Unicode object using UTF-8 and return the result as Python bytes
522 object. Error handling is "strict". Return *NULL* if an exception was
523 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000524
Georg Brandl54a3faa2008-01-20 09:30:57 +0000525
Victor Stinner77c38622010-05-14 15:58:55 +0000526UTF-32 Codecs
527"""""""""""""
528
529These are the UTF-32 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000530
531
532.. cfunction:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
533
534 Decode *length* bytes from a UTF-32 encoded buffer string and return the
535 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
536 handling. It defaults to "strict".
537
538 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
539 order::
540
541 *byteorder == -1: little endian
542 *byteorder == 0: native order
543 *byteorder == 1: big endian
544
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000545 If ``*byteorder`` is zero, and the first four bytes of the input data are a
546 byte order mark (BOM), the decoder switches to this byte order and the BOM is
547 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
548 ``1``, any byte order mark is copied to the output.
549
550 After completion, *\*byteorder* is set to the current byte order at the end
551 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000552
553 In a narrow build codepoints outside the BMP will be decoded as surrogate pairs.
554
555 If *byteorder* is *NULL*, the codec starts in native order mode.
556
557 Return *NULL* if an exception was raised by the codec.
558
559
560.. cfunction:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
561
562 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF32`. If
563 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF32Stateful` will not treat
564 trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible
565 by four) as an error. Those bytes will not be decoded and the number of bytes
566 that have been decoded will be stored in *consumed*.
567
568
569.. cfunction:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
570
571 Return a Python bytes object holding the UTF-32 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000572 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000573
574 byteorder == -1: little endian
575 byteorder == 0: native byte order (writes a BOM mark)
576 byteorder == 1: big endian
577
578 If byteorder is ``0``, the output string will always start with the Unicode BOM
579 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
580
581 If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output
582 as a single codepoint.
583
584 Return *NULL* if an exception was raised by the codec.
585
586
587.. cfunction:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode)
588
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000589 Return a Python byte string using the UTF-32 encoding in native byte
590 order. The string always starts with a BOM mark. Error handling is "strict".
591 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000592
593
Victor Stinner77c38622010-05-14 15:58:55 +0000594UTF-16 Codecs
595"""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000596
Victor Stinner77c38622010-05-14 15:58:55 +0000597These are the UTF-16 codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000598
599
600.. cfunction:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
601
602 Decode *length* bytes from a UTF-16 encoded buffer string and return the
603 corresponding Unicode object. *errors* (if non-*NULL*) defines the error
604 handling. It defaults to "strict".
605
606 If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte
607 order::
608
609 *byteorder == -1: little endian
610 *byteorder == 0: native order
611 *byteorder == 1: big endian
612
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000613 If ``*byteorder`` is zero, and the first two bytes of the input data are a
614 byte order mark (BOM), the decoder switches to this byte order and the BOM is
615 not copied into the resulting Unicode string. If ``*byteorder`` is ``-1`` or
616 ``1``, any byte order mark is copied to the output (where it will result in
617 either a ``\ufeff`` or a ``\ufffe`` character).
618
619 After completion, *\*byteorder* is set to the current byte order at the end
620 of input data.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000621
622 If *byteorder* is *NULL*, the codec starts in native order mode.
623
624 Return *NULL* if an exception was raised by the codec.
625
626
627.. cfunction:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed)
628
629 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF16`. If
630 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF16Stateful` will not treat
631 trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a
632 split surrogate pair) as an error. Those bytes will not be decoded and the
633 number of bytes that have been decoded will be stored in *consumed*.
634
635
636.. cfunction:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder)
637
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000638 Return a Python bytes object holding the UTF-16 encoded value of the Unicode
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +0000639 data in *s*. Output is written according to the following byte order::
Georg Brandl54a3faa2008-01-20 09:30:57 +0000640
641 byteorder == -1: little endian
642 byteorder == 0: native byte order (writes a BOM mark)
643 byteorder == 1: big endian
644
645 If byteorder is ``0``, the output string will always start with the Unicode BOM
646 mark (U+FEFF). In the other two modes, no BOM mark is prepended.
647
648 If *Py_UNICODE_WIDE* is defined, a single :ctype:`Py_UNICODE` value may get
649 represented as a surrogate pair. If it is not defined, each :ctype:`Py_UNICODE`
650 values is interpreted as an UCS-2 character.
651
652 Return *NULL* if an exception was raised by the codec.
653
654
655.. cfunction:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode)
656
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000657 Return a Python byte string using the UTF-16 encoding in native byte
658 order. The string always starts with a BOM mark. Error handling is "strict".
659 Return *NULL* if an exception was raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000660
Georg Brandl54a3faa2008-01-20 09:30:57 +0000661
Victor Stinner77c38622010-05-14 15:58:55 +0000662Unicode-Escape Codecs
663"""""""""""""""""""""
664
665These are the "Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000666
667
668.. cfunction:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
669
670 Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded
671 string *s*. Return *NULL* if an exception was raised by the codec.
672
673
674.. cfunction:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size)
675
676 Encode the :ctype:`Py_UNICODE` buffer of the given size using Unicode-Escape and
677 return a Python string object. Return *NULL* if an exception was raised by the
678 codec.
679
680
681.. cfunction:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode)
682
683 Encode a Unicode object using Unicode-Escape and return the result as Python
684 string object. Error handling is "strict". Return *NULL* if an exception was
685 raised by the codec.
686
Georg Brandl54a3faa2008-01-20 09:30:57 +0000687
Victor Stinner77c38622010-05-14 15:58:55 +0000688Raw-Unicode-Escape Codecs
689"""""""""""""""""""""""""
690
691These are the "Raw Unicode Escape" codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000692
693
694.. cfunction:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors)
695
696 Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape
697 encoded string *s*. Return *NULL* if an exception was raised by the codec.
698
699
700.. cfunction:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
701
702 Encode the :ctype:`Py_UNICODE` buffer of the given size using Raw-Unicode-Escape
703 and return a Python string object. Return *NULL* if an exception was raised by
704 the codec.
705
706
707.. cfunction:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode)
708
709 Encode a Unicode object using Raw-Unicode-Escape and return the result as
710 Python string object. Error handling is "strict". Return *NULL* if an exception
711 was raised by the codec.
712
Victor Stinner77c38622010-05-14 15:58:55 +0000713
714Latin-1 Codecs
715""""""""""""""
716
Georg Brandl54a3faa2008-01-20 09:30:57 +0000717These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode
718ordinals and only these are accepted by the codecs during encoding.
719
Georg Brandl54a3faa2008-01-20 09:30:57 +0000720
721.. cfunction:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors)
722
723 Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string
724 *s*. Return *NULL* if an exception was raised by the codec.
725
726
727.. cfunction:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
728
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000729 Encode the :ctype:`Py_UNICODE` buffer of the given size using Latin-1 and
730 return a Python bytes object. Return *NULL* if an exception was raised by
731 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000732
733
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
Victor Stinner77c38622010-05-14 15:58:55 +0000740
741ASCII Codecs
742""""""""""""
743
Georg Brandl54a3faa2008-01-20 09:30:57 +0000744These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other
745codes generate errors.
746
Georg Brandl54a3faa2008-01-20 09:30:57 +0000747
748.. cfunction:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors)
749
750 Create a Unicode object by decoding *size* bytes of the ASCII encoded string
751 *s*. Return *NULL* if an exception was raised by the codec.
752
753
754.. cfunction:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
755
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000756 Encode the :ctype:`Py_UNICODE` buffer of the given size using ASCII and
757 return a Python bytes object. Return *NULL* if an exception was raised by
758 the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000759
760
761.. cfunction:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode)
762
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000763 Encode a Unicode object using ASCII and return the result as Python bytes
764 object. Error handling is "strict". Return *NULL* if an exception was
765 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000766
Georg Brandl54a3faa2008-01-20 09:30:57 +0000767
Victor Stinner77c38622010-05-14 15:58:55 +0000768Character Map Codecs
769""""""""""""""""""""
770
771These are the mapping codec APIs:
Georg Brandl54a3faa2008-01-20 09:30:57 +0000772
773This codec is special in that it can be used to implement many different codecs
774(and this is in fact what was done to obtain most of the standard codecs
775included in the :mod:`encodings` package). The codec uses mapping to encode and
776decode characters.
777
778Decoding mappings must map single string characters to single Unicode
779characters, integers (which are then interpreted as Unicode ordinals) or None
780(meaning "undefined mapping" and causing an error).
781
782Encoding mappings must map single Unicode characters to single string
783characters, integers (which are then interpreted as Latin-1 ordinals) or None
784(meaning "undefined mapping" and causing an error).
785
786The mapping objects provided must only support the __getitem__ mapping
787interface.
788
789If a character lookup fails with a LookupError, the character is copied as-is
790meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal
791resp. Because of this, mappings only need to contain those mappings which map
792characters to different code points.
793
794
795.. cfunction:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors)
796
797 Create a Unicode object by decoding *size* bytes of the encoded string *s* using
798 the given *mapping* object. Return *NULL* if an exception was raised by the
799 codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a
800 dictionary mapping byte or a unicode string, which is treated as a lookup table.
801 Byte values greater that the length of the string and U+FFFE "characters" are
802 treated as "undefined mapping".
803
804
805.. cfunction:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors)
806
807 Encode the :ctype:`Py_UNICODE` buffer of the given size using the given
808 *mapping* object and return a Python string object. Return *NULL* if an
809 exception was raised by the codec.
810
811
812.. cfunction:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping)
813
814 Encode a Unicode object using the given *mapping* object and return the result
815 as Python string object. Error handling is "strict". Return *NULL* if an
816 exception was raised by the codec.
817
818The following codec API is special in that maps Unicode to Unicode.
819
820
821.. cfunction:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors)
822
823 Translate a :ctype:`Py_UNICODE` buffer of the given length by applying a
824 character mapping *table* to it and return the resulting Unicode object. Return
825 *NULL* when an exception was raised by the codec.
826
827 The *mapping* table must map Unicode ordinal integers to Unicode ordinal
828 integers or None (causing deletion of the character).
829
830 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
831 and sequences work well. Unmapped character ordinals (ones which cause a
832 :exc:`LookupError`) are left untouched and are copied as-is.
833
Jeroen Ruigrok van der Werven47a7d702009-04-27 05:43:17 +0000834
Georg Brandl54a3faa2008-01-20 09:30:57 +0000835These are the MBCS codec APIs. They are currently only available on Windows and
836use the Win32 MBCS converters to implement the conversions. Note that MBCS (or
837DBCS) is a class of encodings, not just one. The target encoding is defined by
838the user settings on the machine running the codec.
839
Victor Stinner77c38622010-05-14 15:58:55 +0000840
841MBCS codecs for Windows
842"""""""""""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000843
844
845.. cfunction:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors)
846
847 Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*.
848 Return *NULL* if an exception was raised by the codec.
849
850
851.. cfunction:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed)
852
853 If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeMBCS`. If
854 *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeMBCSStateful` will not decode
855 trailing lead byte and the number of bytes that have been decoded will be stored
856 in *consumed*.
857
858
859.. cfunction:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors)
860
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000861 Encode the :ctype:`Py_UNICODE` buffer of the given size using MBCS and return
862 a Python bytes object. Return *NULL* if an exception was raised by the
863 codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000864
865
866.. cfunction:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode)
867
Benjamin Petersonb6eba4f2009-01-13 23:14:04 +0000868 Encode a Unicode object using MBCS and return the result as Python bytes
869 object. Error handling is "strict". Return *NULL* if an exception was
870 raised by the codec.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000871
Martin v. Löwisc15bdef2009-05-29 14:47:46 +0000872
Victor Stinner77c38622010-05-14 15:58:55 +0000873Methods & Slots
874"""""""""""""""
Georg Brandl54a3faa2008-01-20 09:30:57 +0000875
876
877.. _unicodemethodsandslots:
878
879Methods and Slot Functions
880^^^^^^^^^^^^^^^^^^^^^^^^^^
881
882The following APIs are capable of handling Unicode objects and strings on input
883(we refer to them as strings in the descriptions) and return Unicode objects or
884integers as appropriate.
885
886They all return *NULL* or ``-1`` if an exception occurs.
887
888
889.. cfunction:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right)
890
891 Concat two strings giving a new Unicode string.
892
893
894.. cfunction:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
895
896 Split a string giving a list of Unicode strings. If sep is *NULL*, splitting
897 will be done at all whitespace substrings. Otherwise, splits occur at the given
898 separator. At most *maxsplit* splits will be done. If negative, no limit is
899 set. Separators are not included in the resulting list.
900
901
902.. cfunction:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend)
903
904 Split a Unicode string at line breaks, returning a list of Unicode strings.
905 CRLF is considered to be one line break. If *keepend* is 0, the Line break
906 characters are not included in the resulting strings.
907
908
909.. cfunction:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors)
910
911 Translate a string by applying a character mapping table to it and return the
912 resulting Unicode object.
913
914 The mapping table must map Unicode ordinal integers to Unicode ordinal integers
915 or None (causing deletion of the character).
916
917 Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries
918 and sequences work well. Unmapped character ordinals (ones which cause a
919 :exc:`LookupError`) are left untouched and are copied as-is.
920
921 *errors* has the usual meaning for codecs. It may be *NULL* which indicates to
922 use the default error handling.
923
924
925.. cfunction:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq)
926
927 Join a sequence of strings using the given separator and return the resulting
928 Unicode string.
929
930
931.. cfunction:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
932
933 Return 1 if *substr* matches *str*[*start*:*end*] at the given tail end
934 (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match),
935 0 otherwise. Return ``-1`` if an error occurred.
936
937
938.. cfunction:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction)
939
940 Return the first position of *substr* in *str*[*start*:*end*] using the given
941 *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a
942 backward search). The return value is the index of the first match; a value of
943 ``-1`` indicates that no match was found, and ``-2`` indicates that an error
944 occurred and an exception has been set.
945
946
947.. cfunction:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end)
948
949 Return the number of non-overlapping occurrences of *substr* in
950 ``str[start:end]``. Return ``-1`` if an error occurred.
951
952
953.. cfunction:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount)
954
955 Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and
956 return the resulting Unicode object. *maxcount* == -1 means replace all
957 occurrences.
958
959
960.. cfunction:: int PyUnicode_Compare(PyObject *left, PyObject *right)
961
962 Compare two strings and return -1, 0, 1 for less than, equal, and greater than,
963 respectively.
964
965
Benjamin Petersonc22ed142008-07-01 19:12:34 +0000966.. cfunction:: int PyUnicode_CompareWithASCIIString(PyObject *uni, char *string)
967
968 Compare a unicode object, *uni*, with *string* and return -1, 0, 1 for less
969 than, equal, and greater than, respectively.
970
971
Georg Brandl54a3faa2008-01-20 09:30:57 +0000972.. cfunction:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
973
974 Rich compare two unicode strings and return one of the following:
975
976 * ``NULL`` in case an exception was raised
977 * :const:`Py_True` or :const:`Py_False` for successful comparisons
978 * :const:`Py_NotImplemented` in case the type combination is unknown
979
980 Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a
981 :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails
982 with a :exc:`UnicodeDecodeError`.
983
984 Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`,
985 :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`.
986
987
988.. cfunction:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args)
989
990 Return a new string object from *format* and *args*; this is analogous to
991 ``format % args``. The *args* argument must be a tuple.
992
993
994.. cfunction:: int PyUnicode_Contains(PyObject *container, PyObject *element)
995
996 Check whether *element* is contained in *container* and return true or false
997 accordingly.
998
999 *element* has to coerce to a one element Unicode string. ``-1`` is returned if
1000 there was an error.
1001
1002
1003.. cfunction:: void PyUnicode_InternInPlace(PyObject **string)
1004
1005 Intern the argument *\*string* in place. The argument must be the address of a
1006 pointer variable pointing to a Python unicode string object. If there is an
1007 existing interned string that is the same as *\*string*, it sets *\*string* to
1008 it (decrementing the reference count of the old string object and incrementing
1009 the reference count of the interned string object), otherwise it leaves
1010 *\*string* alone and interns it (incrementing its reference count).
1011 (Clarification: even though there is a lot of talk about reference counts, think
1012 of this function as reference-count-neutral; you own the object after the call
1013 if and only if you owned it before the call.)
1014
1015
1016.. cfunction:: PyObject* PyUnicode_InternFromString(const char *v)
1017
1018 A combination of :cfunc:`PyUnicode_FromString` and
1019 :cfunc:`PyUnicode_InternInPlace`, returning either a new unicode string object
1020 that has been interned, or a new ("owned") reference to an earlier interned
1021 string object with the same value.
1022