You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=Noneclosed_at=Nonecreated_at=<Date2009-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=<Date2020-11-06.15:00:03.316>user='https://github.com/bitdancer'
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.
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")
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: