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

ruby-changes:35504

From: nagachika <ko1@a...>
Date: Mon, 15 Sep 2014 00:25:48 +0900 (JST)
Subject: [ruby-changes:35504] nagachika:r47586 (ruby_2_1): merge revision(s) r46391, r46395: [Backport #9766]

nagachika	2014-09-15 00:25:34 +0900 (Mon, 15 Sep 2014)

  New Revision: 47586

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

  Log:
    merge revision(s) r46391,r46395: [Backport #9766]
    
    * lib/csv.rb (CSV#<<): honor explicity given encoding.  based on
      the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at
      [ruby-core:62113].  [Bug #9766]
    
    * lib/csv.rb (CSV#<<): honor explicitly given encoding.  based on

  Modified directories:
    branches/ruby_2_1/
  Modified files:
    branches/ruby_2_1/ChangeLog
    branches/ruby_2_1/lib/csv.rb
    branches/ruby_2_1/test/csv/test_encodings.rb
    branches/ruby_2_1/version.h
Index: ruby_2_1/ChangeLog
===================================================================
--- ruby_2_1/ChangeLog	(revision 47585)
+++ ruby_2_1/ChangeLog	(revision 47586)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/ChangeLog#L1
+Mon Sep 15 00:02:20 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/csv.rb (CSV#<<): honor explicitly given encoding.  based on
+	  the patch by DAISUKE TANIWAKI <daisuketaniwaki AT gmail.com> at
+	  [ruby-core:62113].  [Bug #9766]
+
 Wed Sep 10 23:36:38 2014  Koichi Sasada  <ko1@a...>
 
 	* test/ruby/test_object.rb: extend timeout.
Index: ruby_2_1/lib/csv.rb
===================================================================
--- ruby_2_1/lib/csv.rb	(revision 47585)
+++ ruby_2_1/lib/csv.rb	(revision 47586)
@@ -1148,9 +1148,9 @@ class CSV https://github.com/ruby/ruby/blob/trunk/ruby_2_1/lib/csv.rb#L1148
       io.seek(0, IO::SEEK_END)
       args.unshift(io)
     else
-      encoding = (args[-1] = args[-1].dup).delete(:encoding) if args.last.is_a?(Hash)
+      encoding = args[-1][:encoding] if args.last.is_a?(Hash)
       str      = ""
-      str.encode!(encoding) if encoding
+      str.force_encoding(encoding) if encoding
       args.unshift(str)
     end
     csv = new(*args)  # wrap
@@ -1515,7 +1515,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/ruby_2_1/lib/csv.rb#L1515
     init_headers(options)
     init_comments(options)
 
-    options.delete(:encoding)
+    @force_encoding = !!(encoding || options.delete(:encoding))
     options.delete(:internal_encoding)
     options.delete(:external_encoding)
     unless options.empty?
@@ -1655,10 +1655,13 @@ class CSV https://github.com/ruby/ruby/blob/trunk/ruby_2_1/lib/csv.rb#L1655
 
     output = row.map(&@quote).join(@col_sep) + @row_sep  # quote and separate
     if @io.is_a?(StringIO)             and
-       output.encoding != raw_encoding and
-       (compatible_encoding = Encoding.compatible?(@io.string, output))
-      @io.set_encoding(compatible_encoding)
-      @io.seek(0, IO::SEEK_END)
+       output.encoding != (encoding = raw_encoding)
+      if @force_encoding
+        output = output.encode(encoding)
+      elsif (compatible_encoding = Encoding.compatible?(@io.string, output))
+        @io.set_encoding(compatible_encoding)
+        @io.seek(0, IO::SEEK_END)
+      end
     end
     @io << output
 
Index: ruby_2_1/version.h
===================================================================
--- ruby_2_1/version.h	(revision 47585)
+++ ruby_2_1/version.h	(revision 47586)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_1/version.h#L1
 #define RUBY_VERSION "2.1.2"
-#define RUBY_RELEASE_DATE "2014-09-10"
-#define RUBY_PATCHLEVEL 235
+#define RUBY_RELEASE_DATE "2014-09-15"
+#define RUBY_PATCHLEVEL 236
 
 #define RUBY_RELEASE_YEAR 2014
 #define RUBY_RELEASE_MONTH 9
-#define RUBY_RELEASE_DAY 10
+#define RUBY_RELEASE_DAY 15
 
 #include "ruby/version.h"
 
Index: ruby_2_1/test/csv/test_encodings.rb
===================================================================
--- ruby_2_1/test/csv/test_encodings.rb	(revision 47585)
+++ ruby_2_1/test/csv/test_encodings.rb	(revision 47586)
@@ -257,6 +257,14 @@ class TestCSV::Encodings < TestCSV https://github.com/ruby/ruby/blob/trunk/ruby_2_1/test/csv/test_encodings.rb#L257
     assert_equal("UTF-8",      data.to_csv.encoding.name)
   end
 
+  def test_explicit_encoding
+    bug9766 = '[ruby-core:62113] [Bug #9766]'
+    s = CSV.generate(encoding: "Windows-31J") do |csv|
+      csv << ["foo".force_encoding("ISO-8859-1"), "\u3042"]
+    end
+    assert_equal(["foo,\u3042\n".encode(Encoding::Windows_31J), Encoding::Windows_31J], [s, s.encoding], bug9766)
+  end
+
   private
 
   def assert_parses(fields, encoding, options = { })

Property changes on: ruby_2_1
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r46391,46395


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

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