Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is this the right way to use unicode in a user defined Exception?

Reply
Thread Tools

Is this the right way to use unicode in a user defined Exception?

 
 
Guest
Posts: n/a
 
      02-26-2009
#------------------------------------------------
class MyError(Exception):
def __init__(self):
self.message = u'Some Chinese:中文'

def __str__(self):
return self.message.encode('utf8')
#------------------------------------------------

This is an exception that I defined. I have to pass it to third
party libraries.

As many libraries simply use str(e) to log, if I don't encode it in
__str___, they will fail.

But I am not quite certain if it's the right thing to do. Shouldn't
every library expect to use unicode everywhere?

Shouldn't they use something like :

log(unicode(e))
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      02-26-2009
wrote:
> #------------------------------------------------
> class MyError(Exception):
> def __init__(self):
> self.message = u'Some Chinese:*'
>
> def __str__(self):
> return self.message.encode('utf8')
> #------------------------------------------------
>
> This is an exception that I defined. I have to pass it to third
> party libraries.
>
> As many libraries simply use str(e) to log, if I don't encode it in
> __str___, they will fail.
>
> But I am not quite certain if it's the right thing to do. Shouldn't
> every library expect to use unicode everywhere?
>
> Shouldn't they use something like :
>
> log(unicode(e))


In 3.0, text is unicode. So libraries will mostly expect it.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User-defined exception: "global name 'TestRunError' is not defined" jmike@alum.mit.edu Python 1 07-10-2008 12:37 PM
sqlite user-defined functions & unicode issue Ben Wolfson Python 0 01-24-2007 05:24 AM
sqlite user-defined functions & unicode issue Ben Wolfson Python 0 01-24-2007 05:23 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM
Help needed: Printing unicode characters in user defined format Pekka Niiranen Python 1 10-21-2004 06:57 PM



Advertisments