Skip to content

PyO3 deletes traceback from TypeError in FromPyObject #5348

@orlp

Description

@orlp

Bug Description

Consider this simple example:

#[derive(Debug)]
struct Foo(u32);

impl<'a> FromPyObject<'a> for Foo {
    fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
        Ok(dbg!(Foo(ob.call_method0("foo")?.extract::<u32>()?)))
    }
}

#[pyfunction]
fn test(foo: Foo) {
    println!("Test print from Rust: {}", foo.0);
}

#[pymodule]
fn rustpackage(_py: Python, m: &Bound<PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(test, m)?)?;
    Ok(())
}

We can call it from Python just fine and it works:

class Foo:
    def foo(self):
        return 42

test(Foo())
Test print from Rust: 42

If Foo.foo returns an error, this also correctly works:

class Foo:
    def foo(self):
        raise RuntimeError("not implemented")

test(Foo())
Traceback (most recent call last):
  File "test.py", line 17, in <module>
    test(Foo())
  File "test.py", line 15, in foo
    raise RuntimeError("not implemented")
RuntimeError: not implemented

However, if Foo.foo specifically returns a TypeError, the important part of the traceback is deleted:

class Foo:
    def foo(self):
        raise TypeError("wrong type")

test(Foo())
Traceback (most recent call last):
  File "/Users/orlp/programming/rust/polars-rs-repro/test.py", line 17, in <module>
    test(Foo())
TypeError: argument 'foo': wrong type

Steps to Reproduce

See above.

Backtrace

Your operating system and version

MacOS Ventura 13.5

Your Python version (python --version)

3.11.4

Your Rust version (rustc --version)

rustc 1.90.0-nightly

Your PyO3 version

0.25.1

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

uv venv

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions