Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

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

 
Thread Tools Search this Thread
Old 02-26-2009, 02:14 AM   #1
Default Is this the right way to use unicode in a user defined Exception?


#------------------------------------------------
class MyError(Exception):
def __init__(self):
self.message = u'Some Chinese:$BCfJ8(B'

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
Old 02-26-2009, 08:12 AM   #2
Terry Reedy
 
Posts: n/a
Default Re: Is this the right way to use unicode in a user defined Exception?
一首诗 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.



Terry Reedy
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating user defined service in windows suresh_rtp Software 0 05-05-2009 08:34 AM
java.net.SocketException: Broken pipe exception at HttpServletRequest sadasivar Software 0 03-11-2008 04:40 AM
exception unknown software exception error - Excel.exe mfinamore Software 0 11-15-2007 04:54 PM
Exception in thread "main" java.lang.NoClassDefFoundError: prakash.nmsp@gmail.com Software 0 06-29-2007 10:03 AM
how to overcome the nullpointer exception arputharaj Software 0 11-11-2006 07:05 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46