Skip to content

Commit

Permalink
Add additional unit test for GC traversal
Browse files Browse the repository at this point in the history
Since we're just testing a bug during traversal, we don't actually have
to reap the object, we just have to make a reference cycle so that it's
traverse method is called.
  • Loading branch information
neachdainn committed Jan 27, 2023
1 parent f38841a commit 92c882d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/test_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,44 @@ fn gc_integration() {
});
}

#[pyclass]
struct GcNullTraversal {
cycle: Option<Py<Self>>,
null: Option<Py<Self>>,
}

#[pymethods]
impl GcNullTraversal {
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
visit.call(&self.cycle)?;
visit.call(&self.null)?; // Should not segfault
Ok(())
}

fn __clear__(&mut self) {
self.cycle = None;
self.null = None;
}
}

#[test]
fn gc_null_traversal() {
Python::with_gil(|py| {
let obj = Py::new(
py,
GcNullTraversal {
cycle: None,
null: None,
},
)
.unwrap();
obj.borrow_mut(py).null = Some(obj.clone_ref(py));

// the object doesn't have to be cleaned up, it just needs to be traversed.
py.run("import gc; gc.collect()", None, None).unwrap();
});
}

#[pyclass(subclass)]
struct BaseClassWithDrop {
data: Option<Arc<AtomicBool>>,
Expand Down

0 comments on commit 92c882d

Please sign in to comment.