From 0532ec4d0c3c35f44cd99f94e16549c44ab5c3ff Mon Sep 17 00:00:00 2001 From: PrimozGodec Date: Mon, 30 Oct 2023 14:30:03 +0100 Subject: [PATCH] EmbedderCache - Log loading/saving errors --- Orange/misc/tests/test_embedder_utils.py | 14 ++++++++++++++ Orange/misc/utils/embedder_utils.py | 20 ++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Orange/misc/tests/test_embedder_utils.py b/Orange/misc/tests/test_embedder_utils.py index 6127292997a..d10aab747e4 100644 --- a/Orange/misc/tests/test_embedder_utils.py +++ b/Orange/misc/tests/test_embedder_utils.py @@ -130,6 +130,20 @@ def test_load_cache_no_permission(self): cache = EmbedderCache("TestModel") self.assertDictEqual({}, cache._cache_dict) + def test_load_cache_eof_error(self): + # prepare a file + cache = EmbedderCache("TestModel") + self.assertDictEqual({}, cache._cache_dict) + cache.add("abc", [1, 2, 3]) + cache.persist_cache() + + # eof error + with patch( + "Orange.misc.utils.embedder_utils.pickle.load", side_effect=EOFError, + ): + cache = EmbedderCache("TestModel") + self.assertDictEqual({}, cache._cache_dict) + if __name__ == "__main__": unittest.main() diff --git a/Orange/misc/utils/embedder_utils.py b/Orange/misc/utils/embedder_utils.py index ef1e4c5e36e..23594bd3e19 100644 --- a/Orange/misc/utils/embedder_utils.py +++ b/Orange/misc/utils/embedder_utils.py @@ -46,16 +46,28 @@ def save_pickle(obj, file_name): try: with open(file_name, 'wb') as f: pickle.dump(obj, f) - except PermissionError: - # do not save cache if no right permission - pass + except PermissionError as ex: + # skip saving cache if no right permissions + log.warning( + "Can't save embedding to %s due to %s.", + file_name, + type(ex).__name__, + exc_info=True, + ) @staticmethod def load_pickle(file_name): try: with open(file_name, 'rb') as f: return pickle.load(f) - except (EOFError, PermissionError): + except (EOFError, PermissionError) as ex: + # load empty cache if no permission or EOF error + log.warning( + "Can't load embedding from %s due to %s.", + file_name, + type(ex).__name__, + exc_info=True, + ) return {} @staticmethod