def _windows_shell_quote(s):
    """Return a Windows shell-escaped version of the string *s*

    Windows has potentially bizarre rules depending on where you look. When
    spawning a process via the Windows C runtime the rules are as follows:

    https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments

    To summarize the relevant bits:

    * Only space and tab are valid delimiters
    * Double quotes are the only valid quotes
    * Backslash is interpreted literally unless it is part of a chain that
      leads up to a double quote. Then the backslashes escape the backslashes,
      and if there is an odd number the final backslash escapes the quote.

    :param s: A string to escape
    :return: An escaped string
    """
    if not s:
        return '""'
    buff = []
    num_backspaces = 0
    for character in s:
        if character == '\\':
            num_backspaces += 1
        elif character == '"':
            if num_backspaces > 0:
                buff.append('\\' * (num_backspaces * 2))
                num_backspaces = 0
            buff.append('\\"')
        else:
            if num_backspaces > 0:
                buff.append('\\' * num_backspaces)
                num_backspaces = 0
            buff.append(character)
    if num_backspaces > 0:
        buff.append('\\' * num_backspaces)
    new_s = ''.join(buff)
    if ' ' in new_s or '\t' in new_s:
        return '"%s"' % new_s
    return new_s
