Skip to content

Commit

Permalink
bugfix: fix __getattr__() for KeyMapDB
Browse files Browse the repository at this point in the history
- the reference to `self._db` inside `__getattr__` was resulting in a
segfault
  • Loading branch information
charles-cooper authored and fselmo committed Apr 5, 2024
1 parent fb28fa4 commit 13b423a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion eth/db/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __contains__(self, key: bytes) -> bool: # type: ignore # Breaks LSP
return mapped_key in self._db

def __getattr__(self, attr: Any) -> Any:
return getattr(self._db, attr)
return getattr(super().__getattribute__("_db"), attr)

def __setattr__(self, attr: Any, val: Any) -> None:
if attr in ("_db", "keymap"):
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2116.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``__getattr__()`` for ``KeyMapDB`` and thus allow it to be copied / deep copied.
22 changes: 22 additions & 0 deletions tests/core/db/test_keymap_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import copy

from eth.db.backends.memory import (
MemoryDB,
)
from eth.db.hash_trie import (
HashTrie,
)


def test_keymap_db_can_be_copied_and_deep_copied():
hash_trie = HashTrie(MemoryDB({b"a": b"1", b"b": b"2"}))

copied_hash_trie = copy.copy(hash_trie)
deep_copied_hash_trie = copy.deepcopy(hash_trie)

assert hash_trie._db == copied_hash_trie._db
assert hash_trie._db == deep_copied_hash_trie._db

hash_trie[b"c"] = b"3"
assert b"c" in hash_trie
assert b"c" not in deep_copied_hash_trie

0 comments on commit 13b423a

Please sign in to comment.