import six


def ensure_bytes(str_or_bytes, encoding='utf-8', errors='strict'):
    """Ensures an input is bytes, encoding if it is a string.
    """
    if isinstance(str_or_bytes, six.text_type):
        return str_or_bytes.encode(encoding, errors)
    return str_or_bytes