code: def _write_with_fallback(s, write, fileobj):
      
  
      if IPythonIOStream is not None and isinstance(fileobj, IPythonIOStream):
          # If the output stream is an IPython.utils.io.IOStream object that's
          # not going to be very helpful to us since it doesn't raise any
          # exceptions when an error occurs writing to its underlying stream.
          # There's no advantage to us using IOStream.write directly though;
          # instead just write directly to its underlying stream:
          write = fileobj.stream.write
  
      try:
          write(s)
          return write
      except UnicodeEncodeError:
          # Let's try the next approach...
          pass
  
      enc = locale.getpreferredencoding()
      try:
          Writer = codecs.getwriter(enc)
      except LookupError:
          Writer = codecs.getwriter(_DEFAULT_ENCODING)
  
      f = Writer(fileobj)
      write = f.write
  
      try:
          write(s)
          return write
      except UnicodeEncodeError:
          Writer = codecs.getwriter('latin-1')
          f = Writer(fileobj)
          write = f.write
  
      # If this doesn't work let the exception bubble up; I'm out of ideas
      write(s)
      return write
docs: Write the supplied string with the given write function like
      ``write(s)``, but use a writer for the locale's preferred encoding in case
      of a UnicodeEncodeError.  Failing that attempt to write with 'utf-8' or
      'latin-1'.