encodeutils

oslo_utils.encodeutils.exception_to_unicode(exc: Any) str

Get the message of an exception as a Unicode string.

On Python 3, the exception message is always a Unicode string.

Added in version 1.6.

oslo_utils.encodeutils.safe_decode(text: str | bytes, incoming: str | None = None, errors: str = 'strict') str
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: str | bytes, incoming: str | None = None, encoding: str = 'utf-8', errors: str = 'strict') bytes

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: str | bytes) bytes

Encode Unicode to UTF-8, return bytes unchanged.

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

Added in version 3.5.