Skip to content

Commit

Permalink
Add test case for dropping unsendable class elsewhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed May 22, 2023
1 parent a3c4fd2 commit 7afd658
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/test_class_basics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,3 +526,42 @@ fn access_frozen_class_without_gil() {

assert_eq!(py_counter.get().value.load(Ordering::Relaxed), 1);
}

#[test]
fn drop_unsendable_elsewhere() {
use std::cell::RefCell;
use std::rc::Rc;
use std::thread::{current, spawn, ThreadId};

#[pyclass(unsendable)]
struct Unsendable {
thread_id: Rc<RefCell<ThreadId>>,
}

impl Drop for Unsendable {
fn drop(&mut self) {
let created = *self.thread_id.borrow();
let dropped = current().id();
assert_eq!(
created, dropped,
"I was created on {created:?}, but dropped on {dropped:?}."
);
}
}

let unsendable = Python::with_gil(|py| {
Py::new(
py,
Unsendable {
thread_id: Rc::new(RefCell::new(current().id())),
},
)
.unwrap()
});

spawn(move || {
Python::with_gil(move |_py| {
drop(unsendable);
});
});
}

0 comments on commit 7afd658

Please sign in to comment.