error messages containing unicode

Peter Otten __peter__ at web.de
Tue Jan 30 08:18:58 EST 2007


Jim wrote:

> On Jan 30, 7:41 am, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
>> >> (2) convert the file name to ASCII before you store it; or
>> > I need the non-ascii information, though, which is why I included it
>> > in the error message.
>> Then convert it to utf-8, or some encoding you know it will be used by
>> your terminal.
> Thank you for the suggestion.   Remember please that I am asking for a
> safe way to pull the unicode object from the exception object (derived
> from a Python built-in), so I can't store it as unicode first and then
> convert to regular string  when I need to print it out-- my exact
> question is how to get the unicode.  So I take your answer to be to
> refuse to put in a unicode-not-ascii in there in the first place.
> 
> It then seems to me that you are saying that the best practice is that
> every function definition should contain a parameter, like so.
> 
>   def openNewFile(fn,errorEncoding='utf-8'):
>        :
>       try:
>            open(fn,'r')
>       except Exception, err
>            raise myException 'unable to open
> '+fn.encode(errorEncoding,'replace')
> 
> I guess that beyond that passing those parameters and putting encode
> on every variable in my routines that occurs in an error message it is
> ugly, it seems to me that it violates the principle that you should do
> everything inside the program in unicode and only encode at the
> instant you need the output, in that the exception object is carrying
> around an ascii-not-unicode object.

Printing to a terminal should work:

>>> try:
...     raise Exception(u"gewöhnlich ähnlich üblich")
... except Exception, e:
...     print e.message
...
gewöhnlich ähnlich üblich

If you're writing to a file you still have to encode explicitly.

Peter



More information about the Python-list mailing list