From 0c94ae048a277485aa691820277108c2335ec3d3 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Mon, 12 May 2025 17:51:05 +0200 Subject: [PATCH] [ruby/psych] Fix dumping `StringIO` (and potentially others) on Ruby <= 2.7 In Ruby < 3.0, the superclass of StringIO was actually already `Data`, but it doesn't have the expected shape. So, on these earlier versions it errors: > NoMethodError: undefined method `members' for # > vendor/bundle/ruby/2.6.0/gems/psych-5.2.5/lib/psych/visitors/yaml_tree.rb:170:in `visit_Data' This test doesn't fail on 2.7, presumably because it can pull in a newer `stringio` version. https://github.com/ruby/psych/commit/0f40f56268 --- ext/psych/lib/psych/visitors/yaml_tree.rb | 2 +- test/psych/test_stringio.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 test/psych/test_stringio.rb diff --git a/ext/psych/lib/psych/visitors/yaml_tree.rb b/ext/psych/lib/psych/visitors/yaml_tree.rb index d7958a8431..b6c86f4c94 100644 --- a/ext/psych/lib/psych/visitors/yaml_tree.rb +++ b/ext/psych/lib/psych/visitors/yaml_tree.rb @@ -198,7 +198,7 @@ module Psych @emitter.end_mapping end - end + end unless RUBY_VERSION < "3.2" def visit_Struct o tag = ['!ruby/struct', o.class.name].compact.join(':') diff --git a/test/psych/test_stringio.rb b/test/psych/test_stringio.rb new file mode 100644 index 0000000000..7fef1402a0 --- /dev/null +++ b/test/psych/test_stringio.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true +require_relative 'helper' + +module Psych + class TestStringIO < TestCase + # The superclass of StringIO before Ruby 3.0 was `Data`, + # which can interfere with the Ruby 3.2+ `Data` dumping. + def test_stringio + assert_nothing_raised do + Psych.dump(StringIO.new("foo")) + end + end + end +end