Skip to content

Hash a str subclass name in the csv dialect registry - #9918

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

youknowone merged 1 commit into
RustPython:mainfrom
jseop-lim:fix/csv-dialect-name-hash

Conversation

@jseop-lim

Copy link
Copy Markdown
Contributor

Summary

register_dialect, get_dialect and unregister_dialect key the registry on the text of a str name and hash only a name that is not a str (#9514), so a str subclass that overrides __hash__ is never hashed. CPython keeps the registry in a dict and passes the name object to PyDict_SetItem, PyDict_GetItemRef and PyDict_Pop, which call its __hash__.

AS-IS

>>> import csv
>>> class BadHash(str):
...     def __hash__(self): raise ValueError
...
>>> csv.register_dialect(BadHash("x"))
>>> csv.get_dialect(BadHash("excel"))
<_csv.Dialect object at 0xa91be8180>
>>> csv.unregister_dialect(BadHash("excel"))

TO-BE

>>> csv.register_dialect(BadHash("x"))
ValueError
>>> csv.get_dialect(BadHash("excel"))
ValueError
>>> csv.unregister_dialect(BadHash("excel"))
ValueError

(matching CPython 3.15.2)

A __hash__ that returns a non-integer raises TypeError in both; CPython 3.15 words it as cannot use 'StrHash' as a dict key (__hash__ method should return an integer).

Changes

  • In crates/stdlib/src/csv.rs, registry_key() hashes every name before checking that it is a str, and register_dialect() hashes the name once the dialect is validated, where CPython inserts it.
  • Extend test_dialect_name_hashing in extra_tests/snippets/stdlib_csv.py with a str subclass whose __hash__ raises, passed to all three functions, and with an invalid dialect, which is still reported first.

Test plan

Built and run on macOS aarch64 against upstream/main 7abccf0, 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.register_dialect(UnhashableStr("excel"))) and passes with the patch.
  • rustpython -m test test_csv: 140 run, 6 skipped, all pass, as on the unpatched build.
  • cargo fmt --check clean, cargo clippy -p rustpython-vm -p rustpython-stdlib --all-targets reports nothing in the changed files, and the pre-commit hooks other than cspell and prettier pass on the changed files.

I used Claude Code (claude-fable-5-2) to draft the analysis, the patch and this description; Claude Code ran the commands above; I reviewed the diff and their output.

register_dialect(), get_dialect() and unregister_dialect() keyed the
registry on a str name's text without hashing it, so a str subclass
whose __hash__ raises was accepted. Hash every name as the dict
lookup would.

Assisted-by: Claude Code:claude-fable-5-2
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • 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

@youknowone
youknowone marked this pull request as ready for review November 18, 2026 01:01
@chatgpt-codex-connector

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

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T01:05:24.252643Z 6f9cd45 Draft marked ready
ℹ️ 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.

@youknowone
youknowone merged commit 403d458 into RustPython:main Nov 18, 2026
19 of 20 checks passed
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.

2 participants