From 7dddb8b0c09e416d567b70e2819ed92d50eda24d Mon Sep 17 00:00:00 2001 From: Nicholas Sim Date: Tue, 26 Jan 2021 17:52:26 +0800 Subject: [PATCH] Remove `unsafe` for some safe functions --- CHANGELOG.md | 1 + src/pycell.rs | 4 ++-- src/pyclass_init.rs | 4 +--- src/pyclass_slots.rs | 6 +++--- src/type_object.rs | 2 +- src/types/datetime.rs | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0e1204..3c9b76a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Mark FFI definitions `PyMarshal_WriteObjectToString`, `PyMarshal_ReadObjectFromString` as available in limited API. - Mark FFI definitions `PyListObject` and those from `funcobject.h` as requiring non-limited API. [#1387](https://github.com/PyO3/pyo3/pull/1387) - Fix typo in FFI definition `PyFunction_Code` to `PyFunction_GetCode`. [#1387](https://github.com/PyO3/pyo3/pull/1387) +- Mark `PyLayout::py_init`, `PyClassDict::clear_dict`, and `opt_to_pyobj` safe, as they do not perform any unsafe operations. [#1404](https://github.com/PyO3/pyo3/pull/1404) ### Fixed diff --git a/src/pycell.rs b/src/pycell.rs index 31c998ae..9979b2b7 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -66,7 +66,7 @@ unsafe impl PyLayout for PyCellInner { fn get_super(&mut self) -> Option<&mut T::BaseLayout> { Some(&mut self.ob_base) } - unsafe fn py_init(&mut self, value: T) { + fn py_init(&mut self, value: T) { self.value = ManuallyDrop::new(UnsafeCell::new(value)); } unsafe fn py_drop(&mut self, py: Python) { @@ -396,7 +396,7 @@ unsafe impl PyLayout for PyCell { fn get_super(&mut self) -> Option<&mut T::BaseLayout> { Some(&mut self.inner.ob_base) } - unsafe fn py_init(&mut self, value: T) { + fn py_init(&mut self, value: T) { self.inner.value = ManuallyDrop::new(UnsafeCell::new(value)); } unsafe fn py_drop(&mut self, py: Python) { diff --git a/src/pyclass_init.rs b/src/pyclass_init.rs index 2ae441af..a9c0cbec 100644 --- a/src/pyclass_init.rs +++ b/src/pyclass_init.rs @@ -149,9 +149,7 @@ impl PyClassInitializer { impl PyObjectInit for PyClassInitializer { fn init_class>(self, layout: &mut L) { let Self { init, super_init } = self; - unsafe { - layout.py_init(init); - } + layout.py_init(init); if let Some(super_obj) = layout.get_super() { super_init.init_class(super_obj); } diff --git a/src/pyclass_slots.rs b/src/pyclass_slots.rs index a5ba8093..5b182102 100644 --- a/src/pyclass_slots.rs +++ b/src/pyclass_slots.rs @@ -6,7 +6,7 @@ use crate::{ffi, Python}; pub trait PyClassDict { const IS_DUMMY: bool = true; fn new() -> Self; - unsafe fn clear_dict(&mut self, _py: Python) {} + fn clear_dict(&mut self, _py: Python) {} private_decl! {} } @@ -47,9 +47,9 @@ impl PyClassDict for PyClassDictSlot { fn new() -> Self { Self(std::ptr::null_mut()) } - unsafe fn clear_dict(&mut self, _py: Python) { + fn clear_dict(&mut self, _py: Python) { if !self.0.is_null() { - ffi::PyDict_Clear(self.0) + unsafe { ffi::PyDict_Clear(self.0) } } } } diff --git a/src/type_object.rs b/src/type_object.rs index 17d67973..f249628d 100644 --- a/src/type_object.rs +++ b/src/type_object.rs @@ -20,7 +20,7 @@ pub unsafe trait PyLayout { fn get_super(&mut self) -> Option<&mut T::BaseLayout> { None } - unsafe fn py_init(&mut self, _value: T) {} + fn py_init(&mut self, _value: T) {} unsafe fn py_drop(&mut self, _py: Python) {} } diff --git a/src/types/datetime.rs b/src/types/datetime.rs index 83c4191c..aef3dc36 100644 --- a/src/types/datetime.rs +++ b/src/types/datetime.rs @@ -403,7 +403,7 @@ impl PyDeltaAccess for PyDelta { } // Utility function -unsafe fn opt_to_pyobj(py: Python, opt: Option<&PyObject>) -> *mut ffi::PyObject { +fn opt_to_pyobj(py: Python, opt: Option<&PyObject>) -> *mut ffi::PyObject { // Convenience function for unpacking Options to either an Object or None match opt { Some(tzi) => tzi.as_ptr(),