From 04d5e644fcf4e0171383403e69d8fff429bba425 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 7 Apr 2016 09:03:33 +0100 Subject: [PATCH] py/objarray: Fix array.append so it doesn't extend if append fails. Addresses issue #1965. --- py/objarray.c | 4 +++- tests/basics/bytearray_append.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/basics/bytearray_append.py diff --git a/py/objarray.c b/py/objarray.c index da9dd528c5..bafba7623f 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -336,7 +336,9 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) { self->items = m_renew(byte, self->items, item_sz * self->len, item_sz * (self->len + self->free)); mp_seq_clear(self->items, self->len + 1, self->len + self->free, item_sz); } - mp_binary_set_val_array(self->typecode, self->items, self->len++, arg); + mp_binary_set_val_array(self->typecode, self->items, self->len, arg); + // only update length/free if set succeeded + self->len++; self->free--; return mp_const_none; // return None, as per CPython } diff --git a/tests/basics/bytearray_append.py b/tests/basics/bytearray_append.py new file mode 100644 index 0000000000..06e62c6bcb --- /dev/null +++ b/tests/basics/bytearray_append.py @@ -0,0 +1,15 @@ +# test bytearray.append method + +a = bytearray(4) +print(a) + +# append should append a single byte +a.append(2) +print(a) + +# a should not be modified if append fails +try: + a.append(None) +except TypeError: + print('TypeError') +print(a)