Refactor imghdr compatibility hack to a dedicated compat module

Moved the imghdr compatibility shim from `src/seedpass/core/password_generation.py`
to a new `src/seedpass/core/compat.py` module. This improves code
cleanliness and separates concerns by centralizing Python version
compatibility logic.

The shim ensures that `imghdr` is available in `sys.modules` (using
`utils.imghdr_stub` as a fallback for Python 3.13+) so that dependencies
like `pgpy` can function correctly.

Verified the change by:
- Confirming `compat.py` correctly populates `sys.modules["imghdr"]`.
- Verifying `password_generation.py` imports correctly and triggers the shim.
- Ensuring syntax correctness via `py_compile`.

Co-authored-by: PR0M3TH3AN <[email protected]>
This commit is contained in:
google-labs-jules[bot]andPR0M3TH3AN committed 2026-02-26 17:23:37 +00:00
1 parent 88781479aa
commit f9023e8f5f
2 files changed
+12 -9

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
"""Compatibility layer for different Python versions."""
import sys
# Ensure the ``imghdr`` module is available for ``pgpy`` on Python 3.13+
try: # pragma: no cover - only executed on Python >= 3.13
import imghdr # type: ignore
except ModuleNotFoundError: # pragma: no cover - fallback for removed module
from utils import imghdr_stub
sys.modules.setdefault("imghdr", imghdr_stub)
+1 -9
View File
@@ -35,15 +35,7 @@ from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.backends import default_backend
from bip_utils import Bip39SeedGenerator
# Ensure the ``imghdr`` module is available for ``pgpy`` on Python 3.13+
try: # pragma: no cover - only executed on Python >= 3.13
import imghdr # type: ignore
except ModuleNotFoundError: # pragma: no cover - fallback for removed module
from utils import imghdr_stub as imghdr # type: ignore
import sys
sys.modules.setdefault("imghdr", imghdr)
from . import compat # noqa: F401
from local_bip85.bip85 import BIP85
from constants import (