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


Groups > comp.lang.python > #64083

Re: Unicode strings as arguments to exceptions

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Unicode strings as arguments to exceptions
Date 2014-01-16 09:32 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-19E588.09321716012014@news.panix.com> (permalink)
References <mailman.5586.1389876759.18130.python-list@python.org> <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


In article <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com>,
 Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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.

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 :-)

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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