fix: Python-Rust combining char diff in isalnum#7612
Conversation
📝 WalkthroughWalkthroughUpdated alphanumeric checks to consult ICU canonical combining class data so combining marks are excluded from Changes
Sequence Diagram(s)sequenceDiagram
participant Test as Python test / Caller
participant VM as VM (`PyStr::isalnum`)
participant SRE as sre_engine (`is_uni_alnum`)
participant ICU as ICU properties (CodePointMapData<CanonicalCombiningClass>)
Test->>VM: call isalnum() on string
VM->>SRE: check code point alnum predicate
SRE->>ICU: lookup CanonicalCombiningClass.for_char(code_point)
ICU-->>SRE: return CanonicalCombiningClass
SRE-->>VM: return (is_alnum && CCC == NotReordered)
VM-->>Test: final boolean result
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@extra_tests/snippets/stdlib_re.py`:
- Around line 82-84: The commented-out test shows that is_uni_word() currently
inherits is_uni_alnum() which uses Rust's char::is_alphanumeric() and therefore
wrongly treats combining marks (e.g. U+0345, category Mn) as word characters;
update is_uni_alnum() (and thus is_uni_word()) to exclude characters whose
Unicode General Category is a Mark (Mn, Mc, Me) rather than relying solely on
char::is_alphanumeric(), for example by querying the character's general
category and returning false for Mark categories so the test re.match(r"\w",
"\u0345") will not match; alternatively, if you intend to keep this limitation,
uncomment the test and mark it as an expected failure with a comment referencing
issue `#7518` and the functions is_uni_alnum() / is_uni_word().
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 04cb8ffa-9709-4846-a2ad-187862235904
📒 Files selected for processing (3)
crates/vm/src/builtins/str.rsextra_tests/snippets/builtin_str.pyextra_tests/snippets/stdlib_re.py
Sorry, something went wrong.
Related to: RustPython#7518 Rust and Python differ on alphanumeric characters. Rust follows the Unicode standard closer than Python. This means that is_alphanumeric (char function in Rust) is different from isalnum (Python). To fix the discrepancy, RustPython needs to mimic Python by rejecting certain characters. Some classes of combining characters count as alphanumeric in Rust but not Python. Combining characters are accent marks that are combined with other characters to create a single grapheme. It's possible that this PR is not exhaustive. I fixed the combining character issue BUT I don't know the full range of discrepancies.
64dd760 to
f79d6df
Compare
April 16, 2026 03:23
|
I fixed the issues and force pushed. 😁 |
Sorry, something went wrong.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@crates/vm/src/builtins/str.rs`:
- Around line 949-954: Replace the current isalnum implementation (which uses
char::is_alphanumeric plus CanonicalCombiningClass check) with a
CPython-compatible predicate that accepts only Unicode letter categories
(Lu/Ll/Lt/Lm/Lo) or number categories (Nd/Nl/Np) — i.e., check the character's
general category starts with 'L' or 'N' — so spacing/combing marks like U+093F
(Mc) are rejected; update the isalnum method (and the char_all predicate it
uses) to use this category test, update the mirror predicate used for \w in the
sre_engine string predicate to the same logic so they stay aligned, and add a
regression test asserting str.isalnum() returns false for U+093F.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: 509798df-be24-4b49-ba31-08eb4f8b25c1
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (5)
crates/sre_engine/Cargo.tomlcrates/sre_engine/src/string.rscrates/vm/src/builtins/str.rsextra_tests/snippets/builtin_str.pyextra_tests/snippets/stdlib_re.py
✅ Files skipped from review due to trivial changes (3)
- crates/sre_engine/Cargo.toml
- extra_tests/snippets/stdlib_re.py
- extra_tests/snippets/builtin_str.py
🚧 Files skipped from review as they are similar to previous changes (1)
- crates/sre_engine/src/string.rs
Sorry, something went wrong.
ShaharNaveh
left a comment
There was a problem hiding this comment.
Looks great!
tysm:)
Sorry, something went wrong.
|
CodeRabbit's comment is interesting. 🤔 I assumed there were more edge cases. I didn't look at |
Sorry, something went wrong.
youknowone
left a comment
There was a problem hiding this comment.
Rust and Python differ on alphanumeric characters. Rust follows the Unicode standard closer than Python.
If it does, isn't it better to patch CPython to follow unicode standard better?
Sorry, something went wrong.
aac2070
into
RustPython:main
Apr 17, 2026
|
Sorry, I'm not sure if I'm supposed to respond here to you two after PRs have been merged. 😅 I don't fully understand the issue, but I think that the differences are due to Unicode version and how each project chooses to define "alphanumeric." I'm trying to figure out if I can just combine Unicode properties to mimic cpython. |
Sorry, something went wrong.
Closes: #7518
Rust and Python differ on alphanumeric characters. Rust follows the Unicode standard closer than Python. This means that
is_alphanumeric(char function in Rust) is different fromisalnum(Python). To fix the discrepancy, RustPython needs to mimic Python by rejecting certain characters. Some classes of combining characters count as alphanumeric in Rust but not Python. Combining characters are accent marks that are combined with other characters to create a single grapheme.It's possible that this PR is not exhaustive. I fixed the combining character issue BUT I don't know the full range of discrepancies.
This doesn't actually fix #7518, but it fixes a similar issue based on that report.Actually, CodeRabbit pointed me in the right direction so now it does fix #7518.^Note the differences which are accounted for by the new tests.
Summary by CodeRabbit
Bug Fixes
Tests