diff --git a/newsfragments/4065.removed.md b/newsfragments/4065.removed.md new file mode 100644 index 00000000000..e7bc05c99de --- /dev/null +++ b/newsfragments/4065.removed.md @@ -0,0 +1 @@ +Removed `get_refcnt`. Can be replaced with `pyo3::ffi::Py_REFCNT(obj.as_ptr())`. If you use this api, please let us know on [#3357](https://github.com/PyO3/pyo3/issues/3357) \ No newline at end of file diff --git a/src/gil.rs b/src/gil.rs index e2f36037755..4694a5baa1c 100644 --- a/src/gil.rs +++ b/src/gil.rs @@ -521,7 +521,6 @@ mod tests { #[allow(deprecated)] use super::GILPool; use super::{gil_is_acquired, GIL_COUNT, OWNED_OBJECTS, POOL}; - use crate::types::any::PyAnyMethods; use crate::{ffi, gil, PyObject, Python}; #[cfg(not(target_arch = "wasm32"))] use parking_lot::{const_mutex, Condvar, Mutex}; diff --git a/src/instance.rs b/src/instance.rs index 88a550ffd30..0bd6f94e816 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -202,6 +202,14 @@ impl<'py> Bound<'py, PyAny> { ) -> &'a Option { &*(ptr as *const *mut ffi::PyObject).cast::>>() } + + /// Returns the reference count for the Python object. + /// + /// Only used to validate behaviour in tests. + #[cfg(test)] + pub(crate) fn get_refcnt(&self) -> isize { + unsafe { ffi::Py_REFCNT(self.as_ptr()) } + } } impl<'py, T> Bound<'py, T> @@ -1288,8 +1296,10 @@ impl Py { } /// Gets the reference count of the `ffi::PyObject` pointer. - #[inline] - pub fn get_refcnt(&self, _py: Python<'_>) -> isize { + /// + /// Only used to validate behaviour in tests. + #[cfg(test)] + pub(crate) fn get_refcnt(&self, _py: Python<'_>) -> isize { unsafe { ffi::Py_REFCNT(self.0.as_ptr()) } } diff --git a/src/types/any.rs b/src/types/any.rs index ab4f5727623..2004d103e9d 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -810,7 +810,10 @@ impl PyAny { } /// Returns the reference count for the Python object. - pub fn get_refcnt(&self) -> isize { + /// + /// Only used to validate behaviour in tests. + #[cfg(test)] + pub(crate) fn get_refcnt(&self) -> isize { self.as_borrowed().get_refcnt() } @@ -1648,9 +1651,6 @@ pub trait PyAnyMethods<'py>: crate::sealed::Sealed { where T: FromPyObjectBound<'a, 'py>; - /// Returns the reference count for the Python object. - fn get_refcnt(&self) -> isize; - /// Computes the "repr" representation of self. /// /// This is equivalent to the Python expression `repr(self)`. @@ -2188,10 +2188,6 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { FromPyObjectBound::from_py_object_bound(self.as_borrowed()) } - fn get_refcnt(&self) -> isize { - unsafe { ffi::Py_REFCNT(self.as_ptr()) } - } - fn repr(&self) -> PyResult> { unsafe { ffi::PyObject_Repr(self.as_ptr()) diff --git a/tests/test_inheritance.rs b/tests/test_inheritance.rs index fc9a7699c1b..ac8cd3d0e35 100644 --- a/tests/test_inheritance.rs +++ b/tests/test_inheritance.rs @@ -242,16 +242,16 @@ mod inheriting_native_type { fn inherit_dict_drop() { Python::with_gil(|py| { let dict_sub = pyo3::Py::new(py, DictWithName::new()).unwrap(); - assert_eq!(dict_sub.get_refcnt(py), 1); + assert_eq!(unsafe { pyo3::ffi::Py_REFCNT(dict_sub.as_ptr()) }, 1); let item = &py.eval_bound("object()", None, None).unwrap(); - assert_eq!(item.get_refcnt(), 1); + assert_eq!(unsafe { pyo3::ffi::Py_REFCNT(item.as_ptr()) }, 1); dict_sub.bind(py).set_item("foo", item).unwrap(); - assert_eq!(item.get_refcnt(), 2); + assert_eq!(unsafe { pyo3::ffi::Py_REFCNT(item.as_ptr()) }, 2); drop(dict_sub); - assert_eq!(item.get_refcnt(), 1); + assert_eq!(unsafe { pyo3::ffi::Py_REFCNT(item.as_ptr()) }, 1); }) }