From 96f61665bf7e6fd396e7ee55fb9c6b8cc8956feb Mon Sep 17 00:00:00 2001 From: Weaselbot Date: Mon, 29 Jun 2026 14:03:03 -0400 Subject: [PATCH] python: raise OSError when shared library is missing Replace sys.exit(1) in WeaselJsonParser.__init__ with an OSError so callers can handle a missing libweaseljson gracefully. Also add a test that verifies the constructor raises OSError for a non-existent build directory. Closes #38 --- test_python_bindings.py | 12 ++++++++++++ weaseljson.py | 8 +------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/test_python_bindings.py b/test_python_bindings.py index 48f01e0..44cc30c 100644 --- a/test_python_bindings.py +++ b/test_python_bindings.py @@ -95,8 +95,20 @@ def test_create_rejects_too_small_stack(): raise AssertionError(f"expected ValueError for stackSize={stack_size}") +def test_missing_library_raises_oserror(): + try: + weaseljson.WeaselJsonParser( + weaseljson.WeaselJsonCallbacksBase(), + build_dir="/nonexistent", + ) + except OSError: + return + raise AssertionError("expected OSError when the shared library is missing") + + if __name__ == "__main__": test_object_keys_routed_correctly() test_mixed_values() test_create_rejects_too_small_stack() + test_missing_library_raises_oserror() print("python bindings ok") diff --git a/weaseljson.py b/weaseljson.py index b6c20d6..7063c98 100644 --- a/weaseljson.py +++ b/weaseljson.py @@ -84,13 +84,7 @@ class WeaselJsonParser: pass if self._lib is None: - import sys - - print( - "Could not find libweaseljson implementation", - file=sys.stderr, - ) - sys.exit(1) + raise OSError(f"Could not load libweaseljson from {build_dir}") self._lib.WeaselJsonParser_create.argtypes = ( ctypes.c_int,