Author techtonik
Recipients techtonik
Date 2013-04-28.07:37:55
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1367134676.21.0.161863892006.issue17859@psf.upfronthosting.co.za>
In-reply-to
Content
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'))
History
Date User Action Args
2013-04-28 07:37:56techtoniksetrecipients: + techtonik
2013-04-28 07:37:56techtoniksetmessageid: <1367134676.21.0.161863892006.issue17859@psf.upfronthosting.co.za>
2013-04-28 07:37:56techtoniklinkissue17859 messages
2013-04-28 07:37:55techtonikcreate