Skip to content

Instantly share code, notes, and snippets.

@earthnuker
Last active July 8, 2019 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earthnuker/f48453afc562a5552276fef6b4148410 to your computer and use it in GitHub Desktop.
Save earthnuker/f48453afc562a5552276fef6b4148410 to your computer and use it in GitHub Desktop.
PyO3 Python callback from Rust
error[E0277]: the trait bound `pyo3::object::PyObject: pyo3::instance::PyNativeType` is not satisfied
--> src\lib.rs:16:13
|
16 | ObjectProtocol::is_callable(&callback)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `pyo3::instance::PyNativeType` is not implemented for `pyo3::object::PyObject`
|
= note: required because of the requirements on the impl of `pyo3::objectprotocol::ObjectProtocol` for `pyo3::object::PyObject`
= note: required by `pyo3::objectprotocol::ObjectProtocol::is_callable`
use pyo3::exceptions;
use pyo3::prelude::*;
#[pymodule]
fn py_test(py: Python, m: &PyModule) -> PyResult<()> {
#[pyfn(m, "sum_as_string")]
fn sum_as_string(py: Python, a: usize, b: usize, callback: PyObject) -> PyResult<String> {
println!("Hello from Rust!");
println!("callback is: {:?}", callback);
println!(
"callback is: {:?}",
ObjectProtocol::is_callable(&callback)
);
println!("Raising exception...");
Ok("bye!".to_string())
}
Ok(())
}
import py_test
print(repr(py_test.sum_as_string(1,2,print)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment