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

ruby-changes:64783

From: Nobuhiro <ko1@a...>
Date: Fri, 8 Jan 2021 13:38:34 +0900 (JST)
Subject: [ruby-changes:64783] ed3264d37a (master): [ruby/irb] refactoring an error handling in `IRB::Inspector`

https://git.ruby-lang.org/ruby.git/commit/?id=ed3264d37a

From ed3264d37abc54e3aade229751a9165ffd37ca2e Mon Sep 17 00:00:00 2001
From: Nobuhiro IMAI <nov@y...>
Date: Thu, 7 Jan 2021 19:21:06 +0900
Subject: [ruby/irb] refactoring an error handling in `IRB::Inspector`

* moved rescue clause to `#inspect_value` to catch all failures in inspectors
* test with all (currently five kind of) inspect modes
  - tweaked the input due to only `Marshal` can inspect(dump) a `BasicObject`

https://github.com/ruby/irb/commit/9d112fab8e

diff --git a/lib/irb/inspector.rb b/lib/irb/inspector.rb
index 66837f1..92de283 100644
--- a/lib/irb/inspector.rb
+++ b/lib/irb/inspector.rb
@@ -100,21 +100,19 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/inspector.rb#L100
     # Proc to call when the input is evaluated and output in irb.
     def inspect_value(v)
       @inspect.call(v)
+    rescue
+      puts "(Object doesn't support #inspect)"
+      ''
     end
   end
 
   Inspector.def_inspector([false, :to_s, :raw]){|v| v.to_s}
   Inspector.def_inspector([:p, :inspect]){|v|
-    begin
-      result = v.inspect
-      if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
-        result = Color.colorize_code(result)
-      end
-      result
-    rescue NoMethodError
-      puts "(Object doesn't support #inspect)"
-      ''
+    result = v.inspect
+    if IRB.conf[:MAIN_CONTEXT]&.use_colorize? && Color.inspect_colorable?(v)
+      result = Color.colorize_code(result)
     end
+    result
   }
   Inspector.def_inspector([true, :pp, :pretty_inspect], proc{require "irb/color_printer"}){|v|
     if IRB.conf[:MAIN_CONTEXT]&.use_colorize?
diff --git a/test/irb/test_context.rb b/test/irb/test_context.rb
index a8ff640..d1c3ec6 100644
--- a/test/irb/test_context.rb
+++ b/test/irb/test_context.rb
@@ -106,15 +106,22 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_context.rb#L106
 
     def test_eval_object_without_inspect_method
       verbose, $VERBOSE = $VERBOSE, nil
-      input = TestInputMethod.new([
-        "BasicObject.new\n",
-      ])
-      irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
-      out, err = capture_output do
-        irb.eval_input
+      all_assertions do |all|
+        IRB::Inspector::INSPECTORS.invert.each_value do |mode|
+          all.for(mode) do
+            input = TestInputMethod.new([
+                "[BasicObject.new, Class.new]\n",
+              ])
+            irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
+            irb.context.inspect_mode = mode
+            out, err = capture_output do
+              irb.eval_input
+            end
+            assert_empty err
+            assert_match(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
+          end
+        end
       end
-      assert_empty err
-      assert(/\(Object doesn't support #inspect\)\n(=> )?\n/, out)
     ensure
       $VERBOSE = verbose
     end
-- 
cgit v0.10.2


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

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