error messages containing unicode

Jim jhefferon at smcvt.edu
Tue Jan 30 07:34:24 EST 2007


Thank you for the reply.  It happens that, as I understand it, none of 
the options that you mentioned is a solution for my situation.

On Jan 29, 9:48 pm, Steven D'Aprano <s... at REMOVEME.cybersource.com.au> 
wrote:
> The easiest ways to fix that are:
>
> (1) subclass an exception that already knows about Unicode;
But I often raise one of Python's built-in errors.  And also, is it 
really true that subclassing one of Python's built-ins give me 
something that is unicode deficient?  I assumed that I had missed 
something (because that's happened so many times before :-) ).

For instance, I write a lot of CGI and I want to wrap everything in a 
try .. except.
  try:
      main()
  except Exception, err:
      print "Terrible blunder: ",str(err)
so that the err can be one of my exceptions, or can be one that came 
with Python. (And, that I can see, err.args can be either the relevant 
string or a tuple containing the relevant string and the documentation 
is silent on whether in the built-in exceptions if err.args is a tuple 
then the string is guaranteed to be first in the tuple.)

> (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.

> (3) add a __str__ method to your exception that is Unicode aware.
I have two difficulties with this: (1) as above I often raise Python's 
built-in exceptions and for those __str__() is what it is, and (2) 
this goes against the meaning of __str__() that I find in the 
documentation in ref/customization.html which says that the return 
value must be a string object.  Obviously no one will come to my house 
and slap me if I violate that, but I'll venture that it would be odd 
if the best practice were to be to do the opposite of  the 
documentation.

> I'm going to be lazy and do a real simple-minded version of (2):
>
> >>> class MyBetterException(Exception):...     def __init__(self, arg):
> ...             self.args = arg.encode('ascii', 'replace')
> ...             self.unicode_arg = arg  # save the original in case
This is illuminating.  How do you know that for exceptions __init__() 
should take one non-self argument?  I missed finding this information.

Thanks again,
Jim




More information about the Python-list mailing list