diff --git a/src/impl_/pyclass.rs b/src/impl_/pyclass.rs index 23cebb26..fa03b81e 100644 --- a/src/impl_/pyclass.rs +++ b/src/impl_/pyclass.rs @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } @@ -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()) } } diff --git a/src/types/any.rs b/src/types/any.rs index 9cdf78d6..4536c2b8 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -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. diff --git a/tests/test_buffer.rs b/tests/test_buffer.rs index 12d1756f..de8638ba 100644 --- a/tests/test_buffer.rs +++ b/tests/test_buffer.rs @@ -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; @@ -80,6 +78,8 @@ impl TestBufferErrors { } } + (*view).obj = slf.into_ptr(); + Ok(()) } } diff --git a/tests/test_buffer_protocol.rs b/tests/test_buffer_protocol.rs index 4d5dc69f..b9f3861c 100644 --- a/tests/test_buffer_protocol.rs +++ b/tests/test_buffer_protocol.rs @@ -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;