ruby-changes:50983
From: nobu <ko1@a...>
Date: Thu, 19 Apr 2018 16:05:45 +0900 (JST)
Subject: [ruby-changes:50983] nobu:r63190 (trunk): dir.c: warning for NUL
nobu 2018-04-19 16:05:39 +0900 (Thu, 19 Apr 2018) New Revision: 63190 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63190 Log: dir.c: warning for NUL * dir.c (rb_push_glob): warn NUL-separated glob patterns. [Feature #14643] Modified files: trunk/NEWS trunk/dir.c trunk/spec/ruby/core/dir/shared/glob.rb trunk/test/ruby/test_dir.rb Index: spec/ruby/core/dir/shared/glob.rb =================================================================== --- spec/ruby/core/dir/shared/glob.rb (revision 63189) +++ spec/ruby/core/dir/shared/glob.rb (revision 63190) @@ -25,9 +25,11 @@ describe :dir_glob, shared: true do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/dir/shared/glob.rb#L25 Dir.send(@method, obj).should == %w[file_one.ext] end - it "splits the string on \\0 if there is only one string given" do - Dir.send(@method, "file_o*\0file_t*").should == - %w!file_one.ext file_two.ext! + ruby_version_is ""..."2.6" do + it "splits the string on \\0 if there is only one string given" do + Dir.send(@method, "file_o*\0file_t*").should == + %w!file_one.ext file_two.ext! + end end it "matches non-dotfiles with '*'" do Index: dir.c =================================================================== --- dir.c (revision 63189) +++ dir.c (revision 63190) @@ -2536,6 +2536,7 @@ rb_push_glob(VALUE str, VALUE base, int https://github.com/ruby/ruby/blob/trunk/dir.c#L2536 long offset = 0; long len; VALUE ary; + int warned = FALSE; /* can contain null bytes as separators */ if (!RB_TYPE_P((str), T_STRING)) { @@ -2553,6 +2554,10 @@ rb_push_glob(VALUE str, VALUE base, int https://github.com/ruby/ruby/blob/trunk/dir.c#L2554 const char *pbeg = RSTRING_PTR(str), *p = pbeg + offset; const char *pend = memchr(p, '\0', rest); if (pend) { + if (!warned) { + rb_warn("use glob patterns list instead of nul-separated patterns"); + warned = TRUE; + } rest = ++pend - p; offset = pend - pbeg; } Index: NEWS =================================================================== --- NEWS (revision 63189) +++ NEWS (revision 63190) @@ -152,6 +152,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L152 and File.readlines do not invoke external commands even if the path starts with the pipe character |. [Feature #14245] +* Dir + + * Dir.glob with '\0'-separated pattern list will be deprecated, and + is now warned. [Feature #14643] + === Stdlib compatibility issues (excluding feature bug fixes) === C API updates Index: test/ruby/test_dir.rb =================================================================== --- test/ruby/test_dir.rb (revision 63189) +++ test/ruby/test_dir.rb (revision 63190) @@ -136,8 +136,10 @@ class TestDir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_dir.rb#L136 Dir.glob(File.join(@root, "*"), File::FNM_DOTMATCH).sort) assert_equal([@root] + ("a".."z").map {|f| File.join(@root, f) }.sort, Dir.glob([@root, File.join(@root, "*")]).sort) - assert_equal([@root] + ("a".."z").map {|f| File.join(@root, f) }.sort, - Dir.glob(@root + "\0\0\0" + File.join(@root, "*")).sort) + assert_warning(/nul-separated patterns/) do + assert_equal([@root] + ("a".."z").map {|f| File.join(@root, f) }.sort, + Dir.glob(@root + "\0\0\0" + File.join(@root, "*")).sort) + end assert_equal(("a".."z").step(2).map {|f| File.join(File.join(@root, f), "") }.sort, Dir.glob(File.join(@root, "*/")).sort) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/