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

IntoPyResult Trait Missing For PyResult<PyBytes> #874

Closed
danii opened this issue Apr 20, 2020 · 3 comments
Closed

IntoPyResult Trait Missing For PyResult<PyBytes> #874

danii opened this issue Apr 20, 2020 · 3 comments

Comments

@danii
Copy link

danii commented Apr 20, 2020

💥 Reproducing

Make any old Python function in Rust with Pyo3 that:

  • Returns a Python bytes / Rust PyBytes object
  • That may also throw an exception
#[pyfunction]
fn get_bytes(py: Python, throw: bool) -> PyResult<PyBytes> {
  if throw {
    Err(PyErr::new::<exceptions::Exception, _>("Throw was set."))
  } else {
    Ok(*PyBytes::new(py, &[1, 2, 3, 4]))
  }
}

All names shown are from std or pyo3.

Rust complains that the trait 'pyo3::derive_utils::IntoPyResult<_>' is not implemented for 'std::result::Result<pyo3::types::bytes::PyBytes, pyo3::err::PyErr>'.

🌍 Environment

  • Manjaro 19.0.2 Kyria, x86_64 Linux 5.4.31-1-MANJARO
  • Python 3.8.2, installed via pamac
  • Rustc 1.43.0-nightly (564758c4c 2020-03-08), installed via rustup pamac
  • Pyo3 0.9.2
@davidhewitt
Copy link
Member

davidhewitt commented Apr 20, 2020

I think your return type needs to be PyResult<&PyBytes>, and then things should work correctly.

(It is expected that this won't work for the non-reference type PyBytes at the moment; this might change after #679)

@davidhewitt
Copy link
Member

To clarify, this is the working solution @Daniihh:

#[pyfunction]
fn get_bytes(py: Python, throw: bool) -> PyResult<&PyBytes> {
  if throw {
    Err(PyErr::new::<exceptions::Exception, _>("Throw was set."))
  } else {
    Ok(PyBytes::new(py, &[1, 2, 3, 4]))
  }
}

@danii
Copy link
Author

danii commented Apr 21, 2020

Oh wow, LOL, this is quite embarrassing.
I'm very sorry for wasting your time, I'll be sure to do more tinkering and research before I open any other issues. ;p

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

2 participants