Skip to content

TypeError converting Rust's FromUtf8Error to Python's UnicodeDecodeError #5651

@staticintlucas

Description

@staticintlucas

Bug Description

Getting an unexpected TypeError with strange message when converting Rust's FromUtf8Error to Python's UnicodeDecodeError. Not sure if other error/exception types are also affected, I haven't had a chance to check more thoroughly.

Steps to Reproduce

Using a simple function to create a Rust FromUtf8Error:

#[pyfunction]
fn unicode_error() -> PyResult<String> {
    Ok(String::from_utf8(b"\xff".to_vec())?)
}

From Python:

>>> unicode_error()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: function takes exactly 5 arguments (1 given)
Normalization failed: type=UnicodeDecodeError args='invalid utf-8 sequence of 1 bytes from index 0'
>>>

I expected to get the UnicodeDecodeError, instead getting a message (presumably) about PyO3 failing to construct the UnicodeDecodeError.

Backtrace

Your operating system and version

Ubuntu 24.04

Your Python version (python --version)

Python 3.12.3

Your Rust version (rustc --version)

rustc 1.89.0 (29483883e 2025-08-04)

Your PyO3 version

0.27.1

How did you install python? Did you use a virtualenv?

Installed Python through apt

Using a virtualenv and Maturin

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions