New submission from anatoly techtonik:

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:
  ...
  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'))

----------
messages: 187968
nosy: techtonik
priority: normal
severity: normal
status: open
title: improve error message for saving ints to file
versions: Python 3.3, Python 3.4, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue17859>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to