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

Confusing error message when passing bytes to print with file pointing to a binary file #50553

Open
bitdancer opened this issue Jun 18, 2009 · 3 comments
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error

Comments

@bitdancer
Copy link
Member

BPO 6304
Nosy @birkenfeld, @amauryfa, @pitrou, @bitdancer

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 = None
created_at = <Date 2009-06-18.02:51:44.668>
labels = ['3.10', 'type-bug', '3.8', '3.9', 'expert-IO']
title = 'Confusing error message when passing bytes to print with file pointing to a binary file'
updated_at = <Date 2020-11-06.15:00:03.316>
user = 'https://github.com/bitdancer'

bugs.python.org fields:

activity = <Date 2020-11-06.15:00:03.316>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['IO']
creation = <Date 2009-06-18.02:51:44.668>
creator = 'r.david.murray'
dependencies = []
files = []
hgrepos = []
issue_num = 6304
keywords = []
message_count = 3.0
messages = ['89490', '89493', '89725']
nosy_count = 4.0
nosy_names = ['georg.brandl', 'amaury.forgeotdarc', 'pitrou', 'r.david.murray']
pr_nums = []
priority = 'low'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue6304'
versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

@bitdancer
Copy link
Member Author

The documentation for the print function specifies that the input
argument is coerced to string. However, if bytes are passed and the
file= parameter points to a file open for binary write, the error
message produced is:

>>> out = open('temp', 'wb')
>>> print(b'abc', file=out)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument 1 must be bytes or buffer, not str

which is, to say the least, confusing, since I clearly passed print a
bytes object as argument 1.

@bitdancer bitdancer added topic-IO type-bug An unexpected behavior, bug, or error labels Jun 18, 2009
@birkenfeld
Copy link
Member

How would you propose to fix this? print() should certainly not catch
all TypeErrors and raise a new one...

Maybe exception chaining could be used, so that print raises a TypeError
of its own with the cause being the original message?

@amauryfa
Copy link
Member

The problem here is not the bytes object: it is correctly coerced to a
string.

The problem is the binary stream, which cannot accept strings.
We could maybe detect common errors and add a check at the beginning of
the print() function? something like
if isinstance(file, (BufferedWriter, RawIOBase)):
raise ValueError("file should be a text stream")

@iritkatriel iritkatriel added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Nov 6, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@iritkatriel iritkatriel added 3.11 only security fixes 3.12 bugs and security fixes and removed 3.9 only security fixes 3.8 only security fixes labels Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants