Expand description
python literal encoders.
encodes untrusted strings for safe embedding in python source literals.
for_python_string— safe for python string literals ("..."or'...')for_python_bytes— safe for python bytes literals (b"..."orb'...')for_python_raw_string— safe for python raw string literals (r"..."orr'...')
§encoding rules
§string and bytes
both encoders use python’s native escape syntax:
- named escapes:
\a,\b,\t,\n,\v,\f,\r,\\,\",\' - other C0 controls and DEL →
\xHH - unicode non-characters → space (string) or
\xHHper byte (bytes)
both quote characters are escaped, making the output safe regardless of
which delimiter (" or ') is used.
the encoders differ in how non-ASCII is handled:
| encoder | non-ASCII |
|---|---|
for_python_string | passes through |
for_python_bytes | each UTF-8 byte → \xHH |
§raw string
raw strings do not process escape sequences, so the encoder replaces dangerous characters with space:
- quotes (
"and') → space - C0 controls and DEL → space
- unicode non-characters → space
- trailing odd backslash → replaced with space (raw strings cannot end with an odd number of backslashes)
Functions§
- for_
python_ bytes - encodes
inputfor safe embedding in a python bytes literal (b"..."orb'...'). - for_
python_ raw_ string - encodes
inputfor safe embedding in a python raw string literal (r"..."orr'...'). - for_
python_ string - encodes
inputfor safe embedding in a python string literal ("..."or'...'). - write_
python_ bytes - writes the python-bytes-encoded form of
inputtoout. - write_
python_ raw_ string - writes the python-raw-string-encoded form of
inputtoout. - write_
python_ string - writes the python-string-encoded form of
inputtoout.