From 30d40f20dd19a44b7c54193db59980d94d5517dd Mon Sep 17 00:00:00 2001 From: shum Date: Fri, 25 Sep 2026 12:51:14 +0000 Subject: [PATCH] python: match backend switch error text --- .../src/simplex_chat/_native.py | 4 ++-- .../tests/test_native_cache.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/simplex-chat-python/src/simplex_chat/_native.py b/packages/simplex-chat-python/src/simplex_chat/_native.py index 009ff9a1e0..6918026cd3 100644 --- a/packages/simplex-chat-python/src/simplex_chat/_native.py +++ b/packages/simplex-chat-python/src/simplex_chat/_native.py @@ -230,8 +230,8 @@ def lib_for(backend: Backend) -> ctypes.CDLL: if _lib is not None: if _backend != backend: raise RuntimeError( - f"libsimplex already loaded with backend={_backend!r}; " - f"cannot switch to {backend!r} in the same process" + f"libsimplex already loaded with backend={_backend}; " + f"cannot switch to {backend} in the same process" ) return _lib libs_dir = _resolve_libs_dir(backend) diff --git a/packages/simplex-chat-python/tests/test_native_cache.py b/packages/simplex-chat-python/tests/test_native_cache.py index 6c4f2a923d..5bd68ddfcd 100644 --- a/packages/simplex-chat-python/tests/test_native_cache.py +++ b/packages/simplex-chat-python/tests/test_native_cache.py @@ -3,7 +3,8 @@ from pathlib import Path import pytest -from simplex_chat._native import _cache_root, _download, _resolve_libs_dir +from simplex_chat import _native +from simplex_chat._native import _cache_root, _download, _resolve_libs_dir, lib_for from simplex_chat._version import LIBS_VERSION @@ -97,7 +98,18 @@ def test_libc_on_windows_is_ucrt(monkeypatch): loaded: list[str | None] = [] monkeypatch.setattr("sys.platform", "win32") monkeypatch.setattr("ctypes.CDLL", lambda name: loaded.append(name)) - from simplex_chat import _native - _native._load_libc() assert loaded == ["ucrtbase"] + + +def test_lib_for_rejects_backend_switch(monkeypatch): + monkeypatch.setattr(_native, "_lib", object()) + monkeypatch.setattr(_native, "_backend", "sqlite") + monkeypatch.setattr( + _native, "_resolve_libs_dir", lambda _: pytest.fail("must not resolve libs") + ) + with pytest.raises(RuntimeError) as exc: + lib_for("postgres") + assert str(exc.value) == ( + "libsimplex already loaded with backend=sqlite; cannot switch to postgres in the same process" + )