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

[ruby/csv] Don't raise on eof?

GitHub: fix #86

Reported by krororo. Thanks!!!

https://github.com/ruby/csv/commit/5a8d9d9297
This commit is contained in:
Kouhei Sutou 2019-04-17 22:02:40 +09:00 committed by Hiroshi SHIBATA
parent 9171f83305
commit 8392592a0a
2 changed files with 14 additions and 0 deletions

View file

@ -955,6 +955,8 @@ class CSV
strip: strip,
}
@parser = nil
@parser_enumerator = nil
@eof_error = nil
@writer_options = {
encoding: @encoding,
@ -1156,9 +1158,13 @@ class CSV
end
def eof?
return false if @eof_error
begin
parser_enumerator.peek
false
rescue MalformedCSVError => error
@eof_error = error
false
rescue StopIteration
true
end
@ -1169,6 +1175,7 @@ class CSV
def rewind
@parser = nil
@parser_enumerator = nil
@eof_error = nil
@writer.rewind if @writer
@io.rewind
end
@ -1264,6 +1271,10 @@ class CSV
# The data source must be open for reading.
#
def shift
if @eof_error
eof_error, @eof_error = @eof_error, nil
raise eof_error
end
begin
parser_enumerator.next
rescue StopIteration

View file

@ -25,12 +25,15 @@ ggg,hhh,iii
csv.shift)
assert_equal(CSV::Row.new(headers, ["aaa", "bbb", "ccc"]),
csv.shift)
assert_equal(false, csv.eof?)
error = assert_raise(CSV::MalformedCSVError) do
csv.shift
end
assert_equal("Illegal quoting in line 3.",
error.message)
assert_equal(false, csv.eof?)
assert_equal(CSV::Row.new(headers, ["ggg", "hhh", "iii"]),
csv.shift)
assert_equal(true, csv.eof?)
end
end