__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.

    >>> e = RuntimeError(u'àèìòù')
    >>> str(e)

    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)
    >>> unicode(e)

    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. Advertising

  2. Manlio Perillo

    Ben Finney Guest

    Manlio Perillo <> writes:

    > I have just noticed that exception objects does not handle well
    > Unicode arguments.


    This error is unrelated to the fact that you created an exception
    object.

    > >>> e = RuntimeError(u'àèìòù')
    > >>> str(e)

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    > UnicodeEncodeError: 'ascii' codec can't encode characters in position


    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.

    --
    \ "The World is not dangerous because of those who do harm but |
    `\ because of those who look at it without doing anything." -- |
    _o__) Albert Einstein |
    Ben Finney
     
    Ben Finney, Jul 8, 2007
    #2
    1. Advertising

  3. Ben Finney <> wrote:
    > 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.


    AFAIK that's not a terminal problem. I have a UTF-8 terminal and the
    problem is still there.

    --
    Lawrence, oluyede.org - neropercaso.it
    "It is difficult to get a man to understand
    something when his salary depends on not
    understanding it" - Upton Sinclair
     
    Lawrence Oluyede, Jul 8, 2007
    #3
  4. Il Sun, 08 Jul 2007 10:02:01 +1000, Ben Finney ha scritto:

    > Manlio Perillo <> writes:
    >
    >> I have just noticed that exception objects does not handle well Unicode
    >> arguments.

    >
    > This error is unrelated to the fact that you created an exception
    > object.
    >


    No, it is related.

    >> >>> e = RuntimeError(u'àèìòù')
    >> >>> str(e)

    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in ?
    >> UnicodeEncodeError: 'ascii' codec can't encode characters in position

    >
    > 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.



    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. 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. Marcin 'Qrczak' Kowalczyk

    The __unicode__ method in the C API, and other __-methods

    Marcin 'Qrczak' Kowalczyk, Jun 9, 2004, in forum: Python
    Replies:
    2
    Views:
    333
    Marcin 'Qrczak' Kowalczyk
    Jun 11, 2004
  2. Bengt Richter
    Replies:
    0
    Views:
    364
    Bengt Richter
    Jul 7, 2005
  3. Replies:
    2
    Views:
    335
  4. Elf M. Sternberg
    Replies:
    15
    Views:
    271
    Matthias Reitinger
    Jul 29, 2009
  5. Roy Smith
    Replies:
    3
    Views:
    190
    Terry Reedy
    Nov 4, 2012
Loading...

Share This Page