Skip to content

Commit 23fa166

Browse files
miss-islingtonbizzyvinciCAM-Gerlach
authored
gh-99991: improve docs on str.encode and bytes.decode (GH-100198)
(cherry picked from commit a2bb3b7) Co-authored-by: Bisola Olasehinde <horlasehinde@gmail.com> Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
1 parent 73d2b15 commit 23fa166

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

Doc/library/stdtypes.rst

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,25 +1623,28 @@ expression support in the :mod:`re` module).
16231623

16241624
.. method:: str.encode(encoding="utf-8", errors="strict")
16251625

1626-
Return an encoded version of the string as a bytes object. Default encoding
1627-
is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
1628-
The default for *errors* is ``'strict'``, meaning that encoding errors raise
1629-
a :exc:`UnicodeError`. Other possible
1630-
values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1631-
``'backslashreplace'`` and any other name registered via
1632-
:func:`codecs.register_error`, see section :ref:`error-handlers`. For a
1633-
list of possible encodings, see section :ref:`standard-encodings`.
1634-
1635-
By default, the *errors* argument is not checked for best performances, but
1636-
only used at the first encoding error. Enable the :ref:`Python Development
1637-
Mode <devmode>`, or use a :ref:`debug build <debug-build>` to check
1638-
*errors*.
1626+
Return the string encoded to :class:`bytes`.
1627+
1628+
*encoding* defaults to ``'utf-8'``;
1629+
see :ref:`standard-encodings` for possible values.
1630+
1631+
*errors* controls how encoding errors are handled.
1632+
If ``'strict'`` (the default), a :exc:`UnicodeError` exception is raised.
1633+
Other possible values are ``'ignore'``,
1634+
``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and any
1635+
other name registered via :func:`codecs.register_error`.
1636+
See :ref:`error-handlers` for details.
1637+
1638+
For performance reasons, the value of *errors* is not checked for validity
1639+
unless an encoding error actually occurs,
1640+
:ref:`devmode` is enabled
1641+
or a :ref:`debug build <debug-build>` is used.
16391642

16401643
.. versionchanged:: 3.1
1641-
Support for keyword arguments added.
1644+
Added support for keyword arguments.
16421645

16431646
.. versionchanged:: 3.9
1644-
The *errors* is now checked in development mode and
1647+
The value of the *errors* argument is now checked in :ref:`devmode` and
16451648
in :ref:`debug mode <debug-build>`.
16461649

16471650

@@ -2755,29 +2758,32 @@ arbitrary binary data.
27552758
.. method:: bytes.decode(encoding="utf-8", errors="strict")
27562759
bytearray.decode(encoding="utf-8", errors="strict")
27572760

2758-
Return a string decoded from the given bytes. Default encoding is
2759-
``'utf-8'``. *errors* may be given to set a different
2760-
error handling scheme. The default for *errors* is ``'strict'``, meaning
2761-
that encoding errors raise a :exc:`UnicodeError`. Other possible values are
2762-
``'ignore'``, ``'replace'`` and any other name registered via
2763-
:func:`codecs.register_error`, see section :ref:`error-handlers`. For a
2764-
list of possible encodings, see section :ref:`standard-encodings`.
2761+
Return the bytes decoded to a :class:`str`.
2762+
2763+
*encoding* defaults to ``'utf-8'``;
2764+
see :ref:`standard-encodings` for possible values.
2765+
2766+
*errors* controls how decoding errors are handled.
2767+
If ``'strict'`` (the default), a :exc:`UnicodeError` exception is raised.
2768+
Other possible values are ``'ignore'``, ``'replace'``,
2769+
and any other name registered via :func:`codecs.register_error`.
2770+
See :ref:`error-handlers` for details.
27652771

2766-
By default, the *errors* argument is not checked for best performances, but
2767-
only used at the first decoding error. Enable the :ref:`Python Development
2768-
Mode <devmode>`, or use a :ref:`debug build <debug-build>` to check *errors*.
2772+
For performance reasons, the value of *errors* is not checked for validity
2773+
unless a decoding error actually occurs,
2774+
:ref:`devmode` is enabled or a :ref:`debug build <debug-build>` is used.
27692775

27702776
.. note::
27712777

27722778
Passing the *encoding* argument to :class:`str` allows decoding any
27732779
:term:`bytes-like object` directly, without needing to make a temporary
2774-
bytes or bytearray object.
2780+
:class:`!bytes` or :class:`!bytearray` object.
27752781

27762782
.. versionchanged:: 3.1
27772783
Added support for keyword arguments.
27782784

27792785
.. versionchanged:: 3.9
2780-
The *errors* is now checked in development mode and
2786+
The value of the *errors* argument is now checked in :ref:`devmode` and
27812787
in :ref:`debug mode <debug-build>`.
27822788

27832789

0 commit comments

Comments
 (0)