def test_valid(self): assert_true(is_bytes(random_bytes)) assert_false(is_bytes(unicode_string)) assert_false(is_bytes(False)) assert_false(is_bytes(5)) assert_false(is_bytes(None)) assert_false(is_bytes([])) assert_false(is_bytes(())) assert_false(is_bytes([])) assert_false(is_bytes(object))
def unicode_to_utf8(value): """ Converts a string argument to a UTF-8 encoded byte string if it is a Unicode string. :param value: If already a byte string or None, it is returned unchanged. Otherwise it must be a Unicode string and is encoded as UTF-8. """ if value is None or is_bytes(value): return value assert is_unicode(value) return value.encode("utf-8")
def to_unicode_if_bytes(value, encoding="utf-8"): """ Converts an argument to Unicode string if the argument is a byte string decoding it as specified by the encoding. :param value: The value that will be converted to a Unicode string. :param encoding: The encoding used to decode bytes. Defaults to UTF-8. :returns: Unicode string if the argument is a byte string. Otherwise the value is returned unchanged. """ return bytes_to_unicode(value, encoding) if is_bytes(value) else value
def bytes_to_unicode(value, encoding="utf-8"): """ Converts bytes to a Unicode string decoding it according to the encoding specified. :param value: If already a Unicode string or None, it is returned unchanged. Otherwise it must be a byte string. :param encoding: The encoding used to decode bytes. Defaults to UTF-8 """ if value is None or is_unicode(value): return value assert is_bytes(value) return value.decode(encoding)