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!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'encoded': 0.05; 'ascii': 0.07; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; '(also': 0.09; '>>>>': 0.09; 'bytes,': 0.09; 'handlers': 0.09; 'presume': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:error': 0.09; 'output': 0.10; 'am,': 0.12; 'codec': 0.16; 'incomplete': 0.16; 'reedy': 0.16; 'subject:unicode': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'bytes': 0.18; 'this?': 0.19; 'jan': 0.19; '(most': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'documented': 0.23; 'extending': 0.23; 'numeric': 0.23; 'replacing': 0.23; "shouldn't": 0.23; 'similarly': 0.23; 'byte': 0.24; 'traceback': 0.24; 'work.': 0.27; 'bit': 0.28; 'unicode': 0.29; 'example': 0.29; 'handling': 0.30; 'subject:some': 0.30; 'least': 0.30; 'error': 0.30; 'skip:b 30': 0.31; 'url:library': 0.31; 'xml': 0.31; 'actually': 0.31; 'does': 0.32; 'there': 0.33; "can't": 0.33; 'header:User-Agent:1': 0.33; 'instead': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'character': 0.34; 'skip:b 40': 0.34; 'steven': 0.34; 'last):': 0.34; 'problem.': 0.35; 'to:addr:python-list': 0.35; 'url:python': 0.35; 'two': 0.36; 'received:org': 0.36; 'encoding': 0.37; 'sequence': 0.37; 'but': 0.37; 'skip:" 10': 0.37; 'using': 0.37; 'references': 0.38; 'somewhat': 0.38; 'some': 0.38; 'url:org': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'illegal': 0.63; 'fit.': 0.67; 'only:': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Why are some unicode error handlers "encode only"? Date: Sun, 11 Mar 2012 13:10:33 -0400 References: <4f5cb8c2$0$29891$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-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4f5cb8c2$0$29891$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331485853 news.xs4all.nl 6847 [2001:888:2000:d::a6]:49841 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21499 On 3/11/2012 10:37 AM, 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 presume the purpose of both is to facilitate transmission of unicode=20 text via byte transmission by extending incomplete byte encodings by=20 replacing unicode chars that do not fit in the given encoding by a ascii = byte sequence that will fit. > I don't see why they shouldn't work for decoding as well. > Consider this example using Python 3.2: > >>>> b"aaa--\xe9z--\xe9!--bbb".decode("cp932") > Traceback (most recent call last): > File "", line 1, in > 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 (als= o > known as MS-KANJI or SHIFT-JIS). Is there some reason why this shouldn'= t > or can't be supported? > > # This doesn't actually work. > b"aaa--\xe9z--\xe9!--bbb".decode("cp932", "backslashreplace") > =3D> r'aaa--=E9=A8=B7--\xe9\x21--bbb' This output does not round-trip and would be a bit of a fib since it=20 somewhat misrepresents what the encoded bytes were: >>> r'aaa--=E9=A8=B7--\xe9\x21--bbb'.encode("cp932") b'aaa--\xe9z--\\xe9\\x21--bbb' >>> b'aaa--\xe9z--\\xe9\\x21--bbb'.decode("cp932") 'aaa--=E9=A8=B7--\\xe9\\x21--bbb' Python 3 added surrogateescape error handling to solve this problem. > and similarly for xmlcharrefreplace. Since xml character references are representations of unicode chars, and = not bytes, I do not see how that would work. By analogy, perhaps you=20 mean to have '&#e9;' in your output instead of '\xe9\x21', but=20 those would not properly be xml numeric character references. --=20 Terry Jan Reedy