From 80a18e8f422e30204e7386fc9a1fc37667b20b2a Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Wed, 13 Aug 2025 13:37:34 -0400 Subject: [PATCH 01/10] [ruby/pp] Support new instance_variables_to_inspect method from Ruby core This supports the new `instance_variables_to_inspect` method from Ruby core that was added in ruby/ruby#13555. If `instance_variables_to_inspect` is defined, then `pretty_print_instance_variables` will use it. Additionally, this commit introduces tests for both `pretty_print_instance_variables` and `instance_variables_to_inspect`. https://github.com/ruby/pp/commit/9cea466c95 --- lib/pp.rb | 3 ++- test/test_pp.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/pp.rb b/lib/pp.rb index 531839563167cf..eb9f80db4e4b25 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -399,7 +399,8 @@ def pretty_print_cycle(q) # This method should return an array of names of instance variables as symbols or strings as: # +[:@a, :@b]+. def pretty_print_instance_variables - instance_variables.sort + ivars = respond_to?(:instance_variables_to_inspect) ? instance_variables_to_inspect : instance_variables + ivars.sort end # Is #inspect implementation using #pretty_print. diff --git a/test/test_pp.rb b/test/test_pp.rb index c71445c9bc90ee..e721260e01ea38 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -130,6 +130,20 @@ def a.to_s() "aaa" end assert_equal("#{a.inspect}\n", result) end + def test_iv_hiding + a = Object.new + def a.pretty_print_instance_variables() [:@b] end + a.instance_eval { @a = "aaa"; @b = "bbb" } + assert_match(/\A#\n\z/, PP.pp(a, ''.dup)) + end + + def test_iv_hiding_via_ruby + a = Object.new + def a.instance_variables_to_inspect() [:@b] end + a.instance_eval { @a = "aaa"; @b = "bbb" } + assert_match(/\A#\n\z/, PP.pp(a, ''.dup)) + end + def test_basic_object a = BasicObject.new assert_match(/\A#\n\z/, PP.pp(a, ''.dup)) From 340777078c88478bae1e39b10bd6409491fd584b Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 4 Oct 2025 15:30:47 +0200 Subject: [PATCH 02/10] [ruby/pp] Fix ::Data warning on Ruby 2.7 * It was showing on require 'pp': lib/pp.rb:525: warning: constant ::Data is deprecated * Fixes https://github.com/ruby/pp/issues/51 https://github.com/ruby/pp/commit/4fd8f4e0bb --- lib/pp.rb | 9 ++++++++- test/test_pp.rb | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index eb9f80db4e4b25..f10fe7f2f0e3f9 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -490,6 +490,13 @@ def pretty_print_cycle(q) # :nodoc: end end +verbose, $VERBOSE = $VERBOSE, nil +begin + has_data_define = defined?(Data.define) +ensure + $VERBOSE = verbose +end + class Data # :nodoc: def pretty_print(q) # :nodoc: class_name = PP.mcall(self, Kernel, :class).name @@ -522,7 +529,7 @@ def pretty_print(q) # :nodoc: def pretty_print_cycle(q) # :nodoc: q.text sprintf("#", PP.mcall(self, Kernel, :class).name) end -end if defined?(Data.define) +end if has_data_define class Range # :nodoc: def pretty_print(q) # :nodoc: diff --git a/test/test_pp.rb b/test/test_pp.rb index e721260e01ea38..28da00e3e7769a 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -172,7 +172,14 @@ def test_struct assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) unless RUBY_ENGINE == "truffleruby" end - if defined?(Data.define) + verbose, $VERBOSE = $VERBOSE, nil + begin + has_data_define = defined?(Data.define) + ensure + $VERBOSE = verbose + end + + if has_data_define D = Data.define(:aaa, :bbb) def test_data a = D.new("aaa", "bbb") From 4b50d0b41cc0a4c808d0d128e7068e4b3b9331bf Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 00:06:25 +0900 Subject: [PATCH 03/10] [ruby/pp] Do not override the methods in set.rb These methods are defined for built-in `Set` class on Ruby 3.5. https://github.com/ruby/pp/commit/352081dbbf --- lib/pp.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index f10fe7f2f0e3f9..0e55441e574897 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -453,11 +453,11 @@ def pretty_print(pp) # :nodoc: } } } - end + end unless method_defined?(:pretty_print) def pretty_print_cycle(pp) # :nodoc: pp.text sprintf('#', empty? ? '' : '...') - end + end unless method_defined?(:pretty_print_cycle) end class << ENV # :nodoc: From 4ddbee33097c431e83d4dbc6a130349067bf7c0a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 00:52:32 +0900 Subject: [PATCH 04/10] [ruby/pp] Refine `Set#pretty_print` check https://github.com/ruby/pp/commit/6615b62d7b --- lib/pp.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 0e55441e574897..ed801f010afb02 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -443,6 +443,13 @@ def pretty_print_cycle(q) # :nodoc: end end +if defined?(Set) + if set_pp = Set.instance_method(:initialize).source_location + set_pp = !set_pp.first.end_with?("/set.rb") # not defined in set.rb + else + set_pp = true # defined in C + end +end class Set # :nodoc: def pretty_print(pp) # :nodoc: pp.group(1, '#') { @@ -453,12 +460,12 @@ def pretty_print(pp) # :nodoc: } } } - end unless method_defined?(:pretty_print) + end def pretty_print_cycle(pp) # :nodoc: pp.text sprintf('#', empty? ? '' : '...') - end unless method_defined?(:pretty_print_cycle) -end + end +end if set_pp class << ENV # :nodoc: def pretty_print(q) # :nodoc: From ec1655d52019e66725efe94b5bc88b3a98af284e Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Sun, 5 Oct 2025 01:06:07 +0900 Subject: [PATCH 05/10] [ruby/pp] Update pp for Set to use new inspect format (https://github.com/ruby/pp/pull/43) Ruby 3.5 will use `Set[1, 2, 3]`. This updates pp to use the same format. https://github.com/ruby/pp/commit/507eebf711 --- lib/pp.rb | 12 +++++------- test/test_pp.rb | 13 +++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index ed801f010afb02..60602826e19f51 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -452,18 +452,16 @@ def pretty_print_cycle(q) # :nodoc: end class Set # :nodoc: def pretty_print(pp) # :nodoc: - pp.group(1, '#') { - pp.breakable - pp.group(1, '{', '}') { - pp.seplist(self) { |o| - pp.pp o - } + pp.group(1, "#{self.class.name}[", ']') { + pp.seplist(self) { |o| + pp.pp o } } end def pretty_print_cycle(pp) # :nodoc: - pp.text sprintf('#', empty? ? '' : '...') + name = self.class.name + pp.text(empty? ? "#{name}[]" : "#{name}[...]") end end if set_pp diff --git a/test/test_pp.rb b/test/test_pp.rb index 28da00e3e7769a..c5340a3e65ca10 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -2,11 +2,14 @@ require 'pp' require 'delegate' +require 'set' require 'test/unit' require 'ruby2_keywords' module PPTestModule +SetPP = Set.instance_method(:pretty_print).source_location[0].end_with?("/pp.rb") + class PPTest < Test::Unit::TestCase def test_list0123_12 assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12)) @@ -16,6 +19,10 @@ def test_list0123_11 assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11)) end + def test_set + assert_equal("Set[0, 1, 2, 3]\n", PP.pp(Set[0,1,2,3], ''.dup, 16)) + end if SetPP + OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) def test_struct_override_members # [ruby-core:7865] a = OverriddenStruct.new(1,2) @@ -164,6 +171,12 @@ def test_hash assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) end + def test_set + s = Set[] + s.add s + assert_equal("Set[Set[...]]\n", PP.pp(s, ''.dup)) + end if SetPP + S = Struct.new("S", :a, :b) def test_struct a = S.new(1,2) From deb9f45229bc32f3c607b001d46092f69f86664f Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 10:03:19 +0900 Subject: [PATCH 06/10] [ruby/pp] Exclude out-of-scope test instead of omitting https://github.com/ruby/pp/commit/40b713d70f --- test/test_pp.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_pp.rb b/test/test_pp.rb index c5340a3e65ca10..8345e5e9873014 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -257,7 +257,6 @@ def test_hash end def test_hash_symbol_colon_key - omit if RUBY_VERSION < "3.4." no_quote = "{a: 1, a!: 1, a?: 1}" unicode_quote = "{\u{3042}: 1}" quote0 = '{"": 1}' @@ -270,7 +269,7 @@ def test_hash_symbol_colon_key assert_equal(quote1, PP.singleline_pp(eval(quote1), ''.dup)) assert_equal(quote2, PP.singleline_pp(eval(quote2), ''.dup)) assert_equal(quote3, PP.singleline_pp(eval(quote3), ''.dup)) - end + end if RUBY_VERSION >= "3.4." def test_hash_in_array omit if RUBY_ENGINE == "jruby" From 252c253b7537d6f5ac9ab612afcf6d8938cbb529 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 10:22:35 +0900 Subject: [PATCH 07/10] [ruby/pp] Simplify recursive state handling https://github.com/ruby/pp/commit/0e89466269 --- lib/pp.rb | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 60602826e19f51..2c7f41eadda188 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -145,21 +145,13 @@ module PPMethods # Yields to a block # and preserves the previous set of objects being printed. def guard_inspect_key - if Thread.current[:__recursive_key__] == nil - Thread.current[:__recursive_key__] = {}.compare_by_identity - end - - if Thread.current[:__recursive_key__][:inspect] == nil - Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity - end - - save = Thread.current[:__recursive_key__][:inspect] - + recursive_state = Thread.current[:__recursive_key__] ||= {}.compare_by_identity + save = recursive_state[:inspect] ||= {}.compare_by_identity begin - Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity + recursive_state[:inspect] = {}.compare_by_identity yield ensure - Thread.current[:__recursive_key__][:inspect] = save + recursive_state[:inspect] = save end end @@ -167,9 +159,8 @@ def guard_inspect_key # to be pretty printed. Used to break cycles in chains of objects to be # pretty printed. def check_inspect_key(id) - Thread.current[:__recursive_key__] && - Thread.current[:__recursive_key__][:inspect] && - Thread.current[:__recursive_key__][:inspect].include?(id) + recursive_state = Thread.current[:__recursive_key__] or return false + recursive_state[:inspect]&.include?(id) end # Adds the object_id +id+ to the set of objects being pretty printed, so @@ -186,7 +177,7 @@ def pop_inspect_key(id) private def guard_inspect(object) recursive_state = Thread.current[:__recursive_key__] - if recursive_state && recursive_state.key?(:inspect) + if recursive_state&.key?(:inspect) begin push_inspect_key(object) yield From 1dd11fe8c8a238ea88f5abbdb673de313a82ede2 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 10:25:23 +0900 Subject: [PATCH 08/10] [ruby/pp] Suppress warnings in test on Ruby 2.7 TODO: Revert when dropping Ruby 2.7 support. https://github.com/ruby/pp/commit/feb417e152 --- test/test_pp.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/test_pp.rb b/test/test_pp.rb index 8345e5e9873014..4a273e6edd0aec 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -273,8 +273,21 @@ def test_hash_symbol_colon_key def test_hash_in_array omit if RUBY_ENGINE == "jruby" - assert_equal("[{}]", PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup)) - assert_equal("[{}]", PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup)) + assert_equal("[{}]", passing_keywords {PP.singleline_pp([->(*a){a.last.clear}.ruby2_keywords.call(a: 1)], ''.dup)}) + assert_equal("[{}]", passing_keywords {PP.singleline_pp([Hash.ruby2_keywords_hash({})], ''.dup)}) + end + + if RUBY_VERSION >= "3.0" + def passing_keywords(&_) + yield + end + else + def passing_keywords(&_) + verbose, $VERBOSE = $VERBOSE, nil + yield + ensure + $VERBOSE = verbose + end end def test_direct_pp From 674e2ca9452121a3e3066e04ecd51ba8d863cf2b Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 12:59:52 +0900 Subject: [PATCH 09/10] [ruby/pp] Reduce substring creations https://github.com/ruby/pp/commit/fee2d39099 --- lib/pp.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/pp.rb b/lib/pp.rb index 2c7f41eadda188..e790f499d42041 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -313,12 +313,10 @@ def pp_hash(obj) # A pretty print for a pair of Hash def pp_hash_pair(k, v) if Symbol === k - sym_s = k.inspect - if sym_s[1].match?(/["$@!]/) || sym_s[-1].match?(/[%&*+\-\/<=>@\]^`|~]/) - text "#{k.to_s.inspect}:" - else - text "#{k}:" + if k.inspect.match?(%r[\A:["$@!]|[%&*+\-\/<=>@\]^`|~]\z]) + k = k.to_s.inspect end + text "#{k}:" else pp k text ' ' From ab49e8a0f0d61ef38b4006d2e8f3976550ba23f8 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 5 Oct 2025 20:12:35 +0900 Subject: [PATCH 10/10] [ruby/pp] [Feature #21389] Update rubyspec --- spec/ruby/core/set/pretty_print_cycle_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/ruby/core/set/pretty_print_cycle_spec.rb b/spec/ruby/core/set/pretty_print_cycle_spec.rb index c3b383fe808333..d4cca515e2a2d3 100644 --- a/spec/ruby/core/set/pretty_print_cycle_spec.rb +++ b/spec/ruby/core/set/pretty_print_cycle_spec.rb @@ -3,7 +3,12 @@ describe "Set#pretty_print_cycle" do it "passes the 'pretty print' representation of a self-referencing Set to the pretty print writer" do pp = mock("PrettyPrint") - pp.should_receive(:text).with("#") + ruby_version_is(""..."3.5") do + pp.should_receive(:text).with("#") + end + ruby_version_is("3.5") do + pp.should_receive(:text).with("Set[...]") + end Set[1, 2, 3].pretty_print_cycle(pp) end end