Skip to content

Commit 9075529

Browse files
authored
Raise save errors (#615)
* 🐛 catch exceptions Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> * Add a test Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> --------- Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com>
1 parent edc4245 commit 9075529

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

pyiron_workflow/storage.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,16 @@ def _save(
231231
if self._fallback(cloudpickle_fallback):
232232
attacks += [(self._CLOUDPICKLE, cloudpickle.dump)]
233233

234-
e = None
234+
e: Exception | None = None
235235
for suffix, save_method in attacks:
236+
e = None
236237
p = filename.with_suffix(suffix)
237238
try:
238239
with open(p, "wb") as filehandle:
239240
save_method(node, filehandle)
240241
return
241-
except Exception:
242+
except Exception as ee:
243+
e = ee
242244
p.unlink(missing_ok=True)
243245
if e is not None:
244246
raise e

tests/unit/test_storage.py

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from pathlib import Path
33
from tempfile import TemporaryDirectory
44

5+
import cloudpickle
6+
from pint import UnitRegistry
7+
58
from pyiron_workflow.nodes.function import as_function_node
69
from pyiron_workflow.nodes.standard import UserInput
710
from pyiron_workflow.storage import PickleStorage, TypeNotFoundError, available_backends
@@ -134,6 +137,21 @@ def Unimportable(x):
134137
finally:
135138
interface.delete(node=u, cloudpickle_fallback=True)
136139

140+
def test_uncloudpickleable(self):
141+
ureg = UnitRegistry()
142+
with self.assertRaises(
143+
TypeError,
144+
msg="Sanity check that this can't even be cloudpickled"
145+
):
146+
cloudpickle.dumps(ureg)
147+
148+
interface = PickleStorage(cloudpickle_fallback=True)
149+
n = UserInput(ureg, label="uncloudpicklable_node")
150+
with self.assertRaises(
151+
TypeError,
152+
msg="Exception should be caught and saving should fail"
153+
):
154+
interface.save(n)
137155

138156
if __name__ == "__main__":
139157
unittest.main()

0 commit comments

Comments
 (0)