msg187968 - (view) |
Author: anatoly techtonik (techtonik) |
Date: 2013-04-28 07:37 |
I needed to write some bytes to file and got this message. >>> hex = open('hex', 'wb') >>> for x in range(0, 0xff, 0x11): ... hex.write(x) ... Traceback (most recent call last): File "", line 2, in 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')) |
|
|
msg187970 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-04-28 07:47 |
Maybe the error could be replaced with something like: TypeError: write() requires an object that supports the buffer interface, not '' 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 '' [0]: #16518 has a discussion about the best term to describe "objects that support the buffer protocol" |
|
|
msg187994 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2013-04-28 16:12 |
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. |
|
|
msg188027 - (view) |
Author: R. David Murray (r.david.murray) *  |
Date: 2013-04-28 21:40 |
É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? |
|
|
msg188304 - (view) |
Author: Éric Araujo (eric.araujo) *  |
Date: 2013-05-03 16:28 |
Ah, right, I missed the part about the file being opened in binary mode. |
|
|
msg188332 - (view) |
Author: Terry J. Reedy (terry.reedy) *  |
Date: 2013-05-03 23:19 |
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' |
|
|
msg188370 - (view) |
Author: Ezio Melotti (ezio.melotti) *  |
Date: 2013-05-04 15:32 |
> 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 #16518 that improves this and other error messages (this particular error is replaced by "a bytes-like object is required, not 'int'"). |
|
|
msg263566 - (view) |
Author: (random832) |
Date: 2016-04-16 16:05 |
This bug should be closed since #16518 was accepted and the error is now "TypeError: a bytes-like object is required, not 'int'" |
|
|