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))
 
T

Terry Reedy

一首诗 said:
#------------------------------------------------
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top