Simple unicode-safe version of str(exception)?

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue Apr 29 19:21:45 CEST 2008


Russell E. Owen <rowen at cesmail.net> wrote:
>No. e.message is only set if the exeption object receives exactly one 
>argument.

And not always then:

>>> e1 = Exception(u"\u00fe")
>>> e1.message
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: Exception instance has no attribute 'message'

>That is why my replacement code reads:
>    errStr = ",".join([unicode(s) for s in f.args])

errStr = ",".join(e.args)

There is something distinctly odd going on here, though:

>>> str(e1)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfe' in position 0: ordinal not in range(128)
>>> e2 = Exception(u"\u00fe", "foo")
>>> str(e2)
"(u'\\xfe', 'foo')"
>>> 

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
        -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list