Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21362

Re: sys.stdout.detach() results in ValueError

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 <python-python-list@m.gmane.org>
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 <tjreedy@udel.edu>
Subject Re: sys.stdout.detach() results in ValueError
Date Wed, 07 Mar 2012 19:10:25 -0500
References <alpine.DEB.2.00.1203072032590.2657@pebbe> <mailman.494.1331158511.3037.python-list@python.org> <alpine.DEB.2.00.1203072335240.2657@pebbe>
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 <alpine.DEB.2.00.1203072335240.2657@pebbe>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.496.1331165460.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

sys.stdout.detach() results in ValueError Peter Kleiweg <pkleiweg@xs4all.nl> - 2012-03-07 20:41 +0100
  Re: sys.stdout.detach() results in ValueError Dave Angel <d@davea.name> - 2012-03-07 17:14 -0500
    Re: sys.stdout.detach() results in ValueError Peter Kleiweg <pkleiweg@xs4all.nl> - 2012-03-07 23:35 +0100
      Re: sys.stdout.detach() results in ValueError Terry Reedy <tjreedy@udel.edu> - 2012-03-07 19:10 -0500
        Re: sys.stdout.detach() results in ValueError Mark Tolonen <metolone@gmail.com> - 2012-03-07 16:41 -0800
  Re: sys.stdout.detach() results in ValueError Benjamin Peterson <benjamin@python.org> - 2012-03-08 03:49 +0000

csiph-web