Skip to content

Commit

Permalink
Fix an error for CSV.open (#131)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koic committed Jun 4, 2020
1 parent cff8b18 commit a4b528c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/csv/writer.rb
Expand Up @@ -156,7 +156,7 @@ def quote(field)
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
Expand Down
10 changes: 10 additions & 0 deletions test/csv/interface/test_read.rb
Expand Up @@ -125,6 +125,16 @@ def test_open_encoding_utf_8_with_bom
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|
Expand Down

0 comments on commit a4b528c

Please sign in to comment.