Skip to content

Commit

Permalink
EmbedderCache - Log loading/saving errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Oct 30, 2023
1 parent dde4638 commit 7b2ef20
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Orange/misc/tests/test_embedder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 13 additions & 3 deletions Orange/misc/utils/embedder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7b2ef20

Please sign in to comment.