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

ruby-changes:19330

From: nobu <ko1@a...>
Date: Thu, 28 Apr 2011 06:07:18 +0900 (JST)
Subject: [ruby-changes:19330] Ruby:r31370 (trunk): * lib/csv.rb (CSV::open): suppress universal newline decorator.

nobu	2011-04-28 06:07:09 +0900 (Thu, 28 Apr 2011)

  New Revision: 31370

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

  Log:
    * lib/csv.rb (CSV::open): suppress universal newline decorator.
      fixes #4603

  Modified files:
    trunk/ChangeLog
    trunk/lib/csv.rb
    trunk/test/csv/test_encodings.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31369)
+++ ChangeLog	(revision 31370)
@@ -1,5 +1,8 @@
-Thu Apr 28 06:07:02 2011  Nobuyoshi Nakada  <nobu@r...>
+Thu Apr 28 06:07:06 2011  Nobuyoshi Nakada  <nobu@r...>
 
+	* lib/csv.rb (CSV::open): suppress universal newline decorator.
+	  fixes #4603
+
 	* lib/csv.rb (CSV.read): no mode is needed.
 
 Thu Apr 28 06:06:56 2011  Nobuyoshi Nakada  <nobu@r...>
Index: lib/csv.rb
===================================================================
--- lib/csv.rb	(revision 31369)
+++ lib/csv.rb	(revision 31370)
@@ -1334,10 +1334,18 @@
   def self.open(*args)
     # find the +options+ Hash
     options = if args.last.is_a? Hash then args.pop else Hash.new end
-    # default to a binary open mode
-    args << "rb" if args.size == 1 and !options.key?(:mode)
-    # wrap a File opened with the remaining +args+
-    csv     = new(File.open(*args, options), options)
+    # wrap a File opened with the remaining +args+ with no newline
+    # decorator
+    file_opts = {universal_newline: false}.merge(options)
+    begin
+      f = File.open(*args, file_opts)
+    rescue ArgumentError => e
+      raise unless /needs binmode/ =~ e.message and args.size == 1
+      args << "rb"
+      file_opts = {encoding: Encoding.default_external}.merge(file_opts)
+      retry
+    end
+    csv = new(f, options)
 
     # handle blocks like Ruby's open(), not like the CSV library
     if block_given?
Index: test/csv/test_encodings.rb
===================================================================
--- test/csv/test_encodings.rb	(revision 31369)
+++ test/csv/test_encodings.rb	(revision 31370)
@@ -79,6 +79,21 @@
     end
   end
 
+  def test_read_with_default_encoding
+    data = "abc"
+    default_external = Encoding.default_external
+    each_encoding do |encoding|
+      File.open(@temp_csv_path, "wb", encoding: encoding) {|f| f << data}
+      begin
+        Encoding.default_external = encoding
+        result = CSV.read(@temp_csv_path)[0][0]
+      ensure
+        Encoding.default_external = default_external
+      end
+      assert_equal(encoding, result.encoding)
+    end
+  end
+
   #######################################################################
   ### Stress Test ASCII Compatible and Non-ASCII Compatible Encodings ###
   #######################################################################

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

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