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
{{ message }}
Repository navigation
improve error message for saving ints to file #62059
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=<Date2016-04-16.16:14:57.876>created_at=<Date2013-04-28.07:37:56.173>labels= ['type-feature']
title='improve error message for saving ints to file'updated_at=<Date2016-04-16.16:14:57.875>user='https://bugs.python.org/techtonik'
I needed to write some bytes to file and got this message.
>>> hex=open('hex', 'wb')
>>> for x inrange(0, 0xff, 0x11):
... hex.write(x)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: 'int' does not support the buffer interface
The cause of the error is not that 'int' doesn't support some interface (which is strange already, because the function analyzes and writes int, not the int writes itself), but because it is impossible to know how many binary bytes the int type should take when written.
In Python 2 the solution is:
...
hex.write(chr(x))
But in Python 3 this is again:
TypeError: 'str' does not support the buffer interface
In Python 3 the solution is:
...
hex.write(x.to_bytes(1, 'little'))
Maybe the error could be replaced with something like:
TypeError: write() requires an object that supports the buffer interface, not '<type>'
But that's a bit long and also not entirely accurate, because the type accepted by write depends on the type of the file (binary or text). The first problem could be solved by using "requires a bytes-like object"[0], the second problem could be fixed by omitting the name of the function:
TypeError: a bytes-like object is required, not '<type>'
[0]: bpo-16518 has a discussion about the best term to describe "objects that support the buffer protocol"
If one wants to call the write method of a file object opened in binary mode, one needs to pass bytes, as the doc surely explains. The same error that is seen here with ints would be seen with any other objects. A more practical way is to use print (with its file, sep and end parameters) which will do the conversion for you.
Éric, print doesn't help if one is writing binary data.
What do you mean by not understanding the first message? If you are agreeing that the first error message in Anatoly's initial post isn't clear, does Ezio's proposed change make it clearer?
I think the point is that the error message does not make much sense unless one knows a) that the file is open in binary mode (and that could have happened in a different file) and b) that 'does not support the buffer interface' means 'is not a bytes-like object'. A possible replacement that addresses both problems:
binary-mode write requires bytes-like object, not 'int'
binary-mode write requires bytes-like object, not 'int'
Looks like the function that raises the error (Objects/abstract.c:352) doesn't have enough information to produce a message like that.
I attached a patch to bpo-16518 that improves this and other error messages (this particular error is replaced by "a bytes-like object is required, not 'int'").
Activity
techtonik commentedon Apr 28, 2013
I needed to write some bytes to file and got this message.
The cause of the error is not that 'int' doesn't support some interface (which is strange already, because the function analyzes and writes int, not the int writes itself), but because it is impossible to know how many binary bytes the int type should take when written.
In Python 2 the solution is:
...
hex.write(chr(x))
But in Python 3 this is again:
TypeError: 'str' does not support the buffer interface
In Python 3 the solution is:
...
hex.write(x.to_bytes(1, 'little'))
ezio-melotti commentedon Apr 28, 2013
Maybe the error could be replaced with something like:
TypeError: write() requires an object that supports the buffer interface, not '<type>'
But that's a bit long and also not entirely accurate, because the type accepted by write depends on the type of the file (binary or text). The first problem could be solved by using "requires a bytes-like object"[0], the second problem could be fixed by omitting the name of the function:
TypeError: a bytes-like object is required, not '<type>'
[0]: bpo-16518 has a discussion about the best term to describe "objects that support the buffer protocol"
merwok commentedon Apr 28, 2013
I don’t understand that the first message says.
If one wants to call the write method of a file object opened in binary mode, one needs to pass bytes, as the doc surely explains. The same error that is seen here with ints would be seen with any other objects. A more practical way is to use print (with its file, sep and end parameters) which will do the conversion for you.
bitdancer commentedon Apr 28, 2013
Éric, print doesn't help if one is writing binary data.
What do you mean by not understanding the first message? If you are agreeing that the first error message in Anatoly's initial post isn't clear, does Ezio's proposed change make it clearer?
merwok commentedon May 3, 2013
Ah, right, I missed the part about the file being opened in binary mode.
terryjreedy commentedon May 3, 2013
I think the point is that the error message does not make much sense unless one knows a) that the file is open in binary mode (and that could have happened in a different file) and b) that 'does not support the buffer interface' means 'is not a bytes-like object'. A possible replacement that addresses both problems:
binary-mode write requires bytes-like object, not 'int'
ezio-melotti commentedon May 4, 2013
Looks like the function that raises the error (Objects/abstract.c:352) doesn't have enough information to produce a message like that.
I attached a patch to bpo-16518 that improves this and other error messages (this particular error is replaced by "a bytes-like object is required, not 'int'").
random832 commentedon Apr 16, 2016
This bug should be closed since bpo-16518 was accepted and the error is now "TypeError: a bytes-like object is required, not 'int'"