Skip to content

Commit 32ca164

Browse files
committed
py: Tidy up array.array; add more error handling.
1 parent 90edf9e commit 32ca164

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

py/objarray.c

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
3535
} else {
3636
print(env, "array('%c'", o->typecode);
3737
if (o->len > 0) {
38-
print(env, ", [", o->typecode);
38+
print(env, ", [");
3939
for (int i = 0; i < o->len; i++) {
4040
if (i > 0) {
4141
print(env, ", ");
@@ -75,32 +75,37 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
7575
}
7676

7777
STATIC mp_obj_t array_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
78-
if (n_args < 1 || n_args > 2) {
79-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "unexpected # of arguments, %d given", n_args));
80-
}
81-
// TODO check args
78+
mp_check_nargs(n_args, 1, 2, n_kw, false);
79+
80+
// get typecode
8281
uint l;
8382
const char *typecode = mp_obj_str_get_data(args[0], &l);
83+
8484
if (n_args == 1) {
85+
// 1 arg: make an empty array
8586
return array_new(*typecode, 0);
87+
} else {
88+
// 2 args: construct the array from the given iterator
89+
return array_construct(*typecode, args[1]);
8690
}
87-
88-
return array_construct(*typecode, args[1]);
8991
}
9092

9193
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-
}
94+
mp_check_nargs(n_args, 0, 1, n_kw, false);
9595

96-
if (MP_OBJ_IS_SMALL_INT(args[0])) {
96+
if (n_args == 0) {
97+
// no args: construct an empty bytearray
98+
return array_new(BYTEARRAY_TYPECODE, 0);
99+
} else if (MP_OBJ_IS_SMALL_INT(args[0])) {
100+
// 1 arg, an integer: construct a blank bytearray of that length
97101
uint len = MP_OBJ_SMALL_INT_VALUE(args[0]);
98102
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, len);
99103
memset(o->items, 0, len);
100104
return o;
105+
} else {
106+
// 1 arg, an iterator: construct the bytearray from that
107+
return array_construct(BYTEARRAY_TYPECODE, args[0]);
101108
}
102-
103-
return array_construct(BYTEARRAY_TYPECODE, args[0]);
104109
}
105110

106111
STATIC mp_obj_t array_unary_op(int op, mp_obj_t o_in) {
@@ -129,7 +134,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
129134

130135
STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
131136
if (value == MP_OBJ_NULL) {
132-
// delete item; does this need to be implemented?
137+
// delete item
138+
// TODO implement
133139
return MP_OBJ_NOT_SUPPORTED;
134140
} else {
135141
mp_obj_array_t *o = self_in;
@@ -183,12 +189,16 @@ const mp_obj_type_t mp_type_bytearray = {
183189
};
184190

185191
STATIC mp_obj_array_t *array_new(char typecode, uint n) {
192+
int typecode_size = mp_binary_get_size(typecode);
193+
if (typecode_size <= 0) {
194+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
195+
}
186196
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
187197
o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
188198
o->typecode = typecode;
189199
o->free = 0;
190200
o->len = n;
191-
o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
201+
o->items = m_malloc(typecode_size * o->len);
192202
return o;
193203
}
194204

0 commit comments

Comments
 (0)