error messages containing unicode

Jim jhefferon at smcvt.edu
Tue Jan 30 08:07:39 EST 2007


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.

Jim




More information about the Python-list mailing list