Skip to content

Commit 014d345

Browse files
unicodedata: Fix name, unmask test_name
1 parent f9d986b commit 014d345

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

‎Lib/test/test_unicodedata.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def test_function_checksum(self):
131131
result = h.hexdigest()
132132
self.assertEqual(result, self.expectedchecksum)
133133

134-
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: None != 'TANGUT IDEOGRAPH-17000'
135134
def test_name(self):
136135
name = self.db.name
137136
self.assertRaises(ValueError, name, '\0')

‎crates/stdlib/src/unicodedata.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ mod unicodedata {
102102
fn lookup(&self, name: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
103103
if let Some(name_str) = name.to_str()
104104
&& let Some(character) = unicode_core::lookup_character(name_str)
105+
&& self.inner.membership(character)
105106
{
106107
return Ok(character.to_string());
107108
}
@@ -119,11 +120,12 @@ mod unicodedata {
119120
default: OptionalArg<PyObjectRef>,
120121
vm: &VirtualMachine,
121122
) -> PyResult {
122-
if let Some(name) = self
123-
.extract_char(character, vm)?
124-
.to_char()
125-
.and_then(unicode_core::character_name)
126-
{
123+
if let Some(name) = self.extract_char(character, vm)?.to_char().and_then(|ch| {
124+
self.inner
125+
.membership(ch)
126+
.then(|| unicode_core::character_name(ch))
127+
.flatten()
128+
}) {
127129
return Ok(vm.ctx.new_str(name).into());
128130
}
129131
default.ok_or_else(|| vm.new_value_error("no such name"))

‎crates/unicode/src/data.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ fn lookup_table<T: Copy>(table: &[(u32, u32, T)], ch: char) -> Option<T> {
8787
.map(|i| table[i].2)
8888
}
8989

90-
fn membership_3_2(ch: u32) -> bool {
90+
#[cold]
91+
#[inline(never)]
92+
#[must_use]
93+
pub fn membership_3_2(ch: u32) -> bool {
9194
MEMBERSHIP_3_2
9295
.binary_search_by(|&(start, end)| {
9396
if ch > end {
@@ -155,6 +158,17 @@ impl Ucd {
155158
Self { modern }
156159
}
157160

161+
#[must_use]
162+
pub fn membership(self, ch: char) -> bool {
163+
if !self.modern {
164+
cold_path();
165+
membership_3_2(ch as u32)
166+
} else {
167+
// Rust chars always "exist" for modern Unicode or else they wouldn't be chars.
168+
true
169+
}
170+
}
171+
158172
#[must_use]
159173
pub fn category(&self, c: CodePoint) -> &'static str {
160174
let Some(c) = c.to_char() else {

0 commit comments

Comments
 (0)