Unicode strings as arguments to exceptions

E

Ernest Adrogué

Hi,

There seems to be some inconsistency in the way exceptions handle Unicode
strings. For instance, KeyError seems to not have a problem with them
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: u'\xe4'

On the other hand ValueError doesn't print anything.
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError

I'm using Python 2.7.6 on a Unix machine.
 
S

Steven D'Aprano

Hi,

There seems to be some inconsistency in the way exceptions handle
Unicode strings.

Yes. I believe the problem lies in the __str__ method. For example,
KeyError manages to handle Unicode, although in an ugly way:

py> str(KeyError(u'ä'))
"u'\\xe4'"

Hence:

py> raise KeyError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: u'\xe4'


While ValueError assumes ASCII and fails:

py> str(ValueError(u'ä'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)

When displaying the traceback, the error is suppressed, hence:

py> raise ValueError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError


I believe this might be accepted as a bug report on ValueError.
 
R

Roy Smith

Steven D'Aprano said:
Yes. I believe the problem lies in the __str__ method. For example,
KeyError manages to handle Unicode, although in an ugly way:

py> str(KeyError(u'ä'))
"u'\\xe4'"

Hence:

py> raise KeyError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: u'\xe4'


While ValueError assumes ASCII and fails:

py> str(ValueError(u'ä'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)

When displaying the traceback, the error is suppressed, hence:

py> raise ValueError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError


I believe this might be accepted as a bug report on ValueError.

If you try to construct an instance of ValueError with an argument it
can't handle, the obvious thing for it to do is raise ValueError :)
 
T

Terry Reedy

Yes. I believe the problem lies in the __str__ method. For example,
KeyError manages to handle Unicode, although in an ugly way:

py> str(KeyError(u'ä'))
"u'\\xe4'"

Hence:

py> raise KeyError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: u'\xe4'


While ValueError assumes ASCII and fails:

py> str(ValueError(u'ä'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in
position 0: ordinal not in range(128)

When displaying the traceback, the error is suppressed, hence:

py> raise ValueError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError

I believe this might be accepted as a bug report on ValueError.

Or a change might be rejected as a feature change or as a bugfix that
might break existing code. We do change exception messages in new
versions but do not normally do so in bugfix releases.

http://bugs.python.org/issue1012952 is related but different. The issue
there was that unicode(ValueError(u'ä')) gave the same
UnicodeEncodeError as str(ValueError(u'ä')). That was fixed by giving
exceptions a __unicode__ method, but that did not fix the traceback
display issue above.

http://bugs.python.org/issue6108
unicode(exception) and str(exception) should return the same message
also seems related. The issue was raised what str should do if the
unicode message had non-ascii chars. I did not read enough to find an
answer. The same question would arise here.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top