sys.stdout.detach() results in ValueError

Discussion in 'Python' started by Peter Kleiweg, Mar 7, 2012.

  1. 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?



    --
    Peter Kleiweg
    http://pkleiweg.home.xs4all.nl/
     
    Peter Kleiweg, Mar 7, 2012
    #1
    1. Advertising

  2. Peter Kleiweg

    Dave Angel Guest

    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 )

    Why should you need to do some funny manipulation?

    If you have some other unstated motivation, better ask a clearer question.



    --

    DaveA
     
    Dave Angel, Mar 7, 2012
    #2
    1. Advertising

  3. 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




    --
    Peter Kleiweg
    http://pkleiweg.home.xs4all.nl/
     
    Peter Kleiweg, Mar 7, 2012
    #3
  4. Peter Kleiweg

    Terry Reedy Guest

    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
     
    Terry Reedy, Mar 8, 2012
    #4
  5. Peter Kleiweg

    Mark Tolonen Guest

    On Mar 7, 4:10 pm, Terry Reedy <> wrote:
    > 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


    Write binary data to sys.stdout.buffer.

    -Mark
     
    Mark Tolonen, Mar 8, 2012
    #5
  6. Peter Kleiweg <pkleiweg <at> xs4all.nl> writes:
    > Not yet using fp in any way, this script gives the following error:
    >
    > Exception ValueError: 'underlying buffer has been detached' in


    You're probably using print() or some such which tries to write to sys.stdout.
    It's safest to just write to sys.stdout.buffer rather than using detach.
     
    Benjamin Peterson, Mar 8, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    671
    Roman Neuhauser
    Apr 4, 2005
  2. Replies:
    2
    Views:
    666
    velle
    Jan 5, 2006
  3. Replies:
    0
    Views:
    491
  4. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    581
    Nobody
    Jan 11, 2010
  5. Michel Albert
    Replies:
    5
    Views:
    1,449
    Daniel Dehennin
    Oct 24, 2012
Loading...

Share This Page