Issue #7895 has been updated by headius (Charles Nutter).


For now I have implemented it in JRuby.

Here's the commit: https://github.com/jruby/jruby/commit/281ead709e1e855b552a42e86e48f8f91ca8ebb5

It works simply enough; Exception#backtrace_locations always reflects the original exception trace and (at least for now) it can't be modified.

    irb(main):004:0> def foo; raise; end
    => nil
    irb(main):005:0> def bar; 1.times { foo }; end
    => nil
    irb(main):006:0> ex = begin; bar; rescue; $!; end
    => RuntimeError
    irb(main):007:0> ex.backtrace_locations.each {|loc| puts "method: #{loc.label}, line: #{loc.lineno}"}
    method: foo, line: 4
    method: bar, line: 5
    method: times, line: 271
    method: bar, line: 5
    method: evaluate, line: 6

----------------------------------------
Feature #7895: Exception#backtrace_locations to go with Thread#backtrace_locations and Kernel#caller_locations
https://bugs.ruby-lang.org/issues/7895#change-37518

Author: headius (Charles Nutter)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


Thread#backtrace_locations and Kernel#caller_locations were added in Ruby 2.0.0, but no equivalent method was added to Exception to get backtrace locations. The String format of Exception#backtrace elements makes it difficult to do any inspection of the actual files and lines it reports, so having backtrace_locations would be very useful.

In #7435 ko1 pointed out that Exception defines set_backtrace, which takes an array (presumably of Strings in the same format as Exception#backtrace) and that this would need to be addressed in order to support backtrace_locations. I propose that if you set_backtrace, you are already breaking the ability to get structured Location elements, so set_backtrace should cause backtrace_locations to return nil or an empty array (probably an empty array, so it can always be expected to be non-nil).

We could consider also adding set_backtrace_locations as a way to set Location elements into the exception.

An example script where backtrace_locations would be very useful is here (avoiding the need to regexp match to get file and line number): https://gist.github.com/headius/4999244


-- 
http://bugs.ruby-lang.org/