Last active
July 8, 2019 17:57
-
-
Save earthnuker/f48453afc562a5552276fef6b4148410 to your computer and use it in GitHub Desktop.
PyO3 Python callback from Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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