py/objarray: Fix array.append so it doesn't extend if append fails.

Addresses issue #1965.
This commit is contained in:
Damien George 2016-04-07 09:03:33 +01:00
parent 2c915e1ae6
commit 04d5e644fc
2 changed files with 18 additions and 1 deletions

View File

@ -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
}

View File

@ -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)