Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyErr::new has surprising edge cases #4412

Open
davidhewitt opened this issue Aug 2, 2024 · 0 comments
Open

PyErr::new has surprising edge cases #4412

davidhewitt opened this issue Aug 2, 2024 · 0 comments

Comments

@davidhewitt
Copy link
Member

Continuing from #4400, where we observed that a tuple was being flattened as part of a call to PyErr::new.

The other special case is that PyErr::new(py.None()) counts as no arguments.

#[pyo3::pymodule]
mod pyo3_scratch {
    use pyo3::prelude::*;

    #[pyfunction]
    fn bug(py: Python<'_>) -> PyResult<()> {
        Err(pyo3::exceptions::PyValueError::new_err(py.None()))
    }
}
>>> try:
...     pyo3_scratch.bug()
... except ValueError as e:
...     print(e.args)
... 
()

Again if we wrap the py.None() in a tuple as (py.None(),) then the final e.args will be (None,).

I'm open to debating whether we should change PyErr::new, because this doesn't match Python users' instincts, where there is no special case:

>>> print(ValueError(None).args)
(None,)
>>> print(ValueError((1,)).args)
((1,),)
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

No branches or pull requests

1 participant