Remove `unsafe` for some safe functions

This commit is contained in:
Nicholas Sim 2021-01-26 17:52:26 +08:00
parent fbe7de1efb
commit 7dddb8b0c0
6 changed files with 9 additions and 10 deletions

View File

@ -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 `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) - 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) - 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 ### Fixed

View File

@ -66,7 +66,7 @@ unsafe impl<T: PyClass> PyLayout<T> for PyCellInner<T> {
fn get_super(&mut self) -> Option<&mut T::BaseLayout> { fn get_super(&mut self) -> Option<&mut T::BaseLayout> {
Some(&mut self.ob_base) 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)); self.value = ManuallyDrop::new(UnsafeCell::new(value));
} }
unsafe fn py_drop(&mut self, py: Python) { unsafe fn py_drop(&mut self, py: Python) {
@ -396,7 +396,7 @@ unsafe impl<T: PyClass> PyLayout<T> for PyCell<T> {
fn get_super(&mut self) -> Option<&mut T::BaseLayout> { fn get_super(&mut self) -> Option<&mut T::BaseLayout> {
Some(&mut self.inner.ob_base) 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)); self.inner.value = ManuallyDrop::new(UnsafeCell::new(value));
} }
unsafe fn py_drop(&mut self, py: Python) { unsafe fn py_drop(&mut self, py: Python) {

View File

@ -149,9 +149,7 @@ impl<T: PyClass> PyClassInitializer<T> {
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> { impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> {
fn init_class<L: PyLayout<T>>(self, layout: &mut L) { fn init_class<L: PyLayout<T>>(self, layout: &mut L) {
let Self { init, super_init } = self; let Self { init, super_init } = self;
unsafe {
layout.py_init(init); layout.py_init(init);
}
if let Some(super_obj) = layout.get_super() { if let Some(super_obj) = layout.get_super() {
super_init.init_class(super_obj); super_init.init_class(super_obj);
} }

View File

@ -6,7 +6,7 @@ use crate::{ffi, Python};
pub trait PyClassDict { pub trait PyClassDict {
const IS_DUMMY: bool = true; const IS_DUMMY: bool = true;
fn new() -> Self; fn new() -> Self;
unsafe fn clear_dict(&mut self, _py: Python) {} fn clear_dict(&mut self, _py: Python) {}
private_decl! {} private_decl! {}
} }
@ -47,9 +47,9 @@ impl PyClassDict for PyClassDictSlot {
fn new() -> Self { fn new() -> Self {
Self(std::ptr::null_mut()) 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() { if !self.0.is_null() {
ffi::PyDict_Clear(self.0) unsafe { ffi::PyDict_Clear(self.0) }
} }
} }
} }

View File

@ -20,7 +20,7 @@ pub unsafe trait PyLayout<T: PyTypeInfo> {
fn get_super(&mut self) -> Option<&mut T::BaseLayout> { fn get_super(&mut self) -> Option<&mut T::BaseLayout> {
None None
} }
unsafe fn py_init(&mut self, _value: T) {} fn py_init(&mut self, _value: T) {}
unsafe fn py_drop(&mut self, _py: Python) {} unsafe fn py_drop(&mut self, _py: Python) {}
} }

View File

@ -403,7 +403,7 @@ impl PyDeltaAccess for PyDelta {
} }
// Utility function // 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 // Convenience function for unpacking Options to either an Object or None
match opt { match opt {
Some(tzi) => tzi.as_ptr(), Some(tzi) => tzi.as_ptr(),