Skip to content

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

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

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.
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.

1 participant