code: def warning_record_to_str(warning_message):
      """Convert a warnings.WarningMessage to a string.
  
      This takes lot of unicode shenaningans into account for Python 2.
      When Python 2 support is dropped this function can be greatly simplified.
      """
      warn_msg = warning_message.message
      unicode_warning = False
      if compat._PY2 and any(
          isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args
      ):
          new_args = []
          for m in warn_msg.args:
              new_args.append(
                  compat.ascii_escaped(m)
                  if isinstance(m, compat.UNICODE_TYPES)
                  else m
              )
          unicode_warning = list(warn_msg.args) != new_args
          warn_msg.args = new_args
  
      msg = warnings.formatwarning(
          warn_msg,
          warning_message.category,
          warning_message.filename,
          warning_message.lineno,
          warning_message.line,
      )
      if unicode_warning:
          warnings.warn(
              "Warning is using unicode non convertible to ascii, "
              "converting to a safe representation:\n  {!r}".format(
                  compat.safe_str(msg)
              ),
              UnicodeWarning,
          )
      return msg
  
file_path: catboost/contrib/python/pytest/py2/_pytest/warnings.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import sys
  import warnings
  from contextlib import contextmanager
  import pytest
  from _pytest import compat
label_desc: Use image node_id 5 for calling a global function with example usage: warning_record_to_str(warning_message) and returns: msg