Reputation: 21201
I've seen a couple tutorials to create a Python module using the cpython
crate but still have errors when building:
extern crate cpython;
use cpython::{PyObject, PyResult, Python, PyTuple, PyDict, ToPyObject, PythonObject};
fn add_two(py: Python, args: &PyTuple, _: Option<&PyDict>) -> PyResult<PyObject> {
match args.as_slice() {
[ref a_obj, ref b_obj] => {
let a = a_obj.extract::<i32>(py).unwrap();
let b = b_obj.extract::<i32>(py).unwrap();
let mut acc:i32 = 0;
for _ in 0..1000 {
acc += a + b;
}
Ok(acc.to_py_object(py).into_object())
},
_ => Ok(py.None())
}
}
py_module_initializer!(example, |py, module| {
try!(module.add(py, "add_two", py_fn!(add_two)));
Ok(())
});
I get:
error: macro undefined: 'py_module_initializer!'
Where do I get it? I am using Rust 1.12.
UPD
#[macro_use]
(as in answers)Upvotes: 2
Views: 524
Reputation: 22193
You probably need to declare cpython
as follows:
#[macro_use] extern crate cpython;
To be able to use cpython
's macros. You can consult the example in its docs.
Upvotes: 4
Reputation: 65752
You must add the #[macro_use]
attribute on the extern crate
declaration to ask the compiler to bring the macros exported by the crate into your crate's namespace.
#[macro_use]
extern crate cpython;
Upvotes: 2