summaryrefslogtreecommitdiff
path: root/py/objstr.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-12-04 15:46:14 +0000
committerDamien George <damien.p.george@gmail.com>2014-12-04 15:46:14 +0000
commit32ef3a3517caa79f5d2237dbed8078e7fa79c742 (patch)
tree59a132d0ab29a5732a64bad9433967da6ea222c5 /py/objstr.c
parent3a5352b483bc2ceb0886eca7d9c82f7fce01f679 (diff)
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).
Diffstat (limited to 'py/objstr.c')
-rw-r--r--py/objstr.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c
index a8e276025..cfe0ef115 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -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;