Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _unicodeobjects: |
| 4 | |
| 5 | Unicode Objects and Codecs |
| 6 | -------------------------- |
| 7 | |
| 8 | .. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com> |
| 9 | |
| 10 | Unicode Objects |
| 11 | ^^^^^^^^^^^^^^^ |
| 12 | |
| 13 | These are the basic Unicode object types used for the Unicode implementation in |
| 14 | Python: |
| 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 | |
| 33 | Note that UCS2 and UCS4 Python builds are not binary compatible. Please keep |
| 34 | this 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 | |
| 47 | The following APIs are really C macros and can be used to do fast checks and to |
| 48 | access 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 | |
| 68 | |
| 69 | .. cfunction:: Py_ssize_t PyUnicode_GET_DATA_SIZE(PyObject *o) |
| 70 | |
| 71 | Return the size of the object's internal buffer in bytes. *o* has to be a |
| 72 | :ctype:`PyUnicodeObject` (not checked). |
| 73 | |
| 74 | |
| 75 | .. cfunction:: Py_UNICODE* PyUnicode_AS_UNICODE(PyObject *o) |
| 76 | |
| 77 | Return a pointer to the internal :ctype:`Py_UNICODE` buffer of the object. *o* |
| 78 | has to be a :ctype:`PyUnicodeObject` (not checked). |
| 79 | |
| 80 | |
| 81 | .. cfunction:: const char* PyUnicode_AS_DATA(PyObject *o) |
| 82 | |
| 83 | Return a pointer to the internal buffer of the object. *o* has to be a |
| 84 | :ctype:`PyUnicodeObject` (not checked). |
| 85 | |
| 86 | Unicode provides many different character properties. The most often needed ones |
| 87 | are available through these macros which are mapped to C functions depending on |
| 88 | the Python configuration. |
| 89 | |
| 90 | .. % --- Unicode character properties --------------------------------------- |
| 91 | |
| 92 | |
| 93 | .. cfunction:: int Py_UNICODE_ISSPACE(Py_UNICODE ch) |
| 94 | |
| 95 | Return 1 or 0 depending on whether *ch* is a whitespace character. |
| 96 | |
| 97 | |
| 98 | .. cfunction:: int Py_UNICODE_ISLOWER(Py_UNICODE ch) |
| 99 | |
| 100 | Return 1 or 0 depending on whether *ch* is a lowercase character. |
| 101 | |
| 102 | |
| 103 | .. cfunction:: int Py_UNICODE_ISUPPER(Py_UNICODE ch) |
| 104 | |
| 105 | Return 1 or 0 depending on whether *ch* is an uppercase character. |
| 106 | |
| 107 | |
| 108 | .. cfunction:: int Py_UNICODE_ISTITLE(Py_UNICODE ch) |
| 109 | |
| 110 | Return 1 or 0 depending on whether *ch* is a titlecase character. |
| 111 | |
| 112 | |
| 113 | .. cfunction:: int Py_UNICODE_ISLINEBREAK(Py_UNICODE ch) |
| 114 | |
| 115 | Return 1 or 0 depending on whether *ch* is a linebreak character. |
| 116 | |
| 117 | |
| 118 | .. cfunction:: int Py_UNICODE_ISDECIMAL(Py_UNICODE ch) |
| 119 | |
| 120 | Return 1 or 0 depending on whether *ch* is a decimal character. |
| 121 | |
| 122 | |
| 123 | .. cfunction:: int Py_UNICODE_ISDIGIT(Py_UNICODE ch) |
| 124 | |
| 125 | Return 1 or 0 depending on whether *ch* is a digit character. |
| 126 | |
| 127 | |
| 128 | .. cfunction:: int Py_UNICODE_ISNUMERIC(Py_UNICODE ch) |
| 129 | |
| 130 | Return 1 or 0 depending on whether *ch* is a numeric character. |
| 131 | |
| 132 | |
| 133 | .. cfunction:: int Py_UNICODE_ISALPHA(Py_UNICODE ch) |
| 134 | |
| 135 | Return 1 or 0 depending on whether *ch* is an alphabetic character. |
| 136 | |
| 137 | |
| 138 | .. cfunction:: int Py_UNICODE_ISALNUM(Py_UNICODE ch) |
| 139 | |
| 140 | Return 1 or 0 depending on whether *ch* is an alphanumeric character. |
| 141 | |
| 142 | These APIs can be used for fast direct character conversions: |
| 143 | |
| 144 | |
| 145 | .. cfunction:: Py_UNICODE Py_UNICODE_TOLOWER(Py_UNICODE ch) |
| 146 | |
| 147 | Return the character *ch* converted to lower case. |
| 148 | |
| 149 | |
| 150 | .. cfunction:: Py_UNICODE Py_UNICODE_TOUPPER(Py_UNICODE ch) |
| 151 | |
| 152 | Return the character *ch* converted to upper case. |
| 153 | |
| 154 | |
| 155 | .. cfunction:: Py_UNICODE Py_UNICODE_TOTITLE(Py_UNICODE ch) |
| 156 | |
| 157 | Return the character *ch* converted to title case. |
| 158 | |
| 159 | |
| 160 | .. cfunction:: int Py_UNICODE_TODECIMAL(Py_UNICODE ch) |
| 161 | |
| 162 | Return the character *ch* converted to a decimal positive integer. Return |
| 163 | ``-1`` if this is not possible. This macro does not raise exceptions. |
| 164 | |
| 165 | |
| 166 | .. cfunction:: int Py_UNICODE_TODIGIT(Py_UNICODE ch) |
| 167 | |
| 168 | Return the character *ch* converted to a single digit integer. Return ``-1`` if |
| 169 | this is not possible. This macro does not raise exceptions. |
| 170 | |
| 171 | |
| 172 | .. cfunction:: double Py_UNICODE_TONUMERIC(Py_UNICODE ch) |
| 173 | |
| 174 | Return the character *ch* converted to a double. Return ``-1.0`` if this is not |
| 175 | possible. This macro does not raise exceptions. |
| 176 | |
| 177 | To create Unicode objects and access their basic sequence properties, use these |
| 178 | APIs: |
| 179 | |
| 180 | .. % --- Plain Py_UNICODE --------------------------------------------------- |
| 181 | |
| 182 | |
| 183 | .. cfunction:: PyObject* PyUnicode_FromUnicode(const Py_UNICODE *u, Py_ssize_t size) |
| 184 | |
| 185 | Create a Unicode Object from the Py_UNICODE buffer *u* of the given size. *u* |
| 186 | may be *NULL* which causes the contents to be undefined. It is the user's |
| 187 | responsibility to fill in the needed data. The buffer is copied into the new |
| 188 | object. If the buffer is not *NULL*, the return value might be a shared object. |
| 189 | Therefore, modification of the resulting Unicode object is only allowed when *u* |
| 190 | is *NULL*. |
| 191 | |
| 192 | |
| 193 | .. cfunction:: PyObject* PyUnicode_FromStringAndSize(const char *u, Py_ssize_t size) |
| 194 | |
| 195 | Create a Unicode Object from the char buffer *u*. The bytes will be interpreted |
| 196 | as being UTF-8 encoded. *u* may also be *NULL* which |
| 197 | causes the contents to be undefined. It is the user's responsibility to fill in |
| 198 | the needed data. The buffer is copied into the new object. If the buffer is not |
| 199 | *NULL*, the return value might be a shared object. Therefore, modification of |
| 200 | the resulting Unicode object is only allowed when *u* is *NULL*. |
| 201 | |
| 202 | |
| 203 | .. cfunction:: PyObject *PyUnicode_FromString(const char *u) |
| 204 | |
| 205 | Create a Unicode object from an UTF-8 encoded null-terminated char buffer |
| 206 | *u*. |
| 207 | |
| 208 | |
| 209 | .. cfunction:: PyObject* PyUnicode_FromFormat(const char *format, ...) |
| 210 | |
| 211 | Take a C :cfunc:`printf`\ -style *format* string and a variable number of |
| 212 | arguments, calculate the size of the resulting Python unicode string and return |
| 213 | a string with the values formatted into it. The variable arguments must be C |
| 214 | types and must correspond exactly to the format characters in the *format* |
| 215 | string. The following format characters are allowed: |
| 216 | |
| 217 | .. % The descriptions for %zd and %zu are wrong, but the truth is complicated |
| 218 | .. % because not all compilers support the %z width modifier -- we fake it |
| 219 | .. % when necessary via interpolating PY_FORMAT_SIZE_T. |
| 220 | |
| 221 | +-------------------+---------------------+--------------------------------+ |
| 222 | | Format Characters | Type | Comment | |
| 223 | +===================+=====================+================================+ |
| 224 | | :attr:`%%` | *n/a* | The literal % character. | |
| 225 | +-------------------+---------------------+--------------------------------+ |
| 226 | | :attr:`%c` | int | A single character, | |
| 227 | | | | represented as an C int. | |
| 228 | +-------------------+---------------------+--------------------------------+ |
| 229 | | :attr:`%d` | int | Exactly equivalent to | |
| 230 | | | | ``printf("%d")``. | |
| 231 | +-------------------+---------------------+--------------------------------+ |
| 232 | | :attr:`%u` | unsigned int | Exactly equivalent to | |
| 233 | | | | ``printf("%u")``. | |
| 234 | +-------------------+---------------------+--------------------------------+ |
| 235 | | :attr:`%ld` | long | Exactly equivalent to | |
| 236 | | | | ``printf("%ld")``. | |
| 237 | +-------------------+---------------------+--------------------------------+ |
| 238 | | :attr:`%lu` | unsigned long | Exactly equivalent to | |
| 239 | | | | ``printf("%lu")``. | |
| 240 | +-------------------+---------------------+--------------------------------+ |
| 241 | | :attr:`%zd` | Py_ssize_t | Exactly equivalent to | |
| 242 | | | | ``printf("%zd")``. | |
| 243 | +-------------------+---------------------+--------------------------------+ |
| 244 | | :attr:`%zu` | size_t | Exactly equivalent to | |
| 245 | | | | ``printf("%zu")``. | |
| 246 | +-------------------+---------------------+--------------------------------+ |
| 247 | | :attr:`%i` | int | Exactly equivalent to | |
| 248 | | | | ``printf("%i")``. | |
| 249 | +-------------------+---------------------+--------------------------------+ |
| 250 | | :attr:`%x` | int | Exactly equivalent to | |
| 251 | | | | ``printf("%x")``. | |
| 252 | +-------------------+---------------------+--------------------------------+ |
| 253 | | :attr:`%s` | char\* | A null-terminated C character | |
| 254 | | | | array. | |
| 255 | +-------------------+---------------------+--------------------------------+ |
| 256 | | :attr:`%p` | void\* | The hex representation of a C | |
| 257 | | | | pointer. Mostly equivalent to | |
| 258 | | | | ``printf("%p")`` except that | |
| 259 | | | | it is guaranteed to start with | |
| 260 | | | | the literal ``0x`` regardless | |
| 261 | | | | of what the platform's | |
| 262 | | | | ``printf`` yields. | |
| 263 | +-------------------+---------------------+--------------------------------+ |
| 264 | | :attr:`%U` | PyObject\* | A unicode object. | |
| 265 | +-------------------+---------------------+--------------------------------+ |
| 266 | | :attr:`%V` | PyObject\*, char \* | A unicode object (which may be | |
| 267 | | | | *NULL*) and a null-terminated | |
| 268 | | | | C character array as a second | |
| 269 | | | | parameter (which will be used, | |
| 270 | | | | if the first parameter is | |
| 271 | | | | *NULL*). | |
| 272 | +-------------------+---------------------+--------------------------------+ |
| 273 | | :attr:`%S` | PyObject\* | The result of calling | |
| 274 | | | | :func:`PyObject_Unicode`. | |
| 275 | +-------------------+---------------------+--------------------------------+ |
| 276 | | :attr:`%R` | PyObject\* | The result of calling | |
| 277 | | | | :func:`PyObject_Repr`. | |
| 278 | +-------------------+---------------------+--------------------------------+ |
| 279 | |
| 280 | An unrecognized format character causes all the rest of the format string to be |
| 281 | copied as-is to the result string, and any extra arguments discarded. |
| 282 | |
| 283 | |
| 284 | .. cfunction:: PyObject* PyUnicode_FromFormatV(const char *format, va_list vargs) |
| 285 | |
| 286 | Identical to :func:`PyUnicode_FromFormat` except that it takes exactly two |
| 287 | arguments. |
| 288 | |
| 289 | |
| 290 | .. cfunction:: Py_UNICODE* PyUnicode_AsUnicode(PyObject *unicode) |
| 291 | |
| 292 | Return a read-only pointer to the Unicode object's internal :ctype:`Py_UNICODE` |
| 293 | buffer, *NULL* if *unicode* is not a Unicode object. |
| 294 | |
| 295 | |
| 296 | .. cfunction:: Py_ssize_t PyUnicode_GetSize(PyObject *unicode) |
| 297 | |
| 298 | Return the length of the Unicode object. |
| 299 | |
| 300 | |
| 301 | .. cfunction:: PyObject* PyUnicode_FromEncodedObject(PyObject *obj, const char *encoding, const char *errors) |
| 302 | |
| 303 | Coerce an encoded object *obj* to an Unicode object and return a reference with |
| 304 | incremented refcount. |
| 305 | |
| 306 | String and other char buffer compatible objects are decoded according to the |
| 307 | given encoding and using the error handling defined by errors. Both can be |
| 308 | *NULL* to have the interface use the default values (see the next section for |
| 309 | details). |
| 310 | |
| 311 | All other objects, including Unicode objects, cause a :exc:`TypeError` to be |
| 312 | set. |
| 313 | |
| 314 | The API returns *NULL* if there was an error. The caller is responsible for |
| 315 | decref'ing the returned objects. |
| 316 | |
| 317 | |
| 318 | .. cfunction:: PyObject* PyUnicode_FromObject(PyObject *obj) |
| 319 | |
| 320 | Shortcut for ``PyUnicode_FromEncodedObject(obj, NULL, "strict")`` which is used |
| 321 | throughout the interpreter whenever coercion to Unicode is needed. |
| 322 | |
| 323 | If the platform supports :ctype:`wchar_t` and provides a header file wchar.h, |
| 324 | Python can interface directly to this type using the following functions. |
| 325 | Support is optimized if Python's own :ctype:`Py_UNICODE` type is identical to |
| 326 | the system's :ctype:`wchar_t`. |
| 327 | |
| 328 | .. % --- wchar_t support for platforms which support it --------------------- |
| 329 | |
| 330 | |
| 331 | .. cfunction:: PyObject* PyUnicode_FromWideChar(const wchar_t *w, Py_ssize_t size) |
| 332 | |
| 333 | Create a Unicode object from the :ctype:`wchar_t` buffer *w* of the given size. |
| 334 | Return *NULL* on failure. |
| 335 | |
| 336 | |
| 337 | .. cfunction:: Py_ssize_t PyUnicode_AsWideChar(PyUnicodeObject *unicode, wchar_t *w, Py_ssize_t size) |
| 338 | |
| 339 | Copy the Unicode object contents into the :ctype:`wchar_t` buffer *w*. At most |
| 340 | *size* :ctype:`wchar_t` characters are copied (excluding a possibly trailing |
| 341 | 0-termination character). Return the number of :ctype:`wchar_t` characters |
| 342 | copied or -1 in case of an error. Note that the resulting :ctype:`wchar_t` |
| 343 | string may or may not be 0-terminated. It is the responsibility of the caller |
| 344 | to make sure that the :ctype:`wchar_t` string is 0-terminated in case this is |
| 345 | required by the application. |
| 346 | |
| 347 | |
| 348 | .. _builtincodecs: |
| 349 | |
| 350 | Built-in Codecs |
| 351 | ^^^^^^^^^^^^^^^ |
| 352 | |
| 353 | Python provides a set of builtin codecs which are written in C for speed. All of |
| 354 | these codecs are directly usable via the following functions. |
| 355 | |
| 356 | Many of the following APIs take two arguments encoding and errors. These |
| 357 | parameters encoding and errors have the same semantics as the ones of the |
| 358 | builtin unicode() Unicode object constructor. |
| 359 | |
| 360 | Setting encoding to *NULL* causes the default encoding to be used which is |
| 361 | ASCII. The file system calls should use :cdata:`Py_FileSystemDefaultEncoding` |
| 362 | as the encoding for file names. This variable should be treated as read-only: On |
| 363 | some systems, it will be a pointer to a static string, on others, it will change |
| 364 | at run-time (such as when the application invokes setlocale). |
| 365 | |
| 366 | Error handling is set by errors which may also be set to *NULL* meaning to use |
| 367 | the default handling defined for the codec. Default error handling for all |
| 368 | builtin codecs is "strict" (:exc:`ValueError` is raised). |
| 369 | |
| 370 | The codecs all use a similar interface. Only deviation from the following |
| 371 | generic ones are documented for simplicity. |
| 372 | |
| 373 | These are the generic codec APIs: |
| 374 | |
| 375 | .. % --- Generic Codecs ----------------------------------------------------- |
| 376 | |
| 377 | |
| 378 | .. cfunction:: PyObject* PyUnicode_Decode(const char *s, Py_ssize_t size, const char *encoding, const char *errors) |
| 379 | |
| 380 | Create a Unicode object by decoding *size* bytes of the encoded string *s*. |
| 381 | *encoding* and *errors* have the same meaning as the parameters of the same name |
| 382 | in the :func:`unicode` builtin function. The codec to be used is looked up |
| 383 | using the Python codec registry. Return *NULL* if an exception was raised by |
| 384 | the codec. |
| 385 | |
| 386 | |
| 387 | .. cfunction:: PyObject* PyUnicode_Encode(const Py_UNICODE *s, Py_ssize_t size, const char *encoding, const char *errors) |
| 388 | |
| 389 | Encode the :ctype:`Py_UNICODE` buffer of the given size and return a Python |
| 390 | string object. *encoding* and *errors* have the same meaning as the parameters |
| 391 | of the same name in the Unicode :meth:`encode` method. The codec to be used is |
| 392 | looked up using the Python codec registry. Return *NULL* if an exception was |
| 393 | raised by the codec. |
| 394 | |
| 395 | |
| 396 | .. cfunction:: PyObject* PyUnicode_AsEncodedString(PyObject *unicode, const char *encoding, const char *errors) |
| 397 | |
| 398 | Encode a Unicode object and return the result as Python string object. |
| 399 | *encoding* and *errors* have the same meaning as the parameters of the same name |
| 400 | in the Unicode :meth:`encode` method. The codec to be used is looked up using |
| 401 | the Python codec registry. Return *NULL* if an exception was raised by the |
| 402 | codec. |
| 403 | |
| 404 | These are the UTF-8 codec APIs: |
| 405 | |
| 406 | .. % --- UTF-8 Codecs ------------------------------------------------------- |
| 407 | |
| 408 | |
| 409 | .. cfunction:: PyObject* PyUnicode_DecodeUTF8(const char *s, Py_ssize_t size, const char *errors) |
| 410 | |
| 411 | Create a Unicode object by decoding *size* bytes of the UTF-8 encoded string |
| 412 | *s*. Return *NULL* if an exception was raised by the codec. |
| 413 | |
| 414 | |
| 415 | .. cfunction:: PyObject* PyUnicode_DecodeUTF8Stateful(const char *s, Py_ssize_t size, const char *errors, Py_ssize_t *consumed) |
| 416 | |
| 417 | If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF8`. If |
| 418 | *consumed* is not *NULL*, trailing incomplete UTF-8 byte sequences will not be |
| 419 | treated as an error. Those bytes will not be decoded and the number of bytes |
| 420 | that have been decoded will be stored in *consumed*. |
| 421 | |
| 422 | |
| 423 | .. cfunction:: PyObject* PyUnicode_EncodeUTF8(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
| 424 | |
| 425 | Encode the :ctype:`Py_UNICODE` buffer of the given size using UTF-8 and return a |
| 426 | Python string object. Return *NULL* if an exception was raised by the codec. |
| 427 | |
| 428 | |
| 429 | .. cfunction:: PyObject* PyUnicode_AsUTF8String(PyObject *unicode) |
| 430 | |
| 431 | Encode a Unicode object using UTF-8 and return the result as Python string |
| 432 | object. Error handling is "strict". Return *NULL* if an exception was raised |
| 433 | by the codec. |
| 434 | |
| 435 | These are the UTF-32 codec APIs: |
| 436 | |
| 437 | .. % --- UTF-32 Codecs ------------------------------------------------------ */ |
| 438 | |
| 439 | |
| 440 | .. cfunction:: PyObject* PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder) |
| 441 | |
| 442 | Decode *length* bytes from a UTF-32 encoded buffer string and return the |
| 443 | corresponding Unicode object. *errors* (if non-*NULL*) defines the error |
| 444 | handling. It defaults to "strict". |
| 445 | |
| 446 | If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte |
| 447 | order:: |
| 448 | |
| 449 | *byteorder == -1: little endian |
| 450 | *byteorder == 0: native order |
| 451 | *byteorder == 1: big endian |
| 452 | |
| 453 | and then switches if the first four bytes of the input data are a byte order mark |
| 454 | (BOM) and the specified byte order is native order. This BOM is not copied into |
| 455 | the resulting Unicode string. After completion, *\*byteorder* is set to the |
| 456 | current byte order at the end of input data. |
| 457 | |
| 458 | In a narrow build codepoints outside the BMP will be decoded as surrogate pairs. |
| 459 | |
| 460 | If *byteorder* is *NULL*, the codec starts in native order mode. |
| 461 | |
| 462 | Return *NULL* if an exception was raised by the codec. |
| 463 | |
| 464 | |
| 465 | .. cfunction:: PyObject* PyUnicode_DecodeUTF32Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed) |
| 466 | |
| 467 | If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF32`. If |
| 468 | *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF32Stateful` will not treat |
| 469 | trailing incomplete UTF-32 byte sequences (such as a number of bytes not divisible |
| 470 | by four) as an error. Those bytes will not be decoded and the number of bytes |
| 471 | that have been decoded will be stored in *consumed*. |
| 472 | |
| 473 | |
| 474 | .. cfunction:: PyObject* PyUnicode_EncodeUTF32(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder) |
| 475 | |
| 476 | Return a Python bytes object holding the UTF-32 encoded value of the Unicode |
| 477 | data in *s*. If *byteorder* is not ``0``, output is written according to the |
| 478 | following byte order:: |
| 479 | |
| 480 | byteorder == -1: little endian |
| 481 | byteorder == 0: native byte order (writes a BOM mark) |
| 482 | byteorder == 1: big endian |
| 483 | |
| 484 | If byteorder is ``0``, the output string will always start with the Unicode BOM |
| 485 | mark (U+FEFF). In the other two modes, no BOM mark is prepended. |
| 486 | |
| 487 | If *Py_UNICODE_WIDE* is not defined, surrogate pairs will be output |
| 488 | as a single codepoint. |
| 489 | |
| 490 | Return *NULL* if an exception was raised by the codec. |
| 491 | |
| 492 | |
| 493 | .. cfunction:: PyObject* PyUnicode_AsUTF32String(PyObject *unicode) |
| 494 | |
| 495 | Return a Python string using the UTF-32 encoding in native byte order. The |
| 496 | string always starts with a BOM mark. Error handling is "strict". Return |
| 497 | *NULL* if an exception was raised by the codec. |
| 498 | |
| 499 | |
| 500 | These are the UTF-16 codec APIs: |
| 501 | |
| 502 | .. % --- UTF-16 Codecs ------------------------------------------------------ */ |
| 503 | |
| 504 | |
| 505 | .. cfunction:: PyObject* PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors, int *byteorder) |
| 506 | |
| 507 | Decode *length* bytes from a UTF-16 encoded buffer string and return the |
| 508 | corresponding Unicode object. *errors* (if non-*NULL*) defines the error |
| 509 | handling. It defaults to "strict". |
| 510 | |
| 511 | If *byteorder* is non-*NULL*, the decoder starts decoding using the given byte |
| 512 | order:: |
| 513 | |
| 514 | *byteorder == -1: little endian |
| 515 | *byteorder == 0: native order |
| 516 | *byteorder == 1: big endian |
| 517 | |
| 518 | and then switches if the first two bytes of the input data are a byte order mark |
| 519 | (BOM) and the specified byte order is native order. This BOM is not copied into |
| 520 | the resulting Unicode string. After completion, *\*byteorder* is set to the |
| 521 | current byte order at the end of input data. |
| 522 | |
| 523 | If *byteorder* is *NULL*, the codec starts in native order mode. |
| 524 | |
| 525 | Return *NULL* if an exception was raised by the codec. |
| 526 | |
| 527 | |
| 528 | .. cfunction:: PyObject* PyUnicode_DecodeUTF16Stateful(const char *s, Py_ssize_t size, const char *errors, int *byteorder, Py_ssize_t *consumed) |
| 529 | |
| 530 | If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeUTF16`. If |
| 531 | *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeUTF16Stateful` will not treat |
| 532 | trailing incomplete UTF-16 byte sequences (such as an odd number of bytes or a |
| 533 | split surrogate pair) as an error. Those bytes will not be decoded and the |
| 534 | number of bytes that have been decoded will be stored in *consumed*. |
| 535 | |
| 536 | |
| 537 | .. cfunction:: PyObject* PyUnicode_EncodeUTF16(const Py_UNICODE *s, Py_ssize_t size, const char *errors, int byteorder) |
| 538 | |
| 539 | Return a Python string object holding the UTF-16 encoded value of the Unicode |
| 540 | data in *s*. If *byteorder* is not ``0``, output is written according to the |
| 541 | following byte order:: |
| 542 | |
| 543 | byteorder == -1: little endian |
| 544 | byteorder == 0: native byte order (writes a BOM mark) |
| 545 | byteorder == 1: big endian |
| 546 | |
| 547 | If byteorder is ``0``, the output string will always start with the Unicode BOM |
| 548 | mark (U+FEFF). In the other two modes, no BOM mark is prepended. |
| 549 | |
| 550 | If *Py_UNICODE_WIDE* is defined, a single :ctype:`Py_UNICODE` value may get |
| 551 | represented as a surrogate pair. If it is not defined, each :ctype:`Py_UNICODE` |
| 552 | values is interpreted as an UCS-2 character. |
| 553 | |
| 554 | Return *NULL* if an exception was raised by the codec. |
| 555 | |
| 556 | |
| 557 | .. cfunction:: PyObject* PyUnicode_AsUTF16String(PyObject *unicode) |
| 558 | |
| 559 | Return a Python string using the UTF-16 encoding in native byte order. The |
| 560 | string always starts with a BOM mark. Error handling is "strict". Return |
| 561 | *NULL* if an exception was raised by the codec. |
| 562 | |
| 563 | These are the "Unicode Escape" codec APIs: |
| 564 | |
| 565 | .. % --- Unicode-Escape Codecs ---------------------------------------------- |
| 566 | |
| 567 | |
| 568 | .. cfunction:: PyObject* PyUnicode_DecodeUnicodeEscape(const char *s, Py_ssize_t size, const char *errors) |
| 569 | |
| 570 | Create a Unicode object by decoding *size* bytes of the Unicode-Escape encoded |
| 571 | string *s*. Return *NULL* if an exception was raised by the codec. |
| 572 | |
| 573 | |
| 574 | .. cfunction:: PyObject* PyUnicode_EncodeUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size) |
| 575 | |
| 576 | Encode the :ctype:`Py_UNICODE` buffer of the given size using Unicode-Escape and |
| 577 | return a Python string object. Return *NULL* if an exception was raised by the |
| 578 | codec. |
| 579 | |
| 580 | |
| 581 | .. cfunction:: PyObject* PyUnicode_AsUnicodeEscapeString(PyObject *unicode) |
| 582 | |
| 583 | Encode a Unicode object using Unicode-Escape and return the result as Python |
| 584 | string object. Error handling is "strict". Return *NULL* if an exception was |
| 585 | raised by the codec. |
| 586 | |
| 587 | These are the "Raw Unicode Escape" codec APIs: |
| 588 | |
| 589 | .. % --- Raw-Unicode-Escape Codecs ------------------------------------------ |
| 590 | |
| 591 | |
| 592 | .. cfunction:: PyObject* PyUnicode_DecodeRawUnicodeEscape(const char *s, Py_ssize_t size, const char *errors) |
| 593 | |
| 594 | Create a Unicode object by decoding *size* bytes of the Raw-Unicode-Escape |
| 595 | encoded string *s*. Return *NULL* if an exception was raised by the codec. |
| 596 | |
| 597 | |
| 598 | .. cfunction:: PyObject* PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
| 599 | |
| 600 | Encode the :ctype:`Py_UNICODE` buffer of the given size using Raw-Unicode-Escape |
| 601 | and return a Python string object. Return *NULL* if an exception was raised by |
| 602 | the codec. |
| 603 | |
| 604 | |
| 605 | .. cfunction:: PyObject* PyUnicode_AsRawUnicodeEscapeString(PyObject *unicode) |
| 606 | |
| 607 | Encode a Unicode object using Raw-Unicode-Escape and return the result as |
| 608 | Python string object. Error handling is "strict". Return *NULL* if an exception |
| 609 | was raised by the codec. |
| 610 | |
| 611 | These are the Latin-1 codec APIs: Latin-1 corresponds to the first 256 Unicode |
| 612 | ordinals and only these are accepted by the codecs during encoding. |
| 613 | |
| 614 | .. % --- Latin-1 Codecs ----------------------------------------------------- |
| 615 | |
| 616 | |
| 617 | .. cfunction:: PyObject* PyUnicode_DecodeLatin1(const char *s, Py_ssize_t size, const char *errors) |
| 618 | |
| 619 | Create a Unicode object by decoding *size* bytes of the Latin-1 encoded string |
| 620 | *s*. Return *NULL* if an exception was raised by the codec. |
| 621 | |
| 622 | |
| 623 | .. cfunction:: PyObject* PyUnicode_EncodeLatin1(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
| 624 | |
| 625 | Encode the :ctype:`Py_UNICODE` buffer of the given size using Latin-1 and return |
| 626 | a Python string object. Return *NULL* if an exception was raised by the codec. |
| 627 | |
| 628 | |
| 629 | .. cfunction:: PyObject* PyUnicode_AsLatin1String(PyObject *unicode) |
| 630 | |
| 631 | Encode a Unicode object using Latin-1 and return the result as Python string |
| 632 | object. Error handling is "strict". Return *NULL* if an exception was raised |
| 633 | by the codec. |
| 634 | |
| 635 | These are the ASCII codec APIs. Only 7-bit ASCII data is accepted. All other |
| 636 | codes generate errors. |
| 637 | |
| 638 | .. % --- ASCII Codecs ------------------------------------------------------- |
| 639 | |
| 640 | |
| 641 | .. cfunction:: PyObject* PyUnicode_DecodeASCII(const char *s, Py_ssize_t size, const char *errors) |
| 642 | |
| 643 | Create a Unicode object by decoding *size* bytes of the ASCII encoded string |
| 644 | *s*. Return *NULL* if an exception was raised by the codec. |
| 645 | |
| 646 | |
| 647 | .. cfunction:: PyObject* PyUnicode_EncodeASCII(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
| 648 | |
| 649 | Encode the :ctype:`Py_UNICODE` buffer of the given size using ASCII and return a |
| 650 | Python string object. Return *NULL* if an exception was raised by the codec. |
| 651 | |
| 652 | |
| 653 | .. cfunction:: PyObject* PyUnicode_AsASCIIString(PyObject *unicode) |
| 654 | |
| 655 | Encode a Unicode object using ASCII and return the result as Python string |
| 656 | object. Error handling is "strict". Return *NULL* if an exception was raised |
| 657 | by the codec. |
| 658 | |
| 659 | These are the mapping codec APIs: |
| 660 | |
| 661 | .. % --- Character Map Codecs ----------------------------------------------- |
| 662 | |
| 663 | This codec is special in that it can be used to implement many different codecs |
| 664 | (and this is in fact what was done to obtain most of the standard codecs |
| 665 | included in the :mod:`encodings` package). The codec uses mapping to encode and |
| 666 | decode characters. |
| 667 | |
| 668 | Decoding mappings must map single string characters to single Unicode |
| 669 | characters, integers (which are then interpreted as Unicode ordinals) or None |
| 670 | (meaning "undefined mapping" and causing an error). |
| 671 | |
| 672 | Encoding mappings must map single Unicode characters to single string |
| 673 | characters, integers (which are then interpreted as Latin-1 ordinals) or None |
| 674 | (meaning "undefined mapping" and causing an error). |
| 675 | |
| 676 | The mapping objects provided must only support the __getitem__ mapping |
| 677 | interface. |
| 678 | |
| 679 | If a character lookup fails with a LookupError, the character is copied as-is |
| 680 | meaning that its ordinal value will be interpreted as Unicode or Latin-1 ordinal |
| 681 | resp. Because of this, mappings only need to contain those mappings which map |
| 682 | characters to different code points. |
| 683 | |
| 684 | |
| 685 | .. cfunction:: PyObject* PyUnicode_DecodeCharmap(const char *s, Py_ssize_t size, PyObject *mapping, const char *errors) |
| 686 | |
| 687 | Create a Unicode object by decoding *size* bytes of the encoded string *s* using |
| 688 | the given *mapping* object. Return *NULL* if an exception was raised by the |
| 689 | codec. If *mapping* is *NULL* latin-1 decoding will be done. Else it can be a |
| 690 | dictionary mapping byte or a unicode string, which is treated as a lookup table. |
| 691 | Byte values greater that the length of the string and U+FFFE "characters" are |
| 692 | treated as "undefined mapping". |
| 693 | |
| 694 | |
| 695 | .. cfunction:: PyObject* PyUnicode_EncodeCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *mapping, const char *errors) |
| 696 | |
| 697 | Encode the :ctype:`Py_UNICODE` buffer of the given size using the given |
| 698 | *mapping* object and return a Python string object. Return *NULL* if an |
| 699 | exception was raised by the codec. |
| 700 | |
| 701 | |
| 702 | .. cfunction:: PyObject* PyUnicode_AsCharmapString(PyObject *unicode, PyObject *mapping) |
| 703 | |
| 704 | Encode a Unicode object using the given *mapping* object and return the result |
| 705 | as Python string object. Error handling is "strict". Return *NULL* if an |
| 706 | exception was raised by the codec. |
| 707 | |
| 708 | The following codec API is special in that maps Unicode to Unicode. |
| 709 | |
| 710 | |
| 711 | .. cfunction:: PyObject* PyUnicode_TranslateCharmap(const Py_UNICODE *s, Py_ssize_t size, PyObject *table, const char *errors) |
| 712 | |
| 713 | Translate a :ctype:`Py_UNICODE` buffer of the given length by applying a |
| 714 | character mapping *table* to it and return the resulting Unicode object. Return |
| 715 | *NULL* when an exception was raised by the codec. |
| 716 | |
| 717 | The *mapping* table must map Unicode ordinal integers to Unicode ordinal |
| 718 | integers or None (causing deletion of the character). |
| 719 | |
| 720 | Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries |
| 721 | and sequences work well. Unmapped character ordinals (ones which cause a |
| 722 | :exc:`LookupError`) are left untouched and are copied as-is. |
| 723 | |
| 724 | These are the MBCS codec APIs. They are currently only available on Windows and |
| 725 | use the Win32 MBCS converters to implement the conversions. Note that MBCS (or |
| 726 | DBCS) is a class of encodings, not just one. The target encoding is defined by |
| 727 | the user settings on the machine running the codec. |
| 728 | |
| 729 | .. % --- MBCS codecs for Windows -------------------------------------------- |
| 730 | |
| 731 | |
| 732 | .. cfunction:: PyObject* PyUnicode_DecodeMBCS(const char *s, Py_ssize_t size, const char *errors) |
| 733 | |
| 734 | Create a Unicode object by decoding *size* bytes of the MBCS encoded string *s*. |
| 735 | Return *NULL* if an exception was raised by the codec. |
| 736 | |
| 737 | |
| 738 | .. cfunction:: PyObject* PyUnicode_DecodeMBCSStateful(const char *s, int size, const char *errors, int *consumed) |
| 739 | |
| 740 | If *consumed* is *NULL*, behave like :cfunc:`PyUnicode_DecodeMBCS`. If |
| 741 | *consumed* is not *NULL*, :cfunc:`PyUnicode_DecodeMBCSStateful` will not decode |
| 742 | trailing lead byte and the number of bytes that have been decoded will be stored |
| 743 | in *consumed*. |
| 744 | |
| 745 | |
| 746 | .. cfunction:: PyObject* PyUnicode_EncodeMBCS(const Py_UNICODE *s, Py_ssize_t size, const char *errors) |
| 747 | |
| 748 | Encode the :ctype:`Py_UNICODE` buffer of the given size using MBCS and return a |
| 749 | Python string object. Return *NULL* if an exception was raised by the codec. |
| 750 | |
| 751 | |
| 752 | .. cfunction:: PyObject* PyUnicode_AsMBCSString(PyObject *unicode) |
| 753 | |
| 754 | Encode a Unicode object using MBCS and return the result as Python string |
| 755 | object. Error handling is "strict". Return *NULL* if an exception was raised |
| 756 | by the codec. |
| 757 | |
| 758 | .. % --- Methods & Slots ---------------------------------------------------- |
| 759 | |
| 760 | |
| 761 | .. _unicodemethodsandslots: |
| 762 | |
| 763 | Methods and Slot Functions |
| 764 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 765 | |
| 766 | The following APIs are capable of handling Unicode objects and strings on input |
| 767 | (we refer to them as strings in the descriptions) and return Unicode objects or |
| 768 | integers as appropriate. |
| 769 | |
| 770 | They all return *NULL* or ``-1`` if an exception occurs. |
| 771 | |
| 772 | |
| 773 | .. cfunction:: PyObject* PyUnicode_Concat(PyObject *left, PyObject *right) |
| 774 | |
| 775 | Concat two strings giving a new Unicode string. |
| 776 | |
| 777 | |
| 778 | .. cfunction:: PyObject* PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit) |
| 779 | |
| 780 | Split a string giving a list of Unicode strings. If sep is *NULL*, splitting |
| 781 | will be done at all whitespace substrings. Otherwise, splits occur at the given |
| 782 | separator. At most *maxsplit* splits will be done. If negative, no limit is |
| 783 | set. Separators are not included in the resulting list. |
| 784 | |
| 785 | |
| 786 | .. cfunction:: PyObject* PyUnicode_Splitlines(PyObject *s, int keepend) |
| 787 | |
| 788 | Split a Unicode string at line breaks, returning a list of Unicode strings. |
| 789 | CRLF is considered to be one line break. If *keepend* is 0, the Line break |
| 790 | characters are not included in the resulting strings. |
| 791 | |
| 792 | |
| 793 | .. cfunction:: PyObject* PyUnicode_Translate(PyObject *str, PyObject *table, const char *errors) |
| 794 | |
| 795 | Translate a string by applying a character mapping table to it and return the |
| 796 | resulting Unicode object. |
| 797 | |
| 798 | The mapping table must map Unicode ordinal integers to Unicode ordinal integers |
| 799 | or None (causing deletion of the character). |
| 800 | |
| 801 | Mapping tables need only provide the :meth:`__getitem__` interface; dictionaries |
| 802 | and sequences work well. Unmapped character ordinals (ones which cause a |
| 803 | :exc:`LookupError`) are left untouched and are copied as-is. |
| 804 | |
| 805 | *errors* has the usual meaning for codecs. It may be *NULL* which indicates to |
| 806 | use the default error handling. |
| 807 | |
| 808 | |
| 809 | .. cfunction:: PyObject* PyUnicode_Join(PyObject *separator, PyObject *seq) |
| 810 | |
| 811 | Join a sequence of strings using the given separator and return the resulting |
| 812 | Unicode string. |
| 813 | |
| 814 | |
| 815 | .. cfunction:: int PyUnicode_Tailmatch(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction) |
| 816 | |
| 817 | Return 1 if *substr* matches *str*[*start*:*end*] at the given tail end |
| 818 | (*direction* == -1 means to do a prefix match, *direction* == 1 a suffix match), |
| 819 | 0 otherwise. Return ``-1`` if an error occurred. |
| 820 | |
| 821 | |
| 822 | .. cfunction:: Py_ssize_t PyUnicode_Find(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end, int direction) |
| 823 | |
| 824 | Return the first position of *substr* in *str*[*start*:*end*] using the given |
| 825 | *direction* (*direction* == 1 means to do a forward search, *direction* == -1 a |
| 826 | backward search). The return value is the index of the first match; a value of |
| 827 | ``-1`` indicates that no match was found, and ``-2`` indicates that an error |
| 828 | occurred and an exception has been set. |
| 829 | |
| 830 | |
| 831 | .. cfunction:: Py_ssize_t PyUnicode_Count(PyObject *str, PyObject *substr, Py_ssize_t start, Py_ssize_t end) |
| 832 | |
| 833 | Return the number of non-overlapping occurrences of *substr* in |
| 834 | ``str[start:end]``. Return ``-1`` if an error occurred. |
| 835 | |
| 836 | |
| 837 | .. cfunction:: PyObject* PyUnicode_Replace(PyObject *str, PyObject *substr, PyObject *replstr, Py_ssize_t maxcount) |
| 838 | |
| 839 | Replace at most *maxcount* occurrences of *substr* in *str* with *replstr* and |
| 840 | return the resulting Unicode object. *maxcount* == -1 means replace all |
| 841 | occurrences. |
| 842 | |
| 843 | |
| 844 | .. cfunction:: int PyUnicode_Compare(PyObject *left, PyObject *right) |
| 845 | |
| 846 | Compare two strings and return -1, 0, 1 for less than, equal, and greater than, |
| 847 | respectively. |
| 848 | |
| 849 | |
| 850 | .. cfunction:: int PyUnicode_RichCompare(PyObject *left, PyObject *right, int op) |
| 851 | |
| 852 | Rich compare two unicode strings and return one of the following: |
| 853 | |
| 854 | * ``NULL`` in case an exception was raised |
| 855 | * :const:`Py_True` or :const:`Py_False` for successful comparisons |
| 856 | * :const:`Py_NotImplemented` in case the type combination is unknown |
| 857 | |
| 858 | Note that :const:`Py_EQ` and :const:`Py_NE` comparisons can cause a |
| 859 | :exc:`UnicodeWarning` in case the conversion of the arguments to Unicode fails |
| 860 | with a :exc:`UnicodeDecodeError`. |
| 861 | |
| 862 | Possible values for *op* are :const:`Py_GT`, :const:`Py_GE`, :const:`Py_EQ`, |
| 863 | :const:`Py_NE`, :const:`Py_LT`, and :const:`Py_LE`. |
| 864 | |
| 865 | |
| 866 | .. cfunction:: PyObject* PyUnicode_Format(PyObject *format, PyObject *args) |
| 867 | |
| 868 | Return a new string object from *format* and *args*; this is analogous to |
| 869 | ``format % args``. The *args* argument must be a tuple. |
| 870 | |
| 871 | |
| 872 | .. cfunction:: int PyUnicode_Contains(PyObject *container, PyObject *element) |
| 873 | |
| 874 | Check whether *element* is contained in *container* and return true or false |
| 875 | accordingly. |
| 876 | |
| 877 | *element* has to coerce to a one element Unicode string. ``-1`` is returned if |
| 878 | there was an error. |
| 879 | |
| 880 | |
| 881 | .. cfunction:: void PyUnicode_InternInPlace(PyObject **string) |
| 882 | |
| 883 | Intern the argument *\*string* in place. The argument must be the address of a |
| 884 | pointer variable pointing to a Python unicode string object. If there is an |
| 885 | existing interned string that is the same as *\*string*, it sets *\*string* to |
| 886 | it (decrementing the reference count of the old string object and incrementing |
| 887 | the reference count of the interned string object), otherwise it leaves |
| 888 | *\*string* alone and interns it (incrementing its reference count). |
| 889 | (Clarification: even though there is a lot of talk about reference counts, think |
| 890 | of this function as reference-count-neutral; you own the object after the call |
| 891 | if and only if you owned it before the call.) |
| 892 | |
| 893 | |
| 894 | .. cfunction:: PyObject* PyUnicode_InternFromString(const char *v) |
| 895 | |
| 896 | A combination of :cfunc:`PyUnicode_FromString` and |
| 897 | :cfunc:`PyUnicode_InternInPlace`, returning either a new unicode string object |
| 898 | that has been interned, or a new ("owned") reference to an earlier interned |
| 899 | string object with the same value. |
| 900 | |