[前][次][番号順一覧][スレッド一覧]

ruby-changes:50943

From: nobu <ko1@a...>
Date: Sat, 14 Apr 2018 22:05:59 +0900 (JST)
Subject: [ruby-changes:50943] nobu:r63150 (trunk): irb.rb: restore the last error

nobu	2018-04-14 22:05:52 +0900 (Sat, 14 Apr 2018)

  New Revision: 63150

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63150

  Log:
    irb.rb: restore the last error
    
    * lib/irb.rb (eval_input): restore the last error `$!`, as the
      previous result.  [Feature #14684]
    
    * lib/irb/context.rb (evaluate): add `exception` keyword argument
      to set the last error.

  Modified files:
    trunk/lib/irb/context.rb
    trunk/lib/irb.rb
    trunk/test/irb/test_context.rb
Index: lib/irb/context.rb
===================================================================
--- lib/irb/context.rb	(revision 63149)
+++ lib/irb/context.rb	(revision 63150)
@@ -376,8 +376,12 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L376
       @debug_level > 0
     end
 
-    def evaluate(line, line_no) # :nodoc:
+    def evaluate(line, line_no, exception: nil) # :nodoc:
       @line_no = line_no
+      if exception
+        line = "begin ::Kernel.raise _; rescue _.class; #{line}; end"
+        @workspace.local_variable_set(:_, exception)
+      end
       set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
     end
 
Index: lib/irb.rb
===================================================================
--- lib/irb.rb	(revision 63149)
+++ lib/irb.rb	(revision 63150)
@@ -439,6 +439,8 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L439
 
     # Evaluates input for this session.
     def eval_input
+      last_error = nil
+
       @scanner.set_prompt do
         |ltype, indent, continue, line_no|
         if ltype
@@ -488,7 +490,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L490
         signal_status(:IN_EVAL) do
           begin
             line.untaint
-            @context.evaluate(line, line_no)
+            @context.evaluate(line, line_no, exception: last_error)
             output_value if @context.echo?
             exc = nil
           rescue Interrupt => exc
@@ -497,6 +499,7 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb.rb#L499
           rescue Exception => exc
           end
           if exc
+            last_error = exc
             handle_exception(exc)
           end
         end
Index: test/irb/test_context.rb
===================================================================
--- test/irb/test_context.rb	(revision 63149)
+++ test/irb/test_context.rb	(revision 63150)
@@ -40,5 +40,14 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L40
       assert_same(obj, @context.last_value)
       assert_same(obj, @context.evaluate('_', 1))
     end
+
+    def test_evaluate_with_exception
+      assert_nil(@context.evaluate("$!", 1))
+      e = assert_raise_with_message(RuntimeError, 'foo') {
+        @context.evaluate("raise 'foo'", 1)
+      }
+      assert_equal('foo', e.message)
+      assert_same(e, @context.evaluate('$!', 1, exception: e))
+    end
   end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]