def dequote(s):
    """ Convert s as a possibly quoted string to an unquoted string.

    If s is quoted, strip the quotes and unescape internal quotes..
    """

    # Misc.log("DEQUOTE", "IN q=%r= s=%r=" % (q, s))

    if s is None:
        return None

    assert isinstance(s, str)

    if len(s) >= 2:
        c0 = s[0]
        if c0 not in ('"', "'") or s[-1] != c0:
            return s

        # OK, we have a quoted string. Now build a new string and strip quotes.
        #
        if c0 == '"':
            findQuote = '\\"'
            replaceQuote = '"'
        else:
            findQuote = "\\'"
            replaceQuote = "'"

        sNoQuotes = s[1:-1].replace(findQuote, replaceQuote)

        # Then strip escapes.
        #
        sNoQuotes = sNoQuotes.replace('\\\\', '\\')

        # Misc.log("DEQUOTE", "OUT q=%r= s=%r=" % (q, sNoQuotes))

        return sNoQuotes

    return s