ruby-changes:62301
From: Koichi <ko1@a...>
Date: Mon, 20 Jul 2020 03:35:53 +0900 (JST)
Subject: [ruby-changes:62301] cee10c1b70 (master): [ruby/csv] Fix an error for `CSV.open` (#131)
https://git.ruby-lang.org/ruby.git/commit/?id=cee10c1b70 From cee10c1b7033f4682b3f4c76220c0c190e70aa1a Mon Sep 17 00:00:00 2001 From: Koichi ITO <koic.ito@g...> Date: Thu, 4 Jun 2020 12:08:05 +0900 Subject: [ruby/csv] Fix an error for `CSV.open` (#131) Follow up to https://github.com/ruby/csv/pull/130/files#r434885191. This PR fixes `ArgumentError` for `CSV.open` when processing invalid byte sequence in UTF-8. https://github.com/ruby/csv/commit/a4b528c209 diff --git a/lib/csv/writer.rb b/lib/csv/writer.rb index 9243d23..6c02d74 100644 --- a/lib/csv/writer.rb +++ b/lib/csv/writer.rb @@ -156,7 +156,7 @@ class CSV https://github.com/ruby/ruby/blob/trunk/lib/csv/writer.rb#L156 else field = String(field) # Stringify fields # represent empty fields as empty quoted fields - if (@quote_empty and field.empty?) or @quotable_pattern.match?(field) + if (@quote_empty and field.empty?) or (field.valid_encoding? and @quotable_pattern.match?(field)) quote_field(field) else field # unquoted field diff --git a/test/csv/interface/test_read.rb b/test/csv/interface/test_read.rb index 4b0f5e7..a2bcced 100644 --- a/test/csv/interface/test_read.rb +++ b/test/csv/interface/test_read.rb @@ -125,6 +125,16 @@ class TestCSVInterfaceRead < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/csv/interface/test_read.rb#L125 end end + def test_open_invalid_byte_sequence_in_utf_8 + CSV.open(@input.path, "w", encoding: Encoding::CP932) do |rows| + error = assert_raise(Encoding::InvalidByteSequenceError) do + rows << ["\x82\xa0"] + end + assert_equal('"\x82" on UTF-8', + error.message) + end + end + def test_open_with_undef_replace # U+00B7 Middle Dot CSV.open(@input.path, "w", encoding: Encoding::CP932, undef: :replace) do |rows| -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/