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
This commit is contained in:
2026-06-29 14:03:03 -04:00
parent 08b864d31b
commit 96f61665bf
2 changed files with 13 additions and 7 deletions
+12
View File
@@ -95,8 +95,20 @@ def test_create_rejects_too_small_stack():
raise AssertionError(f"expected ValueError for stackSize={stack_size}") 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__": if __name__ == "__main__":
test_object_keys_routed_correctly() test_object_keys_routed_correctly()
test_mixed_values() test_mixed_values()
test_create_rejects_too_small_stack() test_create_rejects_too_small_stack()
test_missing_library_raises_oserror()
print("python bindings ok") print("python bindings ok")
+1 -7
View File
@@ -84,13 +84,7 @@ class WeaselJsonParser:
pass pass
if self._lib is None: if self._lib is None:
import sys raise OSError(f"Could not load libweaseljson from {build_dir}")
print(
"Could not find libweaseljson implementation",
file=sys.stderr,
)
sys.exit(1)
self._lib.WeaselJsonParser_create.argtypes = ( self._lib.WeaselJsonParser_create.argtypes = (
ctypes.c_int, ctypes.c_int,