encodeutils

oslo_utils.encodeutils.exception_to_unicode(exc)

Get the message of an exception as a Unicode string.

On Python 3, the exception message is always a Unicode string. On Python 2, the exception message is a bytes string most of the time.

If the exception message is a bytes strings, try to decode it from UTF-8 (superset of ASCII), from the locale encoding, or fallback to decoding it from ISO-8859-1 (which never fails).

New in version 1.6.

oslo_utils.encodeutils.safe_decode(text, incoming=None, errors='strict')
Decodes incoming text/bytes string using incoming if they’re not

already unicode.

Parameters:
Returns:

text or a unicode incoming encoded representation of it.

Raises:

TypeError – If text is not an instance of str

oslo_utils.encodeutils.safe_encode(text, incoming=None, encoding='utf-8', errors='strict')

Encodes incoming text/bytes string using encoding.

If incoming is not specified, text is expected to be encoded with current python’s default encoding. (sys.getdefaultencoding)

Parameters:
Returns:

text or a bytestring encoding encoded representation of it.

Raises:

TypeError – If text is not an instance of str

See also to_utf8() function which is simpler and don’t depend on the locale encoding.

oslo_utils.encodeutils.to_utf8(text)

Encode Unicode to UTF-8, return bytes unchanged.

Raise TypeError if text is not a bytes string or a Unicode string.

New in version 3.5.