File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -1579,3 +1579,35 @@ This module implements a variant of the UTF-8 codec. On encoding, a UTF-8 encode
15791579BOM will be prepended to the UTF-8 encoded bytes. For the stateful encoder this
15801580is only done once (on the first write to the byte stream). On decoding, an
15811581optional 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\n bytes\t data"
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
You can’t perform that action at this time.
0 commit comments