forked from PyO3/pyo3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_super.rs
51 lines (43 loc) · 884 Bytes
/
test_super.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![cfg(all(feature = "macros", not(PyPy)))]
use pyo3::prelude::*;
#[pyclass(subclass)]
struct BaseClass {
val1: usize,
}
#[pymethods]
impl BaseClass {
#[new]
fn new() -> Self {
BaseClass { val1: 10 }
}
pub fn method(&self) -> usize {
self.val1
}
}
#[pyclass(extends=BaseClass)]
struct SubClass {}
#[pymethods]
impl SubClass {
#[new]
fn new() -> (Self, BaseClass) {
(SubClass {}, BaseClass::new())
}
fn method(self_: &PyCell<Self>) -> PyResult<&PyAny> {
let super_ = self_.py_super()?;
super_.call_method("method", (), None)
}
}
#[test]
fn test_call_super_method() {
Python::with_gil(|py| {
let cls = py.get_type::<SubClass>();
pyo3::py_run!(
py,
cls,
r#"
obj = cls()
assert obj.method() == 10
"#
)
});
}