Skip to content

Commit 21bcecf

Browse files
[3.13] gh-136162: Document encodings package functions (GH-136164) (#136453)
gh-136162: Document `encodings` package functions (GH-136164) Closes GH-136162. (cherry picked from commit ffd7f2f) Co-authored-by: Stan Ulbrych <[email protected]>
1 parent faee362 commit 21bcecf

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Doc/library/codecs.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,66 @@ mapping. It is not supported by :meth:`str.encode` (which only produces
14871487
Restoration of the ``rot13`` alias.
14881488

14891489

1490+
:mod:`encodings` --- Encodings package
1491+
--------------------------------------
1492+
1493+
.. module:: encodings
1494+
:synopsis: Encodings package
1495+
1496+
This module implements the following functions:
1497+
1498+
.. function:: normalize_encoding(encoding)
1499+
1500+
Normalize encoding name *encoding*.
1501+
1502+
Normalization works as follows: all non-alphanumeric characters except the
1503+
dot used for Python package names are collapsed and replaced with a single
1504+
underscore, leading and trailing underscores are removed.
1505+
For example, ``' -;#'`` becomes ``'_'``.
1506+
1507+
Note that *encoding* should be ASCII only.
1508+
1509+
1510+
.. note::
1511+
The following functions should not be used directly, except for testing
1512+
purposes; :func:`codecs.lookup` should be used instead.
1513+
1514+
1515+
.. function:: search_function(encoding)
1516+
1517+
Search for the codec module corresponding to the given encoding name
1518+
*encoding*.
1519+
1520+
This function first normalizes the *encoding* using
1521+
:func:`normalize_encoding`, then looks for a corresponding alias.
1522+
It attempts to import a codec module from the encodings package using either
1523+
the alias or the normalized name. If the module is found and defines a valid
1524+
``getregentry()`` function that returns a :class:`codecs.CodecInfo` object,
1525+
the codec is cached and returned.
1526+
1527+
If the codec module defines a ``getaliases()`` function any returned aliases
1528+
are registered for future use.
1529+
1530+
1531+
.. function:: win32_code_page_search_function(encoding)
1532+
1533+
Search for a Windows code page encoding *encoding* of the form ``cpXXXX``.
1534+
1535+
If the code page is valid and supported, return a :class:`codecs.CodecInfo`
1536+
object for it.
1537+
1538+
.. availability:: Windows.
1539+
1540+
.. versionadded:: 3.14
1541+
1542+
1543+
This module implements the following exception:
1544+
1545+
.. exception:: CodecRegistryError
1546+
1547+
Raised when a codec is invalid or incompatible.
1548+
1549+
14901550
:mod:`encodings.idna` --- Internationalized Domain Names in Applications
14911551
------------------------------------------------------------------------
14921552

Doc/whatsnew/3.9.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ Changes in the Python API
11391139
(Contributed by Christian Heimes in :issue:`36384`).
11401140

11411141
* :func:`codecs.lookup` now normalizes the encoding name the same way as
1142-
:func:`!encodings.normalize_encoding`, except that :func:`codecs.lookup` also
1142+
:func:`encodings.normalize_encoding`, except that :func:`codecs.lookup` also
11431143
converts the name to lower case. For example, ``"latex+latin1"`` encoding
11441144
name is now normalized to ``"latex_latin1"``.
11451145
(Contributed by Jordon Xu in :issue:`37751`.)

0 commit comments

Comments
 (0)