From 965285d49dd90d158c07a4d0bef3877a9af751b8 Mon Sep 17 00:00:00 2001 From: CLRT <40119568+CL4R3T@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:14:01 +0800 Subject: [PATCH 1/3] fix: persist personal records immediately after a win --- src/mineSweeperGUI.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/mineSweeperGUI.py b/src/mineSweeperGUI.py index 1ae7746f..35e37b2e 100644 --- a/src/mineSweeperGUI.py +++ b/src/mineSweeperGUI.py @@ -1107,6 +1107,7 @@ def try_record_pop(self): del_items += [13, 14, 15] if len(del_items) < 9: + self.record_setting.sync() ui = gameRecordPop.ui_Form( del_items, b.bbbv, nf_items, self.mainWindow) ui.label_16.setText(mode_text) From 8a254d494c57c9465005b5eaadea873dd79e49bb Mon Sep 17 00:00:00 2001 From: CLRT <40119568+CL4R3T@users.noreply.github.com> Date: Thu, 26 Nov 2026 06:19:08 +0800 Subject: [PATCH 2/3] fix: preserve existing INI records when saving fails --- src/superGUI.py | 16 ++++++++++++++-- tests/test_ini_sync.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/test_ini_sync.py diff --git a/src/superGUI.py b/src/superGUI.py index 3a4d88d9..c8a5ed70 100644 --- a/src/superGUI.py +++ b/src/superGUI.py @@ -5,6 +5,7 @@ from PySide6.QtWidgets import QApplication import configparser import shutil +import tempfile from PySide6.QtGui import QPalette, QPixmap, QIcon from ui.ui_main_board import Ui_MainWindow from pathlib import Path @@ -176,8 +177,19 @@ def sync(self): """ 将内存中的配置写入文件 """ - with open(self.file_path, 'w', encoding="utf-8") as f: - self.config.write(f) + target = Path(self.file_path) + temp_file = tempfile.NamedTemporaryFile( + mode="w", encoding="utf-8", dir=target.parent, + prefix=f".{target.name}.", suffix=".tmp", delete=False, + ) + try: + with temp_file as stream: + self.config.write(stream) + stream.flush() + os.fsync(stream.fileno()) + os.replace(temp_file.name, target) + finally: + Path(temp_file.name).unlink(missing_ok=True) class Ui_MainWindow(Ui_MainWindow): diff --git a/tests/test_ini_sync.py b/tests/test_ini_sync.py new file mode 100644 index 00000000..5e543ebc --- /dev/null +++ b/tests/test_ini_sync.py @@ -0,0 +1,34 @@ +import os +from pathlib import Path +from tempfile import TemporaryDirectory +from unittest import TestCase +from unittest.mock import patch + +from superGUI import IniConfig + + +class TestIniSync(TestCase): + def test_failed_save_preserves_records_and_can_be_retried(self): + def fail_mid_write(stream): + stream.write("[B]\n") + raise OSError("partial write") + + with TemporaryDirectory() as directory: + path = Path(directory) / "record.ini" + original = b"[B]\nrtime = 10\n" + path.write_bytes(original) + records = IniConfig(path) + records.set_value("B/rtime", 9) + + for failure in ( + patch.object(records.config, "write", side_effect=fail_mid_write), + patch.object(os, "fsync", side_effect=OSError("flush failed")), + patch.object(os, "replace", side_effect=PermissionError("file in use")), + ): + with failure, self.assertRaises(OSError): + records.sync() + self.assertEqual(path.read_bytes(), original) + self.assertEqual(list(path.parent.iterdir()), [path]) + + records.sync() + self.assertEqual(IniConfig(path).value("B/rtime", value_type=int), 9) From 2656ebf2ba01cbb62202437575e34c4560a67481 Mon Sep 17 00:00:00 2001 From: Claret Date: Thu, 26 Nov 2026 16:43:54 +0800 Subject: [PATCH 3/3] fix: show record popup even when saving fails --- src/mineSweeperGUI.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/mineSweeperGUI.py b/src/mineSweeperGUI.py index 35e37b2e..0b7d384b 100644 --- a/src/mineSweeperGUI.py +++ b/src/mineSweeperGUI.py @@ -21,6 +21,7 @@ from dialogs.CheckUpdateGui import CheckUpdateGui from network.githubApi import GitHub, SourceManager import utils +from utils.app_logger import logger import ms_toollib as ms import os import hashlib @@ -1107,7 +1108,10 @@ def try_record_pop(self): del_items += [13, 14, 15] if len(del_items) < 9: - self.record_setting.sync() + try: + self.record_setting.sync() + except OSError as exc: + logger.warning("保存 record.ini 失败,新纪录仍保留在内存中:{}", exc) ui = gameRecordPop.ui_Form( del_items, b.bbbv, nf_items, self.mainWindow) ui.label_16.setText(mode_text)