import six


def _EscapePattern(pattern):
  """Escapes special regex characters and double quotes in the pattern.

  This is basically identical to Python 3.7's implementation of re.escape(),
  except that it also includes double quotes in the set of characters that need
  escaping (needed for proper filter rewriting behavior).

  Args:
    pattern: A regex pattern.

  Returns:
    The pattern with double quotes and special regex characters escaped.
  """
  special_chars_map = {
      i: '\\' + chr(i)
      for i in six.iterbytes(b'"()[]{}?*+-|^$\\.&~# \t\n\r\v\f')}
  return pattern.translate(special_chars_map)