code: def ensure_unicode_string(obj):
      
      try:
          return unicode_type(obj)
      except UnicodeDecodeError:
          if hasattr(obj, 'decode'):
              return obj.decode('utf-8', 'replace')
          return str(obj).decode('utf-8', 'replace')
docs: Return a unicode string representation of the given obj.
  
      :param obj:
          The obj we want to represent in unicode
      :type obj:
          varies
      :rtype:
          `unicode`