Skip to content

Commit 333528b

Browse files
StanFromIrelandmiss-islington
authored andcommitted
pythongh-136162: Document encodings package functions (pythonGH-136164)
Closes pythonGH-136162. (cherry picked from commit ffd7f2f) Co-authored-by: Stan Ulbrych <[email protected]>
1 parent c44070b commit 333528b

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
@@ -1473,6 +1473,66 @@ mapping. It is not supported by :meth:`str.encode` (which only produces
14731473
Restoration of the ``rot13`` alias.
14741474

14751475

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

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)