code: def ensure_str(s, encoding='utf-8', errors='strict'):
      
      if not isinstance(s, (text_type, binary_type)):
          raise TypeError("not expecting type '%s'" % type(s))
      if PY2 and isinstance(s, text_type):
          s = s.encode(encoding, errors)
      elif PY3 and isinstance(s, binary_type):
          s = s.decode(encoding, errors)
      return s
docs: Coerce *s* to `str`.
  
      For Python 2:
        - `unicode` -> encoded to `str`
        - `str` -> `str`
  
      For Python 3:
        - `str` -> `str`
        - `bytes` -> decoded to `str`