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