Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64115

Re: Unicode strings as arguments to exceptions

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Unicode strings as arguments to exceptions
Date 2014-01-16 18:48 -0500
References <mailman.5586.1389876759.18130.python-list@python.org> <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.5611.1389916099.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 1/16/2014 9:16 AM, Steven D'Aprano wrote:
> 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.

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.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Unicode strings as arguments to exceptions Ernest Adrogué <nfdisco@gmail.com> - 2014-01-16 13:34 +0100
  Re: Unicode strings as arguments to exceptions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-01-16 14:16 +0000
    Re: Unicode strings as arguments to exceptions Roy Smith <roy@panix.com> - 2014-01-16 09:32 -0500
    Re: Unicode strings as arguments to exceptions Terry Reedy <tjreedy@udel.edu> - 2014-01-16 18:48 -0500

csiph-web