Ejemplo n.º 1
0
 def test_valid(self):
     assert_false(is_unicode(random_bytes))
     assert_true(is_unicode(unicode_string))
     assert_false(is_unicode(False))
     assert_false(is_unicode(5))
     assert_false(is_unicode(None))
     assert_false(is_unicode([]))
     assert_false(is_unicode(()))
     assert_false(is_unicode({}))
     assert_false(is_unicode(object))
Ejemplo n.º 2
0
def to_utf8_if_unicode(value):
    """
    Converts an argument to a UTF-8 encoded byte string if the argument
    is a Unicode string.

    :param value:
        The value that will be UTF-8 encoded if it is a string.
    :returns:
        UTF-8 encoded byte string if the argument is a Unicode string; otherwise
        the value is returned unchanged.
    """
    return unicode_to_utf8(value) if is_unicode(value) else value
Ejemplo n.º 3
0
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")
Ejemplo n.º 4
0
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)