__unicode__ method for exception object

M

Manlio Perillo

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
 
B

Ben Finney

Manlio Perillo said:
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.
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.
 
L

Lawrence Oluyede

Ben Finney said:
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.
 
M

Manlio Perillo

Il Sun, 08 Jul 2007 10:02:01 +1000, Ben Finney ha scritto:
This error is unrelated to the fact that you created an exception
object.

No, it is related.
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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top