Issue 17859: improve error message for saving ints to file (original) (raw)

Created on 2013-04-28 07:37 by techtonik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (8)
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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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'"
History
Date User Action Args
2022-04-11 14:57:45 admin set github: 62059
2016-04-16 16:14:57 serhiy.storchaka set status: open -> closedsuperseder: add "buffer protocol" to glossaryresolution: duplicatestage: needs patch -> resolved
2016-04-16 16:05:46 random832 set nosy: + random832messages: +
2013-05-04 15:32:37 ezio.melotti set type: enhancementmessages: +
2013-05-03 23:19:30 terry.reedy set versions: - Python 3.5nosy: + terry.reedymessages: + stage: needs patch
2013-05-03 16:28:39 eric.araujo set messages: +
2013-04-28 21:40:11 r.david.murray set nosy: + r.david.murraymessages: +
2013-04-28 16:12:42 eric.araujo set nosy: + eric.araujomessages: +
2013-04-28 07:47:54 ezio.melotti set nosy: + ezio.melottimessages: +
2013-04-28 07:37:56 techtonik create