]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136162: Document `encodings` package functions (#136164)
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Tue, 8 Jul 2025 21:34:48 +0000 (23:34 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Jul 2025 21:34:48 +0000 (23:34 +0200)
Closes #136162.

Doc/library/codecs.rst
Doc/whatsnew/3.9.rst

index b231fa568cf3423101120647b797c22e324087b7..efe211cd2db6c89c660a1df59ff695c9efe0e892 100644 (file)
@@ -1484,6 +1484,66 @@ mapping. It is not supported by :meth:`str.encode` (which only produces
    Restoration of the ``rot13`` alias.
 
 
+:mod:`encodings` --- Encodings package
+--------------------------------------
+
+.. module:: encodings
+   :synopsis: Encodings package
+
+This module implements the following functions:
+
+.. function:: normalize_encoding(encoding)
+
+   Normalize encoding name *encoding*.
+
+   Normalization works as follows: all non-alphanumeric characters except the
+   dot used for Python package names are collapsed and replaced with a single
+   underscore, leading and trailing underscores are removed.
+   For example, ``'  -;#'`` becomes ``'_'``.
+
+   Note that *encoding* should be ASCII only.
+
+
+.. note::
+   The following functions should not be used directly, except for testing
+   purposes; :func:`codecs.lookup` should be used instead.
+
+
+.. function:: search_function(encoding)
+
+   Search for the codec module corresponding to the given encoding name
+   *encoding*.
+
+   This function first normalizes the *encoding* using
+   :func:`normalize_encoding`, then looks for a corresponding alias.
+   It attempts to import a codec module from the encodings package using either
+   the alias or the normalized name. If the module is found and defines a valid
+   ``getregentry()`` function that returns a :class:`codecs.CodecInfo` object,
+   the codec is cached and returned.
+
+   If the codec module defines a ``getaliases()`` function any returned aliases
+   are registered for future use.
+
+
+.. function:: win32_code_page_search_function(encoding)
+
+   Search for a Windows code page encoding *encoding* of the form ``cpXXXX``.
+
+   If the code page is valid and supported, return a :class:`codecs.CodecInfo`
+   object for it.
+
+   .. availability:: Windows.
+
+   .. versionadded:: 3.14
+
+
+This module implements the following exception:
+
+.. exception:: CodecRegistryError
+
+   Raised when a codec is invalid or incompatible.
+
+
 :mod:`encodings.idna` --- Internationalized Domain Names in Applications
 ------------------------------------------------------------------------
 
index 7fd9e6ac66e6c8b68cb01a610205966628ffe9ac..40d4a27bff9fee5ce4d9d0aaf6cdf312f36ced84 100644 (file)
@@ -1139,7 +1139,7 @@ Changes in the Python API
   (Contributed by Christian Heimes in :issue:`36384`).
 
 * :func:`codecs.lookup` now normalizes the encoding name the same way as
-  :func:`!encodings.normalize_encoding`, except that :func:`codecs.lookup` also
+  :func:`encodings.normalize_encoding`, except that :func:`codecs.lookup` also
   converts the name to lower case. For example, ``"latex+latin1"`` encoding
   name is now normalized to ``"latex_latin1"``.
   (Contributed by Jordon Xu in :issue:`37751`.)