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..73e423c040d 100644 --- a/Orange/misc/utils/embedder_utils.py +++ b/Orange/misc/utils/embedder_utils.py @@ -47,15 +47,25 @@ def save_pickle(obj, file_name): with open(file_name, 'wb') as f: pickle.dump(obj, f) except PermissionError: - # do not save cache if no right permission - pass + # skip saving cache if no right permissions + log.warning( + "Embedding cache cannot be saved to %s due to permission error.", + file_name, + ) @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 + errors = {EOFError: "end of file", PermissionError: "permission"} + log.warning( + "Embedding cache cannot be loaded from %s due to %s error.", + file_name, + errors[type(ex)], + ) return {} @staticmethod