Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write to stdout in binary mode - is it possible? #48821

Closed
lopgok mannequin opened this issue Dec 7, 2008 · 6 comments
Closed

write to stdout in binary mode - is it possible? #48821

lopgok mannequin opened this issue Dec 7, 2008 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@lopgok
Copy link
Mannequin

lopgok mannequin commented Dec 7, 2008

BPO 4571
Nosy @birkenfeld, @tiran, @benjaminp

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-12-07.14:58:27.655>
created_at = <Date 2008-12-07.02:40:10.041>
labels = ['interpreter-core', 'type-crash']
title = 'write to stdout in binary mode - is it possible?'
updated_at = <Date 2008-12-07.15:51:10.395>
user = 'https://bugs.python.org/lopgok'

bugs.python.org fields:

activity = <Date 2008-12-07.15:51:10.395>
actor = 'christian.heimes'
assignee = 'none'
closed = True
closed_date = <Date 2008-12-07.14:58:27.655>
closer = 'benjamin.peterson'
components = ['Interpreter Core']
creation = <Date 2008-12-07.02:40:10.041>
creator = 'lopgok'
dependencies = []
files = []
hgrepos = []
issue_num = 4571
keywords = []
message_count = 6.0
messages = ['77208', '77210', '77215', '77228', '77229', '77230']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'christian.heimes', 'benjamin.peterson', 'lopgok']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4571'
versions = ['Python 3.0']

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 7, 2008

I have a program that needs to output binary data to stdout.
I don't want to convert it to a string.

for example something like
sys.stdout.write('0o377')
to write a byte with all the bits turned on.

When I try this, I get an error like:
sys.stdout.write(data)
File "/usr/local/lib/python3.0/io.py", line 1484, in write
s.__class__.__name__)
TypeError: can't write bytes to text stream

I know I can open a file in 'wb' mode and write to it, but what
I want to do is somehow switch the mode of stdout to 'wb' mode.
I read lots of python 3 documentation, as well as searched without
finding a way.

@lopgok lopgok mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Dec 7, 2008
@benjaminp
Copy link
Contributor

You can write to the underlying raw stream:

sys.stdout.buffer.write(b'abc')

@birkenfeld
Copy link
Member

Is that an official way? If yes, it needs to be documented.

@benjaminp
Copy link
Contributor

Documented in r67365.

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 7, 2008

I don't consider sys.stdout.buffer.write(b'abc')
to be an acceptable solution. There are many programs that need
to produce binary output with standard output. Consider uudecode
and similar programs.

There needs to be a standard, portable, documented way to put stdout
into binary mode in order to write binary output. For example,
all the flavors of the print command need to be able to send binary
data to standard output.

I don't have a problem changing open statements to support binary
file i/o, but I do have a problem changing every print or write statement
in order to support binary file i/o.

@tiran
Copy link
Member

tiran commented Dec 7, 2008

switch to binary mode
>>> sys.stdout = sys.stdout.buffer

switch back
>>> sys.stdout = sys.__stdout__

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

3 participants