diff --git a/src/types/string.rs b/src/types/string.rs index 031598a90bf..dd29a5400bc 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -8,6 +8,7 @@ use crate::{ Python, ToPyObject, }; use std::borrow::Cow; +use std::ffi::CStr; use std::os::raw::c_char; use std::str; @@ -236,13 +237,13 @@ impl PyString { (0..len).map(move |i| { let c = ffi::PyUnicode_ReadChar(self.as_ptr(), i); char::from_u32(c).ok_or_else(|| { - exceptions::PyUnicodeDecodeError::new_err(( - "utf-32", - PyBytes::new(self.py(), &c.to_ne_bytes()).to_object(self.py()), - i, - i + 1, - format!("invalid codepoint"), - )) + exceptions::PyUnicodeDecodeError::new( + self.py(), + CStr::from_bytes_with_nul(b"utf-8\0").unwrap(), + &c.to_ne_bytes(), + i as usize..(i + 1) as usize, + CStr::from_bytes_with_nul(b"invalid codepoint\0").unwrap(), + ) }) }) }