def ensure_binary(s, encoding="utf-8", errors="strict"):
    """
    Coerce ``s`` to bytes.

    * `str` -> encoded to `bytes`
    * `bytes` -> `bytes`

    :param s: the string to convert
    :type s: str | bytes
    :param encoding: the encoding to apply, defaults to "utf-8"
    :type encoding: str
    :param errors: set a different error handling scheme;
      other possible values are `ignore`, `replace`, and
      `xmlcharrefreplace` as well as any other name
      registered with :func:`codecs.register_error`.
      Defaults to "strict".
    :type errors: str
    :raises TypeError: if ``s`` is not str or bytes type
    :return: the converted string
    :rtype: str
    """
    if isinstance(s, str):
        return s.encode(encoding, errors)
    elif isinstance(s, bytes):
        return s
    else:
        raise TypeError("not expecting type '%s'" % type(s))