Repository navigation
Report the right line for invalid encoding in an IO sample - #393
Open
sourya2704 wants to merge 1 commit into
Open
sourya2704 wants to merge 1 commit into
sourya2704 wants to merge 1 commit into
Conversation
GitHub: fix rubyGH-385 With row_sep: :auto, a File or another IO is sampled to detect the row separator. build_scanner computed the line of an invalid byte only when the sample was the whole input. Otherwise the sample went to InputsScanner, which raised InvalidEncoding for the whole sample before any row was parsed, so the error always said line 1. Check the samples the same way before building InputsScanner. They're joined only when one of them is invalid, so valid input isn't copied.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix GH-385.
With
row_sep: :auto, CSV reads a sample from aFileor another IO to detect the row separator.build_scanneronly finds the line of an invalid byte when that sample is the whole input. Otherwise the sample goes toInputsScanner, whose firstread_chunkraisesInvalidEncodingfor the whole sample before any row is parsed.@scanneris stillnil, soparsereports line 1.This checks the samples the same way before building
InputsScanner. The existing check is moved into acheck_encodinghelper and used for both cases. As @wtn suggested in the issue, the samples are joined only when one of them is invalid, so valid input isn't copied.With the script from the issue:
A pipe (
IO.popen) and a bad byte on line 3 give the right line too. A bad byte after the sample was already reported correctly and still is.The issue's "small" file also took the
InputsScannerpath.gets(nil, 32 * 1024)stops after the invalid byte, so the input isn't at EOF after the sample.Tests: two tests in
test/csv/interface/test_read.rb, one for a file that fits in the sample and one for a file longer than it. They usereadbecause withCSV_PARSER_SCANNER_TEST=yesthe error is raised later, not on the firstshift. Both fail without the fix. The full suite passes with and withoutCSV_PARSER_SCANNER_TEST=yes.