Skip to content

Instantly share code, notes, and snippets.

@superbobry
Created December 22, 2014 16:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superbobry/b90c0cc7c44beaa51ef9 to your computer and use it in GitHub Desktop.
Save superbobry/b90c0cc7c44beaa51ef9 to your computer and use it in GitHub Desktop.
Python 2/3 buffer protocol inconsistency
# Python 2
>>> b = bytearray(8)
>>> v = memoryview(b)
>>> v[0] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' does not have the buffer interface
>>> b
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
# Python 3
>>> b = bytearray(8)
>>> v = memoryview(b)
>>> v[0] = 42
>>> b
bytearray(b'*\x00\x00\x00\x00\x00\x00\x00')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment