Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove get_refcnt from public api #4065

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions newsfragments/4065.removed.md
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 12 additions & 2 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ impl<'py> Bound<'py, PyAny> {
) -> &'a Option<Self> {
&*(ptr as *const *mut ffi::PyObject).cast::<Option<Bound<'py, PyAny>>>()
}

/// 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>
Expand Down Expand Up @@ -1288,8 +1296,10 @@ impl<T> Py<T> {
}

/// 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()) }
}

Expand Down
12 changes: 4 additions & 8 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)`.
Expand Down Expand Up @@ -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<Bound<'py, PyString>> {
unsafe {
ffi::PyObject_Repr(self.as_ptr())
Expand Down
8 changes: 4 additions & 4 deletions tests/test_inheritance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment on lines +245 to +254
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reasoning for changing these? If I understand the issue, we want to keep get_refcnt for internal tests like this.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in tests are integration tests, they are compiled as a separate crate, and thus don't have access to private functions internal to the crate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! That makes sense!

})
}

Expand Down
Loading