]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136278: Document codecs.escape_encode() and codecs.escape_decode() (#136314)
authorToshaksha <147024929+Toshaksha@users.noreply.github.com>
Sun, 27 Jul 2025 16:37:52 +0000 (22:07 +0530)
committerGitHub <noreply@github.com>
Sun, 27 Jul 2025 16:37:52 +0000 (16:37 +0000)
Closes #136278

Doc/library/codecs.rst

index 1cb0e225bca043a68517b4edf43e6078dc3f97db..f96f2f8281f4502a6a02a7c9ff1f0c4196ca38ee 100644 (file)
@@ -1483,6 +1483,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