Skip to content

Commit

Permalink
Merge pull request #207 from yast/better-eval-backtrace
Browse files Browse the repository at this point in the history
Better eval backtrace
  • Loading branch information
jreidinger committed Nov 30, 2017
2 parents 3db843a + f453907 commit 5ff449c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 31 deletions.
6 changes: 6 additions & 0 deletions package/yast2-ruby-bindings.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Fri Nov 24 14:26:14 UTC 2017 - mvidner@suse.com

- Better backtrace for dynamically defined methods (bsc#1066290)
- 4.0.2

-------------------------------------------------------------------
Thu Nov 23 09:45:04 UTC 2017 - lslezak@suse.cz

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-ruby-bindings.spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


Name: yast2-ruby-bindings
Version: 4.0.1
Version: 4.0.2
Url: https://github.com/yast/yast-ruby-bindings
Release: 0
BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
8 changes: 3 additions & 5 deletions src/ruby/yast/convert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ module Convert
# @!method self.to_locale( object )
# @return [String, nil] *object*, or `nil` if it is not a String
Ops::SHORTCUT_TYPES.each do |type|
eval <<END
def self.to_#{type}(object)
convert object, :from => "any", :to => "#{type}"
end
END
define_singleton_method("to_#{type}") do |object|
convert object, :from => "any", :to => type
end
end

# Converts object from given type to target one.
Expand Down
18 changes: 8 additions & 10 deletions src/ruby/yast/ops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ module Ops
# @!method self.get_locale( obj, idx, def )
# @return [String, nil] {Convert.to_locale}({get}(obj, idx, def))
Ops::SHORTCUT_TYPES.each do |type|
eval <<END, binding, __FILE__, __LINE__ + 1
def self.get_#{type}(object, indexes, default=nil, &block)
Yast::Convert.to_#{type} get(object, indexes, default, 1, &block)
end
END
define_singleton_method("get_#{type}") do |object, indexes, default=nil, &block|
Yast::Convert.public_send("to_#{type}", get(object, indexes, default, 1, &block))
end
end

# To log the caller frame we need to skip 3 frames as 1 is method itself
Expand Down Expand Up @@ -404,17 +402,17 @@ def self.greater_or_equal(first, second)
end

TYPES_MAP.keys.each do |type|
class_eval "def self.is_#{type}? (object)
Ops.is(object, \"#{type}\")
end"
define_singleton_method("is_#{type}?") do |object|
Ops.is(object, type)
end
end

# Checks if object is given YCP type. There is also shorfcuts for most of types in
# format is_<type>
def self.is(object, type)
type = "function" if type =~ /\(.*\)/ # reference to function
type.gsub!(/<.*>/, "")
type.gsub!(/\s+/, "")
type = type.gsub(/<.*>/, "")
type = type.gsub(/\s+/, "")
classes = TYPES_MAP[type]
raise "Invalid type to detect in is '#{type}'" unless classes
classes = [classes] unless classes.is_a? ::Array
Expand Down
3 changes: 2 additions & 1 deletion src/ruby/yast/wfm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def self.call_builtin_wrapper(*args)

# Handles a generic Exception
private_class_method def self.handle_exception(e, client)
Builtins.y2error("Client call failed with '%1' (%2).\nBacktrace:\n%3",
Builtins.y2error("Client %1 failed with '%2' (%3).\nBacktrace:\n%4",
client,
e.message,
e.class,
e.backtrace.join("\n"))
Expand Down
24 changes: 10 additions & 14 deletions src/ruby/yast/yast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,17 @@ def self.import(mname)
symbols(mname).each do |sname, stype|
next if sname.empty?
if stype == :function
m.module_eval <<-"END"
def self.#{sname}(*args)
caller(1,1).first.match BACKTRACE_REGEXP
return Yast::call_yast_function("#{mname}", :#{sname}, $1, $2.to_i, *args)
end
END
m.define_singleton_method(sname) do |*args|
caller(1,1).first.match BACKTRACE_REGEXP
next Yast::call_yast_function(mname, :"#{sname}", $1, $2.to_i, *args)
end
elsif stype == :variable
m.module_eval <<-"END"
def self.#{sname}
return Yast::call_yast_function("#{mname}", :#{sname})
end
def self.#{sname}= (value)
return Yast::call_yast_function("#{mname}", :#{sname}, value)
end
END
m.define_singleton_method(sname) do
Yast::call_yast_function(mname, :"#{sname}")
end
m.define_method("#{sname}=") do |value|
Yast::call_yast_function(mname, :"#{sname}", value)
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions tests/ops_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@
expect(Yast::Ops.get_integer(list, 0, "n")).to eq(nil)
end

it "returns nil for index out of range" do
expect(Yast::Ops.get_string(list, 5)).to eq(nil)
end

it "returns the specified default" do
expect(Yast::Ops.get_string(list, 5, "c")).to eq("c")
end

it "returns the block-specified default" do
expect(Yast::Ops.get_string(list, 5){"n"}).to eq("n")
end

it "warns when the container is nil" do
any_frame = kind_of(Integer)
expect(Yast).to receive(:y2milestone).with(any_frame, /called on nil/)
Expand Down

0 comments on commit 5ff449c

Please sign in to comment.