diff --git a/lib/fiddle/ffi_backend.rb b/lib/fiddle/ffi_backend.rb index 0c7c3797..c8804629 100644 --- a/lib/fiddle/ffi_backend.rb +++ b/lib/fiddle/ffi_backend.rb @@ -475,8 +475,11 @@ class Handle RTLD_NOW = FFI::DynamicLibrary::RTLD_NOW def initialize(libname = nil, flags = RTLD_LAZY | RTLD_GLOBAL) - @lib = FFI::DynamicLibrary.open(libname, flags) rescue LoadError - raise DLError.new("Could not open #{libname}") unless @lib + begin + @lib = FFI::DynamicLibrary.open(libname, flags) + rescue + raise DLError, "Could not open #{libname}" + end @open = true diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index e0ad17a9..ad8c45a0 100644 --- a/test/fiddle/test_handle.rb +++ b/test/fiddle/test_handle.rb @@ -8,6 +8,15 @@ module Fiddle class TestHandle < TestCase include Fiddle + def test_library_unavailable + assert_raise(DLError) do + Fiddle::Handle.new("does-not-exist-library") + end + assert_raise(DLError) do + Fiddle::Handle.new("/does/not/exist/library.#{RbConfig::CONFIG['SOEXT']}") + end + end + def test_to_i if ffi_backend? omit("Fiddle::Handle#to_i is unavailable with FFI backend")