[openstack-dev] [oslo] Logging exceptions and Python 3

Johannes Erdfelt johannes at erdfelt.com
Sat May 17 22:28:46 UTC 2014


On Fri, May 16, 2014, Victor Stinner <victor.stinner at enovance.com> wrote:
> See my documentation:
> https://wiki.openstack.org/wiki/Python3#logging_module_and_format_exceptions
> 
> " six.text_type(exc): always use Unicode. It may raise unicode error depending 
> on the exception, be careful. Example of such error in python 2: 
> unicode(Exception("nonascii:\xe9")). "
> 
> unicode(exc) works with such exception classes:
> ---
> class MyException1(Exception):
>     pass
> 
> exc = MyException1()
> exc.message = u"\u20ac"
> unicode(exc) #ok
> 
> class MyException2(Exception):
>     def __unicode__(self):
>         return u"\20ac"
> 
> exc = MyException2()
> unicode(exc) #ok
> ---
> 
> If we want to format an exception as Unicode, we need a function trying 
> unicode(), or use str() and then guess the encoding. It means adding a new 
> safe function to Olso to format an exception.

This is unnecessarily complicated.

Strings should be decoded to unicode as soon as possible. When a request
is read from a user, it should be decoded to a unicode type. When it's
read from a file, it should be decoded to a unicode type.

Nothing should be stored internally as an encoded string.

If a third party library raises exceptions with strings in an encoding
other than ASCII, they should be shot :)

JE




More information about the OpenStack-dev mailing list