[Bug #20984] ENV.inspect should be encoding aware

This commit is contained in:
Nobuyoshi Nakada 2024-12-26 13:42:56 +09:00
parent 037bffec07
commit 19c39e4cfa
No known key found for this signature in database
GPG Key ID: 3582D74E1FEE4465
Notes: git 2024-12-26 05:23:42 +00:00
2 changed files with 13 additions and 7 deletions

13
hash.c
View File

@ -5909,24 +5909,23 @@ env_to_s(VALUE _)
static VALUE static VALUE
env_inspect(VALUE _) env_inspect(VALUE _)
{ {
VALUE i;
VALUE str = rb_str_buf_new2("{"); VALUE str = rb_str_buf_new2("{");
rb_encoding *enc = env_encoding();
ENV_LOCK(); ENV_LOCK();
{ {
char **env = GET_ENVIRON(environ); char **env = GET_ENVIRON(environ);
while (*env) { while (*env) {
char *s = strchr(*env, '='); const char *s = strchr(*env, '=');
if (env != environ) { if (env != environ) {
rb_str_buf_cat2(str, ", "); rb_str_buf_cat2(str, ", ");
} }
if (s) { if (s) {
rb_str_buf_cat2(str, "\""); rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(*env, s-*env, enc)));
rb_str_buf_cat(str, *env, s-*env); rb_str_buf_cat2(str, "=>");
rb_str_buf_cat2(str, "\"=>"); s++;
i = rb_inspect(rb_str_new2(s+1)); rb_str_buf_append(str, rb_str_inspect(env_enc_str_new(s, strlen(s), enc)));
rb_str_buf_append(str, i);
} }
env++; env++;
} }

View File

@ -357,6 +357,13 @@ class TestEnv < Test::Unit::TestCase
assert_equal(expected, s) assert_equal(expected, s)
end end
def test_inspect_encoding
ENV.clear
key = "VAR\u{e5 e1 e2 e4 e3 101 3042}"
ENV[key] = "foo"
assert_equal(%{{"VAR\u{e5 e1 e2 e4 e3 101 3042}"=>"foo"}}, ENV.inspect)
end
def test_to_a def test_to_a
ENV.clear ENV.clear
ENV["foo"] = "bar" ENV["foo"] = "bar"