Skip to content

Commit 76dfee9

Browse files
committed
[GR-18163] Add spec and fix exception for Fiddle::Handle.new with a missing library (#2714)
PullRequest: truffleruby/3477
2 parents 621db11 + 3fd4d3a commit 76dfee9

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Compatibility:
3434
* Fix `rb_class2name` to call `inspect` on anonymous classes like in CRuby (#2701, @aardvark179).
3535
* Implement `rb_ivar_foreach` to iterate over instance and class variables like in CRuby (#2701, @aardvark179).
3636
* Fix the absolute path of the main script after chdir (#2709, @eregon).
37+
* Fix exception for `Fiddle::Handle.new` with a missing library (#2714, @eregon).
3738

3839
Performance:
3940

lib/truffle/truffle/fiddle_backend.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,15 +253,16 @@ def self.[](*args)
253253
RTLD_GLOBAL = Truffle::Config['platform.dlopen.RTLD_GLOBAL']
254254

255255
def initialize(library = nil, flags = RTLD_LAZY | RTLD_GLOBAL)
256-
raise DLError, 'unsupported dlopen flags' if flags != RTLD_LAZY | RTLD_GLOBAL
256+
raise DLError, 'unsupported dlopen flags' if flags != (RTLD_LAZY | RTLD_GLOBAL)
257+
257258
if library == Truffle::FiddleBackend::RTLD_NEXT
258259
@handle = :rtld_next
259260
else
260261
library = nil if library == Truffle::FiddleBackend::RTLD_DEFAULT
261262
begin
262263
@handle = Primitive.interop_eval_nfi(library ? "load '#{library}'" : 'default')
263-
rescue RuntimeError
264-
raise DLError, "#{library}: cannot open shared object file: No such file or directory"
264+
rescue Polyglot::ForeignException => e
265+
raise DLError, "#{e.message}"
265266
end
266267
end
267268
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require_relative '../../../spec_helper'
2+
require 'fiddle'
3+
4+
describe "Fiddle::Handle#initialize" do
5+
it "raises Fiddle::DLError if the library cannot be found" do
6+
-> {
7+
Fiddle::Handle.new("doesnotexist.doesnotexist")
8+
}.should raise_error(Fiddle::DLError)
9+
end
10+
end

spec/ruby/spec_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def report_on_exception=(value)
2727
end
2828
end
2929

30-
ruby_version_is ""..."2.7" do
30+
# Compare with SpecVersion directly here so it works even with --unguarded
31+
if VersionGuard::FULL_RUBY_VERSION < SpecVersion.new('2.7')
3132
abort "This version of ruby/spec requires Ruby 2.7+"
3233
end
3334

test/mri/excludes/TestFiddle.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)