Skip to content

gh-153634: Raise csv.Error, not ValueError, from csv.Sniffer for an inconsistent guessed dialect#153635

Closed
tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:csv-sniffer-error
Closed

gh-153634: Raise csv.Error, not ValueError, from csv.Sniffer for an inconsistent guessed dialect#153635
tonghuaroot wants to merge 3 commits into
python:mainfrom
tonghuaroot:csv-sniffer-error

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

csv.Sniffer.sniff() could return a dialect whose delimiter equals its quotechar, so using it (directly or via has_header) leaked a raw ValueError from _csv instead of csv.Error. Validate the guessed dialect before returning it and re-raise the ValueError as csv.Error; the throwaway reader validates the full _csv invariant set, so future _guess_* changes cannot silently return another invalid dialect.

…r an inconsistent guessed dialect

csv.Sniffer.sniff could return a dialect whose delimiter equaled its quote character. Using that dialect (directly, or via csv.Sniffer.has_header, which builds a reader from the sniffed dialect) leaked a raw ValueError from the _csv extension instead of the module's own csv.Error. sniff now validates its guessed dialect before returning it and re-raises the _csv ValueError as csv.Error, so both sniff and has_header report the module exception type. The direct csv.reader and csv.writer paths and the Dialect._validate TypeError/ValueError behavior are unchanged.
@serhiy-storchaka

Copy link
Copy Markdown
Member

Thank you for the patch, but I think that the fix should be made earlier: the delimiter should not be guessed to be the detected quote character in the first place. _guess_quote_and_delimiter() already excludes quote characters from the delimiter candidates, and only _guess_delimiter(), which counts the frequency of every ASCII character, can return one.

Note also that on the main branch the sniffer was rewritten (gh-83273) and it already raises csv.Error for these samples, so this change is only needed for the maintenance branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants