From 707c5ffab5e2ae5105c7d9823411332b160f33df Mon Sep 17 00:00:00 2001 From: nobu Date: Sat, 12 May 2018 09:13:45 +0000 Subject: [PATCH] irb.rb: update `_` * lib/irb.rb (IRB::Irb#eval_input): update `_` after exception. [ruby-core:86989] [Bug #14749] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63409 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/irb.rb | 13 ++++++------- test/irb/test_context.rb | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/irb.rb b/lib/irb.rb index 98bcac016d..a7ab9d7bce 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -439,7 +439,7 @@ module IRB # Evaluates input for this session. def eval_input - last_error = nil + exc = nil @scanner.set_prompt do |ltype, indent, continue, line_no| @@ -490,18 +490,17 @@ module IRB signal_status(:IN_EVAL) do begin line.untaint - @context.evaluate(line, line_no, exception: last_error) + @context.evaluate(line, line_no, exception: exc) output_value if @context.echo? - exc = nil rescue Interrupt => exc rescue SystemExit, SignalException raise rescue Exception => exc + else + exc = nil + next end - if exc - last_error = exc - handle_exception(exc) - end + handle_exception(exc) end end end diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb index 75e47cf1e9..fa2432b3f3 100644 --- a/test/irb/test_context.rb +++ b/test/irb/test_context.rb @@ -22,6 +22,10 @@ module TestIRB def eof? @line_no >= @list.size end + + def encoding + Encoding.default_external + end end def setup @@ -49,5 +53,24 @@ module TestIRB assert_equal('foo', e.message) assert_same(e, @context.evaluate('$!', 1, exception: e)) end + + def test_eval_input + input = TestInputMethod.new([ + "raise 'Foo'\n", + "_\n", + "0\n", + "_\n", + ]) + irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input) + out, err = capture_io do + irb.eval_input + end + assert_empty err + assert_pattern_list([:*, /RuntimeError \(.*Foo.*\).*\n/, + :*, /#\n/, + :*, /0$/, + :*, /0$/, + /\s*/], out) + end end end