Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions crates/stdlib/src/unicodedata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod unicodedata {
use super::{NormalizeFormArg, unicode_core};
use crate::vm::{
Py, PyObject, PyObjectRef, PyPayload, PyResult, VirtualMachine,
builtins::{PyModule, PyStr, PyStrRef},
builtins::{PyBytes, PyModule, PyStr, PyStrRef, PyUtf8Str},
common::lock::PyMutex,
function::OptionalArg,
protocol::PyIterReturn,
Expand Down Expand Up @@ -342,18 +342,33 @@ mod unicodedata {
}

#[pymethod]
fn lookup(zelf: &Py<Self>, name: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
if let Some(name_str) = name.to_str()
fn lookup(zelf: &Py<Self>, name: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
let name_bytes = if let Some(bytes) = name.downcast_ref::<PyBytes>() {
bytes.as_bytes()
} else {
name.try_downcast_ref::<PyUtf8Str>(vm)?.as_bytes()
};
// CPython's generated Unicode name database limits names to 256 bytes.
if name_bytes.len() > 256 {
return Err(vm.new_key_error(vm.ctx.new_str("name too long").into()));
}
if let Ok(name_str) = core::str::from_utf8(name_bytes)
&& let Some(found) = zelf.inner.lookup(name_str)
{
return Ok(match found {
unicode_core::LookupResult::Character(ch) => ch.to_string(),
unicode_core::LookupResult::Sequence(seq) => seq.to_string(),
});
}
// CPython formats the failed name as a NUL-terminated UTF-8 string.
let prefix = name_bytes
.split(|&byte| byte == 0)
.next()
.unwrap_or_default();
let display = String::from_utf8_lossy(prefix);
Err(vm.new_key_error(
vm.ctx
.new_str(format!("undefined character name '{name}'"))
.new_str(format!("undefined character name '{display}'"))
.into(),
))
}
Expand Down
Loading