Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21334 > unrolled thread
| Started by | Peter Kleiweg <pkleiweg@xs4all.nl> |
|---|---|
| First post | 2012-03-07 20:41 +0100 |
| Last post | 2012-03-08 03:49 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Peter Kleiweg <pkleiweg@xs4all.nl> |
|---|---|
| Date | 2012-03-07 20:41 +0100 |
| Subject | sys.stdout.detach() results in ValueError |
| Message-ID | <alpine.DEB.2.00.1203072032590.2657@pebbe> |
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/
[toc] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-03-07 17:14 -0500 |
| Message-ID | <mailman.494.1331158511.3037.python-list@python.org> |
| In reply to | #21334 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Kleiweg <pkleiweg@xs4all.nl> |
|---|---|
| Date | 2012-03-07 23:35 +0100 |
| Message-ID | <alpine.DEB.2.00.1203072335240.2657@pebbe> |
| In reply to | #21358 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2012-03-07 19:10 -0500 |
| Message-ID | <mailman.496.1331165460.3037.python-list@python.org> |
| In reply to | #21359 |
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
[toc] | [prev] | [next] | [standalone]
| From | Mark Tolonen <metolone@gmail.com> |
|---|---|
| Date | 2012-03-07 16:41 -0800 |
| Message-ID | <483e3ddf-6452-411c-a9c2-5e9318a1a34e@d7g2000pbl.googlegroups.com> |
| In reply to | #21362 |
On Mar 7, 4:10 pm, Terry Reedy <tjre...@udel.edu> 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
[toc] | [prev] | [next] | [standalone]
| From | Benjamin Peterson <benjamin@python.org> |
|---|---|
| Date | 2012-03-08 03:49 +0000 |
| Message-ID | <mailman.500.1331178585.3037.python-list@python.org> |
| In reply to | #21334 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web