cpython/Doc/c-api/codec.rst
Miss Islington (bot) 8fe7cfad87
Some checks failed
Tests / Change detection (push) Has been cancelled
Lint / lint (push) Has been cancelled
Tail calling interpreter / aarch64-apple-darwin/clang (push) Has been cancelled
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc-free-threading (push) Has been cancelled
Tail calling interpreter / x86_64-unknown-linux-gnu/gcc (push) Has been cancelled
Tail calling interpreter / aarch64-unknown-linux-gnu/gcc (push) Has been cancelled
Tests / Docs (push) Has been cancelled
Tests / Check if the ABI has changed (push) Has been cancelled
Tests / Check if Autoconf files are up to date (push) Has been cancelled
Tests / Check if generated files are up to date (push) Has been cancelled
Tests / C API Docs (push) Has been cancelled
Tests / (push) Has been cancelled
Tests / -1 (push) Has been cancelled
Tests / -2 (push) Has been cancelled
Tests / -3 (push) Has been cancelled
Tests / -4 (push) Has been cancelled
Tests / -5 (push) Has been cancelled
Tests / -6 (push) Has been cancelled
Tests / Windows MSI (push) Has been cancelled
Tests / Windows MSI-1 (push) Has been cancelled
Tests / Windows MSI-2 (push) Has been cancelled
Tests / -7 (push) Has been cancelled
Tests / -8 (push) Has been cancelled
Tests / -9 (push) Has been cancelled
Tests / -10 (push) Has been cancelled
Tests / -11 (push) Has been cancelled
Tests / -12 (push) Has been cancelled
Tests / -13 (push) Has been cancelled
Tests / -14 (push) Has been cancelled
Tests / Ubuntu (installed) (push) Has been cancelled
Tests / Ubuntu SSL tests (push) Has been cancelled
Tests / Ubuntu SSL tests-1 (push) Has been cancelled
Tests / Ubuntu SSL tests-2 (push) Has been cancelled
Tests / Ubuntu SSL tests-3 (push) Has been cancelled
Tests / Ubuntu SSL tests-4 (push) Has been cancelled
Tests / Ubuntu SSL tests-5 (push) Has been cancelled
Tests / Android (aarch64) (push) Has been cancelled
Tests / Android (x86_64) (push) Has been cancelled
Tests / iOS (push) Has been cancelled
Tests / Emscripten (push) Has been cancelled
Tests / WASI (push) Has been cancelled
Tests / Hypothesis tests on Ubuntu (push) Has been cancelled
Tests / Address sanitizer (push) Has been cancelled
Tests / Sanitizers (push) Has been cancelled
Tests / Sanitizers-1 (push) Has been cancelled
Tests / Sanitizers-2 (push) Has been cancelled
Tests / Sanitizers-3 (push) Has been cancelled
Tests / Cross build Linux (push) Has been cancelled
Tests / CIFuzz (push) Has been cancelled
Tests / CIFuzz-1 (push) Has been cancelled
Tests / CIFuzz-2 (push) Has been cancelled
Tests / CIFuzz-3 (push) Has been cancelled
Tests / All required checks pass (push) Has been cancelled
Docs: correct API guidance and examples (GH-160907)
(cherry picked from commit bd3be61c4a)

Co-authored-by: Inada Naoki <songofacandy@gmail.com>
2026-10-30 04:19:09 +00:00

141 lines
5.2 KiB
ReStructuredText

