Skip to content

Commit 7cc1fee

Browse files
Add test_env.py and test_writer_error.py for error handling patch (#7980)
1 parent 69f3dd2 commit 7cc1fee

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

test_env.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import torch, monai
2+
print("Torch version:", torch.__version__)
3+
print("MONAI version:", monai.__version__)

test_writer_error.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from monai.data.image_writer import resolve_writer, OptionalImportError, SUPPORTED_WRITERS, EXT_WILDCARD, ITKWriter
2+
3+
ext = "fakeext"
4+
5+
print(f"Before clearing fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}")
6+
7+
# Temporarily clear fallback writers to simulate no support
8+
SUPPORTED_WRITERS[EXT_WILDCARD] = ()
9+
10+
try:
11+
writers = resolve_writer(ext, error_if_not_found=True)
12+
except OptionalImportError as e:
13+
print("Caught OptionalImportError:", e)
14+
finally:
15+
# Restore the fallback writers to avoid side effects
16+
SUPPORTED_WRITERS[EXT_WILDCARD] = (ITKWriter,)
17+
18+
print(f"After restoring fallback writers: {SUPPORTED_WRITERS.get(EXT_WILDCARD)}")

0 commit comments

Comments
 (0)