From 9cd295dbf4e082f46601d74c6e552532bd2af663 Mon Sep 17 00:00:00 2001 From: Kiven <99519599+abaichuan1017@users.noreply.github.com> Date: Mon, 26 Oct 2026 00:15:39 +0800 Subject: [PATCH] fix: atomically replace pronunciation overrides --- abogen/pronunciation_store.py | 22 ++++++--- tests/test_pronunciation_store.py | 74 +++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 tests/test_pronunciation_store.py diff --git a/abogen/pronunciation_store.py b/abogen/pronunciation_store.py index 5a5e7fb..d364812 100644 --- a/abogen/pronunciation_store.py +++ b/abogen/pronunciation_store.py @@ -1,8 +1,9 @@ from __future__ import annotations import json +import os import sqlite3 -import shutil +import tempfile import threading import time import uuid @@ -101,11 +102,20 @@ def _load_db() -> Dict[str, Any]: def _save_db(data: Dict[str, Any]) -> None: path = _store_path() - # Atomic write - temp_path = path.with_suffix(".tmp") - with open(temp_path, "w", encoding="utf-8") as f: - json.dump(data, f, indent=2, ensure_ascii=False) - shutil.move(str(temp_path), str(path)) + temp_path = None + try: + # Keep the replacement on the same filesystem and close it before + # replacing the live file, including on Windows. + with tempfile.NamedTemporaryFile( + mode="w", encoding="utf-8", dir=path.parent, + prefix=f".{path.name}.", suffix=".tmp", delete=False, + ) as f: + temp_path = Path(f.name) + json.dump(data, f, indent=2, ensure_ascii=False) + os.replace(temp_path, path) + finally: + if temp_path is not None: + temp_path.unlink(missing_ok=True) def load_overrides(language: str, tokens: Iterable[str]) -> Dict[str, Dict[str, Any]]: diff --git a/tests/test_pronunciation_store.py b/tests/test_pronunciation_store.py new file mode 100644 index 0000000..c8908c9 --- /dev/null +++ b/tests/test_pronunciation_store.py @@ -0,0 +1,74 @@ +import json +import os +import shutil + +import pytest + +from abogen import pronunciation_store + + +@pytest.fixture +def store(tmp_path, monkeypatch): + path = tmp_path / "overrides.json" + monkeypatch.setattr(pronunciation_store, "_store_path", lambda: path) + return path + + +def test_save_replaces_existing_overrides_without_copying(store, monkeypatch): + store.write_text('{"old": true}', encoding="utf-8") + + def windows_rename(source, target): + raise FileExistsError("Windows rename cannot overwrite an existing file") + + def failed_copy(source, target, **kwargs): + # A copy fallback can truncate the live file before a failed write. + with open(target, "w", encoding="utf-8") as output: + output.write('{"partial":') + raise OSError("write failed") + + monkeypatch.setattr(os, "rename", windows_rename) + monkeypatch.setattr(shutil, "copyfile", failed_copy) + + pronunciation_store._save_db({"token": "你好"}) + + assert json.loads(store.read_text(encoding="utf-8")) == {"token": "你好"} + assert list(store.parent.iterdir()) == [store] + + +def test_override_round_trip_uses_the_replaced_store(store): + pronunciation_store.save_override(language="en", token="hello", pronunciation="hullo") + pronunciation_store.save_override(language="en", token="hello", pronunciation="hi") + entry = pronunciation_store.load_overrides("en", ["hello"])["hello"] + assert entry["pronunciation"] == "hi" + pronunciation_store.delete_override(language="en", token="hello") + assert pronunciation_store.load_overrides("en", ["hello"]) == {} + assert list(store.parent.iterdir()) == [store] + + +def test_failed_serialization_preserves_previous_overrides_and_cleans_up(store): + original = '{"old": true}' + store.write_text(original, encoding="utf-8") + + with pytest.raises(TypeError): + pronunciation_store._save_db({"invalid": object()}) + + assert store.read_text(encoding="utf-8") == original + assert list(store.parent.iterdir()) == [store] + + +def test_failed_replacement_preserves_previous_overrides_and_cleans_up(store, monkeypatch): + original = '{"old": true}' + store.write_text(original, encoding="utf-8") + + def locked(*args, **kwargs): + raise PermissionError("destination is locked") + + monkeypatch.setattr(os, "rename", locked) + monkeypatch.setattr(os, "replace", locked) + monkeypatch.setattr(shutil, "copyfile", locked) + + with pytest.raises(PermissionError): + pronunciation_store._save_db({"new": True}) + + assert store.read_text(encoding="utf-8") == original + assert list(store.parent.iterdir()) == [store]