diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-08 04:42:44 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-08 19:08:34 +0300 |
commit | b9cf3d3730f8350d9756f6cfb32b1240dccf0981 (patch) | |
tree | 8c18164fbfebceb1804240e40ba606f08b97807e | |
parent | 72cfc6ef0a3958a9f9e4e3abce45cc9288c251d2 (diff) | |
download | micropython-b9cf3d3730f8350d9756f6cfb32b1240dccf0981.tar.gz micropython-b9cf3d3730f8350d9756f6cfb32b1240dccf0981.zip |
bytearray: Support bytearray(int) constructor.
To create bytearray of given length.
-rw-r--r-- | py/objarray.c | 7 | ||||
-rw-r--r-- | tests/basics/bytearray1.py | 1 |
2 files changed, 8 insertions, 0 deletions
diff --git a/py/objarray.c b/py/objarray.c index 0383cb17b3..4f9fa49bcb 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -91,6 +91,13 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m // This is top-level factory function, not virtual method // TODO: "bytearray" really should be type, not function STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) { + if (MP_OBJ_IS_SMALL_INT(arg)) { + uint len = MP_OBJ_SMALL_INT_VALUE(arg); + mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len); + memset(o->items, 0, len); + return o; + } + return array_construct(BYTEARRAY_TYPECODE, arg); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray); diff --git a/tests/basics/bytearray1.py b/tests/basics/bytearray1.py index 3111832f6c..201b5b6590 100644 --- a/tests/basics/bytearray1.py +++ b/tests/basics/bytearray1.py @@ -1,3 +1,4 @@ +print(bytearray(4)) a = bytearray([1, 2, 200]) print(a[0], a[2]) print(a[-1]) |