fix module export functions

This commit is contained in:
Nikolay Kim 2017-06-15 14:20:30 -07:00
parent f4d0fe3950
commit e85db971f9
3 changed files with 15 additions and 7 deletions

View File

@ -86,9 +86,8 @@ fn init_mod(py: Python, m: &PyModule) -> PyResult<()> {
m.add(py, "__doc__", "This module is implemented in Rust.")?; m.add(py, "__doc__", "This module is implemented in Rust.")?;
#[pyfn(m, "sum_as_string")] #[pyfn(m, "sum_as_string")]
// pyo3 aware function. All of our python interface could be // pyo3 aware function. All of our python interface could be declared in a separate module.
// declared in a separate module. // Note that the `#[pyfn()]` annotation automatically converts the arguments from
// Note that the py_fn!() macro automatically converts the arguments from
// Python objects to Rust values; and the Rust return value back into a Python object. // Python objects to Rust values; and the Rust return value back into a Python object.
fn sum_as_string_py(_: Python, a:i64, b:i64) -> PyResult<String> { fn sum_as_string_py(_: Python, a:i64, b:i64) -> PyResult<String> {
let out = sum_as_string(a, b); let out = sum_as_string(a, b);

View File

@ -281,12 +281,13 @@ fn wrap_fn(item: &mut syn::Item) -> Option<Box<syn::Block>> {
ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(wrap), ml_meth: pyo3::class::PyMethodType::PyCFunctionWithKeywords(wrap),
ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS, ml_flags: pyo3::ffi::METH_VARARGS | pyo3::ffi::METH_KEYWORDS,
ml_doc: #doc, ml_doc: #doc,
}.as_method_def(); };
unsafe { unsafe {
let func = pyo3::PyObject::from_owned_ptr_or_panic( let func = pyo3::PyObject::from_owned_ptr_or_panic(
py, pyo3::ffi::PyCFunction_New( py, pyo3::ffi::PyCFunction_New(
&def as *const _ as *mut _, std::ptr::null_mut())); Box::into_raw(Box::new(def.as_method_def())),
std::ptr::null_mut()));
std::mem::forget(def); std::mem::forget(def);
#m.add(py, stringify!(#fnname), func)? #m.add(py, stringify!(#fnname), func)?
@ -335,6 +336,8 @@ pub fn impl_wrap(name: &syn::Ident, spec: &method::FnSpec) -> Tokens {
let result: #output = { let result: #output = {
#body #body
}; };
py.release(kwargs);
py.release(args);
_pyo3::callback::cb_convert( _pyo3::callback::cb_convert(
_pyo3::callback::PyObjectCallbackConverter, py, result) _pyo3::callback::PyObjectCallbackConverter, py, result)
}) })

View File

@ -68,10 +68,10 @@
//! for adding the module's members. //! for adding the module's members.
//! //!
//! To creates a Python callable object that invokes a Rust function, specify rust //! To creates a Python callable object that invokes a Rust function, specify rust
//! function and decroate it with `#[pyfn()]` attribute. `pyfn()` accepts three parameters. //! function and decorate it with `#[pyfn()]` attribute. `pyfn()` accepts three parameters.
//! //!
//! 1. `m`: The module name. //! 1. `m`: The module name.
//! 2. function name, name of function visible to Python code. //! 2. name of function visible to Python code.
//! 3. arguments description string, i.e. "param1, param2=None, *, param3=55" //! 3. arguments description string, i.e. "param1, param2=None, *, param3=55"
//! //!
//! //!
@ -83,10 +83,16 @@
//! extern crate pyo3; //! extern crate pyo3;
//! use pyo3::{py, Python, PyResult, PyObject, PyModule, PyString}; //! use pyo3::{py, Python, PyResult, PyObject, PyModule, PyString};
//! //!
//! // add bindings to the generated python module
//! // N.B: names: "libhello" must be the name of the `.so` or `.pyd` file
//! #[py::modinit(hello)] //! #[py::modinit(hello)]
//! fn init_module(py: Python, m: &PyModule) -> PyResult<()> { //! fn init_module(py: Python, m: &PyModule) -> PyResult<()> {
//! m.add(py, "__doc__", "Module documentation string")?; //! m.add(py, "__doc__", "Module documentation string")?;
//! //!
//! // pyo3 aware function. All of our python interface could be declared
//! // in a separate module.
//! // Note that the `#[pyfn()]` annotation automatically converts the arguments from
//! // Python objects to Rust values; and the Rust return value back into a Python object.
//! #[pyfn(m, "run_rust_func")] //! #[pyfn(m, "run_rust_func")]
//! fn run(py: Python, name: PyString) -> PyResult<PyObject> { //! fn run(py: Python, name: PyString) -> PyResult<PyObject> {
//! println!("Rust says: Hello {} of Python!", name); //! println!("Rust says: Hello {} of Python!", name);