2018-06-15 13:59:22 +00:00
|
|
|
use pyo3::prelude::*;
|
2018-11-12 21:28:45 +00:00
|
|
|
|
2019-04-18 05:59:15 +00:00
|
|
|
use pyo3::types::{IntoPyDict, PyTuple};
|
2018-04-30 20:38:48 +00:00
|
|
|
|
2018-09-03 18:50:18 +00:00
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2019-06-03 03:18:44 +00:00
|
|
|
struct AnonClass {}
|
|
|
|
|
2020-03-03 16:23:32 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct ValueClass {
|
|
|
|
value: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl ValueClass {
|
|
|
|
#[new]
|
|
|
|
fn new(value: usize) -> ValueClass {
|
|
|
|
ValueClass { value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 03:18:44 +00:00
|
|
|
#[pyclass(module = "module")]
|
|
|
|
struct LocatedClass {}
|
2018-04-30 20:38:48 +00:00
|
|
|
|
|
|
|
fn sum_as_string(a: i64, b: i64) -> String {
|
2020-02-10 02:20:18 +00:00
|
|
|
format!("{}", a + b)
|
2018-04-30 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyfunction]
|
2018-07-30 20:52:22 +00:00
|
|
|
/// Doubles the given value
|
2018-04-30 21:17:09 +00:00
|
|
|
fn double(x: usize) -> usize {
|
|
|
|
x * 2
|
|
|
|
}
|
|
|
|
|
2018-04-30 20:38:48 +00:00
|
|
|
/// This module is implemented in Rust.
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pymodule]
|
2018-07-09 22:13:02 +00:00
|
|
|
fn module_with_functions(py: Python, m: &PyModule) -> PyResult<()> {
|
2019-02-01 15:31:18 +00:00
|
|
|
use pyo3::wrap_pyfunction;
|
2019-02-01 14:00:41 +00:00
|
|
|
|
2018-04-30 20:38:48 +00:00
|
|
|
#[pyfn(m, "sum_as_string")]
|
|
|
|
fn sum_as_string_py(_py: Python, a: i64, b: i64) -> PyResult<String> {
|
|
|
|
let out = sum_as_string(a, b);
|
2019-02-23 17:38:00 +00:00
|
|
|
Ok(out)
|
2018-04-30 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfn(m, "no_parameters")]
|
|
|
|
fn no_parameters() -> PyResult<usize> {
|
2019-02-23 17:38:00 +00:00
|
|
|
Ok(42)
|
2018-04-30 20:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:23:32 +00:00
|
|
|
#[pyfn(m, "double_value")]
|
|
|
|
fn double_value(v: &ValueClass) -> usize {
|
|
|
|
v.value * 2
|
|
|
|
}
|
|
|
|
|
2019-06-03 03:18:44 +00:00
|
|
|
m.add_class::<AnonClass>().unwrap();
|
2020-03-03 16:23:32 +00:00
|
|
|
m.add_class::<ValueClass>().unwrap();
|
2019-06-03 03:18:44 +00:00
|
|
|
m.add_class::<LocatedClass>().unwrap();
|
2018-04-30 20:38:48 +00:00
|
|
|
|
2018-05-05 13:50:04 +00:00
|
|
|
m.add("foo", "bar").unwrap();
|
2018-04-30 20:38:48 +00:00
|
|
|
|
2019-02-01 13:01:18 +00:00
|
|
|
m.add_wrapped(wrap_pyfunction!(double)).unwrap();
|
|
|
|
m.add("also_double", wrap_pyfunction!(double)(py)).unwrap();
|
2018-04-30 21:17:09 +00:00
|
|
|
|
2018-04-30 20:38:48 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_with_functions() {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::wrap_pymodule;
|
|
|
|
|
2018-04-30 20:38:48 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [(
|
2018-11-12 21:25:45 +00:00
|
|
|
"module_with_functions",
|
2019-02-01 13:01:18 +00:00
|
|
|
wrap_pymodule!(module_with_functions)(py),
|
2019-03-24 16:19:15 +00:00
|
|
|
)]
|
|
|
|
.into_py_dict(py);
|
2018-06-15 19:21:12 +00:00
|
|
|
|
2020-03-03 16:23:32 +00:00
|
|
|
let run = |code| {
|
|
|
|
py.run(code, None, Some(d))
|
|
|
|
.map_err(|e| e.print(py))
|
|
|
|
.unwrap()
|
|
|
|
};
|
2018-06-15 19:21:12 +00:00
|
|
|
|
|
|
|
run("assert module_with_functions.__doc__ == 'This module is implemented in Rust.'");
|
|
|
|
run("assert module_with_functions.sum_as_string(1, 2) == '3'");
|
|
|
|
run("assert module_with_functions.no_parameters() == 42");
|
|
|
|
run("assert module_with_functions.foo == 'bar'");
|
2019-06-03 03:18:44 +00:00
|
|
|
run("assert module_with_functions.AnonClass != None");
|
|
|
|
run("assert module_with_functions.LocatedClass != None");
|
|
|
|
run("assert module_with_functions.LocatedClass.__module__ == 'module'");
|
2018-06-15 19:21:12 +00:00
|
|
|
run("assert module_with_functions.double(3) == 6");
|
2018-07-30 20:52:22 +00:00
|
|
|
run("assert module_with_functions.double.__doc__ == 'Doubles the given value'");
|
2018-06-15 19:21:12 +00:00
|
|
|
run("assert module_with_functions.also_double(3) == 6");
|
2018-07-30 20:52:22 +00:00
|
|
|
run("assert module_with_functions.also_double.__doc__ == 'Doubles the given value'");
|
2020-03-03 16:23:32 +00:00
|
|
|
run("assert module_with_functions.double_value(module_with_functions.ValueClass(1)) == 2");
|
2018-04-30 20:38:48 +00:00
|
|
|
}
|
2018-05-21 08:21:02 +00:00
|
|
|
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pymodule(other_name)]
|
2018-07-09 22:13:02 +00:00
|
|
|
fn some_name(_: Python, _: &PyModule) -> PyResult<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_renaming() {
|
2019-02-23 17:01:22 +00:00
|
|
|
use pyo3::wrap_pymodule;
|
|
|
|
|
2018-07-09 22:13:02 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("different_name", wrap_pymodule!(other_name)(py))].into_py_dict(py);
|
2018-07-09 22:13:02 +00:00
|
|
|
|
2018-07-18 11:08:05 +00:00
|
|
|
py.run(
|
|
|
|
"assert different_name.__name__ == 'other_name'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-07-09 22:13:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 08:21:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_module_from_code() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
let adder_mod = PyModule::from_code(
|
|
|
|
py,
|
|
|
|
"def add(a,b):\n\treturn a+b",
|
|
|
|
"adder_mod.py",
|
|
|
|
"adder_mod",
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.expect("Module code should be loaded");
|
2018-05-21 08:21:02 +00:00
|
|
|
|
|
|
|
let add_func = adder_mod
|
|
|
|
.get("add")
|
2020-01-24 07:44:23 +00:00
|
|
|
.expect("Add function should be in the module")
|
2018-05-21 08:21:02 +00:00
|
|
|
.to_object(py);
|
|
|
|
|
|
|
|
let ret_value: i32 = add_func
|
|
|
|
.call1(py, (1, 2))
|
|
|
|
.expect("A value should be returned")
|
|
|
|
.extract(py)
|
|
|
|
.expect("The value should be able to be converted to an i32");
|
|
|
|
|
|
|
|
assert_eq!(ret_value, 3);
|
2018-07-18 11:08:05 +00:00
|
|
|
}
|
2018-09-03 18:50:18 +00:00
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn r#move() -> usize {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pymodule]
|
2018-09-03 18:50:18 +00:00
|
|
|
fn raw_ident_module(_py: Python, module: &PyModule) -> PyResult<()> {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::wrap_pyfunction;
|
|
|
|
|
2019-02-01 13:01:18 +00:00
|
|
|
module.add_wrapped(wrap_pyfunction!(r#move))
|
2018-09-03 18:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_raw_idents() {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::wrap_pymodule;
|
|
|
|
|
2018-09-03 18:50:18 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-02-01 13:01:18 +00:00
|
|
|
let module = wrap_pymodule!(raw_ident_module)(py);
|
2018-09-03 18:50:18 +00:00
|
|
|
|
|
|
|
py_assert!(py, module, "module.move() == 42");
|
|
|
|
}
|
2018-11-12 21:25:45 +00:00
|
|
|
|
2019-12-17 22:14:28 +00:00
|
|
|
#[pyfunction]
|
|
|
|
#[name = "foobar"]
|
|
|
|
fn custom_named_fn() -> usize {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2020-03-16 06:34:02 +00:00
|
|
|
fn foobar_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
2019-12-17 22:14:28 +00:00
|
|
|
use pyo3::wrap_pyfunction;
|
|
|
|
|
2020-03-16 06:34:02 +00:00
|
|
|
m.add_wrapped(wrap_pyfunction!(custom_named_fn))?;
|
|
|
|
m.dict().set_item("yay", "me")?;
|
|
|
|
Ok(())
|
2019-12-17 22:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_names() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2020-03-16 06:34:02 +00:00
|
|
|
let module = pyo3::wrap_pymodule!(foobar_module)(py);
|
2019-12-17 22:14:28 +00:00
|
|
|
|
|
|
|
py_assert!(py, module, "not hasattr(module, 'custom_named_fn')");
|
|
|
|
py_assert!(py, module, "module.foobar() == 42");
|
|
|
|
}
|
|
|
|
|
2020-03-16 06:34:02 +00:00
|
|
|
#[test]
|
|
|
|
fn test_module_dict() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let module = pyo3::wrap_pymodule!(foobar_module)(py);
|
|
|
|
|
|
|
|
py_assert!(py, module, "module.yay == 'me'");
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:25:45 +00:00
|
|
|
#[pyfunction]
|
|
|
|
fn subfunction() -> String {
|
|
|
|
"Subfunction".to_string()
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pymodule]
|
2018-11-12 21:25:45 +00:00
|
|
|
fn submodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::wrap_pyfunction;
|
|
|
|
|
2019-02-01 13:01:18 +00:00
|
|
|
module.add_wrapped(wrap_pyfunction!(subfunction))?;
|
2018-11-12 21:25:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pyfunction]
|
2018-11-12 21:25:45 +00:00
|
|
|
fn superfunction() -> String {
|
|
|
|
"Superfunction".to_string()
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:28:45 +00:00
|
|
|
#[pymodule]
|
2018-11-12 21:25:45 +00:00
|
|
|
fn supermodule(_py: Python, module: &PyModule) -> PyResult<()> {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::{wrap_pyfunction, wrap_pymodule};
|
|
|
|
|
2019-02-01 13:01:18 +00:00
|
|
|
module.add_wrapped(wrap_pyfunction!(superfunction))?;
|
|
|
|
module.add_wrapped(wrap_pymodule!(submodule))?;
|
2018-11-12 21:25:45 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_module_nesting() {
|
2019-02-01 14:00:41 +00:00
|
|
|
use pyo3::wrap_pymodule;
|
|
|
|
|
2020-07-13 21:38:02 +00:00
|
|
|
let gil = Python::acquire_gil();
|
2018-11-12 21:25:45 +00:00
|
|
|
let py = gil.python();
|
2019-02-01 13:01:18 +00:00
|
|
|
let supermodule = wrap_pymodule!(supermodule)(py);
|
2018-11-12 21:25:45 +00:00
|
|
|
|
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
supermodule,
|
|
|
|
"supermodule.superfunction() == 'Superfunction'"
|
|
|
|
);
|
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
supermodule,
|
|
|
|
"supermodule.submodule.subfunction() == 'Subfunction'"
|
|
|
|
);
|
|
|
|
}
|
2019-04-18 05:59:15 +00:00
|
|
|
|
|
|
|
// Test that argument parsing specification works for pyfunctions
|
|
|
|
|
|
|
|
#[pyfunction(a = 5, vararg = "*")]
|
|
|
|
fn ext_vararg_fn(py: Python, a: i32, vararg: &PyTuple) -> PyObject {
|
|
|
|
[a.to_object(py), vararg.into()].to_object(py)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
|
|
|
fn vararg_module(_py: Python, m: &PyModule) -> PyResult<()> {
|
|
|
|
#[pyfn(m, "int_vararg_fn", a = 5, vararg = "*")]
|
|
|
|
fn int_vararg_fn(py: Python, a: i32, vararg: &PyTuple) -> PyObject {
|
|
|
|
ext_vararg_fn(py, a, vararg)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.add_wrapped(pyo3::wrap_pyfunction!(ext_vararg_fn))
|
|
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_vararg_module() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let m = pyo3::wrap_pymodule!(vararg_module)(py);
|
|
|
|
|
|
|
|
py_assert!(py, m, "m.ext_vararg_fn() == [5, ()]");
|
|
|
|
py_assert!(py, m, "m.ext_vararg_fn(1, 2) == [1, (2,)]");
|
|
|
|
|
|
|
|
py_assert!(py, m, "m.int_vararg_fn() == [5, ()]");
|
|
|
|
py_assert!(py, m, "m.int_vararg_fn(1, 2) == [1, (2,)]");
|
|
|
|
}
|