diff options
| author | nagachika <nagachika@ruby-lang.org> | 2025-01-11 15:28:05 +0900 |
|---|---|---|
| committer | nagachika <nagachika@ruby-lang.org> | 2025-01-11 15:28:05 +0900 |
| commit | 4e59c10646cf012d457da99ad258b0e6a3e42132 (patch) | |
| tree | 3614ee1fffc4cbb69a50297f32224689d23644eb | |
| parent | 1d57924d693534b10699a375cb8400e57a67031c (diff) | |
merge revision(s) 19c39e4cfaa467e69b9848c9c5496d7f50d39c7f, d78ff6a767ca813ac5fa178dd7611f20a993c191: [Backport #20984]
[Bug #20984] ENV.inspect should be encoding aware
[Bug #20984] Fix test with locale encoding
| -rw-r--r-- | hash.c | 13 | ||||
| -rw-r--r-- | test/ruby/test_env.rb | 16 | ||||
| -rw-r--r-- | version.h | 2 |
3 files changed, 19 insertions, 12 deletions
@@ -6027,24 +6027,23 @@ env_to_s(VALUE _) static VALUE env_inspect(VALUE _) { - VALUE i; VALUE str = rb_str_buf_new2("{"); + rb_encoding *enc = env_encoding(); ENV_LOCK(); { char **env = GET_ENVIRON(environ); while (*env) { - char *s = strchr(*env, '='); + const char *s = strchr(*env, '='); if (env != environ) { rb_str_buf_cat2(str, ", "); } if (s) { - rb_str_buf_cat2(str, "\""); - rb_str_buf_cat(str, *env, s-*env); - rb_str_buf_cat2(str, "\"=>"); - i = rb_inspect(rb_str_new2(s+1)); - rb_str_buf_append(str, i); + rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(*env, s-*env, enc))); + rb_str_buf_cat2(str, "=>"); + s++; + rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(s, strlen(s), enc))); } env++; } diff --git a/test/ruby/test_env.rb b/test/ruby/test_env.rb index cdadeac148..949913b590 100644 --- a/test/ruby/test_env.rb +++ b/test/ruby/test_env.rb @@ -2,7 +2,9 @@ require 'test/unit' class TestEnv < Test::Unit::TestCase - IGNORE_CASE = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM + windows = /bccwin|mswin|mingw/ =~ RUBY_PLATFORM + IGNORE_CASE = windows + ENCODING = windows ? Encoding::UTF_8 : Encoding.find("locale") PATH_ENV = "PATH" INVALID_ENVVARS = [ "foo\0bar", @@ -353,6 +355,13 @@ class TestEnv < Test::Unit::TestCase end end + def test_inspect_encoding + ENV.clear + key = "VAR\u{e5 e1 e2 e4 e3 101 3042}" + ENV[key] = "foo" + assert_equal(%{{#{(key.encode(ENCODING) rescue key.b).inspect}=>"foo"}}, ENV.inspect) + end + def test_to_a ENV.clear ENV["foo"] = "bar" @@ -403,8 +412,7 @@ class TestEnv < Test::Unit::TestCase assert_equal("foo", v) end assert_invalid_env {|var| ENV.assoc(var)} - encoding = /mswin|mingw/ =~ RUBY_PLATFORM ? Encoding::UTF_8 : Encoding.find("locale") - assert_equal(encoding, v.encoding) + assert_equal(ENCODING, v.encoding) end def test_has_value2 @@ -517,7 +525,7 @@ class TestEnv < Test::Unit::TestCase assert_equal(huge_value, ENV["foo"]) end - if /mswin|mingw/ =~ RUBY_PLATFORM + if windows def windows_version @windows_version ||= %x[ver][/Version (\d+)/, 1].to_i end @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 6 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 246 +#define RUBY_PATCHLEVEL 247 #include "ruby/version.h" #include "ruby/internal/abi.h" |
