mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[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.
a4b528c209
This commit is contained in:
parent
4e33a87879
commit
cee10c1b70
Notes:
git
2020-07-20 03:35:40 +09:00
2 changed files with 11 additions and 1 deletions
|
@ -156,7 +156,7 @@ class CSV
|
||||||
else
|
else
|
||||||
field = String(field) # Stringify fields
|
field = String(field) # Stringify fields
|
||||||
# represent empty fields as empty quoted 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)
|
quote_field(field)
|
||||||
else
|
else
|
||||||
field # unquoted field
|
field # unquoted field
|
||||||
|
|
|
@ -125,6 +125,16 @@ class TestCSVInterfaceRead < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
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
|
def test_open_with_undef_replace
|
||||||
# U+00B7 Middle Dot
|
# U+00B7 Middle Dot
|
||||||
CSV.open(@input.path, "w", encoding: Encoding::CP932, undef: :replace) do |rows|
|
CSV.open(@input.path, "w", encoding: Encoding::CP932, undef: :replace) do |rows|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue