Skip to content

Commit eea9fd0

Browse files
authored
Fix Fiddle::Handle.new for a missing library in the FFI backend (#156)
* From ruby/reline#766 (comment)
1 parent 1f818e4 commit eea9fd0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/fiddle/ffi_backend.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,11 @@ class Handle
475475
RTLD_NOW = FFI::DynamicLibrary::RTLD_NOW
476476

477477
def initialize(libname = nil, flags = RTLD_LAZY | RTLD_GLOBAL)
478-
@lib = FFI::DynamicLibrary.open(libname, flags) rescue LoadError
479-
raise DLError.new("Could not open #{libname}") unless @lib
478+
begin
479+
@lib = FFI::DynamicLibrary.open(libname, flags)
480+
rescue LoadError, RuntimeError # LoadError for JRuby, RuntimeError for TruffleRuby
481+
raise DLError, "Could not open #{libname}"
482+
end
480483

481484
@open = true
482485

test/fiddle/test_handle.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ module Fiddle
88
class TestHandle < TestCase
99
include Fiddle
1010

11+
def test_library_unavailable
12+
assert_raise(DLError) do
13+
Fiddle::Handle.new("does-not-exist-library")
14+
end
15+
assert_raise(DLError) do
16+
Fiddle::Handle.new("/does/not/exist/library.#{RbConfig::CONFIG['SOEXT']}")
17+
end
18+
end
19+
1120
def test_to_i
1221
if ffi_backend?
1322
omit("Fiddle::Handle#to_i is unavailable with FFI backend")

0 commit comments

Comments
 (0)