Skip to content

Commit

Permalink
persistent_dict: make siphash24 optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ahesford committed Jul 23, 2024
1 parent dbc9ac4 commit 70d215c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
python-version: '3.x'
- name: "Main Script"
run: |
EXTRA_INSTALL="numpy"
EXTRA_INSTALL="numpy siphash24"
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-mypy.sh
. ./prepare-and-run-mypy.sh python3 mypy
Expand All @@ -89,7 +89,7 @@ jobs:
# AK, 2020-12-13
rm pytools/mpiwrap.py
EXTRA_INSTALL="numpy frozendict immutabledict orderedsets constantdict immutables pyrsistent attrs"
EXTRA_INSTALL="numpy frozendict immutabledict orderedsets constantdict immutables pyrsistent attrs siphash24"
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project.sh
. ./build-and-test-py-project.sh
Expand All @@ -104,6 +104,7 @@ jobs:
python-version: '3.x'
- name: "Main Script"
run: |
EXTRA_INSTALL="siphash24"
rm pytools/{convergence,spatial_btree,obj_array,mpiwrap}.py
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project.sh
. ./build-and-test-py-project.sh
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ dependencies = [
"platformdirs>=2.2",
# for dataclass_transform with frozen_default
"typing-extensions>=4; python_version<'3.13'",
"siphash24>=1.6",
]

[project.optional-dependencies]
Expand All @@ -46,6 +45,9 @@ test = [
"pytest",
"ruff",
]
siphash = [
"siphash24>=1.6",
]

[project.urls]
Documentation = "https://documen.tician.de/pytools/"
Expand Down
15 changes: 8 additions & 7 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@
TypeVar,
cast,
)
from warnings import warn

from siphash24 import siphash13

try:
from siphash24 import siphash13 as _default_hash
except ImportError:
warn("pytools.persistent_dict: unable to import 'siphash24.siphash13', "
"falling back to hashlib.sha256", stacklevel=1)
from hashlib import sha256 as _default_hash

if TYPE_CHECKING:
from _typeshed import ReadableBuffer
Expand Down Expand Up @@ -161,7 +166,7 @@ class KeyBuilder:

# this exists so that we can (conceivably) switch algorithms at some point
# down the road
new_hash: Callable[..., Hash] = siphash13
new_hash: Callable[..., Hash] = _default_hash

def rec(self, key_hash: Hash, key: Any) -> Hash:
"""
Expand Down Expand Up @@ -429,7 +434,6 @@ class CollisionWarning(UserWarning):
def __getattr__(name: str) -> Any:
if name in ("NoSuchEntryInvalidKeyError",
"NoSuchEntryInvalidContentsError"):
from warnings import warn
warn(f"pytools.persistent_dict.{name} has been removed.", stacklevel=2)
return NoSuchEntryError

Expand Down Expand Up @@ -506,7 +510,6 @@ def __init__(self,
# https://www.sqlite.org/pragma.html#pragma_synchronous
if safe_sync is None or safe_sync:
if safe_sync is None:
from warnings import warn
warn(f"pytools.persistent_dict '{identifier}': "
"enabling safe_sync as default. "
"This provides strong protection against data loss, "
Expand All @@ -531,7 +534,6 @@ def __del__(self) -> None:
def _collision_check(self, key: K, stored_key: K) -> None:
if stored_key != key:
# Key collision, oh well.
from warnings import warn
warn(f"{self.identifier}: key collision in cache at "
f"'{self.container_dir}' -- these are sufficiently unlikely "
"that they're often indicative of a broken hash key "
Expand Down Expand Up @@ -571,7 +573,6 @@ def _exec_sql_fn(self, fn: Callable[[], T]) -> Optional[T]:
and not e.sqlite_errorcode == sqlite3.SQLITE_BUSY):
raise
if n % 20 == 0:
from warnings import warn
warn(f"PersistentDict: database '{self.filename}' busy, {n} "
"retries", stacklevel=3)
else:
Expand Down

0 comments on commit 70d215c

Please sign in to comment.