2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
2019-03-20 18:37:27 +00:00
|
|
|
use pyo3::types::IntoPyDict;
|
2018-05-02 18:49:40 +00:00
|
|
|
use std::isize;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct BaseClass {
|
2019-02-18 19:07:41 +00:00
|
|
|
#[pyo3(get)]
|
2018-05-02 18:49:40 +00:00
|
|
|
val1: usize,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass(subclass)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct SubclassAble {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subclass() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2019-03-20 18:37:27 +00:00
|
|
|
let d = [("SubclassAble", py.get_type::<SubclassAble>())].into_py_dict(py);
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2018-06-15 19:21:12 +00:00
|
|
|
py.run(
|
|
|
|
"class A(SubclassAble): pass\nassert issubclass(A, SubclassAble)",
|
|
|
|
None,
|
|
|
|
Some(d),
|
2018-09-28 21:34:57 +00:00
|
|
|
)
|
|
|
|
.map_err(|e| e.print(py))
|
2018-07-30 21:01:46 +00:00
|
|
|
.unwrap();
|
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 BaseClass {
|
|
|
|
#[new]
|
2019-02-23 17:38:00 +00:00
|
|
|
fn new(obj: &PyRawObject) {
|
|
|
|
obj.init(BaseClass { val1: 10 })
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 09:58:33 +00:00
|
|
|
#[pyclass(extends=BaseClass)]
|
2018-05-02 18:49:40 +00:00
|
|
|
struct SubClass {
|
2019-02-18 19:07:41 +00:00
|
|
|
#[pyo3(get)]
|
2018-05-02 18:49:40 +00:00
|
|
|
val2: usize,
|
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pymethods]
|
2018-05-02 18:49:40 +00:00
|
|
|
impl SubClass {
|
|
|
|
#[new]
|
2019-02-23 17:38:00 +00:00
|
|
|
fn new(obj: &PyRawObject) {
|
2019-02-13 20:35:26 +00:00
|
|
|
obj.init(SubClass { val2: 5 });
|
2019-02-23 17:38:00 +00:00
|
|
|
BaseClass::new(obj);
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn inheritance_with_new_methods() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let _typebase = py.get_type::<BaseClass>();
|
|
|
|
let typeobj = py.get_type::<SubClass>();
|
2019-02-23 17:01:22 +00:00
|
|
|
let inst = typeobj.call((), None).unwrap();
|
2018-05-02 18:49:40 +00:00
|
|
|
py_run!(py, inst, "assert inst.val1 == 10; assert inst.val2 == 5");
|
2018-06-15 19:21:12 +00:00
|
|
|
}
|