[前][次][番号順一覧][スレッド一覧]

ruby-changes:52027

From: nobu <ko1@a...>
Date: Thu, 9 Aug 2018 17:29:43 +0900 (JST)
Subject: [ruby-changes:52027] nobu:r64243 (trunk): io.c: fix non-ascii filename inplace edit

nobu	2018-08-09 17:29:38 +0900 (Thu, 09 Aug 2018)

  New Revision: 64243

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64243

  Log:
    io.c: fix non-ascii filename inplace edit
    
    * io.c (argf_next_argv): convert filename to the OS encoding to be
      dealt with by system calls.  [ruby-dev:50607] [Bug #14970]

  Modified files:
    trunk/io.c
    trunk/test/ruby/test_argf.rb
Index: test/ruby/test_argf.rb
===================================================================
--- test/ruby/test_argf.rb	(revision 64242)
+++ test/ruby/test_argf.rb	(revision 64243)
@@ -35,8 +35,8 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L35
     open("#{@tmpdir}/#{basename}-#{@tmp_count}", "w")
   end
 
-  def make_tempfile
-    t = make_tempfile0("argf-qux")
+  def make_tempfile(basename = "argf-qux")
+    t = make_tempfile0(basename)
     t.puts "foo"
     t.puts "bar"
     t.puts "baz"
@@ -255,6 +255,23 @@ class TestArgf < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_argf.rb#L255
     assert_warning(/#{base}/) {argf.gets}
   end
 
+  def test_inplace_nonascii
+    ext = Encoding.default_external or
+      skip "no default external encoding"
+    t = nil
+    ["\u{3042}", "\u{e9}"].any? {|n| t = make_tempfile(n.encode(ext))} or
+      skip "no name to test"
+    assert_in_out_err(["-i.bak", "-", t.path],
+                      "#{<<~"{#"}\n#{<<~'};'}")
+    {#
+      puts ARGF.gets.chomp + '.new'
+      puts ARGF.gets.chomp + '.new'
+      puts ARGF.gets.chomp + '.new'
+    };
+    assert_equal("foo.new\n""bar.new\n""baz.new\n", File.read(t.path))
+    assert_equal("foo\n""bar\n""baz\n", File.read(t.path + ".bak"))
+  end
+
   def test_inplace_no_backup
     t = make_tempfile
 
Index: io.c
===================================================================
--- io.c	(revision 64242)
+++ io.c	(revision 64243)
@@ -8502,6 +8502,7 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8502
 	    VALUE filename = rb_ary_shift(ARGF.argv);
 	    FilePathValue(filename);
 	    ARGF.filename = filename;
+	    filename = rb_str_encode_ospath(filename);
 	    fn = StringValueCStr(filename);
 	    if (RSTRING_LEN(filename) == 1 && fn[0] == '-') {
 		ARGF.current_file = rb_stdin;
@@ -8603,6 +8604,7 @@ argf_next_argv(VALUE argf) https://github.com/ruby/ruby/blob/trunk/io.c#L8604
 		if (!NIL_P(write_io)) {
 		    rb_io_set_write_io(ARGF.current_file, write_io);
 		}
+		RB_GC_GUARD(filename);
 	    }
 	    if (ARGF.binmode) rb_io_ascii8bit_binmode(ARGF.current_file);
 	    GetOpenFile(ARGF.current_file, fptr);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]