py: Allow bytes/bytearray/array to be init'd by buffer protocol objects.

Behaviour of array initialisation is subtly different for bytes,
bytearray and array.array when argument has buffer protocol.  This patch
gets us CPython conformant (except we allow initialisation of
array.array by buffer with length not a multiple of typecode).
This commit is contained in:
Damien George
2014-12-04 15:46:14 +00:00
parent 3a5352b483
commit 32ef3a3517
5 changed files with 74 additions and 4 deletions

View File

@@ -0,0 +1,14 @@
# test construction of bytearray from different objects
from array import array
# bytes, tuple, list
print(bytearray(b'123'))
print(bytearray((1, 2)))
print(bytearray([1, 2]))
# arrays
print(bytearray(array('b', [1, 2])))
print(bytearray(array('h', [1, 2])))
print(bytearray(array('I', [1, 2])))
print(bytearray(array('f', [1, 2.3])))