diff --git a/ext/fiddle/lib/fiddle/ffi_backend.rb b/ext/fiddle/lib/fiddle/ffi_backend.rb index 0c7c379777fad6..eac469aff694dd 100644 --- a/ext/fiddle/lib/fiddle/ffi_backend.rb +++ b/ext/fiddle/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 LoadError, RuntimeError # LoadError for JRuby, RuntimeError for TruffleRuby + raise DLError, "Could not open #{libname}" + end @open = true diff --git a/test/fiddle/test_handle.rb b/test/fiddle/test_handle.rb index e0ad17a9cb41fc..ad8c45a00a64d9 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")