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

Johannes Erdfelt johannes at erdfelt.com
Sat May 17 23:03:49 UTC 2014


On Fri, May 16, 2014, Igor Kalnitsky <ikalnitsky at mirantis.com> wrote:
> > unicode(exc) (or six.text_type(exc)) works for all exceptions, built-in or
> custom.
> 
> That's too much of a statement. Sometimes exceptions implement their own
> __str__ / __unitcode__
> methods, that return too many rubbish information or not enough. What do
> you do in that case?

I don't understand the problem.

What are you expecting from unicode(exc)? What exceptions don't meet
that expectation?

Using str(exc) or unicode(exc) is the standard Python convention to get
a useful string out of an exception. This is what the traceback module
does (at least in Python 2.7) for the last line in the traceback output.

It tries str first, then unicode if str fails. If it uses unicode, then
it backslash escapes it back to ASCII.

The behavior to try str first and then unicode second appears to be
because of legacy reasons.

I'm not entirely sure why it backslash escapes unicode back to ASCII.
Maybe to avoid a possible second exception when printing it?

JE




More information about the OpenStack-dev mailing list