pyo3/extensions/hello.rs

29 lines
727 B
Rust
Raw Normal View History

#![crate_type = "dylib"]
#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
py_module_initializer!(hello, inithello, PyInit_hello, |py, m| {
try!(m.add(py, "__doc__", "Module documentation string"));
2016-03-12 17:14:21 +00:00
try!(m.add(py, "run", py_fn!(py, run(*args, **kwargs))));
try!(m.add(py, "val", py_fn!(py, val())));
Ok(())
});
2016-03-12 17:14:21 +00:00
fn run(py: Python, args: &PyTuple, kwargs: &PyDict) -> PyResult<PyObject> {
println!("Rust says: Hello Python!");
for arg in args.iter(py) {
println!("Rust got {}", arg);
}
2016-03-12 17:14:21 +00:00
for (key, val) in kwargs.items(py) {
println!("{} = {}", key, val);
}
Ok(py.None())
}
fn val(_: Python) -> PyResult<i32> {
Ok(42)
}