Fix TypeError when converting Rust encoding errors to Python #5668
+147
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #5651.
Fixes the implementation of
PyErrArgumentsforstring::FromUtf8Errorandffi::IntoStringError.Removes the broken implementation of
PyErrArgumentsforstr::Utf8Error,string::FromUtf16Error, andchar::DecodeUtf16Error.Adds a convenience trait called
IntoPyErrWithBytesforstr::Utf8Errorto allow easy conversion toPyErr. The implementation ofFrom<str::Utf8Error>forPyErrwas being used in a few places internally in PyO3, so this felt like the best solution. And unlike the UTF-16 errors,str::Utf8Errorcontains everything needed to construct a PythonUnicodeDecodeErrorexcept 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).