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


Groups > comp.lang.python > #64115

Re: Unicode strings as arguments to exceptions

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'method.': 0.07; 'ugly': 0.07; 'ascii': 0.09; 'encode': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rejected': 0.09; 'strings.': 0.09; 'way:': 0.09; 'bug': 0.12; 'jan': 0.12; 'codec': 0.16; 'displaying': 0.16; 'keyerror': 0.16; 'non-ascii': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:Unicode': 0.16; 'suppressed,': 0.16; 'unicode,': 0.16; 'valueerror': 0.16; 'exception': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'code.': 0.18; 'normally': 0.19; 'thu,': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'error': 0.23; 'unicode': 0.24; 'versions': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'feature': 0.29; 'url:bugs': 0.29; 'am,': 0.29; 'character': 0.29; 'raise': 0.29; 'related': 0.29; '"",': 0.31; '+0100,': 0.31; 'assumes': 0.31; "d'aprano": 0.31; 'exceptions': 0.31; 'keyerror:': 0.31; 'lies': 0.31; 'raised': 0.31; 'steven': 0.31; 'yes.': 0.31; 'file': 0.32; 'url:python': 0.33; '(most': 0.33; 'problem': 0.35; "can't": 0.35; 'display': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'there': 0.35; '8bit%:9': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'handle': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'break': 0.61; 'new': 0.61; 'received:173': 0.61; '8bit%:10': 0.64; 'believe': 0.68; 'answer.': 0.68; 'different.': 0.84; 'received:fios.verizon.net': 0.84; 'releases.': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Unicode strings as arguments to exceptions
Date Thu, 16 Jan 2014 18:48:00 -0500
References <mailman.5586.1389876759.18130.python-list@python.org> <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding quoted-printable
X-Gmane-NNTP-Posting-Host pool-173-75-254-207.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <52d7e9a0$0$29999$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5611.1389916099.18130.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389916099 news.xs4all.nl 2906 [2001:888:2000:d::a6]:39214
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:64115

Show key headers only | 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