Skip to main content

Module python

Module python 

Source
Expand description

python literal encoders.

encodes untrusted strings for safe embedding in python source literals.

§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 \xHH per 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:

encodernon-ASCII
for_python_stringpasses through
for_python_byteseach 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 input for safe embedding in a python bytes literal (b"..." or b'...').
for_python_raw_string
encodes input for safe embedding in a python raw string literal (r"..." or r'...').
for_python_string
encodes input for safe embedding in a python string literal ("..." or '...').
write_python_bytes
writes the python-bytes-encoded form of input to out.
write_python_raw_string
writes the python-raw-string-encoded form of input to out.
write_python_string
writes the python-string-encoded form of input to out.