Skip to content

Commit 57fce3b

Browse files
jimmodpgeorge
authored andcommitted
py/objdict: Fix popitem for ordered dicts.
The popitem method wasn't implemented for ordered dicts and would result in an invalid state. Fixes issue adafruit#5956.
1 parent 347c891 commit 57fce3b

4 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎py/map.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t
177177
--map->used;
178178
memmove(elem, elem + 1, (top - elem - 1) * sizeof(*elem));
179179
// put the found element after the end so the caller can access it if needed
180+
// note: caller must NULL the value so the GC can clean up (e.g. see dict_get_helper).
180181
elem = &map->table[map->used];
181182
elem->key = MP_OBJ_NULL;
182183
elem->value = value;

‎py/obj.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#ifndef MICROPY_INCLUDED_PY_OBJ_H
2727
#define MICROPY_INCLUDED_PY_OBJ_H
2828

29+
#include <assert.h>
30+
2931
#include "py/mpconfig.h"
3032
#include "py/misc.h"
3133
#include "py/qstr.h"
@@ -423,6 +425,7 @@ typedef enum _mp_map_lookup_kind_t {
423425
extern const mp_map_t mp_const_empty_map;
424426

425427
static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) {
428+
assert(pos < map->alloc);
426429
return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL;
427430
}
428431

‎py/objdict.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
4444
size_t max = dict->map.alloc;
4545
mp_map_t *map = &dict->map;
4646

47-
for (size_t i = *cur; i < max; i++) {
47+
size_t i = *cur;
48+
for (; i < max; i++) {
4849
if (mp_map_slot_is_filled(map, i)) {
4950
*cur = i + 1;
5051
return &(map->table[i]);
5152
}
5253
}
5354

55+
assert(map->used == 0 || i == max);
5456
return NULL;
5557
}
5658

@@ -321,11 +323,17 @@ STATIC mp_obj_t dict_popitem(mp_obj_t self_in) {
321323
mp_check_self(mp_obj_is_dict_type(self_in));
322324
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
323325
mp_ensure_not_fixed(self);
324-
size_t cur = 0;
325-
mp_map_elem_t *next = dict_iter_next(self, &cur);
326-
if (next == NULL) {
326+
if (self->map.used == 0) {
327327
mp_raise_msg(&mp_type_KeyError, MP_ERROR_TEXT("popitem(): dictionary is empty"));
328328
}
329+
size_t cur = 0;
330+
#if MICROPY_PY_COLLECTIONS_ORDEREDDICT
331+
if (self->map.is_ordered) {
332+
cur = self->map.used - 1;
333+
}
334+
#endif
335+
mp_map_elem_t *next = dict_iter_next(self, &cur);
336+
assert(next);
329337
self->map.used--;
330338
mp_obj_t items[] = {next->key, next->value};
331339
next->key = MP_OBJ_SENTINEL; // must mark key as sentinel to indicate that it was deleted

‎tests/basics/ordereddict1.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@
2424
print(len(d))
2525
print(list(d.keys()))
2626
print(list(d.values()))
27+
28+
# pop an element
29+
print(d.popitem())
30+
print(len(d))
31+
print(list(d.keys()))
32+
print(list(d.values()))
33+
34+
# add an element after popping
35+
d["xyz"] = 321
36+
print(len(d))
37+
print(list(d.keys()))
38+
print(list(d.values()))
39+
40+
# pop until empty
41+
print(d.popitem())
42+
print(d.popitem())
43+
try:
44+
d.popitem()
45+
except:
46+
print('empty')

0 commit comments

Comments
 (0)