From f5dc4cb71e29c89cc35eb03c6ad2a312776722b4 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 27 Mar 2024 18:13:21 +0100 Subject: [PATCH] more resilience --- chatmaild/src/chatmaild/filedict.py | 3 +++ chatmaild/src/chatmaild/tests/test_filedict.py | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/chatmaild/src/chatmaild/filedict.py b/chatmaild/src/chatmaild/filedict.py index bfcf42e..006fa73 100644 --- a/chatmaild/src/chatmaild/filedict.py +++ b/chatmaild/src/chatmaild/filedict.py @@ -35,3 +35,6 @@ class FileDict: return marshal.load(f) except FileNotFoundError: return {} + except Exception: + logging.warning("corrupt marshal data at: %r", self.path) + return {} diff --git a/chatmaild/src/chatmaild/tests/test_filedict.py b/chatmaild/src/chatmaild/tests/test_filedict.py index 0f58534..81802cd 100644 --- a/chatmaild/src/chatmaild/tests/test_filedict.py +++ b/chatmaild/src/chatmaild/tests/test_filedict.py @@ -22,3 +22,10 @@ def test_dying_lock(tmp_path, caplog): d["1"] = "3" assert fdict1.read()["1"] == "3" assert fdict2.read()["1"] == "3" + + +def test_bad_marshal_file(tmp_path, caplog): + fdict1 = FileDict(tmp_path.joinpath("metadata")) + fdict1.path.write_bytes(b"l12k3l12k3l") + assert fdict1.read() == {} + assert "corrupt" in caplog.records[0].msg