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

Discussion in 'Python' started by , Feb 26, 2009.

  1. Guest

    #------------------------------------------------
    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))
    , Feb 26, 2009
    #1
    1. Advertising

  2. Terry Reedy Guest

    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, Feb 26, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pekka Niiranen
    Replies:
    1
    Views:
    353
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Oct 21, 2004
  2. Ben Wolfson
    Replies:
    0
    Views:
    391
    Ben Wolfson
    Jan 24, 2007
  3. Ben Wolfson
    Replies:
    0
    Views:
    285
    Ben Wolfson
    Jan 24, 2007
  4. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  5. Replies:
    1
    Views:
    494
    Sion Arrowsmith
    Jul 10, 2008
Loading...

Share This Page