Skip to content

Commit

Permalink
Merge pull request #1404 from nw0/mark-safe
Browse files Browse the repository at this point in the history
Remove `unsafe` for some safe functions
  • Loading branch information
davidhewitt authored Jan 26, 2021
2 parents 26368ee + 7dddb8b commit 3a62758
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- Fix support for using `r#raw_idents` as argument names in pyfunctions. [#1383](https://github.com/PyO3/pyo3/pull/1383)
Expand Down
4 changes: 2 additions & 2 deletions src/pycell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ unsafe impl<T: PyClass> PyLayout<T> for PyCellInner<T> {
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) {
Expand Down Expand Up @@ -396,7 +396,7 @@ unsafe impl<T: PyClass> PyLayout<T> for PyCell<T> {
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) {
Expand Down
4 changes: 1 addition & 3 deletions src/pyclass_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ impl<T: PyClass> PyClassInitializer<T> {
impl<T: PyClass> PyObjectInit<T> for PyClassInitializer<T> {
fn init_class<L: PyLayout<T>>(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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/pyclass_slots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {}
}

Expand Down Expand Up @@ -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) }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub unsafe trait PyLayout<T: PyTypeInfo> {
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) {}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 3a62758

Please sign in to comment.