10000 py/objarray: Fix array.append so it doesn't extend if append fails. · kevincon/circuitpython@04d5e64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04d5e64

Browse files
committed
py/objarray: Fix array.append so it doesn't extend if append fails.
Addresses issue adafruit#1965.
1 parent 2c915e1 commit 04d5e64

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

py/objarray.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,9 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
336336
self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free));
337337
mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz);
338338
}
339-
mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
339+
mp_binary_set_val_array(self->typecode, self->items, self->len, arg);
340+
// only update length/free if set succeeded
341+
self->len++;
340342
self->free--;
341343
return mp_const_none; // return None, as per CPython
342344
}

tests/basics/bytearray_append.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# test bytearray.append method
2+
3+
a = bytearray(4)
4+
print(a)
5+
6+
# append should append a single byte
7+
a.append(2)
8+
print(a)
9+
10+
# a should not be modified if append fails
11+
try:
12+
a.append(None)
13+
except TypeError:
14+
print('TypeError')
15+
print(a)

0 commit comments

Comments
 (0)
0