Skip to content

Commit

Permalink
Merge pull request #3700 from davidhewitt/super-bound
Browse files Browse the repository at this point in the history
introduce `PySuper::new_bound`
  • Loading branch information
adamreichold authored Dec 25, 2023
2 parents ff373eb + d9cc0c0 commit f37c682
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2193,7 +2193,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {

#[cfg(not(PyPy))]
fn py_super(&self) -> PyResult<Bound<'py, PySuper>> {
PySuper::new2(Bound::borrowed_from_gil_ref(&self.get_type()), self)
PySuper::new_bound(Bound::borrowed_from_gil_ref(&self.get_type()), self)
}
}

Expand Down
23 changes: 14 additions & 9 deletions src/types/pysuper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ pyobject_native_type_core!(
);

impl PySuper {
/// Deprecated form of `PySuper::new_bound`.
#[deprecated(
since = "0.21.0",
note = "`PySuper::new` will be replaced by `PySuper::new_bound` in a future PyO3 version"
)]
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> {
Self::new_bound(
Bound::borrowed_from_gil_ref(&ty),
Bound::borrowed_from_gil_ref(&obj),
)
.map(Bound::into_gil_ref)
}

/// Constructs a new super object. More read about super object: [docs](https://docs.python.org/3/library/functions.html#super)
///
/// # Examples
Expand Down Expand Up @@ -56,15 +69,7 @@ impl PySuper {
/// }
/// }
/// ```
pub fn new<'py>(ty: &'py PyType, obj: &'py PyAny) -> PyResult<&'py PySuper> {
Self::new2(
Bound::borrowed_from_gil_ref(&ty),
Bound::borrowed_from_gil_ref(&obj),
)
.map(Bound::into_gil_ref)
}

pub(crate) fn new2<'py>(
pub fn new_bound<'py>(
ty: &Bound<'py, PyType>,
obj: &Bound<'py, PyAny>,
) -> PyResult<Bound<'py, PySuper>> {
Expand Down
1 change: 1 addition & 0 deletions tests/test_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl SubClass {
}

fn method_super_new(self_: &PyCell<Self>) -> PyResult<&PyAny> {
#[allow(deprecated)]
let super_ = PySuper::new(self_.get_type(), self_)?;
super_.call_method("method", (), None)
}
Expand Down

0 comments on commit f37c682

Please sign in to comment.