Skip to content

Commit a13a4b2

Browse files
committed
Fix wrong EOF detection for ARGF
GitHub: fix GH-328 We can't use ARGF.eof? to detect EOF of ARGF. Because ARGF.eof? returns true when an file reached EOF but ARGF may use multiple files. Reported by Takeshi Nishimatsu. Thanks!!!
1 parent f9254d4 commit a13a4b2

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/csv/parser.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ class Parser
1818
# into your Encoding.
1919
#
2020

21+
class << self
22+
ARGF_OBJECT_ID = ARGF.object_id
23+
# Convenient method to check whether the give input reached EOF
24+
# or not.
25+
def eof?(input)
26+
# We can't use input != ARGF in Ractor. Because ARGF isn't a
27+
# shareable object.
28+
input.object_id != ARGF_OBJECT_ID and
29+
input.respond_to?(:eof) and
30+
input.eof?
31+
end
32+
end
33+
2134
# Raised when encoding is invalid.
2235
class InvalidEncoding < StandardError
2336
end
@@ -312,7 +325,7 @@ def read_chunk
312325
raise InvalidEncoding unless chunk.valid_encoding?
313326
# trace(__method__, :chunk, chunk)
314327
@scanner = StringScanner.new(chunk)
315-
if input.respond_to?(:eof?) and input.eof?
328+
if Parser.eof?(input)
316329
@inputs.shift
317330
@last_scanner = @inputs.empty?
318331
end
@@ -869,10 +882,7 @@ def build_scanner
869882
string = nil
870883
if @samples.empty? and @input.is_a?(StringIO)
871884
string = @input.read
872-
elsif @samples.size == 1 and
873-
@input != ARGF and
874-
@input.respond_to?(:eof?) and
875-
@input.eof?
885+
elsif @samples.size == 1 and Parser.eof?(@input)
876886
string = @samples[0]
877887
end
878888
if string

0 commit comments

Comments
 (0)