import warnings


def _greater_warnings_context(context_lines_string):
  """Provide the `line` argument to warnings.showwarning().

  warnings.warn_explicit() doesn't use the `line` argument to showwarning(), but we want to
  make use of the warning filtering provided by warn_explicit(). This contextmanager overwrites the
  showwarning() method to pipe in the desired amount of context lines when using warn_explicit().
  """
  prev_showwarning = warnings.showwarning
  def wrapped(message, category, filename, lineno, file=None, line=None):
    return prev_showwarning(
      message=message,
      category=category,
      filename=filename,
      lineno=lineno,
      file=file,
      line=(line or context_lines_string))
  warnings.showwarning = wrapped
  yield
  warnings.showwarning = prev_showwarning