def _escape(s):
    """
    Return a string like python repr, but always escaped as if surrounding
    quotes were double quotes.

    @param s: The string to escape.
    @type s: L{bytes} or L{unicode}

    @return: An escaped string.
    @rtype: L{unicode}
    """
    if not isinstance(s, bytes):
        s = s.encode("ascii")

    r = repr(s)
    if not isinstance(r, unicode):
        r = r.decode("ascii")
    if r.startswith(u"b"):
        r = r[1:]
    if r.startswith(u"'"):
        return r[1:-1].replace(u'"', u'\\"').replace(u"\\'", u"'")
    return r[1:-1]

    Return a string like python repr, but always escaped as if surrounding
    quotes were double quotes.

    @param s: The string to escape.
    @type s: L{bytes} or L{unicode}

    @return: An escaped string.
    @rtype: L{unicode}
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/web/http.py