Skip to content

Commit 4671392

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
2 parents 1d24ea5 + 4dcb605 commit 4671392

5 files changed

Lines changed: 26 additions & 12 deletions

File tree

py/builtintables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
1717
// built-in types
1818
{ MP_OBJ_NEW_QSTR(MP_QSTR_bool), (mp_obj_t)&mp_type_bool },
1919
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytes), (mp_obj_t)&mp_type_bytes },
20+
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_type_bytearray },
2021
#if MICROPY_ENABLE_FLOAT
2122
{ MP_OBJ_NEW_QSTR(MP_QSTR_complex), (mp_obj_t)&mp_type_complex },
2223
#endif
@@ -72,7 +73,6 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
7273
{ MP_OBJ_NEW_QSTR(MP_QSTR_repr), (mp_obj_t)&mp_builtin_repr_obj },
7374
{ MP_OBJ_NEW_QSTR(MP_QSTR_sorted), (mp_obj_t)&mp_builtin_sorted_obj },
7475
{ MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&mp_builtin_sum_obj },
75-
{ MP_OBJ_NEW_QSTR(MP_QSTR_bytearray), (mp_obj_t)&mp_builtin_bytearray_obj },
7676

7777
// built-in exceptions
7878
{ MP_OBJ_NEW_QSTR(MP_QSTR_BaseException), (mp_obj_t)&mp_type_BaseException },

py/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ extern const mp_obj_type_t mp_type_bool;
259259
extern const mp_obj_type_t mp_type_int;
260260
extern const mp_obj_type_t mp_type_str;
261261
extern const mp_obj_type_t mp_type_bytes;
262+
extern const mp_obj_type_t mp_type_bytearray;
262263
extern const mp_obj_type_t mp_type_float;
263264
extern const mp_obj_type_t mp_type_complex;
264265
extern const mp_obj_type_t mp_type_tuple;

py/objarray.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,20 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const m
8888
return array_construct(*typecode, args[1]);
8989
}
9090

91-
// This is top-level factory function, not virtual method
92-
// TODO: "bytearray" really should be type, not function
93-
STATIC mp_obj_t mp_builtin_bytearray(mp_obj_t arg) {
94-
if (MP_OBJ_IS_SMALL_INT(arg)) {
95-
uint len = MP_OBJ_SMALL_INT_VALUE(arg);
91+
STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
92+
if (n_args > 1) {
93+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args));
94+
}
95+
96+
if (MP_OBJ_IS_SMALL_INT(args[0])) {
97+
uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
9698
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
9799
memset(o->items, 0, len);
98100
return o;
99101
}
100102

101-
return array_construct(BYTEARRAY_TYPECODE, arg);
103+
return array_construct(BYTEARRAY_TYPECODE, args[0]);
102104
}
103-
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_bytearray_obj, mp_builtin_bytearray);
104105

105106
STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
106107
mp_obj_array_t *o = o_in;
@@ -127,7 +128,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
127128
}
128129

129130
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
130-
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array));
131+
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_array) || MP_OBJ_IS_TYPE(self_in, &mp_type_bytearray));
131132
mp_obj_array_t *self = self_in;
132133
if (self->free == 0) {
133134
int item_sz = mp_binary_get_size(self->typecode);
@@ -174,9 +175,22 @@ const mp_obj_type_t mp_type_array = {
174175
.locals_dict = (mp_obj_t)&array_locals_dict,
175176
};
176177

178+
const mp_obj_type_t mp_type_bytearray = {
179+
{ &mp_type_type },
180+
.name = MP_QSTR_bytearray,
181+
.print = array_print,
182+
.make_new = bytearray_make_new,
183+
.getiter = array_iterator_new,
184+
.unary_op = array_unary_op,
185+
.binary_op = array_binary_op,
186+
.store_item = array_store_item,
187+
.buffer_p = { .get_buffer = array_get_buffer },
188+
.locals_dict = (mp_obj_t)&array_locals_dict,
189+
};
190+
177191
STATIC mp_obj_array_t *array_new(char typecode, uint n) {
178192
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
179-
o->base.type = &mp_type_array;
193+
o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
180194
o->typecode = typecode;
181195
o->free = 0;
182196
o->len = n;

py/objarray.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
MP_DECLARE_CONST_FUN_OBJ(mp_builtin_bytearray_obj);
2-
31
mp_obj_t mp_obj_new_bytearray(uint n, void *items);

tests/basics/bytearray1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
print(bytearray(4))
22
a = bytearray([1, 2, 200])
3+
print(type(a))
34
print(a[0], a[2])
45
print(a[-1])
56
print(a)

0 commit comments

Comments
 (0)