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


Groups > comp.lang.python > #21498

Re: Why are some unicode error handlers "encode only"?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <walter@livinglogic.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '*not*': 0.05; 'ascii': 0.07; 'python': 0.08; '(also': 0.09; '>>>>': 0.09; 'bytes)': 0.09; 'handler.': 0.09; 'handlers': 0.09; 'subject:error': 0.09; '*after*': 0.16; 'codec': 0.16; 'handlers.': 0.16; 'subject:unicode': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'bytes': 0.18; 'this?': 0.19; '(most': 0.21; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'documented': 0.23; "shouldn't": 0.23; 'similarly': 0.23; 'byte': 0.24; "python's": 0.24; 'traceback': 0.24; 'cc:2**0': 0.26; 'work.': 0.27; 'produced': 0.28; 'example': 0.29; 'cc:addr:python.org': 0.29; '(and': 0.30; 'subject:some': 0.30; 'least': 0.30; 'error': 0.30; 'url:library': 0.31; 'actually': 0.31; 'does': 0.32; 'there': 0.33; "can't": 0.33; 'header:User-Agent:1': 0.33; 'file': 0.34; 'skip:b 40': 0.34; 'steven': 0.34; 'last):': 0.34; 'probably': 0.35; 'something': 0.35; 'url:python': 0.35; 'two': 0.36; 'encoding': 0.37; 'sequence': 0.37; 'skip:" 10': 0.37; 'using': 0.37; 'some': 0.38; 'problems': 0.38; 'e.g.': 0.39; 'url:org': 0.39; 'received:de': 0.39; 'received:217': 0.61; 'illegal': 0.63; 'received:10.10': 0.68; 'received:10.10.20': 0.84; 'only:': 0.93
X-Virus-Scanned Debian amavisd-new at rhein.livinglogic.de
Date Sun, 11 Mar 2012 17:10:12 +0100
From Walter Dörwald <walter@livinglogic.de>
Organization LivingLogic AG, Bayreuth/Germany
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2
MIME-Version 1.0
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>
Subject Re: Why are some unicode error handlers "encode only"?
References <4f5cb8c2$0$29891$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <4f5cb8c2$0$29891$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.569.1331484185.3037.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1331484185 news.xs4all.nl 6947 [2001:888:2000:d::a6]:46101
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21498

Show key headers only | View raw


On 11.03.12 15:37, Steven D'Aprano wrote:

> At least two standard error handlers are documented as working for
> encoding only:
>
> xmlcharrefreplace
> backslashreplace
>
> See http://docs.python.org/library/codecs.html#codec-base-classes
>
> and http://docs.python.org/py3k/library/codecs.html
>
> Why is this? I don't see why they shouldn't work for decoding as well.

Because xmlcharrefreplace and backslashreplace are *error* handlers. 
However the bytes sequence b'&#12345;' does *not* contain any bytes that 
are not decodable for e.g. the ASCII codec. So there are no errors to 
handle.

> Consider this example using Python 3.2:
>
>>>> b"aaa--\xe9z--\xe9!--bbb".decode("cp932")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in<module>
> UnicodeDecodeError: 'cp932' codec can't decode bytes in position 9-10:
> illegal multibyte sequence
>
> The two bytes b'\xe9!' is an illegal multibyte sequence for CP-932 (also
> known as MS-KANJI or SHIFT-JIS). Is there some reason why this shouldn't
> or can't be supported?

The byte sequence b'\xe9!' however is not something that would have been 
produced by the backslashreplace error handler. b'\\xe9!' (a sequence 
containing 5 bytes) would have been (and this probably would decode 
without any problems with the cp932 codec).

> # This doesn't actually work.
> b"aaa--\xe9z--\xe9!--bbb".decode("cp932", "backslashreplace")
> =>  r'aaa--騷--\xe9\x21--bbb'
>
> and similarly for xmlcharrefreplace.

This would require a postprocess step *after* the bytes have been 
decoded. This is IMHO out of scope for Python's codec machinery.

Servus,
    Walter

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


Thread

Why are some unicode error handlers "encode only"? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-11 14:37 +0000
  Re: Why are some unicode error handlers "encode only"? Walter Dörwald <walter@livinglogic.de> - 2012-03-11 17:10 +0100
  Re: Why are some unicode error handlers "encode only"? Terry Reedy <tjreedy@udel.edu> - 2012-03-11 13:10 -0400

csiph-web