[New-bugs-announce] [issue17859] improve error message for saving ints to file
anatoly techtonik
report at bugs.python.org
Sun Apr 28 09:37:56 CEST 2013
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 <report at bugs.python.org>
<http://bugs.python.org/issue17859>
_______________________________________
More information about the New-bugs-announce
mailing list