2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
2019-03-20 18:37:27 +00:00
|
|
|
use pyo3::types::{IntoPyDict, PyDict, PyString, PyTuple, PyType};
|
2018-09-21 21:32:48 +00:00
|
|
|
use pyo3::PyRawObject;
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct InstanceMethod {
|
|
|
|
member: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl InstanceMethod {
|
|
|
|
/// Test method
|
|
|
|
fn method(&self) -> PyResult<i32> {
|
|
|
|
Ok(self.member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn instance_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-02-13 20:35:26 +00:00
|
|
|
let obj = PyRefMut::new(py, InstanceMethod { member: 42 }).unwrap();
|
2018-08-20 20:53:43 +00:00
|
|
|
assert_eq!(obj.method().unwrap(), 42);
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("obj", obj)].into_py_dict(py);
|
2018-05-02 18:49:40 +00:00
|
|
|
py.run("assert obj.method() == 42", None, Some(d)).unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run("assert obj.method.__doc__ == 'Test method'", None, Some(d))
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct InstanceMethodWithArgs {
|
|
|
|
member: i32,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl InstanceMethodWithArgs {
|
|
|
|
fn method(&self, multiplier: i32) -> PyResult<i32> {
|
|
|
|
Ok(self.member * multiplier)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#[test]
|
2018-05-05 12:26:59 +00:00
|
|
|
#[allow(dead_code)]
|
2018-05-02 18:49:40 +00:00
|
|
|
fn instance_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-02-13 20:35:26 +00:00
|
|
|
let obj = PyRefMut::new(py, InstanceMethodWithArgs { member: 7 }).unwrap();
|
2018-11-12 12:59:19 +00:00
|
|
|
assert_eq!(obj.method(6).unwrap(), 42);
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("obj", obj)].into_py_dict(py);
|
2018-05-02 18:49:40 +00:00
|
|
|
py.run("assert obj.method(3) == 21", None, Some(d)).unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run("assert obj.method(multiplier=6) == 42", None, Some(d))
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-09-02 21:33:45 +00:00
|
|
|
struct ClassMethod {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl ClassMethod {
|
|
|
|
#[new]
|
2019-02-23 17:38:00 +00:00
|
|
|
fn new(obj: &PyRawObject) {
|
|
|
|
obj.init(ClassMethod {})
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[classmethod]
|
|
|
|
fn method(cls: &PyType) -> PyResult<String> {
|
|
|
|
Ok(format!("{}.method()!", cls.name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("C", py.get_type::<ClassMethod>())].into_py_dict(py);
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"assert C.method() == 'ClassMethod.method()!'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"assert C().method() == 'ClassMethod.method()!'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-11-12 12:59:19 +00:00
|
|
|
struct ClassMethodWithArgs {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl ClassMethodWithArgs {
|
|
|
|
#[classmethod]
|
|
|
|
fn method(cls: &PyType, input: &PyString) -> PyResult<String> {
|
|
|
|
Ok(format!("{}.method({})", cls.name(), input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("C", py.get_type::<ClassMethodWithArgs>())].into_py_dict(py);
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"assert C.method('abc') == 'ClassMethodWithArgs.method(abc)'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-11-12 12:59:19 +00:00
|
|
|
struct StaticMethod {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl StaticMethod {
|
|
|
|
#[new]
|
2019-02-23 17:38:00 +00:00
|
|
|
fn new(obj: &PyRawObject) {
|
|
|
|
obj.init(StaticMethod {})
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[staticmethod]
|
|
|
|
fn method(_py: Python) -> PyResult<&'static str> {
|
|
|
|
Ok("StaticMethod.method()!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_method() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
assert_eq!(StaticMethod::method(py).unwrap(), "StaticMethod.method()!");
|
2019-03-20 18:37:27 +00:00
|
|
|
|
|
|
|
let d = [("C", py.get_type::<StaticMethod>())].into_py_dict(py);
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"assert C.method() == 'StaticMethod.method()!'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"assert C().method() == 'StaticMethod.method()!'",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-11-12 12:59:19 +00:00
|
|
|
struct StaticMethodWithArgs {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl StaticMethodWithArgs {
|
|
|
|
#[staticmethod]
|
|
|
|
fn method(_py: Python, input: i32) -> PyResult<String> {
|
|
|
|
Ok(format!("0x{:x}", input))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn static_method_with_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
assert_eq!(StaticMethodWithArgs::method(py, 1234).unwrap(), "0x4d2");
|
|
|
|
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("C", py.get_type::<StaticMethodWithArgs>())].into_py_dict(py);
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run("assert C.method(1337) == '0x539'", None, Some(d))
|
|
|
|
.unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-11-12 12:59:19 +00:00
|
|
|
struct MethArgs {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl MethArgs {
|
2018-05-14 16:47:13 +00:00
|
|
|
#[args(test)]
|
|
|
|
fn get_optional(&self, test: Option<i32>) -> PyResult<i32> {
|
|
|
|
Ok(test.unwrap_or(10))
|
|
|
|
}
|
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
#[args(test = "10")]
|
2018-05-02 18:49:40 +00:00
|
|
|
fn get_default(&self, test: i32) -> PyResult<i32> {
|
|
|
|
Ok(test)
|
|
|
|
}
|
2018-06-15 19:21:12 +00:00
|
|
|
#[args("*", test = 10)]
|
2018-05-02 18:49:40 +00:00
|
|
|
fn get_kwarg(&self, test: i32) -> PyResult<i32> {
|
|
|
|
Ok(test)
|
|
|
|
}
|
2018-06-15 19:21:12 +00:00
|
|
|
#[args(args = "*", kwargs = "**")]
|
2018-11-12 12:59:19 +00:00
|
|
|
fn get_kwargs(
|
|
|
|
&self,
|
|
|
|
py: Python,
|
|
|
|
args: &PyTuple,
|
|
|
|
kwargs: Option<&PyDict>,
|
|
|
|
) -> PyResult<PyObject> {
|
|
|
|
Ok([args.into(), kwargs.to_object(py)].to_object(py))
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn meth_args() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2019-02-13 20:35:26 +00:00
|
|
|
let inst = Py::new(py, MethArgs {}).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-05-14 16:47:13 +00:00
|
|
|
py_run!(py, inst, "assert inst.get_optional() == 10");
|
|
|
|
py_run!(py, inst, "assert inst.get_optional(100) == 100");
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, inst, "assert inst.get_default() == 10");
|
|
|
|
py_run!(py, inst, "assert inst.get_default(100) == 100");
|
|
|
|
py_run!(py, inst, "assert inst.get_kwarg() == 10");
|
|
|
|
py_run!(py, inst, "assert inst.get_kwarg(100) == 10");
|
|
|
|
py_run!(py, inst, "assert inst.get_kwarg(test=100) == 100");
|
|
|
|
py_run!(py, inst, "assert inst.get_kwargs() == [(), None]");
|
|
|
|
py_run!(py, inst, "assert inst.get_kwargs(1,2,3) == [(1,2,3), None]");
|
2018-06-15 19:21:12 +00:00
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
inst,
|
|
|
|
"assert inst.get_kwargs(t=1,n=2) == [(), {'t': 1, 'n': 2}]"
|
|
|
|
);
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
inst,
|
|
|
|
"assert inst.get_kwargs(1,2,3,t=1,n=2) == [(1,2,3), {'t': 1, 'n': 2}]"
|
|
|
|
);
|
2018-05-14 16:47:13 +00:00
|
|
|
}
|