Unicode strings as arguments to exceptions
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jan 16 09:16:00 EST 2014
On Thu, 16 Jan 2014 13:34:08 +0100, Ernest Adrogué wrote:
> 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.
--
Steven
More information about the Python-list
mailing list