Skip to content

Commit

Permalink
Fix PyDictIter with deleted final item
Browse files Browse the repository at this point in the history
Fixes #285.

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk authored and ijl committed Jul 30, 2022
1 parent 1b45364 commit 8622aee
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/ffi/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ impl Iterator for PyDictIter {
Some((key, value))
} else {
let mut entry_ptr = self.indices_ptr.add(self.idx);
while self.idx < self.len && (*entry_ptr).me_value.is_null() {
entry_ptr = entry_ptr.add(1);
while self.idx < self.len {
self.idx += 1;
if !(*entry_ptr).me_value.is_null() {
return Some(((*entry_ptr).me_key, (*entry_ptr).me_value))
}
entry_ptr = entry_ptr.add(1);
}
self.idx += 1;
Some(((*entry_ptr).me_key, (*entry_ptr).me_value))
None
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ def test_dict_pop_replace_first(self):
"""Test pop and replace a first key in a dict with other keys."""
data = {"id": "any", "other": "any"}
data.pop("id")
assert orjson.dumps(data) == b'{"other":"any"}'
data["id"] = "new"
assert orjson.dumps(data) == b'{"other":"any","id":"new"}'

def test_dict_pop_replace_last(self):
"""Test pop and replace a last key in a dict with other keys."""
data = {"other": "any", "id": "any"}
data.pop("id")
assert orjson.dumps(data) == b'{"other":"any"}'
data["id"] = "new"
assert orjson.dumps(data) == b'{"other":"any","id":"new"}'

def test_dict_pop(self):
"""Test pop and replace a key in a dict with no other keys."""
data = {"id": "any"}
data.pop("id")
assert orjson.dumps(data) == b'{}'
data["id"] = "new"
assert orjson.dumps(data) == b'{"id":"new"}'

Expand Down

0 comments on commit 8622aee

Please sign in to comment.