Add a test that confirms __getattr__ doesn't override

This commit is contained in:
kngwyu 2019-07-13 23:46:25 +09:00
parent 7a83cb6afa
commit 60cbe2f47d

22
tests/test_dunder.rs Normal file → Executable file
View file

@ -462,3 +462,25 @@ fn weakref_dunder_dict_support() {
"import weakref; assert weakref.ref(inst)() is inst; inst.a = 1; assert inst.a == 1"
);
}
#[pyclass]
struct ClassWithGetAttr {
#[pyo3(get, set)]
data: u32,
}
#[pyproto]
impl PyObjectProtocol for ClassWithGetAttr {
fn __getattr__(&self, _name: &str) -> PyResult<u32> {
Ok(self.data * 2)
}
}
#[test]
fn getattr_doesnt_override_member() {
let gil = Python::acquire_gil();
let py = gil.python();
let inst = PyRef::new(py, ClassWithGetAttr { data: 4 }).unwrap();
py_assert!(py, inst, "inst.data == 4");
py_assert!(py, inst, "inst.a == 8");
}