From 11d7c75fb33138e9ecadcf222286897826aa0a36 Mon Sep 17 00:00:00 2001 From: tomoya ishida Date: Fri, 24 Nov 2023 02:33:08 +0900 Subject: [ruby/irb] Handle handle_exception's exception (https://github.com/ruby/irb/pull/780) https://github.com/ruby/irb/commit/d42138c477 --- lib/irb.rb | 9 +++- test/irb/test_raise_exception.rb | 77 +++++++++++++++++++++++++++ test/irb/test_raise_no_backtrace_exception.rb | 56 ------------------- 3 files changed, 85 insertions(+), 57 deletions(-) create mode 100644 test/irb/test_raise_exception.rb delete mode 100644 test/irb/test_raise_no_backtrace_exception.rb diff --git a/lib/irb.rb b/lib/irb.rb index 655abaf069..7547654257 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -743,7 +743,14 @@ module IRB message = message.gsub(/\(irb\):(?\d+):in `<(?top \(required\))>'/) { "(irb):#{$~[:num]}:in `
'" } puts message end - print "Maybe IRB bug!\n" if irb_bug + puts 'Maybe IRB bug!' if irb_bug + rescue Exception => handler_exc + begin + puts exc.inspect + puts "backtraces are hidden because #{handler_exc} was raised when processing them" + rescue Exception + puts 'Uninspectable exception occurred' + end end # Evaluates the given block using the given +path+ as the Context#irb_path diff --git a/test/irb/test_raise_exception.rb b/test/irb/test_raise_exception.rb new file mode 100644 index 0000000000..9ca534dba1 --- /dev/null +++ b/test/irb/test_raise_exception.rb @@ -0,0 +1,77 @@ +# frozen_string_literal: false +require "tmpdir" + +require_relative "helper" + +module TestIRB + class RaiseExceptionTest < TestCase + def test_raise_exception_with_nil_backtrace + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<-IRB, /Exception: foo/, []) + e = Exception.new("foo") + puts e.inspect + def e.backtrace; nil; end + raise e +IRB + end + + def test_raise_exception_with_message_exception + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + expected = /#\nbacktraces are hidden because bar was raised when processing them/ + assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<-IRB, expected, []) + e = Exception.new("foo") + def e.message; raise 'bar'; end + raise e +IRB + end + + def test_raise_exception_with_message_inspect_exception + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + expected = /Uninspectable exception occurred/ + assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<-IRB, expected, []) + e = Exception.new("foo") + def e.message; raise; end + def e.inspect; raise; end + raise e +IRB + end + + def test_raise_exception_with_invalid_byte_sequence + pend if RUBY_ENGINE == 'truffleruby' || /mswin|mingw/ =~ RUBY_PLATFORM + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<~IRB, /A\\xF3B \(StandardError\)/, []) + raise StandardError, "A\\xf3B" + IRB + end + + def test_raise_exception_with_different_encoding_containing_invalid_byte_sequence + backup_home = ENV["HOME"] + Dir.mktmpdir("test_irb_raise_no_backtrace_exception_#{$$}") do |tmpdir| + ENV["HOME"] = tmpdir + + bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] + File.open("#{tmpdir}/euc.rb", 'w') do |f| + f.write(<<~EOF) + # encoding: euc-jp + + def raise_euc_with_invalid_byte_sequence + raise "\xA4\xA2\\xFF" + end + EOF + end + env = {} + %w(LC_MESSAGES LC_ALL LC_CTYPE LANG).each {|n| env[n] = "ja_JP.UTF-8" } + # TruffleRuby warns when the locale does not exist + env['TRUFFLERUBYOPT'] = "#{ENV['TRUFFLERUBYOPT']} --log.level=SEVERE" if RUBY_ENGINE == 'truffleruby' + args = [env] + bundle_exec + %W[-rirb -C #{tmpdir} -W0 -e IRB.start(__FILE__) -- -f --] + error = /`raise_euc_with_invalid_byte_sequence': あ\\xFF \(RuntimeError\)/ + assert_in_out_err(args, <<~IRB, error, [], encoding: "UTF-8") + require_relative 'euc' + raise_euc_with_invalid_byte_sequence + IRB + end + ensure + ENV["HOME"] = backup_home + end + end +end diff --git a/test/irb/test_raise_no_backtrace_exception.rb b/test/irb/test_raise_no_backtrace_exception.rb deleted file mode 100644 index 929577ad8e..0000000000 --- a/test/irb/test_raise_no_backtrace_exception.rb +++ /dev/null @@ -1,56 +0,0 @@ -# frozen_string_literal: false -require "tmpdir" - -require_relative "helper" - -module TestIRB - class RaiseNoBacktraceExceptionTest < TestCase - def test_raise_exception - bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] - assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<-IRB, /Exception: foo/, []) - e = Exception.new("foo") - puts e.inspect - def e.backtrace; nil; end - raise e -IRB - end - - def test_raise_exception_with_invalid_byte_sequence - pend if RUBY_ENGINE == 'truffleruby' || /mswin|mingw/ =~ RUBY_PLATFORM - bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] - assert_in_out_err(bundle_exec + %w[-rirb -W0 -e IRB.start(__FILE__) -- -f --], <<~IRB, /A\\xF3B \(StandardError\)/, []) - raise StandardError, "A\\xf3B" - IRB - end - - def test_raise_exception_with_different_encoding_containing_invalid_byte_sequence - backup_home = ENV["HOME"] - Dir.mktmpdir("test_irb_raise_no_backtrace_exception_#{$$}") do |tmpdir| - ENV["HOME"] = tmpdir - - bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : [] - File.open("#{tmpdir}/euc.rb", 'w') do |f| - f.write(<<~EOF) - # encoding: euc-jp - - def raise_euc_with_invalid_byte_sequence - raise "\xA4\xA2\\xFF" - end - EOF - end - env = {} - %w(LC_MESSAGES LC_ALL LC_CTYPE LANG).each {|n| env[n] = "ja_JP.UTF-8" } - # TruffleRuby warns when the locale does not exist - env['TRUFFLERUBYOPT'] = "#{ENV['TRUFFLERUBYOPT']} --log.level=SEVERE" if RUBY_ENGINE == 'truffleruby' - args = [env] + bundle_exec + %W[-rirb -C #{tmpdir} -W0 -e IRB.start(__FILE__) -- -f --] - error = /`raise_euc_with_invalid_byte_sequence': あ\\xFF \(RuntimeError\)/ - assert_in_out_err(args, <<~IRB, error, [], encoding: "UTF-8") - require_relative 'euc' - raise_euc_with_invalid_byte_sequence - IRB - end - ensure - ENV["HOME"] = backup_home - end - end -end -- cgit v1.2.3