Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EmbedderCache - Log loading/saving errors #6615

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading