Preserve surrogates in ImportError repr (#9252)

Use the existing WTF-8 representation interface for the base exception and name/path fields, avoiding lossy conversion of legal Python __repr__ results.

AI assistance: OpenAI Codex prepared the correction and validation. Model version is not exposed in this session.

Assisted-by: Codex:unknown (model version not exposed)
This commit is contained in:
youknowdot authored and GitHub committed 2026-10-21 12:43:30 +09:00
1 parent 1cc366362b
commit 70a2cb20b5
1 file changed
+6 -6
+6 -6
View File
@@ -2214,16 +2214,16 @@ pub(super) mod types {
}
impl Representable for PyImportError {
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
fn repr_wtf8(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<Wtf8Buf> {
let base: &Py<PyBaseException> = zelf
.as_object()
.downcast_ref()
.expect("ImportError is a BaseException");
let mut has_args = !base.args().is_empty();
if zelf.name.load_owned().is_none() && zelf.path.load_owned().is_none() {
return PyBaseException::repr_str(base, vm);
return PyBaseException::repr_wtf8(base, vm);
}
let mut result = PyBaseException::repr_str(base, vm)?;
let mut result = PyBaseException::repr_wtf8(base, vm)?;
result.pop();
for (name, field) in [("name", &zelf.name), ("path", &zelf.path)] {
if let Some(value) = field.load_owned() {
@@ -2231,12 +2231,12 @@ pub(super) mod types {
result.push_str(", ");
}
result.push_str(name);
result.push('=');
result.push_str(&value.repr(vm)?.to_string_lossy());
result.push_char('=');
result.push_wtf8(value.repr(vm)?.as_wtf8());
has_args = true;
}
}
result.push(')');
result.push_char(')');
Ok(result)
}
}