Skip to content

Commit

Permalink
fix: handle case of bad file read (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 authored Oct 22, 2022
1 parent fc30b68 commit b08ee42
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions magicgui/widgets/_bases/container_widget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import inspect
from typing import TYPE_CHECKING, Any, Callable, MutableSequence, Sequence, overload

Expand Down Expand Up @@ -318,16 +319,24 @@ def _load(self, path, quiet=False):
from pathlib import Path

path = Path(path)
if not path.exists() and quiet:
return
for key, val in pickle.loads(path.read_bytes()).items():
if not path.exists():
if quiet:
return
raise FileNotFoundError(f"Widget state file does not exist: {path}")

try:
data: dict = pickle.loads(path.read_bytes())
except Exception:
if quiet:
path.unlink(missing_ok=True)
return
raise

for key, val in data.items():
val = pickle.loads(val)
if val == self.NO_VALUE:
continue
try:
getattr(self, key).value = val
except (ValueError, AttributeError):
pass
if val != self.NO_VALUE:
with contextlib.suppress(ValueError, AttributeError):
getattr(self, key).value = val


class MainWindowWidget(ContainerWidget):
Expand Down

0 comments on commit b08ee42

Please sign in to comment.