summaryrefslogtreecommitdiff
path: root/lib/irb.rb
diff options
context:
space:
mode:
authoraycabta <aycabta@gmail.com>2021-01-08 04:17:21 +0900
committeraycabta <aycabta@gmail.com>2021-01-08 13:25:18 +0900
commit917050220a1fd41bdb3e50ea54a200b0c285bcd4 (patch)
tree462160f61e14a02c2b9d1aa425f7ba68094708bf /lib/irb.rb
parented3264d37abc54e3aade229751a9165ffd37ca2e (diff)
[ruby/irb] Use Exception#full_message to show backtrace in the correct order
[Bug #17466] https://github.com/ruby/irb/commit/1c76845cca
Diffstat (limited to 'lib/irb.rb')
-rw-r--r--lib/irb.rb74
1 files changed, 34 insertions, 40 deletions
diff --git a/lib/irb.rb b/lib/irb.rb
index 2926825..c7563d1 100644
--- a/lib/irb.rb
+++ b/lib/irb.rb
@@ -610,50 +610,44 @@ module IRB
irb_bug = false
end
- if STDOUT.tty?
- attr = ATTR_TTY
- print "#{attr[1]}Traceback#{attr[]} (most recent call last):\n"
- else
- attr = ATTR_PLAIN
- end
- messages = []
- lasts = []
- levels = 0
if exc.backtrace
- count = 0
- exc.backtrace.each do |m|
- m = @context.workspace.filter_backtrace(m) or next unless irb_bug
- count += 1
- if attr == ATTR_TTY
- m = sprintf("%9d: from %s", count, m)
+ order = nil
+ if '2.5.0' == RUBY_VERSION
+ # Exception#full_message doesn't have keyword arguments.
+ message = exc.full_message # the same of (highlight: true, order: bottom)
+ order = :bottom
+ elsif '2.5.1' <= RUBY_VERSION && RUBY_VERSION < '3.0.0'
+ if STDOUT.tty?
+ message = exc.full_message(order: :bottom)
+ order = :bottom
else
- m = "\tfrom #{m}"
- end
- if messages.size < @context.back_trace_limit
- messages.push(m)
- elsif lasts.size < @context.back_trace_limit
- lasts.push(m).shift
- levels += 1
+ message = exc.full_message(order: :top)
+ order = :top
end
+ else # RUBY_VERSION < '2.5.0' || '3.0.0' <= RUBY_VERSION
+ message = exc.full_message(order: :top)
+ order = :top
end
- end
- if attr == ATTR_TTY
- unless lasts.empty?
- puts lasts.reverse
- printf "... %d levels...\n", levels if levels > 0
- end
- puts messages.reverse
- end
- converted_exc_s = convert_invalid_byte_sequence(exc.to_s.dup)
- m = converted_exc_s.split(/\n/)
- print "#{attr[1]}#{exc.class} (#{attr[4]}#{m.shift}#{attr[0, 1]})#{attr[]}\n"
- puts m.map {|s| "#{attr[1]}#{s}#{attr[]}\n"}
- if attr == ATTR_PLAIN
- puts messages
- unless lasts.empty?
- puts lasts
- printf "... %d levels...\n", levels if levels > 0
- end
+ message = convert_invalid_byte_sequence(message)
+ message = message.gsub(/((?:^\t.+$\n)+)/) { |m|
+ case order
+ when :top
+ lines = m.split("\n")
+ when :bottom
+ lines = m.split("\n").reverse
+ end
+ unless irb_bug
+ lines = lines.map { |l| @context.workspace.filter_backtrace(l) }.compact
+ if lines.size > @context.back_trace_limit
+ omit = lines.size - @context.back_trace_limit
+ lines[0..(@context.back_trace_limit - 1)]
+ lines << '... %d levels...' % omit
+ end
+ end
+ lines = lines.reverse if order == :bottom
+ lines.map{ |l| l + "\n" }.join
+ }
+ puts message
end
print "Maybe IRB bug!\n" if irb_bug
end