The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Line numbers, error reporting and backtraces

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Line numbers, error reporting and backtraces Posted: Jun 14, 2006 6:16 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Line numbers, error reporting and backtraces
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

The AST ruby creates when parsing your code doesn't contain that much information regarding its format. In particular, it only stores one line number per node. Why care? It turns out that this is what Ruby needs to generate meaningful backtrace info and report events to the trace_func and/or event_hook*1.

When you have chained method calls with multi-line blocks, the line numbers reported by Ruby will often be wrong:

class Object; def foo; caller end end

a = foo do     # line 3, this is where Ruby tells us the last #foo call
  #            # is being performed from
  #
end.foo do
  #
end.foo        # <--- when it's actually here (line 8)

p a
# >> ["-:3"]

I patched ruby to set the NODE line number to the last line (the one with the end block delimiter), which helps with chained calls, but might be a bit confusing regarding LINE events.

I used this program to show the events reported by Ruby to the trace_func and print the call stack at each point:

class Object
  def foo; 1 end
end
set_trace_func lambda { |event, file, line, id, binding, classname|
  puts "=" * 40
  puts "%8s :%-2d %5s %8s" % [event, line, id, classname]
  puts caller.map{|x| x.inspect}
}

foo do                   # line 11
 1
end.foo do               # line 13
 2
end

[ 
  1,                     # line 18
  2, 
  3 + 4.
  
  abs, 4
].foo                    # line 23

These are the events and backtrace information generated with the above script, for both the unpatched interpreter and the modified one, side by side:


Read more...

Read: Line numbers, error reporting and backtraces

Topic: Stone Code Public Code Pit Previous Topic   Next Topic Topic: Rails satire

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use