From 8b3ade213c4dfd05f9bdb939396bbcac84713216 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 14 Apr 2025 18:51:59 -0700 Subject: [PATCH 1/2] Do not issue warning when calling set_encoding if string is chilled StringIO does not warn for unchilled unfrozen string or for frozen string, so it should not warn for chilled string. --- ext/stringio/stringio.c | 8 +++++++- test/stringio/test_stringio.rb | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 89c1ff7..146a662 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -20,6 +20,7 @@ STRINGIO_VERSION = "3.1.6"; #include "ruby.h" #include "ruby/io.h" #include "ruby/encoding.h" +#include "ruby/version.h" #if defined(HAVE_FCNTL_H) || defined(_WIN32) #include #elif defined(HAVE_SYS_FCNTL_H) @@ -1859,7 +1860,12 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self) } } ptr->enc = enc; - if (!NIL_P(ptr->string) && WRITABLE(self)) { + if (!NIL_P(ptr->string) && WRITABLE(self) +#if RUBY_API_VERSION_MAJOR == 3 || RUBY_API_VERSION_MAJOR >= 4 + // Do not attempt to modify chilled strings on Ruby 3.4+ + && !FL_TEST_RAW(ptr->string, RUBY_FL_USER2 | RUBY_FL_USER3) +#endif + ) { rb_enc_associate(ptr->string, enc); } diff --git a/test/stringio/test_stringio.rb b/test/stringio/test_stringio.rb index 570b3d7..002b946 100644 --- a/test/stringio/test_stringio.rb +++ b/test/stringio/test_stringio.rb @@ -1057,6 +1057,13 @@ def test_chilled_string_string_set assert_equal("test", io.string) assert_same(chilled_string, io.string) end + + def test_chilled_string_set_enocoding + chilled_string = eval(%{""}) + io = StringIO.new(chilled_string) + assert_warning("") { io.set_encoding(Encoding::BINARY) } + assert_same(chilled_string, io.string) + end end private From 5a5287ed8ef421dd8e941e195ca83e273639cbd4 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 14 Apr 2025 19:18:49 -0700 Subject: [PATCH 2/2] Fix Ruby 3.4 check Co-authored-by: Sutou Kouhei --- ext/stringio/stringio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/stringio/stringio.c b/ext/stringio/stringio.c index 146a662..3190561 100644 --- a/ext/stringio/stringio.c +++ b/ext/stringio/stringio.c @@ -1861,7 +1861,7 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self) } ptr->enc = enc; if (!NIL_P(ptr->string) && WRITABLE(self) -#if RUBY_API_VERSION_MAJOR == 3 || RUBY_API_VERSION_MAJOR >= 4 +#if (RUBY_API_VERSION_MAJOR == 3 && RUBY_API_VERSION_MINOR >= 4) || RUBY_API_VERSION_MAJOR >= 4 // Do not attempt to modify chilled strings on Ruby 3.4+ && !FL_TEST_RAW(ptr->string, RUBY_FL_USER2 | RUBY_FL_USER3) #endif