pyo3/extensions/hello.rs

33 lines
791 B
Rust

#![crate_type = "dylib"]
#![feature(plugin)]
#![plugin(interpolate_idents)]
#[macro_use] extern crate cpython;
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict};
py_module_initializer!(hello, |py, m| {
try!(m.add("__doc__", "Module documentation string", py));
try!(m.add("run", py_fn!(run), py));
try!(m.add("val", py_fn!(val()), py));
Ok(())
});
fn run(py: Python, args: &PyTuple, kwargs: Option<&PyDict>) -> PyResult<PyObject> {
println!("Rust says: Hello Python!");
for arg in args.as_slice() {
println!("Rust got {}", arg);
}
if let Some(kwargs) = kwargs {
for (key, val) in kwargs.items(py) {
println!("{} = {}", key, val);
}
}
Ok(py.None())
}
fn val(_: Python) -> PyResult<i32> {
Ok(42)
}