Skip to content

Commit 4bebb71

Browse files
authored
Add documentation for codecs.escape_encode() and codecs.escape_decode()
1 parent d1d5dce commit 4bebb71

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Doc/library/codecs.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,3 +1579,35 @@ This module implements a variant of the UTF-8 codec. On encoding, a UTF-8 encode
15791579
BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this
15801580
is only done once (on the first write to the byte stream). On decoding, an
15811581
optional UTF-8 encoded BOM at the start of the data will be skipped.
1582+
1583+
1584+
1585+
Escape Encoding and Decoding
1586+
----------------------------
1587+
1588+
The functions ``codecs.escape_encode()`` and ``codecs.escape_decode()`` provide
1589+
a mechanism to encode and decode byte sequences with escape sequences. This is
1590+
similar in behavior to how ``str(bytes)`` and ``repr(bytes)`` produce escaped
1591+
string representations of bytes.
1592+
1593+
These APIs are primarily used by the ``pickle`` module to safely handle
1594+
escaped byte sequences.
1595+
1596+
Example usage:
1597+
1598+
.. code-block:: python
1599+
1600+
import codecs
1601+
b = b"example\nbytes\tdata"
1602+
encoded_bytes, _ = codecs.escape_encode(b)
1603+
decoded_bytes, _ = codecs.escape_decode(encoded_bytes)
1604+
assert decoded_bytes == b
1605+
1606+
Background:
1607+
~~~~~~~~~~~
1608+
In Python 2, these functions were part of the now-removed ``string_escape`` codec.
1609+
There is consideration to revive this functionality under a new codec called
1610+
``bytes_escape``.
1611+
1612+
References:
1613+
- Related discussion: https://bugs.python.org/issue136278

0 commit comments

Comments
 (0)