[openstack-dev] [oslo] Logging exceptions and Python 3
Victor Stinner
victor.stinner at enovance.com
Fri May 16 14:04:15 UTC 2014
Le vendredi 16 mai 2014, 06:03:53 Johannes Erdfelt a écrit :
> On Fri, May 16, 2014, Igor Kalnitsky <ikalnitsky at mirantis.com> wrote:
> > > According to http://legacy.python.org/dev/peps/pep-0352/ the message
> > > attribute of BaseException is deprecated since Python 2.6 and was
> > > dropped in Python 3.0.
> >
> > Some projects have custom exception hierarchy, with strictly defined
> > attributes (e.g. message, or something else). In a previous mail, I
> > mean exactly that case, not the case with a built-in exceptions.
>
> That's a fragile assumption to make.
>
> unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in
> or custom. I don't see the reason why it's being avoided.
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.
Victor
More information about the OpenStack-dev
mailing list