def to_bytes(string, encoding="utf-8", errors="ignore"):
    """Force a value to bytes.

    :param string: Some input that can be converted to a bytes.
    :type string: str or bytes unicode or a memoryview subclass
    :param encoding: The encoding to use for conversions, defaults to "utf-8"
    :param encoding: str, optional
    :return: Corresponding byte representation (for use in filesystem operations)
    :rtype: bytes
    """

    if not errors:
        if encoding.lower() == "utf-8":
            errors = "surrogateescape" if six.PY3 else "ignore"
        else:
            errors = "strict"
    if isinstance(string, bytes):
        if encoding.lower() == "utf-8":
            return string
        else:
            return string.decode("utf-8").encode(encoding, errors)
    elif isinstance(string, memoryview):
        return bytes(string)
    elif not isinstance(string, six.string_types):
        try:
            if six.PY3:
                return six.text_type(string).encode(encoding, errors)
            else:
                return bytes(string)
        except UnicodeEncodeError:
            if isinstance(string, Exception):
                return b" ".join(to_bytes(arg, encoding, errors) for arg in string)
            return six.text_type(string).encode(encoding, errors)
    else:
        return string.encode(encoding, errors)
def to_bytes ( string , encoding = "utf-8" , errors = "ignore" ) : if not errors : if encoding . lower ( ) == "utf-8" : errors = "surrogateescape" if six . PY3 else "ignore" else : errors = "strict" if isinstance ( string , bytes ) : if encoding . lower ( ) == "utf-8" : return string else : return string . decode ( "utf-8" ) . encode ( encoding , errors ) elif isinstance ( string , memoryview ) : return bytes ( string ) elif not isinstance ( string , six . string_types ) : try : if six . PY3 : return six . text_type ( string ) . encode ( encoding , errors ) else : return bytes ( string ) except UnicodeEncodeError : if isinstance ( string , Exception ) : return b" " . join ( to_bytes ( arg , encoding , errors ) for arg in string ) return six . text_type ( string ) . encode ( encoding , errors ) else : return string . encode ( encoding , errors )
Force a value to bytes.

    :param string: Some input that can be converted to a bytes.
    :type string: str or bytes unicode or a memoryview subclass
    :param encoding: The encoding to use for conversions, defaults to "utf-8"
    :param encoding: str, optional
    :return: Corresponding byte representation (for use in filesystem operations)
    :rtype: bytes