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