code: def _gettextwriter(out, encoding):
      if out is None:
          import sys
  
          return sys.stdout
  
      if isinstance(out, io.TextIOBase):
          # use a text writer as is
          return out
  
      if isinstance(
          out, (codecs.StreamWriter, codecs.StreamReaderWriter)
      ):
          # use a codecs stream writer as is
          return out
  
      # wrap a binary writer with TextIOWrapper
      if isinstance(out, io.RawIOBase):
          # Keep the original file open when the TextIOWrapper is
          # destroyed
          class _wrapper:
              __class__ = out.__class__
  
              def __getattr__(self, name):
                  return getattr(out, name)
  
          buffer = _wrapper()
          buffer.close = lambda: None
      else:
          # This is to handle passed objects that aren't in the
          # IOBase hierarchy, but just have a write method
          buffer = io.BufferedIOBase()
          buffer.writable = lambda: True
          buffer.write = out.write
          try:
              # TextIOWrapper uses this methods to determine
              # if BOM (for UTF-16, etc) should be added
              buffer.seekable = out.seekable
              buffer.tell = out.tell
          except AttributeError:
              pass
      return io.TextIOWrapper(
          buffer,
          encoding=encoding,
          errors="xmlcharrefreplace",
          newline="\n",
          write_through=True,
      )
  
imports: import os, urllib.parse, urllib.request
  import io
  import codecs
  from .None import handler
  from .None import xmlreader
label_desc: Use image node_id 5 for calling a global function with example usage: _gettextwriter(out, encoding) and returns: io, sys, out, out, getattr