py/objarray: Fix array.append so it doesn't extend if append fails.
Addresses issue #1965.
This commit is contained in:
parent
2c915e1ae6
commit
04d5e644fc
@ -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
|
||||
}
|
||||
|
15
tests/basics/bytearray_append.py
Normal file
15
tests/basics/bytearray_append.py
Normal 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)
|
Loading…
Reference in New Issue
Block a user