Skip to content

Commit

Permalink
Merge pull request #6615 from PrimozGodec/fix-embeder-cache
Browse files Browse the repository at this point in the history
EmbedderCache - Log loading/saving errors
  • Loading branch information
VesnaT committed Nov 3, 2023
2 parents d120a72 + 0532ec4 commit 46a9b28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 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()
20 changes: 16 additions & 4 deletions Orange/misc/utils/embedder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 46a9b28

Please sign in to comment.