code: def _force_correct_text_stream(
      text_stream: t.IO[t.Any],
      encoding: t.Optional[str],
      errors: t.Optional[str],
      is_binary: t.Callable[[t.IO[t.Any], bool], bool],
      find_binary: t.Callable[[t.IO[t.Any]], t.Optional[t.BinaryIO]],
      force_readable: bool = False,
      force_writable: bool = False,
  ) -> t.TextIO:
      if is_binary(text_stream, False):
          binary_reader = t.cast(t.BinaryIO, text_stream)
      else:
          text_stream = t.cast(t.TextIO, text_stream)
          # If the stream looks compatible, and won't default to a
          # misconfigured ascii encoding, return it as-is.
          if _is_compatible_text_stream(
              text_stream, encoding, errors
          ) and not (
              encoding is None and _stream_is_misconfigured(text_stream)
          ):
              return text_stream
  
          # Otherwise, get the underlying binary reader.
          possible_binary_reader = find_binary(text_stream)
  
          # If that's not possible, silently use the original reader
          # and get mojibake instead of exceptions.
          if possible_binary_reader is None:
              return text_stream
  
          binary_reader = possible_binary_reader
  
      # Default errors to replace instead of strict in order to get
      # something that works.
      if errors is None:
          errors = "replace"
  
      # Wrap the binary stream in a text stream with the correct
      # encoding parameters.
      return _make_text_stream(
          binary_reader,
          encoding,
          errors,
          force_readable=force_readable,
          force_writable=force_writable,
      )
  
imports: import codecs
  import io
  import os
  import re
  import sys
  import typing
  from weakref import WeakKeyDictionary
label_desc: Use image node_id 11 for calling a global function with example usage: _force_correct_text_stream(text_stream, encoding, errors, is_binary, find_binary, force_readable, force_writable) and returns: _make_text_stream, text_stream, text_stream