Title: Hash a `str` subclass name in the `csv` dialect registry by jseop-lim · Pull Request #9918 · RustPython/RustPython · GitHub
Open Graph Title: Hash a `str` subclass name in the `csv` dialect registry by jseop-lim · Pull Request #9918 · RustPython/RustPython
X Title: Hash a `str` subclass name in the `csv` dialect registry by jseop-lim · Pull Request #9918 · RustPython/RustPython
Description: Part of #9913 (item 11) This PR follows our AI policy 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.
Open Graph Description: Part of #9913 (item 11) This PR follows our AI policy 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 st...
X Description: Part of #9913 (item 11) This PR follows our AI policy 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 st...
Opengraph URL: https://github.com/RustPython/RustPython/pull/9918
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:4d3b5e7d-a462-8978-2261-d570ee671afa |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B21E:14700E:35E1A95:446EF22:6B01E403 |
| html-safe-nonce | c0e8809d1ee53e7157c64e085aca1b8453f3a6733da2b00267e071e7d915c4cf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjFFOjE0NzAwRTozNUUxQTk1OjQ0NkVGMjI6NkIwMUU0MDMiLCJ2aXNpdG9yX2lkIjoiNzQxMDAxMDQwMjE2NTIyMDM1NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | c2d5832f46f7d9ab07b6cfd8239ba6052eac1ded15ab743e69e98ff071ca0dad |
| hovercard-subject-tag | pull_request:5361502525 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/RustPython/RustPython/pull/9918/files |
| twitter:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/86508420?s=400&v=4 |
| og:image:alt | Part of #9913 (item 11) This PR follows our AI policy 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 st... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width