Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17742dc
commit 1cd1dbf
Showing
5 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::ffi; | ||
use crate::type_object::PyTypeInfo; | ||
use crate::types::PyType; | ||
use crate::{PyAny, PyResult}; | ||
|
||
/// Represents a Python `super` object. | ||
/// | ||
/// This type is immutable. | ||
#[repr(transparent)] | ||
pub struct PySuper(PyAny); | ||
|
||
pyobject_native_type_core!(PySuper, ffi::PySuper_Type); | ||
|
||
impl PySuper { | ||
/// Constructs a new super object. More read about super object: [docs](https://docs.python.org/3/library/functions.html#super) | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// 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) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> { | ||
let py = ty.py(); | ||
let super_ = PySuper::type_object(py).call1((ty, obj))?; | ||
let super_ = super_.downcast::<PySuper>()?; | ||
Ok(super_) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
"# | ||
) | ||
}); | ||
} |