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; 'sys': 0.05; 'received:verizon.net': 0.07; 'terry': 0.07; 'python': 0.08; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'str,': 0.09; 'valueerror:': 0.09; 'error:': 0.10; 'exception': 0.12; 'binary': 0.13; 'detach': 0.16; 'jaar': 0.16; 'mode.': 0.16; 'reedy': 0.16; 'stdout': 0.16; 'subject:() ': 0.16; 'sys.stdout': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'bytes': 0.18; 'subject:skip:s 10': 0.18; 'that?': 0.18; 'jan': 0.19; 'header:In-Reply-To:1': 0.22; 'import': 0.27; '(you': 0.28; 'script': 0.28; 'pm,': 0.29; 'remains': 0.30; 'typeerror:': 0.30; 'van': 0.30; 'pretty': 0.31; 'yet': 0.32; 'header:User-Agent:1': 0.33; 'it.': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'skip:# 10': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'minimal': 0.37; 'but': 0.37; 'using': 0.37; 'some': 0.38; 'data': 0.38; 'open': 0.38; 'right,': 0.39; 'point': 0.40; 'to:addr:python.org': 0.40; 'your': 0.61; 'here': 0.64; 'opened': 0.64; 'dag': 0.84; 'stdout?': 0.84; 'sys.stdout.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: sys.stdout.detach() results in ValueError Date: Wed, 07 Mar 2012 19:10:25 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1331165460 news.xs4all.nl 6947 [2001:888:2000:d::a6]:40152 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21362 On 3/7/2012 5:35 PM, Peter Kleiweg wrote: > Dave Angel schreef op de 7e dag van de lentemaand van het jaar 2012: > >> On 03/07/2012 02:41 PM, Peter Kleiweg wrote: >>> I want to write out some binary data to stdout in Python3. I >>> thought the way to do this was to call detach on sys.stdout. But >>> apparently, you can't. Here is a minimal script: >>> >>> #!/usr/bin/env python3.1 >>> import sys >>> fp = sys.stdout.detach() >>> >>> Not yet using fp in any way, this script gives the following error: >>> >>> Exception ValueError: 'underlying buffer has been detached' in >>> >>> Same in Python 3.1.4 and Python 3.2.2 >>> >>> So, what do I do if I want to send binary data to stdout? >>> >>> >>> >> >> sys.stdout.write( some_binary_data ) > > TypeError: must be str, not bytes Right, you can only send binary data to file opened in binary mode. The default sys.stdout is in text mode. I am pretty sure that remains true even if stdout is redirected. (You did not mention your OS.) You would have to open such a file and make sys.stdout point to it. sys.stdout = my_binary_file. But why do that? Just open the file and write to it directly without the above. -- Terry Jan Reedy