Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64077 > unrolled thread
| Started by | Ernest Adrogué <nfdisco@gmail.com> |
|---|---|
| First post | 2014-01-16 13:34 +0100 |
| Last post | 2014-01-16 18:48 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | Ernest Adrogué <nfdisco@gmail.com> |
|---|---|
| Date | 2014-01-16 13:34 +0100 |
| Subject | Unicode strings as arguments to exceptions |
| Message-ID | <mailman.5586.1389876759.18130.python-list@python.org> |
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
>>> raise KeyError('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
>>> raise KeyError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: u'\xe4'
On the other hand ValueError doesn't print anything.
>>> raise ValueError('a')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: a
>>> raise ValueError(u'ä')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError
I'm using Python 2.7.6 on a Unix machine.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2014-01-16 14:16 +0000 |
| Message-ID | <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #64077 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-01-16 09:32 -0500 |
| Message-ID | <roy-19E588.09321716012014@news.panix.com> |
| In reply to | #64081 |
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 :-)
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2014-01-16 18:48 -0500 |
| Message-ID | <mailman.5611.1389916099.18130.python-list@python.org> |
| In reply to | #64081 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web