From 648042b78f69c3383cd5f7a8f941be77aa55e184 Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Sat, 5 Jul 2025 12:37:49 +0530 Subject: [PATCH 1/4] Add documentation for codecs.escape_encode() and codecs.escape_decode() --- Doc/library/codecs.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index c5dae7c8e8fd04..c0f187bfee8551 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1461,10 +1461,26 @@ to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode` | uu_codec | uu | Convert the operand using | | | | | uuencode. | | +----------------------+------------------+------------------------------+------------------------------+ +| escape_codec | escape | Encode and decode byte | :func:`codecs.escape_encode` | +| | | sequences using escape | / | +| | | sequences, similar to | :func:`codecs.escape_decode` | +| | | :func:`repr` of bytes. | | ++----------------------+------------------+------------------------------+------------------------------+ | zlib_codec | zip, zlib | Compress the operand using | :meth:`zlib.compress` / | | | | gzip. | :meth:`zlib.decompress` | +----------------------+------------------+------------------------------+------------------------------+ +.. function:: codecs.escape_encode(input, errors='strict') + + Encode *input* using escape sequences. Similar to how :func:`repr` on bytes + produces escaped byte values. Returns a tuple of the encoded bytes and + the length consumed. + +.. function:: codecs.escape_decode(input, errors='strict') + + Decode *input* from escape sequences back to the original bytes. + Returns a tuple of the decoded bytes and the length consumed. + .. [#b64] In addition to :term:`bytes-like objects `, ``'base64_codec'`` also accepts ASCII-only instances of :class:`str` for decoding From e0fcfea51cddafaf7950f07674e6a18b9e2e2530 Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Sat, 5 Jul 2025 16:59:14 +0530 Subject: [PATCH 2/4] Fix default value of 'errors' to None in escape_decode function signature --- Doc/library/codecs.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index c0f187bfee8551..1b9e36ca032b3e 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1470,13 +1470,13 @@ to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode` | | | gzip. | :meth:`zlib.decompress` | +----------------------+------------------+------------------------------+------------------------------+ -.. function:: codecs.escape_encode(input, errors='strict') +.. function:: codecs.escape_encode(input, errors=None) Encode *input* using escape sequences. Similar to how :func:`repr` on bytes produces escaped byte values. Returns a tuple of the encoded bytes and the length consumed. -.. function:: codecs.escape_decode(input, errors='strict') +.. function:: codecs.escape_decode(input, errors=None) Decode *input* from escape sequences back to the original bytes. Returns a tuple of the decoded bytes and the length consumed. From 7613bcda46414a4f8a474d2c467f685e1b62e740 Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Tue, 8 Jul 2025 10:28:15 +0530 Subject: [PATCH 3/4] Move escape_encode and escape_decode to standalone section --- Doc/library/codecs.rst | 46 +++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index 1b9e36ca032b3e..b5e906543531c3 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1461,26 +1461,10 @@ to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode` | uu_codec | uu | Convert the operand using | | | | | uuencode. | | +----------------------+------------------+------------------------------+------------------------------+ -| escape_codec | escape | Encode and decode byte | :func:`codecs.escape_encode` | -| | | sequences using escape | / | -| | | sequences, similar to | :func:`codecs.escape_decode` | -| | | :func:`repr` of bytes. | | -+----------------------+------------------+------------------------------+------------------------------+ | zlib_codec | zip, zlib | Compress the operand using | :meth:`zlib.compress` / | | | | gzip. | :meth:`zlib.decompress` | +----------------------+------------------+------------------------------+------------------------------+ -.. function:: codecs.escape_encode(input, errors=None) - - Encode *input* using escape sequences. Similar to how :func:`repr` on bytes - produces escaped byte values. Returns a tuple of the encoded bytes and - the length consumed. - -.. function:: codecs.escape_decode(input, errors=None) - - Decode *input* from escape sequences back to the original bytes. - Returns a tuple of the decoded bytes and the length consumed. - .. [#b64] In addition to :term:`bytes-like objects `, ``'base64_codec'`` also accepts ASCII-only instances of :class:`str` for decoding @@ -1492,6 +1476,36 @@ to :class:`bytes` mappings. They are not supported by :meth:`bytes.decode` Restoration of the aliases for the binary transforms. +.. _standalone-codec-functions: + +Standalone Codec Functions +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The following functions provide encoding and decoding functionality similar to codecs, +but are not available as named codecs through :func:`codecs.encode` or :func:`codecs.decode`. +They are used internally (for example, by :mod:`pickle`) and behave similarly to the +`string_escape` codec that was removed in Python 3. + +.. function:: codecs.escape_encode(input, errors=None) + + Encode *input* using escape sequences. Similar to how :func:`repr` on bytes + produces escaped byte values. + + *input* must be a :class:`bytes` object. + + Returns a tuple ``(output, length)`` where *output* is a :class:`bytes` + object and *length* is the number of bytes consumed. + +.. function:: codecs.escape_decode(input, errors=None) + + Decode *input* from escape sequences back to the original bytes. + + *input* must be a :term:`bytes-like object`. + + Returns a tuple ``(output, length)`` where *output* is a :class:`bytes` + object and *length* is the number of bytes consumed. + + .. _text-transforms: Text Transforms From 4b26d7daaedaa8aa30fe5db87bdce1639ee3beef Mon Sep 17 00:00:00 2001 From: Toshaksha <147024929+Toshaksha@users.noreply.github.com> Date: Tue, 8 Jul 2025 10:43:25 +0530 Subject: [PATCH 4/4] Move escape_encode and escape_decode to standalone section --- Doc/library/codecs.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/codecs.rst b/Doc/library/codecs.rst index b5e906543531c3..6d234794e3c0be 100644 --- a/Doc/library/codecs.rst +++ b/Doc/library/codecs.rst @@ -1484,7 +1484,7 @@ Standalone Codec Functions The following functions provide encoding and decoding functionality similar to codecs, but are not available as named codecs through :func:`codecs.encode` or :func:`codecs.decode`. They are used internally (for example, by :mod:`pickle`) and behave similarly to the -`string_escape` codec that was removed in Python 3. +``string_escape`` codec that was removed in Python 3. .. function:: codecs.escape_encode(input, errors=None)