Skip to content

Commit 8936c8a

Browse files
committed
src/impl_: make ModuleState::drop_impl unsafe, document usage
1 parent bf73b5a commit 8936c8a

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/impl_/pymodule_state.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl ModuleState {
7373
///
7474
/// Calling this function multiple times on a single ModuleState is a noop,
7575
/// beyond the first
76-
fn drop_impl(&mut self) {
76+
unsafe fn drop_impl(&mut self) {
7777
if let Some(ptr) = self.inner.take().map(|state| state.as_ptr()) {
7878
// SAFETY: This ptr is allocated via Box::new in Self::new, and is
7979
// non null
@@ -135,6 +135,8 @@ impl ModuleState {
135135
pub(crate) unsafe fn pymodule_free_state(module: *mut ffi::PyObject) {
136136
unsafe {
137137
if let Some(state) = Self::pymodule_get_state(module) {
138+
// SAFETY: this callback is called when python is freeing the
139+
// associated PyModule, so we should never be accessed again
138140
(*state.as_ptr()).drop_impl()
139141
}
140142
}
@@ -143,7 +145,8 @@ impl ModuleState {
143145

144146
impl Drop for ModuleState {
145147
fn drop(&mut self) {
146-
self.drop_impl();
148+
// SAFETY: we're being dropped, so we'll never be accessed again
149+
unsafe { self.drop_impl() };
147150
}
148151
}
149152

0 commit comments

Comments
 (0)