pyo3/tests/test_getter_setter.rs

192 lines
4.1 KiB
Rust
Raw Normal View History

2021-12-03 00:03:32 +00:00
#![cfg(feature = "macros")]
use pyo3::prelude::*;
2019-06-13 09:09:17 +00:00
use pyo3::py_run;
use pyo3::types::{IntoPyDict, PyList};
mod common;
#[pyclass]
struct ClassWithProperties {
num: i32,
}
#[pymethods]
impl ClassWithProperties {
2021-02-11 21:37:38 +00:00
fn get_num(&self) -> i32 {
self.num
}
#[getter(DATA)]
/// a getter for data
2021-02-11 21:37:38 +00:00
fn get_data(&self) -> i32 {
self.num
}
#[setter(DATA)]
2021-02-11 21:37:38 +00:00
fn set_data(&mut self, value: i32) {
self.num = value;
}
#[getter]
/// a getter with a type un-wrapped by PyResult
fn get_unwrapped(&self) -> i32 {
self.num
}
#[setter]
fn set_unwrapped(&mut self, value: i32) {
self.num = value;
}
#[getter]
fn get_data_list<'py>(&self, py: Python<'py>) -> &'py PyList {
PyList::new(py, &[self.num])
}
}
#[test]
fn class_with_properties() {
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let inst = Py::new(py, ClassWithProperties { num: 10 }).unwrap();
2022-07-19 17:34:23 +00:00
py_run!(py, inst, "assert inst.get_num() == 10");
py_run!(py, inst, "assert inst.get_num() == inst.DATA");
py_run!(py, inst, "inst.DATA = 20");
py_run!(py, inst, "assert inst.get_num() == 20 == inst.DATA");
2022-07-19 17:34:23 +00:00
py_expect_exception!(py, inst, "del inst.DATA", PyAttributeError);
2022-07-19 17:34:23 +00:00
py_run!(py, inst, "assert inst.get_num() == inst.unwrapped == 20");
py_run!(py, inst, "inst.unwrapped = 42");
py_run!(py, inst, "assert inst.get_num() == inst.unwrapped == 42");
py_run!(py, inst, "assert inst.data_list == [42]");
2022-07-19 17:34:23 +00:00
let d = [("C", py.get_type::<ClassWithProperties>())].into_py_dict(py);
py_assert!(py, *d, "C.DATA.__doc__ == 'a getter for data'");
});
}
#[pyclass]
struct GetterSetter {
#[pyo3(get, set)]
num: i32,
#[pyo3(get, set)]
text: String,
}
#[pymethods]
impl GetterSetter {
2021-02-11 21:37:38 +00:00
fn get_num2(&self) -> i32 {
self.num
}
}
#[test]
fn getter_setter_autogen() {
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let inst = Py::new(
py,
GetterSetter {
num: 10,
text: "Hello".to_string(),
},
)
.unwrap();
py_run!(py, inst, "assert inst.num == 10");
py_run!(py, inst, "inst.num = 20; assert inst.num == 20");
py_run!(
py,
inst,
"assert inst.text == 'Hello'; inst.text = 'There'; assert inst.text == 'There'"
);
});
}
#[pyclass]
struct RefGetterSetter {
num: i32,
}
#[pymethods]
impl RefGetterSetter {
#[getter]
2022-03-23 07:07:28 +00:00
fn get_num(slf: PyRef<'_, Self>) -> i32 {
slf.num
}
#[setter]
2022-03-23 07:07:28 +00:00
fn set_num(mut slf: PyRefMut<'_, Self>, value: i32) {
slf.num = value;
}
}
#[test]
fn ref_getter_setter() {
// Regression test for #837
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let inst = Py::new(py, RefGetterSetter { num: 10 }).unwrap();
2022-07-19 17:34:23 +00:00
py_run!(py, inst, "assert inst.num == 10");
py_run!(py, inst, "inst.num = 20; assert inst.num == 20");
});
}
#[pyclass]
struct TupleClassGetterSetter(i32);
#[pymethods]
impl TupleClassGetterSetter {
#[getter(num)]
fn get_num(&self) -> i32 {
self.0
}
#[setter(num)]
fn set_num(&mut self, value: i32) {
self.0 = value;
}
}
#[test]
fn tuple_struct_getter_setter() {
2022-07-19 17:34:23 +00:00
Python::with_gil(|py| {
let inst = Py::new(py, TupleClassGetterSetter(10)).unwrap();
2022-07-19 17:34:23 +00:00
py_assert!(py, inst, "inst.num == 10");
py_run!(py, inst, "inst.num = 20");
py_assert!(py, inst, "inst.num == 20");
});
}
2022-10-17 00:37:43 +00:00
#[pyclass(get_all, set_all)]
struct All {
num: i32,
}
#[test]
fn get_set_all() {
Python::with_gil(|py| {
let inst = Py::new(py, All { num: 10 }).unwrap();
py_run!(py, inst, "assert inst.num == 10");
py_run!(py, inst, "inst.num = 20; assert inst.num == 20");
});
}
#[pyclass(get_all)]
struct All2 {
#[pyo3(set)]
num: i32,
}
#[test]
fn get_all_and_set() {
Python::with_gil(|py| {
let inst = Py::new(py, All2 { num: 10 }).unwrap();
py_run!(py, inst, "assert inst.num == 10");
py_run!(py, inst, "inst.num = 20; assert inst.num == 20");
});
}