You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.7 KiB

  1. .. _codec-registry:
  2. Codec registry and support functions
  3. ====================================
  4. .. c:function:: int PyCodec_Register(PyObject *search_function)
  5. Register a new codec search function.
  6. As side effect, this tries to load the :mod:`encodings` package, if not yet
  7. done, to make sure that it is always first in the list of search functions.
  8. .. c:function:: int PyCodec_KnownEncoding(const char *encoding)
  9. Return ``1`` or ``0`` depending on whether there is a registered codec for
  10. the given *encoding*.
  11. .. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
  12. Generic codec based encoding API.
  13. *object* is passed through the encoder function found for the given
  14. *encoding* using the error handling method defined by *errors*. *errors* may
  15. be *NULL* to use the default method defined for the codec. Raises a
  16. :exc:`LookupError` if no encoder can be found.
  17. .. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
  18. Generic codec based decoding API.
  19. *object* is passed through the decoder function found for the given
  20. *encoding* using the error handling method defined by *errors*. *errors* may
  21. be *NULL* to use the default method defined for the codec. Raises a
  22. :exc:`LookupError` if no encoder can be found.
  23. Codec lookup API
  24. ----------------
  25. In the following functions, the *encoding* string is looked up converted to all
  26. lower-case characters, which makes encodings looked up through this mechanism
  27. effectively case-insensitive. If no codec is found, a :exc:`KeyError` is set
  28. and *NULL* returned.
  29. .. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
  30. Get an encoder function for the given *encoding*.
  31. .. c:function:: PyObject* PyCodec_Decoder(const char *encoding)
  32. Get a decoder function for the given *encoding*.
  33. .. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
  34. Get an :class:`~codecs.IncrementalEncoder` object for the given *encoding*.
  35. .. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
  36. Get an :class:`~codecs.IncrementalDecoder` object for the given *encoding*.
  37. .. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
  38. Get a :class:`~codecs.StreamReader` factory function for the given *encoding*.
  39. .. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
  40. Get a :class:`~codecs.StreamWriter` factory function for the given *encoding*.
  41. Registry API for Unicode encoding error handlers
  42. ------------------------------------------------
  43. .. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error)
  44. Register the error handling callback function *error* under the given *name*.
  45. This callback function will be called by a codec when it encounters
  46. unencodable characters/undecodable bytes and *name* is specified as the error
  47. parameter in the call to the encode/decode function.
  48. The callback gets a single argument, an instance of
  49. :exc:`UnicodeEncodeError`, :exc:`UnicodeDecodeError` or
  50. :exc:`UnicodeTranslateError` that holds information about the problematic
  51. sequence of characters or bytes and their offset in the original string (see
  52. :ref:`unicodeexceptions` for functions to extract this information). The
  53. callback must either raise the given exception, or return a two-item tuple
  54. containing the replacement for the problematic sequence, and an integer
  55. giving the offset in the original string at which encoding/decoding should be
  56. resumed.
  57. Return ``0`` on success, ``-1`` on error.
  58. .. c:function:: PyObject* PyCodec_LookupError(const char *name)
  59. Lookup the error handling callback function registered under *name*. As a
  60. special case *NULL* can be passed, in which case the error handling callback
  61. for "strict" will be returned.
  62. .. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
  63. Raise *exc* as an exception.
  64. .. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc)
  65. Ignore the unicode error, skipping the faulty input.
  66. .. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc)
  67. Replace the unicode encode error with ``?`` or ``U+FFFD``.
  68. .. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
  69. Replace the unicode encode error with XML character references.
  70. .. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)
  71. Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
  72. ``\U``).
  73. .. c:function:: PyObject* PyCodec_NameReplaceErrors(PyObject *exc)
  74. Replace the unicode encode error with ``\N{...}`` escapes.
  75. .. versionadded:: 3.5