Skip to content

Commit

Permalink
ffi: use _Py_NewRef for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Sep 26, 2021
1 parent d8f6028 commit f68a341
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 28 deletions.
12 changes: 4 additions & 8 deletions src/class/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ macro_rules! define_pyclass_binary_operator_slot {
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented());
Ok(ffi::Py_NotImplemented())
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
}
}

Expand All @@ -273,8 +272,7 @@ macro_rules! define_pyclass_binary_operator_slot {
_slf: *mut ffi::PyObject,
_other: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented());
Ok(ffi::Py_NotImplemented())
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
}
}

Expand Down Expand Up @@ -429,8 +427,7 @@ slot_fragment_trait! {
_other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented());
Ok(ffi::Py_NotImplemented())
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
}
}

Expand All @@ -446,8 +443,7 @@ slot_fragment_trait! {
_other: *mut ffi::PyObject,
_mod: *mut ffi::PyObject,
) -> PyResult<*mut ffi::PyObject> {
ffi::Py_INCREF(ffi::Py_NotImplemented());
Ok(ffi::Py_NotImplemented())
Ok(ffi::_Py_NewRef(ffi::Py_NotImplemented()))
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ where
T: AsPyPointer,
{
fn into_ptr(self) -> *mut ffi::PyObject {
let ptr = self.as_ptr();
if !ptr.is_null() {
unsafe {
ffi::Py_INCREF(ptr);
}
}
ptr
unsafe { ffi::_Py_XNewRef(self.as_ptr()) }
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ impl PyDict {
let ptr = ffi::PyDict_GetItem(self.as_ptr(), key);
NonNull::new(ptr).map(|p| {
// 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(p.as_ptr())
self.py().from_owned_ptr(ffi::_Py_NewRef(p.as_ptr()))
})
})
}
Expand Down Expand Up @@ -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 {
let py = self.dict.py();
// PyDict_Next returns borrowed values; for safety must make them owned (see #890)
ffi::Py_INCREF(key);
ffi::Py_INCREF(value);
Some((py.from_owned_ptr(key), py.from_owned_ptr(value)))
Some((
py.from_owned_ptr(ffi::_Py_NewRef(key)),
py.from_owned_ptr(ffi::_Py_NewRef(value)),
))
} else {
None
}
Expand Down
3 changes: 1 addition & 2 deletions src/types/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ impl PyModule {
unsafe {
// PyModule_GetDict returns borrowed ptr; must make owned for safety (see #890).
let ptr = ffi::PyModule_GetDict(self.as_ptr());
ffi::Py_INCREF(ptr);
self.py().from_owned_ptr(ptr)
self.py().from_owned_ptr(ffi::_Py_NewRef(ptr))
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ impl<'py> Iterator for PySetIterator<'py> {
let mut hash: ffi::Py_hash_t = 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)
ffi::Py_INCREF(key);
Some(self.set.py().from_owned_ptr(key))
Some(self.set.py().from_owned_ptr(ffi::_Py_NewRef(key)))
} else {
None
}
Expand Down
3 changes: 1 addition & 2 deletions tests/test_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl PyBufferProtocol for TestBufferErrors {
}

unsafe {
(*view).obj = slf.as_ptr();
ffi::Py_INCREF((*view).obj);
(*view).obj = ffi::_Py_NewRef(slf.as_ptr());
}

let bytes = &slf.buf;
Expand Down
3 changes: 1 addition & 2 deletions tests/test_buffer_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ impl PyBufferProtocol for TestBufferClass {
}

unsafe {
(*view).obj = slf.as_ptr();
ffi::Py_INCREF((*view).obj);
(*view).obj = ffi::_Py_NewRef(slf.as_ptr());
}

let bytes = &slf.vec;
Expand Down

0 comments on commit f68a341

Please sign in to comment.