summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--lib/csv.rb16
-rwxr-xr-xtest/csv/test_encodings.rb15
3 files changed, 31 insertions, 5 deletions
diff --git a/ChangeLog b/ChangeLog
index 6fe9d662bd..a3d676b4fb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Thu Apr 28 06:07:02 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Thu Apr 28 06:07:06 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/csv.rb (CSV::open): suppress universal newline decorator.
+ fixes #4603
* lib/csv.rb (CSV.read): no mode is needed.
diff --git a/lib/csv.rb b/lib/csv.rb
index 085ee8433d..8925de8293 100644
--- a/lib/csv.rb
+++ b/lib/csv.rb
@@ -1334,10 +1334,18 @@ class CSV
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?
diff --git a/test/csv/test_encodings.rb b/test/csv/test_encodings.rb
index 3880f3ad58..54c34f3f38 100755
--- a/test/csv/test_encodings.rb
+++ b/test/csv/test_encodings.rb
@@ -79,6 +79,21 @@ class TestCSV::Encodings < TestCSV
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 ###
#######################################################################