.. _codec-registry:
Codec registry and support functions
====================================
.. c:function:: int PyCodec_Register(PyObject *search_function)
Register a new codec search function.
As a side effect, this tries to load the :mod:`!encodings` package, if not yet
done, to make sure that it is always first in the list of search functions.
.. c:function:: int PyCodec_Unregister(PyObject *search_function)
Unregister a codec search function and clear the registry's cache.
If the search function is not registered, do nothing.
Return 0 on success. Raise an exception and return -1 on error.
.. versionadded:: 3.10
.. c:function:: int PyCodec_KnownEncoding(const char *encoding)
Return ``1`` or ``0`` depending on whether there is a registered codec for
the given *encoding*. This function always succeeds.
.. c:function:: PyObject* PyCodec_Encode(PyObject *object, const char *encoding, const char *errors)
Generic codec based encoding API.
*object* is passed through the encoder function found for the given
*encoding* using the error handling method defined by *errors*. *errors* may
be ``NULL`` to use the default method defined for the codec. Raises a
:exc:`LookupError` if no encoder can be found.
.. c:function:: PyObject* PyCodec_Decode(PyObject *object, const char *encoding, const char *errors)
Generic codec based decoding API.
*object* is passed through the decoder function found for the given
*encoding* using the error handling method defined by *errors*. *errors* may
be ``NULL`` to use the default method defined for the codec. Raises a
:exc:`LookupError` if no decoder can be found.
Codec lookup API
----------------
In the following functions, the *encoding* string is looked up converted to all
lower-case characters, which makes encodings looked up through this mechanism
effectively case-insensitive. If no codec is found, a :exc:`LookupError` is set
and ``NULL`` returned.
.. c:function:: PyObject* PyCodec_Encoder(const char *encoding)
Get an encoder function for the given *encoding*.
.. c:function:: PyObject* PyCodec_Decoder(const char *encoding)
Get a decoder function for the given *encoding*.
.. c:function:: PyObject* PyCodec_IncrementalEncoder(const char *encoding, const char *errors)
Get an :class:`~codecs.IncrementalEncoder` object for the given *encoding*.
.. c:function:: PyObject* PyCodec_IncrementalDecoder(const char *encoding, const char *errors)
Get an :class:`~codecs.IncrementalDecoder` object for the given *encoding*.
.. c:function:: PyObject* PyCodec_StreamReader(const char *encoding, PyObject *stream, const char *errors)
Get a :class:`~codecs.StreamReader` factory function for the given *encoding*.
.. c:function:: PyObject* PyCodec_StreamWriter(const char *encoding, PyObject *stream, const char *errors)
Get a :class:`~codecs.StreamWriter` factory function for the given *encoding*.
Registry API for Unicode encoding error handlers
------------------------------------------------
.. c:function:: int PyCodec_RegisterError(const char *name, PyObject *error)
Register the error handling callback function *error* under the given *name*.
This callback function will be called by a codec when it encounters
unencodable characters/undecodable bytes and *name* is specified as the error
parameter in the call to the encode/decode function.
The callback gets a single argument, an instance of
:exc:`UnicodeEncodeError`, :exc:`UnicodeDecodeError` or
:exc:`UnicodeTranslateError` that holds information about the problematic
sequence of characters or bytes and their offset in the original string (see
:ref:`unicodeexceptions` for functions to extract this information). The
callback must either raise the given exception, or return a two-item tuple
containing the replacement for the problematic sequence, and an integer
giving the offset in the original string at which encoding/decoding should be
resumed.
Return ``0`` on success, ``-1`` on error.
.. c:function:: PyObject* PyCodec_LookupError(const char *name)
Lookup the error handling callback function registered under *name*. As a
special case ``NULL`` can be passed, in which case the error handling callback
for "strict" will be returned.
.. c:function:: PyObject* PyCodec_StrictErrors(PyObject *exc)
Raise *exc* as an exception.
.. c:function:: PyObject* PyCodec_IgnoreErrors(PyObject *exc)
Ignore the unicode error, skipping the faulty input.
.. c:function:: PyObject* PyCodec_ReplaceErrors(PyObject *exc)
Replace the unicode encode error with ``?`` or ``U+FFFD``.
.. c:function:: PyObject* PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
Replace the unicode encode error with XML character references.
.. c:function:: PyObject* PyCodec_BackslashReplaceErrors(PyObject *exc)
Replace the unicode encode error with backslash escapes (``\x``, ``\u`` and
``\U``).
.. c:function:: PyObject* PyCodec_NameReplaceErrors(PyObject *exc)
Replace the unicode encode error with ``\N{...}`` escapes.
.. versionadded:: 3.5
Codec utility variables
-----------------------
.. c:var:: const char *Py_hexdigits
A string constant containing the lowercase hexadecimal digits: ``"0123456789abcdef"``.
.. versionadded:: 3.3