Skip to content

Commit

Permalink
Merge pull request #3765 from davidhewitt/remove-py-newref
Browse files Browse the repository at this point in the history
remove internal uses of `_Py_NewRef`
  • Loading branch information
davidhewitt authored Jan 27, 2024
2 parents f09ad1e + 87e0610 commit 5f320d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
36 changes: 18 additions & 18 deletions src/impl_/pyclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,11 @@ macro_rules! define_pyclass_binary_operator_slot {
#[inline]
unsafe fn $lhs(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand All @@ -525,11 +525,11 @@ macro_rules! define_pyclass_binary_operator_slot {
#[inline]
unsafe fn $rhs(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand Down Expand Up @@ -700,12 +700,12 @@ slot_fragment_trait! {
#[inline]
unsafe fn __pow__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand All @@ -716,12 +716,12 @@ slot_fragment_trait! {
#[inline]
unsafe fn __rpow__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand Down Expand Up @@ -761,11 +761,11 @@ slot_fragment_trait! {
#[inline]
unsafe fn __lt__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand All @@ -776,11 +776,11 @@ slot_fragment_trait! {
#[inline]
unsafe fn __le__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand All @@ -791,11 +791,11 @@ slot_fragment_trait! {
#[inline]
unsafe fn __eq__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand Down Expand Up @@ -824,11 +824,11 @@ slot_fragment_trait! {
#[inline]
unsafe fn __gt__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand All @@ -839,11 +839,11 @@ slot_fragment_trait! {
#[inline]
unsafe fn __ge__(
self,
_py: Python<'_>,
py: Python<'_>,
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(py.NotImplemented().into_ptr())
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,9 @@ impl PyAny {
#[inline]
pub fn into_ptr(&self) -> *mut ffi::PyObject {
// Safety: self.as_ptr() returns a valid non-null pointer
unsafe { ffi::_Py_NewRef(self.as_ptr()) }
let ptr = self.as_ptr();
unsafe { ffi::Py_INCREF(ptr) };
ptr
}

/// Return a proxy object that delegates method calls to a parent or sibling class of type.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ impl TestBufferErrors {
return Err(PyBufferError::new_err("Object is not writable"));
}

(*view).obj = ffi::_Py_NewRef(slf.as_ptr());

let bytes = &slf.buf;

(*view).buf = bytes.as_ptr() as *mut c_void;
Expand Down Expand Up @@ -80,6 +78,8 @@ impl TestBufferErrors {
}
}

(*view).obj = slf.into_ptr();

Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_buffer_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ unsafe fn fill_view_from_readonly_data(
return Err(PyBufferError::new_err("Object is not writable"));
}

(*view).obj = ffi::_Py_NewRef(owner.as_ptr());
(*view).obj = owner.into_ptr();

(*view).buf = data.as_ptr() as *mut c_void;
(*view).len = data.len() as isize;
Expand Down

0 comments on commit 5f320d7

Please sign in to comment.