From 58c2571b597b278e93362f86983b18f7d8c19ad2 Mon Sep 17 00:00:00 2001 From: nobu Date: Thu, 28 Aug 2014 06:18:30 +0000 Subject: [PATCH] io.c: preserve encodings * io.c (argf_next_argv): preserve encodings in warning messages. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47306 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- io.c | 21 +++++++++++---------- test/ruby/test_argf.rb | 7 +++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/io.c b/io.c index 62a948093d..f5792542b3 100644 --- a/io.c +++ b/io.c @@ -7878,7 +7878,7 @@ argf_next_argv(VALUE argf) if (ARGF.next_p == 1) { retry: if (RARRAY_LEN(ARGF.argv) > 0) { - ARGF.filename = rb_ary_shift(ARGF.argv); + ARGF.filename = rb_str_encode_ospath(rb_ary_shift(ARGF.argv)); fn = StringValueCStr(ARGF.filename); if (strlen(fn) == 1 && fn[0] == '-') { ARGF.current_file = rb_stdin; @@ -7904,21 +7904,22 @@ argf_next_argv(VALUE argf) } fstat(fr, &st); if (*ARGF.inplace) { - str = rb_str_new2(fn); + str = rb_str_dup(ARGF.filename); rb_str_cat2(str, ARGF.inplace); + /* TODO: encoding of ARGF.inplace */ #ifdef NO_SAFE_RENAME (void)close(fr); (void)unlink(RSTRING_PTR(str)); if (rename(fn, RSTRING_PTR(str)) < 0) { - rb_warn("Can't rename %s to %s: %s, skipping file", - fn, RSTRING_PTR(str), strerror(errno)); + rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file", + ARGF.filename, str, strerror(errno)); goto retry; } fr = rb_sysopen(str, O_RDONLY, 0); #else if (rename(fn, RSTRING_PTR(str)) < 0) { - rb_warn("Can't rename %s to %s: %s, skipping file", - fn, RSTRING_PTR(str), strerror(errno)); + rb_warn("Can't rename %"PRIsVALUE" to %"PRIsVALUE": %s, skipping file", + ARGF.filename, str, strerror(errno)); close(fr); goto retry; } @@ -7929,8 +7930,8 @@ argf_next_argv(VALUE argf) rb_fatal("Can't do inplace edit without backup"); #else if (unlink(fn) < 0) { - rb_warn("Can't remove %s: %s, skipping file", - fn, strerror(errno)); + rb_warn("Can't remove %"PRIsVALUE": %s, skipping file", + ARGF.filename, strerror(errno)); close(fr); goto retry; } @@ -7953,8 +7954,8 @@ argf_next_argv(VALUE argf) #endif if (err && getuid() == 0 && st2.st_uid == 0) { const char *wkfn = RSTRING_PTR(ARGF.filename); - rb_warn("Can't set owner/group of %s to same as %s: %s, skipping file", - wkfn, fn, strerror(errno)); + rb_warn("Can't set owner/group of %"PRIsVALUE" to same as %"PRIsVALUE": %s, skipping file", + ARGF.filename, str, strerror(errno)); (void)close(fr); (void)close(fw); (void)unlink(wkfn); diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index fb869561a1..95d694116f 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -212,6 +212,13 @@ class TestArgf < Test::Unit::TestCase assert_equal([], r) assert_equal("foo\nbar\nbaz\n", File.read(t.path)) end + + base = "argf-\u{30c6 30b9 30c8}" + name = "#{@tmpdir}/#{base}" + File.write(name, "foo") + argf = ARGF.class.new(name) + argf.inplace_mode = '/\\:' + assert_warning(/#{base}/) {argf.gets} end def test_inplace_no_backup