Skip to content

Do not issue warning when calling set_encoding if string is chilled #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion ext/stringio/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <fcntl.h>
#elif defined(HAVE_SYS_FCNTL_H)
Expand Down Expand Up @@ -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_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
) {
rb_enc_associate(ptr->string, enc);
}

Expand Down
7 changes: 7 additions & 0 deletions test/stringio/test_stringio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down