ffi: use _Py_NewRef for clarity

This commit is contained in:
David Hewitt 2021-09-26 13:45:47 +01:00
parent 7c4503e0ca
commit 03ba4a5597
7 changed files with 14 additions and 28 deletions

View file

@ -257,8 +257,7 @@ macro_rules! define_pyclass_binary_operator_slot {
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented()); Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(ffi::Py_NotImplemented())
} }
} }
@ -273,8 +272,7 @@ macro_rules! define_pyclass_binary_operator_slot {
_slf: *mut ffi::PyObject, _slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented()); Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(ffi::Py_NotImplemented())
} }
} }
@ -429,8 +427,7 @@ slot_fragment_trait! {
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject, _mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented()); Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(ffi::Py_NotImplemented())
} }
} }
@ -446,8 +443,7 @@ slot_fragment_trait! {
_other: *mut ffi::PyObject, _other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject, _mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> { ) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented()); Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
Ok(ffi::Py_NotImplemented())
} }
} }

View file

@ -64,13 +64,7 @@ where
T: AsPyPointer, T: AsPyPointer,
{ {
fn into_ptr(self) -> *mut ffi::PyObject { fn into_ptr(self) -> *mut ffi::PyObject {
let ptr = self.as_ptr(); unsafe { ffi::_Py_XNewRef(self.as_ptr()) }
if !ptr.is_null() {
unsafe {
ffi::Py_INCREF(ptr);
}
}
ptr
} }
} }

View file

@ -105,8 +105,7 @@ impl PyDict {
let ptr = ffi::PyDict_GetItem(self.as_ptr(), key); let ptr = ffi::PyDict_GetItem(self.as_ptr(), key);
NonNull::new(ptr).map(|p| { NonNull::new(ptr).map(|p| {
// PyDict_GetItem return s borrowed ptr, must make it owned for safety (see #890). // PyDict_GetItem return s borrowed ptr, must make it owned for safety (see #890).
ffi::Py_INCREF(p.as_ptr()); self.py().from_owned_ptr(ffi::_Py_NewRef(p.as_ptr()))
self.py().from_owned_ptr(p.as_ptr())
}) })
}) })
} }
@ -196,9 +195,10 @@ impl<'py> Iterator for PyDictIterator<'py> {
if ffi::PyDict_Next(self.dict.as_ptr(), &mut self.pos, &mut key, &mut value) != 0 { if ffi::PyDict_Next(self.dict.as_ptr(), &mut self.pos, &mut key, &mut value) != 0 {
let py = self.dict.py(); let py = self.dict.py();
// PyDict_Next returns borrowed values; for safety must make them owned (see #890) // PyDict_Next returns borrowed values; for safety must make them owned (see #890)
ffi::Py_INCREF(key); Some((
ffi::Py_INCREF(value); py.from_owned_ptr(ffi::_Py_NewRef(key)),
Some((py.from_owned_ptr(key), py.from_owned_ptr(value))) py.from_owned_ptr(ffi::_Py_NewRef(value)),
))
} else { } else {
None None
} }

View file

@ -140,8 +140,7 @@ impl PyModule {
unsafe { unsafe {
// PyModule_GetDict returns borrowed ptr; must make owned for safety (see #890). // PyModule_GetDict returns borrowed ptr; must make owned for safety (see #890).
let ptr = ffi::PyModule_GetDict(self.as_ptr()); let ptr = ffi::PyModule_GetDict(self.as_ptr());
ffi::Py_INCREF(ptr); self.py().from_owned_ptr(ffi::_Py_NewRef(ptr))
self.py().from_owned_ptr(ptr)
} }
} }

View file

@ -164,8 +164,7 @@ impl<'py> Iterator for PySetIterator<'py> {
let mut hash: ffi::Py_hash_t = 0; let mut hash: ffi::Py_hash_t = 0;
if ffi::_PySet_NextEntry(self.set.as_ptr(), &mut self.pos, &mut key, &mut hash) != 0 { if ffi::_PySet_NextEntry(self.set.as_ptr(), &mut self.pos, &mut key, &mut hash) != 0 {
// _PySet_NextEntry returns borrowed object; for safety must make owned (see #890) // _PySet_NextEntry returns borrowed object; for safety must make owned (see #890)
ffi::Py_INCREF(key); Some(self.set.py().from_owned_ptr(ffi::_Py_NewRef(key)))
Some(self.set.py().from_owned_ptr(key))
} else { } else {
None None
} }

View file

@ -39,8 +39,7 @@ impl PyBufferProtocol for TestBufferErrors {
} }
unsafe { unsafe {
(*view).obj = slf.as_ptr(); (*view).obj = ffi::_Py_NewRef(slf.as_ptr());
ffi::Py_INCREF((*view).obj);
} }
let bytes = &slf.buf; let bytes = &slf.buf;

View file

@ -33,8 +33,7 @@ impl PyBufferProtocol for TestBufferClass {
} }
unsafe { unsafe {
(*view).obj = slf.as_ptr(); (*view).obj = ffi::_Py_NewRef(slf.as_ptr());
ffi::Py_INCREF((*view).obj);
} }
let bytes = &slf.vec; let bytes = &slf.vec;