__unicode__ method for exception object

Discussion in 'Python' started by Manlio Perillo, Jul 7, 2007.

  1. Hi all.

    I have just noticed that exception objects does not handle well Unicode
    arguments.
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    UnicodeEncodeError: 'ascii' codec can't encode characters in position
    0-4: ordinal not in range(128)Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    UnicodeEncodeError: 'ascii' codec can't encode characters in position
    0-4: ordinal not in range(128)


    C object struct does not have a tp_unicode slot (maybe it will be added
    in Python 3000?), however I think that the Base Exception class should
    implement the __unicode__ method.

    Any problems in doing so?
    I have not yet required this feature in the Python tracker.


    Thanks Manlio Perillo
     
    Manlio Perillo, Jul 7, 2007
    #1
    1. Advertisements

  2. Manlio Perillo

    Ben Finney Guest

    This error is unrelated to the fact that you created an exception
    object.
    Your terminal has been detected as using the 'ascii' encoding, so
    while that's true no attempt to output non-ASCII characters will work.

    You'll need to change whatever settings are on your terminal emulator
    so that it is using an encoding (such as 'utf-8') which can display
    the characters you want.
     
    Ben Finney, Jul 8, 2007
    #2
    1. Advertisements

  3. AFAIK that's not a terminal problem. I have a UTF-8 terminal and the
    problem is still there.
     
    Lawrence Oluyede, Jul 8, 2007
    #3
  4. Il Sun, 08 Jul 2007 10:02:01 +1000, Ben Finney ha scritto:
    No, it is related.

    This is not a problem with the terminal.
    And the problem is not with str(e) but with unicode(e).

    unicode(e) converts the exception argument to an Unicode object, but
    since no __unicode__ object is defined, it firsts calls the __str__
    method (and this, of course, fails, since the default encoding in CPython
    is us-ascii) and then converts the result to an Unicode object using,
    again, the default encoding.



    Regards Manlio Perillo
     
    Manlio Perillo, Jul 8, 2007
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.