Merge pull request #3765 from davidhewitt/remove-py-newref

remove internal uses of `_Py_NewRef`
This commit is contained in:
David Hewitt 2024-01-27 13:35:29 +00:00 committed by GitHub
commit 5f320d7a04
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 22 deletions

View File

@ -510,11 +510,11 @@ macro_rules! define_pyclass_binary_operator_slot {
#[inline] #[inline]
unsafe fn $lhs( unsafe fn $lhs(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -525,11 +525,11 @@ macro_rules! define_pyclass_binary_operator_slot {
#[inline] #[inline]
unsafe fn $rhs( unsafe fn $rhs(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -700,12 +700,12 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __pow__( unsafe fn __pow__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject, _mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -716,12 +716,12 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __rpow__( unsafe fn __rpow__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject, _mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -761,11 +761,11 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __lt__( unsafe fn __lt__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -776,11 +776,11 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __le__( unsafe fn __le__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -791,11 +791,11 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __eq__( unsafe fn __eq__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -824,11 +824,11 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __gt__( unsafe fn __gt__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }
@ -839,11 +839,11 @@ slot_fragment_trait! {
#[inline] #[inline]
unsafe fn __ge__( unsafe fn __ge__(
self, self,
_py: Python<'_>, py: Python<'_>,
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented())) Ok(py.NotImplemented().into_ptr())
} }
} }

View File

@ -965,7 +965,9 @@ impl PyAny {
#[inline] #[inline]
pub fn into_ptr(&self) -> *mut ffi::PyObject { pub fn into_ptr(&self) -> *mut ffi::PyObject {
// Safety: self.as_ptr() returns a valid non-null pointer // 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. /// Return a proxy object that delegates method calls to a parent or sibling class of type.

View File

@ -41,8 +41,6 @@ impl TestBufferErrors {
return Err(PyBufferError::new_err("Object is not writable")); return Err(PyBufferError::new_err("Object is not writable"));
} }
(*view).obj = ffi::_Py_NewRef(slf.as_ptr());
let bytes = &slf.buf; let bytes = &slf.buf;
(*view).buf = bytes.as_ptr() as *mut c_void; (*view).buf = bytes.as_ptr() as *mut c_void;
@ -80,6 +78,8 @@ impl TestBufferErrors {
} }
} }
(*view).obj = slf.into_ptr();
Ok(()) Ok(())
} }
} }

View File

@ -155,7 +155,7 @@ unsafe fn fill_view_from_readonly_data(
return Err(PyBufferError::new_err("Object is not writable")); 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).buf = data.as_ptr() as *mut c_void;
(*view).len = data.len() as isize; (*view).len = data.len() as isize;