1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/csv.rb (CSV::open): suppress universal newline decorator.

fixes #4603

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31370 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-04-27 21:07:09 +00:00
parent 2f348762fb
commit e858442f4f
3 changed files with 31 additions and 5 deletions

View file

@ -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. * lib/csv.rb (CSV.read): no mode is needed.

View file

@ -1334,10 +1334,18 @@ class CSV
def self.open(*args) def self.open(*args)
# find the +options+ Hash # find the +options+ Hash
options = if args.last.is_a? Hash then args.pop else Hash.new end options = if args.last.is_a? Hash then args.pop else Hash.new end
# default to a binary open mode # wrap a File opened with the remaining +args+ with no newline
args << "rb" if args.size == 1 and !options.key?(:mode) # decorator
# wrap a File opened with the remaining +args+ file_opts = {universal_newline: false}.merge(options)
csv = new(File.open(*args, options), 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 # handle blocks like Ruby's open(), not like the CSV library
if block_given? if block_given?

View file

@ -79,6 +79,21 @@ class TestCSV::Encodings < TestCSV
end end
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 ### ### Stress Test ASCII Compatible and Non-ASCII Compatible Encodings ###
####################################################################### #######################################################################