This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: improve error message for saving ints to file
Type: enhancement Stage: resolved
Components: Versions: Python 3.3, Python 3.4
Status: closed Resolution: duplicate
Dependencies: Superseder: add "buffer protocol" to glossary
View: 16518
Assigned To: Nosy List: eric.araujo, ezio.melotti, r.david.murray, random832, techtonik, terry.reedy
Priority: normal Keywords:

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 "<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:

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 '<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]: #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'"
Date User Action Args
2022-04-11 14:57:45adminsetgithub: 62059
2016-04-16 16:14:57serhiy.storchakasetstatus: open -> closed
superseder: add "buffer protocol" to glossary
resolution: duplicate
stage: needs patch -> resolved
2016-04-16 16:05:46random832setnosy: + random832
messages: + msg263566
2013-05-04 15:32:37ezio.melottisettype: enhancement
messages: + msg188370
2013-05-03 23:19:30terry.reedysetversions: - Python 3.5
nosy: + terry.reedy

messages: + msg188332

stage: needs patch
2013-05-03 16:28:39eric.araujosetmessages: + msg188304
2013-04-28 21:40:11r.david.murraysetnosy: + r.david.murray
messages: + msg188027
2013-04-28 16:12:42eric.araujosetnosy: + eric.araujo
messages: + msg187994
2013-04-28 07:47:54ezio.melottisetnosy: + ezio.melotti
messages: + msg187970
2013-04-28 07:37:56techtonikcreate