Skip to content

Commit cd8b11d

Browse files
fix: Python-Rust combining char diff in isalnum
Related to: #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.
1 parent 7544628 commit cd8b11d

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎crates/vm/src/builtins/str.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ use rustpython_common::{
4444
wtf8::{CodePoint, Wtf8, Wtf8Buf, Wtf8Chunk, Wtf8Concat},
4545
};
4646

47-
use icu_properties::props::{
48-
BidiClass, BinaryProperty, EnumeratedProperty, GeneralCategory, XidContinue, XidStart,
47+
use icu_properties::{
48+
CodePointMapData,
49+
props::{
50+
BidiClass, BinaryProperty, CanonicalCombiningClass, EnumeratedProperty, GeneralCategory,
51+
XidContinue, XidStart,
52+
},
4953
};
5054
use unicode_casing::CharExt;
5155

@@ -946,7 +950,11 @@ impl PyStr {
946950

947951
#[pymethod]
948952
fn isalnum(&self) -> bool {
949-
!self.data.is_empty() && self.char_all(char::is_alphanumeric)
953+
let map = CodePointMapData::<CanonicalCombiningClass>::new();
954+
!self.data.is_empty()
955+
&& self.char_all(|c| {
956+
c.is_alphanumeric() && map.get(c) == CanonicalCombiningClass::NotReordered
957+
})
950958
}
951959

952960
#[pymethod]

‎extra_tests/snippets/builtin_str.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@
7171
assert a.istitle()
7272
assert a.isalpha()
7373

74+
# Combining characters differ slightly between Rust and Python
75+
assert "\u006e".isalnum()
76+
assert not "\u0303".isalnum()
77+
assert not "\u006e\u0303".isalnum()
78+
assert "\u00f1".isalnum()
79+
assert not "\u0345".isalnum()
80+
for raw in range(0x0363, 0x036f):
81+
assert not chr(raw).isalnum()
82+
7483
s = "1 2 3"
7584
assert s.split(" ", 1) == ["1", "2 3"]
7685
assert s.rsplit(" ", 1) == ["1 2", "3"]

‎extra_tests/snippets/stdlib_re.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,6 @@
7979
# Test of fix re.fullmatch POSSESSIVE_REPEAT, issue #7183
8080
assert re.fullmatch(r"([0-9]++(?:\.[0-9]+)*+)", "1.25.38")
8181
assert re.fullmatch(r"([0-9]++(?:\.[0-9]+)*+)", "1.25.38").group(0) == "1.25.38"
82+
83+
# Combining characters; issue #7518
84+
# assert not re.match(r"\w", "\u0345"), r"\w should not match U+0345 (category Mn)"

0 commit comments

Comments
 (0)