Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!news.le-studio75.com!news.univ-fcomte.fr!not-for-mail From: Laurent Claessens Newsgroups: comp.lang.python Subject: Re: what is best method to set sys.stdout to utf-8? Date: Thu, 08 Mar 2012 07:45:32 +0100 Organization: A poorly-installed InterNetNews site Lines: 25 Message-ID: <4F58558C.3000706@gmail.com> References: NNTP-Posting-Host: acces.univ-fcomte.fr Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.univ-fcomte.fr 1331189442 379 194.57.91.212 (8 Mar 2012 06:50:42 GMT) X-Complaints-To: news@news.univ-fcomte.fr NNTP-Posting-Date: Thu, 8 Mar 2012 06:50:42 +0000 (UTC) Cc: python-list@python.org User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.2) Gecko/20120216 Thunderbird/10.0.2 In-Reply-To: Xref: csiph.com comp.lang.python:21375 > I would. The io module is more recent an partly replaces codecs. The > latter remains for back compatibility and whatever it can do that io cannot. I've a naive question : what is wrong with the following system ? class MyStdOut(object): def __init__(self): self.old_stdout=sys.stdout def write(self,x): try: if isinstance(x,unicode): x=x.encode("utf8") except (UnicodeEncodeError,UnicodeDecodeError): sys.stderr.write("This should not happen !") raise self.old_stdout.write(x) sys.stdout=MyStdOut() ... well ... a part of the fact that it is much longer ? Laurent Claessens