Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoded': 0.07; 'interpreter.': 0.07; 'sys': 0.07; 'encode': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'wrote': 0.14; 'codec': 0.16; 'fallback': 0.16; 'interest,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'stderr': 0.16; 'stdout': 0.16; 'subject:Unicode': 0.16; 'sys.stdout': 0.16; 'wrote:': 0.18; 'written': 0.21; 'import': 0.22; 'error': 0.23; 'skip:" 40': 0.26; 'asking': 0.27; 'header:X -Complaints-To:1': 0.27; 'character': 0.29; 'errors': 0.30; "skip:' 10": 0.31; '"",': 0.31; '>>>>': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; "can't": 0.35; 'but': 0.35; 'useful': 0.36; 'thanks': 0.36; 'handle': 0.38; 'to:addr:python- list': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'skip:c 50': 0.60; 'happen': 0.63; 'more': 0.64; 'frank': 0.68; 'differently:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Unicode, stdout, and stderr Date: Tue, 22 Jul 2014 11:29:29 +0200 References: X-Gmane-NNTP-Posting-Host: 197.87.184.209 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-RFC2646: Format=Flowed; Original X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1406021381 news.xs4all.nl 2898 [2001:888:2000:d::a6]:34928 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74989 "Peter Otten" <__peter__@web.de> wrote in message news:lql9oi$hlt$1@ger.gmane.org... > Frank Millman wrote: > [...] > >> Out of interest, does the same thing happen when writing to sys.stderr? > > If you are asking about the fallback mechanism, that is specific to > sys.displayhook in the interactive interpreter. > > But stdout and stderr do handle errors differently: > >>>> import sys >>>> sys.stdout.errors > 'strict' >>>> sys.stderr.errors > 'backslashreplace' > > So a codepoint written to stdout that cannot be encoded with > stdout.encoding > raises an error while a codepoint written to stderr that cannot be encoded > with stderr.encoding is escaped. > > Another way to make stdout more forgiving: > >>>> import sys >>>> print("\u2119") > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python3.4/encodings/cp437.py", line 19, in encode > return codecs.charmap_encode(input,self.errors,encoding_map)[0] > UnicodeEncodeError: 'charmap' codec can't encode character '\u2119' in > position 0: character maps to >>>> sys.stdout = open(1, mode="w", errors="xmlcharrefreplace", > encoding=sys.stdout.encoding, closefd=False) >>>> print("\u2119") > ℙ > That's a lot of very useful information. Thanks very much Frank