Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

persistent_dict: make siphash24 optional #250

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 8 additions & 6 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@
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 +167,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 +435,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 +511,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 +535,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 +574,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
Loading