Skip to content

Commit b3d39c7

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 a757c1b commit b3d39c7

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

Doc/library/stdtypes.rst

+33-27
Original file line numberDiff line numberDiff line change
@@ -1585,25 +1585,28 @@ expression support in the :mod:`re` module).
15851585

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

1588-
Return an encoded version of the string as a bytes object. Default encoding
1589-
is ``'utf-8'``. *errors* may be given to set a different error handling scheme.
1590-
The default for *errors* is ``'strict'``, meaning that encoding errors raise
1591-
a :exc:`UnicodeError`. Other possible
1592-
values are ``'ignore'``, ``'replace'``, ``'xmlcharrefreplace'``,
1593-
``'backslashreplace'`` and any other name registered via
1594-
:func:`codecs.register_error`, see section :ref:`error-handlers`. For a
1595-
list of possible encodings, see section :ref:`standard-encodings`.
1596-
1597-
By default, the *errors* argument is not checked for best performances, but
1598-
only used at the first encoding error. Enable the :ref:`Python Development
1599-
Mode <devmode>`, or use a :ref:`debug build <debug-build>` to check
1600-
*errors*.
1588+
Return the string encoded to :class:`bytes`.
1589+
1590+
*encoding* defaults to ``'utf-8'``;
1591+
see :ref:`standard-encodings` for possible values.
1592+
1593+
*errors* controls how encoding errors are handled.
1594+
If ``'strict'`` (the default), a :exc:`UnicodeError` exception is raised.
1595+
Other possible values are ``'ignore'``,
1596+
``'replace'``, ``'xmlcharrefreplace'``, ``'backslashreplace'`` and any
1597+
other name registered via :func:`codecs.register_error`.
1598+
See :ref:`error-handlers` for details.
1599+
1600+
For performance reasons, the value of *errors* is not checked for validity
1601+
unless an encoding error actually occurs,
1602+
:ref:`devmode` is enabled
1603+
or a :ref:`debug build <debug-build>` is used.
16011604

16021605
.. versionchanged:: 3.1
1603-
Support for keyword arguments added.
1606+
Added support for keyword arguments.
16041607

16051608
.. versionchanged:: 3.9
1606-
The *errors* is now checked in development mode and
1609+
The value of the *errors* argument is now checked in :ref:`devmode` and
16071610
in :ref:`debug mode <debug-build>`.
16081611

16091612

@@ -2715,29 +2718,32 @@ arbitrary binary data.
27152718
.. method:: bytes.decode(encoding="utf-8", errors="strict")
27162719
bytearray.decode(encoding="utf-8", errors="strict")
27172720

2718-
Return a string decoded from the given bytes. Default encoding is
2719-
``'utf-8'``. *errors* may be given to set a different
2720-
error handling scheme. The default for *errors* is ``'strict'``, meaning
2721-
that encoding errors raise a :exc:`UnicodeError`. Other possible values are
2722-
``'ignore'``, ``'replace'`` and any other name registered via
2723-
:func:`codecs.register_error`, see section :ref:`error-handlers`. For a
2724-
list of possible encodings, see section :ref:`standard-encodings`.
2721+
Return the bytes decoded to a :class:`str`.
2722+
2723+
*encoding* defaults to ``'utf-8'``;
2724+
see :ref:`standard-encodings` for possible values.
2725+
2726+
*errors* controls how decoding errors are handled.
2727+
If ``'strict'`` (the default), a :exc:`UnicodeError` exception is raised.
2728+
Other possible values are ``'ignore'``, ``'replace'``,
2729+
and any other name registered via :func:`codecs.register_error`.
2730+
See :ref:`error-handlers` for details.
27252731

2726-
By default, the *errors* argument is not checked for best performances, but
2727-
only used at the first decoding error. Enable the :ref:`Python Development
2728-
Mode <devmode>`, or use a :ref:`debug build <debug-build>` to check *errors*.
2732+
For performance reasons, the value of *errors* is not checked for validity
2733+
unless a decoding error actually occurs,
2734+
:ref:`devmode` is enabled or a :ref:`debug build <debug-build>` is used.
27292735

27302736
.. note::
27312737

27322738
Passing the *encoding* argument to :class:`str` allows decoding any
27332739
:term:`bytes-like object` directly, without needing to make a temporary
2734-
bytes or bytearray object.
2740+
:class:`!bytes` or :class:`!bytearray` object.
27352741

27362742
.. versionchanged:: 3.1
27372743
Added support for keyword arguments.
27382744

27392745
.. versionchanged:: 3.9
2740-
The *errors* is now checked in development mode and
2746+
The value of the *errors* argument is now checked in :ref:`devmode` and
27412747
in :ref:`debug mode <debug-build>`.
27422748

27432749

0 commit comments

Comments
 (0)