docstr: Sends messages that should log to stderr to stream instead. Useful for unittests 
docstr_tok: ["Sends","messages","that","should","log","to","stderr","to","stream","instead",".","Useful","for","unittests"]
code: def patch_stderr_handler(self, stream):
      """Sends messages that should log to stderr to stream instead. Useful for unittests"""
      if self._logger is not None:
          try:
              old_stream = self._handlers["stream_stderr"].stream
              if old_stream is None:
                  raise ValueError
  
              # Port backwards set_stream() from python 3.7
              self._handlers["stream_stderr"].acquire()
              try:
                  self._handlers["stream_stderr"].flush()
                  self._handlers["stream_stderr"].stream = stream
              finally:
                  self._handlers["stream_stderr"].release()
  
              yield stream
          except (KeyError, ValueError):
              raise RuntimeError(
                  "Impossible to patch logging handlers if handler does not exist"
              )
          finally:
              # Port backwards set_stream() from python 3.7
              self._handlers["stream_stderr"].acquire()
              try:
                  self._handlers["stream_stderr"].flush()
                  self._handlers["stream_stderr"].stream = old_stream
              finally:
                  self._handlers["stream_stderr"].release()
  
      else:
          raise RuntimeError(
              "Impossible to patch logging handlers if handler does not exist"
          )
  
imports: import enum
  import logging
  import sys
  import threading
  import warnings
  from contextlib import contextmanager
  from logging.handlers import MemoryHandler
  from nemo.constants import NEMO_ENV_VARNAME_REDIRECT_LOGS_TO_STDERR, NEMO_ENV_VARNAME_TESTING
  from nemo.utils.env_var_parsing import get_envbool
  from nemo.utils.formatters.base import BaseNeMoFormatter, DebugNeMoFormatter
  from nemo.utils.get_rank import is_global_rank_zero
  from nemo.utils.metaclasses import Singleton
label_desc: Use image node_id 12 for calling the Logger obj's underlying member method code with example usage: obj.patch_stderr_handler(stream) without return types