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

@@ -212,6 +212,12 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
return mp_obj_str_builder_end(o);
}
// check if argument has the buffer protocol
mp_buffer_info_t bufinfo;
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_READ)) {
return mp_obj_new_str_of_type(&mp_type_bytes, bufinfo.buf, bufinfo.len);
}
mp_int_t len;
byte *data;
vstr_t *vstr = NULL;