Skip to content

Conversation

@staticintlucas
Copy link

Fixes #5651.

Fixes the implementation of PyErrArguments for string::FromUtf8Error and ffi::IntoStringError.

Removes the broken implementation of PyErrArguments for str::Utf8Error, string::FromUtf16Error, and char::DecodeUtf16Error.

Adds a convenience trait called IntoPyErrWithBytes for str::Utf8Error to allow easy conversion to PyErr. The implementation of From<str::Utf8Error> for PyErr was being used in a few places internally in PyO3, so this felt like the best solution. And unlike the UTF-16 errors, str::Utf8Error contains everything needed to construct a Python UnicodeDecodeError except the source bytes. I exposed this API publicly since it's probably a pretty common use case (at least compared to UTF-16), but let me know if you want any changes (especially regarding API naming).

src/err/mod.rs Outdated
/// str::from_utf8(bytes).map_err(|e| e.into_with_bytes(bytes))
/// # }
/// ```
pub trait IntoPyErrWithBytes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather that adding a new trait I would prefer if we just made a constructor on PyUnicodeDecodeError which returns the PyErr. Internally it could make use of PyUnicodeDecodeError::new_utf8 to construct it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved the implementation to a new constructor for PyUnicodeDecodeError.

I don't want to call PyUnicodeDecodeError::new_utf8 since it is fallible and I don't want to have to worry about handling errors while already creating an error. I've kept the implementation mostly the same using a private Utf8ErrorWithBytes struct which implements PyErrArguments and calls PyUnicodeDecodeError::new_err under the hood.

Lemme know if this approach sounds reasonable to you, or you want any more changes

@staticintlucas staticintlucas force-pushed the main branch 3 times, most recently from feddff7 to a8b289c Compare December 2, 2025 21:28
@staticintlucas
Copy link
Author

Will add some tests for my changes tomorrow to make codecov happy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError converting Rust's FromUtf8Error to Python's UnicodeDecodeError

2 participants