Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.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; 'handler': 0.04; '-*-': 0.07; 'practice,': 0.07; 'skip:/ 10': 0.07; 'utf-8': 0.07; 'python': 0.09; 'alan': 0.09; 'codecs': 0.09; 'coding:': 0.09; 'encode': 0.09; 'encoding.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:()': 0.09; 'unicode,': 0.09; 'def': 0.10; 'encoding': 0.15; 'codec': 0.16; 'foot': 0.16; 'message-id:@dough.gmane.org': 0.16; 'missing?': 0.16; 'ordinal': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'string': 0.17; 'wrote:': 0.17; 'instance,': 0.17; 'unicode': 0.17; 'trying': 0.21; 'import': 0.21; 'doc': 0.22; 'exceptions': 0.22; 'errors': 0.23; 'seems': 0.23; 'header:User- Agent:1': 0.26; 'skip:" 20': 0.26; '(most': 0.27; 'handling': 0.27; 'header:X-Complaints-To:1': 0.28; 'skip:( 20': 0.28; 'behaviour': 0.29; 'cat': 0.29; 'piece': 0.29; 'shoot': 0.29; 'subject:skip:u 10': 0.29; 'character': 0.29; 'probably': 0.29; "i'm": 0.29; 'function': 0.30; 'error': 0.30; 'expect': 0.31; 'url:python': 0.32; 'file': 0.32; 'switch': 0.32; 'print': 0.32; 'says': 0.33; 'strict': 0.33; 'traceback': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; "can't": 0.34; 'there': 0.35; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'why': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'called': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'think': 0.40; 'skip:u 10': 0.60; 'strange': 0.62; 'information': 0.63; 'more': 0.63; 'making': 0.64; '8bit%:10': 0.69; 'yourself': 0.77; 'discovered': 0.83; '2.7.1': 0.84; 'preventing': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: codecs.register_error for "strict", unicode.encode() and str.decode() Date: Fri, 27 Jul 2012 09:16:48 +0200 Organization: None References: <5011CCA5.408@franzoni.eu> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p5084b332.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343373428 news.xs4all.nl 6965 [2001:888:2000:d::a6]:41771 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26134 Alan Franzoni wrote: > Hello, > I think I'm missing some piece here. > > I'm trying to register a default error handler for handling exceptions > for preventing encoding/decoding errors (I know how this works and that > making this global is probably not a good practice, but I found this > strange behaviour while writing a proof of concept of how to let Python > work in a more forgiving way). > > What I discovered is that register_error() for "strict" seems to work in > the way I expect for string decoding, not for unicode encoding. > > That's what happens on Mac, Python 2.7.1 from Apple: > > melquiades:tmp alan$ cat minimal_test_encode.py > # -*- coding: utf-8 -*- > > import codecs > > def handle_encode(e): > return ("ASD", e.end) > > codecs.register_error("strict", handle_encode) > > print u"à".encode("ascii") > > melquiades:tmp alan$ python minimal_test_encode.py > Traceback (most recent call last): > File "minimal_test_encode.py", line 10, in > u"à".encode("ascii") > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in > position 0: ordinal not in range(128) > > > OTOH this works properly: > > melquiades:tmp alan$ cat minimal_test_decode.py > # -*- coding: utf-8 -*- > > import codecs > > def handle_decode(e): > return (u"ASD", e.end) > > codecs.register_error("strict", handle_decode) > > print "à".decode("ascii") > > melquiades:tmp alan$ python minimal_test_decode.py > ASDASD > > > What piece am I missing? The doc at > http://docs.python.org/library/codecs.html says " For > encoding /error_handler/ will be called with a UnicodeEncodeError > > instance, which contains information about the location of the error.", is > there any reason why the standard "strict" handler cannot be replaced? The error handling for the standard erorrs "strict", "replace", "ignore", and "xmlcharrefreplace" is hardwired, see function unicode_encode_ucs1 in Lib/unicodeobject.c: if (known_errorHandler==-1) { if ((errors==NULL) || (!strcmp(errors, "strict"))) known_errorHandler = 1; ... switch (known_errorHandler) { case 1: /* strict */ raise_encode_exception(&exc, encoding, unicode, collstart, collend, reason); goto onError; You need another gun to shoot yourself in the foot ;)