Skip to content

Commit 771558e

Browse files
authored
add safety comments (#6139)
Signed-off-by: person93 <person93.person93@gmail.com>
1 parent 745b074 commit 771558e

6 files changed

Lines changed: 48 additions & 10 deletions

File tree

src/conversions/std/osstr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// TODO https://github.com/PyO3/pyo3/issues/5487
2-
#![allow(clippy::undocumented_unsafe_blocks)]
3-
41
use crate::conversion::IntoPyObject;
52
#[cfg(not(target_os = "wasi"))]
63
use crate::ffi;
@@ -50,6 +47,7 @@ impl<'py> IntoPyObject<'py> for &OsStr {
5047
let bytes = self.as_bytes();
5148
let ptr = bytes.as_ptr().cast();
5249
let len = bytes.len() as ffi::Py_ssize_t;
50+
// SAFETY: passing valid pointer to python API
5351
unsafe {
5452
// DecodeFSDefault automatically chooses an appropriate decoding mechanism to
5553
// parse os strings losslessly (i.e. surrogateescape most of the time)
@@ -62,6 +60,7 @@ impl<'py> IntoPyObject<'py> for &OsStr {
6260
#[cfg(windows)]
6361
{
6462
let wstr: Vec<u16> = self.encode_wide().collect();
63+
// SAFETY: passing valid pointer to python API
6564
unsafe {
6665
// This will not panic because the data from encode_wide is well-formed Windows
6766
// string data
@@ -130,6 +129,7 @@ impl FromPyObject<'_, '_> for OsString {
130129

131130
// Get an owned allocated wide char buffer from PyString, which we have to deallocate
132131
// ourselves
132+
// SAFETY: passing valid pointer to python API
133133
let size =
134134
unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), core::ptr::null_mut(), 0) };
135135
crate::err::error_on_minusone(ob.py(), size)?;
@@ -141,6 +141,7 @@ impl FromPyObject<'_, '_> for OsString {
141141
let size = size - 1; // exclude null terminator
142142

143143
let mut buffer = vec![0; size as usize];
144+
// SAFETY: passing valid pointer to python API
144145
let bytes_read =
145146
unsafe { ffi::PyUnicode_AsWideChar(pystring.as_ptr(), buffer.as_mut_ptr(), size) };
146147
assert_eq!(bytes_read, size);

src/exceptions.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// TODO https://github.com/PyO3/pyo3/issues/5487
2-
#![allow(clippy::undocumented_unsafe_blocks)]
3-
41
//! Exception and warning types defined by Python.
52
//!
63
//! The structs in this module represent Python's built-in exceptions and
@@ -280,7 +277,11 @@ macro_rules! impl_native_exception (
280277
pub struct $name($crate::PyAny);
281278

282279
$crate::impl_exception_boilerplate!($name);
283-
$crate::pyobject_native_type!($name, $layout, |_py| unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject }, "builtins", $python_name $(, #checkfunction=$checkfunction)?);
280+
$crate::pyobject_native_type!($name, $layout, |_py| {
281+
// SAFETY: cpython docs state that all exception types are available as global variales and are class objects
282+
// https://docs.python.org/3/c-api/exceptions.html#exception-and-warning-types
283+
unsafe { $crate::ffi::$exc_name as *mut $crate::ffi::PyTypeObject }
284+
}, "builtins", $python_name $(, #checkfunction=$checkfunction)?);
284285
$crate::pyobject_subclassable_native_type!($name, $layout);
285286
);
286287
($name:ident, $exc_name:ident, $python_name:expr, $doc:expr) => (
@@ -730,6 +731,7 @@ impl PyUnicodeDecodeError {
730731
) -> PyResult<Bound<'py, PyUnicodeDecodeError>> {
731732
use crate::ffi_ptr_ext::FfiPtrExt;
732733
use crate::py_result_ext::PyResultExt;
734+
// SAFETY: calling python API with correct pointers
733735
unsafe {
734736
ffi::PyUnicodeDecodeError_Create(
735737
encoding.as_ptr(),

src/ffi_ptr_ext.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// TODO https://github.com/PyO3/pyo3/issues/5487
2-
#![allow(clippy::undocumented_unsafe_blocks)]
3-
41
use crate::sealed::Sealed;
52
use crate::{
63
ffi,
@@ -41,48 +38,80 @@ pub(crate) trait FfiPtrExt: Sealed {
4138
}
4239

4340
impl FfiPtrExt for *mut ffi::PyObject {
41+
/// # Safety
42+
///
43+
/// see requirements for [`Bound::from_owned_ptr_or_err`]
4444
#[inline]
4545
unsafe fn assume_owned_or_err(self, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
46+
// SAFETY: caller upholds requirements
4647
unsafe { Bound::from_owned_ptr_or_err(py, self) }
4748
}
4849

50+
/// # Safety
51+
///
52+
/// see requirements for [`Bound::from_owned_ptr_or_opt`]
4953
#[inline]
5054
unsafe fn assume_owned_or_opt(self, py: Python<'_>) -> Option<Bound<'_, PyAny>> {
55+
// SAFETY: caller upholds requirements
5156
unsafe { Bound::from_owned_ptr_or_opt(py, self) }
5257
}
5358

59+
/// # Safety
60+
///
61+
/// see requirements for [`Bound::from_owned_ptr`]
5462
#[inline]
5563
#[track_caller]
5664
unsafe fn assume_owned(self, py: Python<'_>) -> Bound<'_, PyAny> {
65+
// SAFETY: caller upholds requirements
5766
unsafe { Bound::from_owned_ptr(py, self) }
5867
}
5968

69+
/// # Safety
70+
///
71+
/// see requirements for [`Bound::from_owned_ptr_unchecked`]
6072
#[inline]
6173
unsafe fn assume_owned_unchecked(self, py: Python<'_>) -> Bound<'_, PyAny> {
74+
// SAFETY: caller upholds requirements
6275
unsafe { Bound::from_owned_ptr_unchecked(py, self) }
6376
}
6477

78+
/// # Safety
79+
///
80+
/// see requirements for [`Borrowed::from_ptr_or_err`]
6581
#[inline]
6682
unsafe fn assume_borrowed_or_err<'a>(
6783
self,
6884
py: Python<'_>,
6985
) -> PyResult<Borrowed<'a, '_, PyAny>> {
86+
// SAFETY: caller upholds requirements
7087
unsafe { Borrowed::from_ptr_or_err(py, self) }
7188
}
7289

90+
/// # Safety
91+
///
92+
/// see requirements for [`Borrowed::from_ptr_or_opt`]
7393
#[inline]
7494
unsafe fn assume_borrowed_or_opt<'a>(self, py: Python<'_>) -> Option<Borrowed<'a, '_, PyAny>> {
95+
// SAFETY: caller upholds requirements
7596
unsafe { Borrowed::from_ptr_or_opt(py, self) }
7697
}
7798

99+
/// # Safety
100+
///
101+
/// see requirements for [`Borrowed::from_ptr`]
78102
#[inline]
79103
#[track_caller]
80104
unsafe fn assume_borrowed<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
105+
// SAFETY: caller upholds requirements
81106
unsafe { Borrowed::from_ptr(py, self) }
82107
}
83108

109+
/// # Safety
110+
///
111+
/// see requirements for [`Borrowed::from_ptr_unchecked`]
84112
#[inline]
85113
unsafe fn assume_borrowed_unchecked<'a>(self, py: Python<'_>) -> Borrowed<'a, '_, PyAny> {
114+
// SAFETY: caller upholds requirements
86115
unsafe { Borrowed::from_ptr_unchecked(py, self) }
87116
}
88117
}

src/sync.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ impl<T> Default for GILOnceCell<T> {
8888
}
8989
}
9090

91+
// SAFETY: Sync is only implemented if the inner type is Sync
9192
// T: Send is needed for Sync because the thread which drops the GILOnceCell can be different
9293
// to the thread which fills it. (e.g. think scoped thread which fills the cell and then exits,
9394
// leaving the cell to be dropped by the main thread).
9495
#[allow(deprecated)]
9596
unsafe impl<T: Send + Sync> Sync for GILOnceCell<T> {}
97+
// SAFETY: send is only implemented if the inner type is send
9698
#[allow(deprecated)]
9799
unsafe impl<T: Send> Send for GILOnceCell<T> {}
98100

src/sync/critical_section.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// TODO https://github.com/PyO3/pyo3/issues/5487
2+
#![allow(clippy::undocumented_unsafe_blocks)]
3+
14
//! Wrappers for the Python critical section API
25
//!
36
//! [Critical Sections](https://docs.python.org/3/c-api/init.html#python-critical-section-api) allow

src/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ macro_rules! pyobject_subclassable_native_type {
241241
#[macro_export]
242242
macro_rules! pyobject_native_type_sized {
243243
($name:ty, $layout:path $(;$generics:ident)*) => {
244+
// SAFETY: native objects are valid
244245
unsafe impl $crate::type_object::PyLayout<$name> for $layout {}
245246
impl $crate::type_object::PySizedLayout<$name> for $layout {}
246247
};

0 commit comments

Comments
 (0)