Skip to content

Commit a2bb3b7

Browse files
gh-99991: improve docs on str.encode and bytes.decode (#100198)
Co-authored-by: C.A.M. Gerlach <[email protected]>
1 parent c18d831 commit a2bb3b7

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
@@ -1624,25 +1624,28 @@ expression support in the :mod:`re` module).
16241624

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

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

16411644
.. versionchanged:: 3.1
1642-
Support for keyword arguments added.
1645+
Added support for keyword arguments.
16431646

16441647
.. versionchanged:: 3.9
1645-
The *errors* is now checked in development mode and
1648+
The value of the *errors* argument is now checked in :ref:`devmode` and
16461649
in :ref:`debug mode <debug-build>`.
16471650

16481651

@@ -2759,29 +2762,32 @@ arbitrary binary data.
27592762
.. method:: bytes.decode(encoding="utf-8", errors="strict")
27602763
bytearray.decode(encoding="utf-8", errors="strict")
27612764

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

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

27742780
.. note::
27752781

27762782
Passing the *encoding* argument to :class:`str` allows decoding any
27772783
:term:`bytes-like object` directly, without needing to make a temporary
2778-
bytes or bytearray object.
2784+
:class:`!bytes` or :class:`!bytearray` object.
27792785

27802786
.. versionchanged:: 3.1
27812787
Added support for keyword arguments.
27822788

27832789
.. versionchanged:: 3.9
2784-
The *errors* is now checked in development mode and
2790+
The value of the *errors* argument is now checked in :ref:`devmode` and
27852791
in :ref:`debug mode <debug-build>`.
27862792

27872793

0 commit comments

Comments
 (0)