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).
Added 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:
incoming – Text’s current encoding
errors – Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
- 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:
incoming – Text’s current encoding
encoding – Expected encoding for text (Default UTF-8)
errors – Errors handling policy. See here for valid values http://docs.python.org/2/library/codecs.html
- 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.
Added in version 3.5.