Exceptions and unicode messages

Tuomas tuomas.vesterinen at pp.inet.fi
Wed Mar 21 17:27:43 CET 2007


This seems to work:

 >>> import sys, traceback
 >>> def excepthook(exctype, value, tb):
...     if tb:
...         lines = traceback.format_tb(tb)
...         for i, line in zip(range(len(lines)), lines):
...             lines[i] = lines[i].decode('utf8')
...         lines.insert(0, u'Traceback (most recent call last):\n')
...     else:
...         lines = []
...     msg = str(exctype).split('.')[-1] + u': ' + unicode(value)
...     lines.append(msg)
...     print u''.join(lines)
...
 >>> sys.excepthook = excepthook
 >>> class UserError(StandardError):
...     def __str__(self):
...         return self.args[0]
...
 >>> raise UserError(u'Väärä tyyppi')
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
UserError: Väärä tyyppi
 >>>

Tuomas

Tuomas wrote:
> This works:
>  >>> raise StandardError(u'Wrong type')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> StandardError: Wrong type
> 
> but don't in Finnish:
>  >>> raise StandardError(u'Väärä tyyppi')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> StandardError>>>
>  >>>
> 
> Any solution in Python?
> 
> TV



More information about the Python-list mailing list