Skip to content

Commit 8b3ade2

Browse files
committed
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.
1 parent cb80e8d commit 8b3ade2

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ext/stringio/stringio.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ STRINGIO_VERSION = "3.1.6";
2020
#include "ruby.h"
2121
#include "ruby/io.h"
2222
#include "ruby/encoding.h"
23+
#include "ruby/version.h"
2324
#if defined(HAVE_FCNTL_H) || defined(_WIN32)
2425
#include <fcntl.h>
2526
#elif defined(HAVE_SYS_FCNTL_H)
@@ -1859,7 +1860,12 @@ strio_set_encoding(int argc, VALUE *argv, VALUE self)
18591860
}
18601861
}
18611862
ptr->enc = enc;
1862-
if (!NIL_P(ptr->string) && WRITABLE(self)) {
1863+
if (!NIL_P(ptr->string) && WRITABLE(self)
1864+
#if RUBY_API_VERSION_MAJOR == 3 || RUBY_API_VERSION_MAJOR >= 4
1865+
// Do not attempt to modify chilled strings on Ruby 3.4+
1866+
&& !FL_TEST_RAW(ptr->string, RUBY_FL_USER2 | RUBY_FL_USER3)
1867+
#endif
1868+
) {
18631869
rb_enc_associate(ptr->string, enc);
18641870
}
18651871

test/stringio/test_stringio.rb

+7
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,13 @@ def test_chilled_string_string_set
10571057
assert_equal("test", io.string)
10581058
assert_same(chilled_string, io.string)
10591059
end
1060+
1061+
def test_chilled_string_set_enocoding
1062+
chilled_string = eval(%{""})
1063+
io = StringIO.new(chilled_string)
1064+
assert_warning("") { io.set_encoding(Encoding::BINARY) }
1065+
assert_same(chilled_string, io.string)
1066+
end
10601067
end
10611068

10621069
private

0 commit comments

Comments
 (0)