Skip to content

Hash a non-str name in csv.get_dialect() and csv.unregister_dialect() - #9514

Merged
youknowone merged 2 commits into
RustPython:mainfrom
jseop-lim:fix/csv-dialect-name-hash
Nov 1, 2026
Merged

youknowone merged 2 commits into
RustPython:mainfrom
jseop-lim:fix/csv-dialect-name-hash

Conversation

@jseop-lim

@jseop-lim jseop-lim commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

get_dialect and unregister_dialect turn any name that is not a str into _csv.Error without hashing it. CPython looks the name up in the registry dict with PyDict_GetItemRef and PyDict_Pop, so an unhashable name raises TypeError, a name whose __hash__ raises propagates that exception, and only a hashable name that is not registered gives unknown dialect.

AS-IS

>>> import csv
>>> csv.get_dialect([])
_csv.Error: argument 0 must be a string, not 'list'
>>> csv.unregister_dialect(BadHash())  # __hash__ raises ValueError
_csv.Error: argument 0 must be a string, not 'BadHash'

TO-BE

>>> csv.get_dialect([])
TypeError: unhashable type: 'list'
>>> csv.unregister_dialect(BadHash())
ValueError
>>> csv.get_dialect(1)
_csv.Error: unknown dialect

(matching CPython's exception types; CPython 3.15 words the unhashable case as cannot use 'list' as a dict key (unhashable type: 'list'))

Changes

  • Add registry_key() to crates/stdlib/src/csv.rs, shared by both functions: a str name is returned as before, any other name is hashed and then reported as unknown dialect, since only str names are ever registered.
  • Add test_dialect_name_hashing to extra_tests/snippets/stdlib_csv.py, covering an unhashable name, a name whose __hash__ raises and a hashable non-str name for both functions.

Test plan

Built and run on macOS aarch64 against upstream/main 713e14c, using the CI feature set --no-default-features --features stdlib,importlib,stdio,encodings,sqlite,ssl-rustls-aws-lc,host_env,doc,ucd_3_2_0, plus threading, compared with CPython 3.15.2.

  • The extended extra_tests/snippets/stdlib_csv.py fails on an unpatched build of that commit (at csv.get_dialect([])) and passes with the patch and under CPython 3.15.2.
  • rustpython -m test test_csv: 140 run, 6 skipped, all pass.
  • pytest -k rustpython in extra_tests: 248 pass.
  • cargo fmt --check clean, cargo clippy -p rustpython-stdlib -- -Dwarnings reports nothing, and the pre-commit hooks pass on both changed files.

I used Claude Code (claude-opus-5-5) to draft the analysis, the patch and this description; I reviewed the change and ran every command above myself.

Summary by CodeRabbit

  • Bug Fixes
    • CSV dialect lookups and removals now handle non-string names consistently. Unhashable names raise TypeError, and errors raised while hashing are preserved.
    • Unknown dialect names raise a CSV error, including when unregistering a dialect while the registry is empty.

get_dialect() and unregister_dialect() rejected any name that was not a
str with _csv.Error before looking it up. The dialect registry is a
dict, so a non-str name is first hashed: an unhashable name raises
TypeError, a name whose __hash__ raises propagates that exception, and
any other name is simply not found, raising _csv.Error("unknown
dialect").

Share the name check between both functions and hash a non-str name
before reporting it as unknown.

Assisted-by: Claude Code:claude-opus-5-5
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T05:23:56.599184Z 2ba1bf9 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 04d83ee and 2ba1bf9.

Files selected for processing: 2
  • crates/stdlib/src/csv.rs
  • extra_tests/snippets/stdlib_csv.py

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

Walkthrough

CSV dialect lookup and removal now hash non-string names before reporting an unknown dialect. Hashing errors propagate. Tests cover list names, custom hash errors, and removal when the registry is empty.

Changes: CSV dialect name handling

Layer / File(s) Summary
Hash names before registry errors
crates/stdlib/src/csv.rs, extra_tests/snippets/stdlib_csv.py
A shared helper converts string names to registry keys and hashes non-string names. get_dialect and unregister_dialect use the helper. Tests check exception types, including the empty-registry case.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: youknowone

Merge Risk: Minimal · up to 2ba1b

No merge-blocking issue is established for the dialect-name hashing changes. Merge after normal checks pass.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: hashing non-string names in both csv.get_dialect() and csv.unregister_dialect().
Linked Issues check Passed Issue #9507 is the only active directly linked issue. registry_key() keeps str names on the registry path and calls obj.hash(vm)? for non-str names. Therefore an unhashable name or a `hash…
Out of Scope Changes check Passed The whole-PR diff changes only crates/stdlib/src/csv.rs and extra_tests/snippets/stdlib_csv.py. The Rust change is limited to shared key handling in get_dialect() and unregister_dialect(). The…
Full details: Linked Issues check

Explanation

Issue #9507 is the only active directly linked issue. registry_key() keeps str names on the registry path and calls obj.hash(vm)? for non-str names. Therefore an unhashable name or a __hash__ exception propagates when the registry is non-empty. A hashable non-str name returns _csv.Error("unknown dialect"). unregister_dialect() checks for an empty registry before hashing, which matches the stated CPython PyDict_Pop qualification. The added test covers both functions, unhashable names, hashing exceptions, and empty-registry misses.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff changes only crates/stdlib/src/csv.rs and extra_tests/snippets/stdlib_csv.py. The Rust change is limited to shared key handling in get_dialect() and unregister_dialect(). The Python change adds regression coverage for the same behavior. No unrelated functional area is changed.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 04d83ee5d4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread extra_tests/snippets/stdlib_csv.py Resolved
Comment thread crates/stdlib/src/csv.rs Resolved
@codspeed

codspeed Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing jseop-lim:fix/csv-dialect-name-hash (2ba1bf9) with main (88f3733)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

unregister_dialect() hashed a non-str name even when no dialect was
registered. CPython's PyDict_Pop reports a miss on an empty dict
without hashing the key, so unregister_dialect([]) raises _csv.Error
there. Check for an empty registry first.

Drop the snippet assertions for a hashable non-str name, which
test_registry_badargs in Lib/test already covers with None.

Assisted-by: Claude Code:claude-opus-5-5
@youknowone
youknowone merged commit 4591756 into RustPython:main Nov 1, 2026
21 checks passed
@jseop-lim
jseop-lim deleted the fix/csv-dialect-name-hash branch November 1, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

csv.get_dialect() and csv.unregister_dialect() raise csv.Error for an unhashable name instead of the hashing error

2 participants