Skip to content

Commit

Permalink
Support ruby 3.4 backtrace formatting change
Browse files Browse the repository at this point in the history
ruby 3.4 changes backtrace formatting, including
opening quote using single quote instead of backtick.

Make DBus::ProxyObject support both backtrace format.

Fixes #144 .
  • Loading branch information
mtasaka committed Dec 20, 2024
1 parent 5f98ee3 commit 1924e25
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/dbus/proxy_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ProxyObject
# @return [ApiOptions]
attr_reader :api

OPEN_QUOTE = RUBY_VERSION >= "3.4" ? "'" : "`"

# Creates a new proxy object living on the given _bus_ at destination _dest_
# on the given _path_.
def initialize(bus, dest, path, api: ApiOptions::CURRENT)
Expand All @@ -58,7 +60,7 @@ def interfaces
def [](intfname)
introspect unless introspected
ifc = @interfaces[intfname]
raise DBus::Error, "no such interface `#{intfname}' on object `#{@path}'" unless ifc
raise DBus::Error, "no such interface #{OPEN_QUOTE}#{intfname}' on object #{OPEN_QUOTE}#{@path}'" unless ifc

ifc
end
Expand Down Expand Up @@ -127,7 +129,7 @@ def has_iface?(name)
# @return [void]
def on_signal(name, &block)
unless @default_iface && has_iface?(@default_iface)
raise NoMethodError, "undefined signal `#{name}' for DBus interface `#{@default_iface}' on object `#{@path}'"
raise NoMethodError, "undefined signal #{OPEN_QUOTE}#{name}' for DBus interface #{OPEN_QUOTE}#{@default_iface}' on object #{OPEN_QUOTE}#{@path}'"
end

@interfaces[@default_iface].on_signal(name, &block)
Expand All @@ -151,18 +153,18 @@ def method_missing(name, *args, &reply_handler)
# - di not specified
# TODO
# - di is specified but not found in introspection data
raise NoMethodError, "undefined method `#{name}' for DBus interface `#{@default_iface}' on object `#{@path}'"
raise NoMethodError, "undefined method #{OPEN_QUOTE}#{name}' for DBus interface #{OPEN_QUOTE}#{@default_iface}' on object #{OPEN_QUOTE}#{@path}'"
end

begin
@interfaces[@default_iface].method(name).call(*args, &reply_handler)
rescue NameError => e
# interesting, foo.method("unknown")
# raises NameError, not NoMethodError
raise unless e.to_s =~ /undefined method `#{name}'/
raise unless e.to_s =~ /undefined method #{OPEN_QUOTE}#{name}'/

# BTW e.exception("...") would preserve the class.
raise NoMethodError, "undefined method `#{name}' for DBus interface `#{@default_iface}' on object `#{@path}'"
raise NoMethodError, "undefined method #{OPEN_QUOTE}#{name}' for DBus interface #{OPEN_QUOTE}#{@default_iface}' on object #{OPEN_QUOTE}#{@path}'"
end
end
# rubocop:enable Lint/MissingSuper
Expand Down
3 changes: 2 additions & 1 deletion spec/signal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ def new_quitter(main_loop)
describe DBus::ProxyObject do
describe "#on_signal" do
it "raises a descriptive error when the default_iface is wrong" do
OPEN_QUOTE = RUBY_VERSION >= "3.4" ? "'" : "`"
@obj.default_iface = "org.ruby.NoSuchInterface"
expect { @obj.on_signal("Foo") {} }
.to raise_error(NoMethodError, /undefined signal.*interface `org.ruby.NoSuchInterface'/)
.to raise_error(NoMethodError, /undefined signal.*interface #{OPEN_QUOTE}org.ruby.NoSuchInterface'/)
end
end
end
Expand Down

0 comments on commit 1924e25

Please sign in to comment.