Stringize ``s``, adding double quotes and escaping as necessary

    Backslash escape backslashes, double quotes, ``\f``, ``\n``, ``\r``, and
    ``\t``. Escape all remaining unprintable characters in ``\xFF``-style.
    The returned string will be surrounded by double quotes.

def dump_string(s):
    '''Stringize ``s``, adding double quotes and escaping as necessary

    Backslash escape backslashes, double quotes, ``\f``, ``\n``, ``\r``, and
    ``\t``. Escape all remaining unprintable characters in ``\xFF``-style.
    The returned string will be surrounded by double quotes.
    '''

    s = (s.replace('\\', '\\\\')
          .replace('"', '\\"')
          .replace('\f', r'\f')
          .replace('\n', r'\n')
          .replace('\r', r'\r')
          .replace('\t', r'\t'))
    s = UNPRINTABLE_CHARACTER_RE.sub(
            lambda m: r'\x{:02x}'.format(ord(m.group(0))),
            s)
    return '"' + s + '"'