diff --git a/newsfragments/2921.fixed.md b/newsfragments/2921.fixed.md new file mode 100644 index 00000000..8cbffabb --- /dev/null +++ b/newsfragments/2921.fixed.md @@ -0,0 +1 @@ +Traversal visit calls to `Option` no longer segfaults when `None`. diff --git a/src/pyclass/gc.rs b/src/pyclass/gc.rs index 8203af07..900027f7 100644 --- a/src/pyclass/gc.rs +++ b/src/pyclass/gc.rs @@ -23,11 +23,16 @@ impl<'p> PyVisit<'p> { where T: AsPyPointer, { - let r = unsafe { (self.visit)(obj.as_ptr(), self.arg) }; - if r == 0 { - Ok(()) + let ptr = obj.as_ptr(); + if !ptr.is_null() { + let r = unsafe { (self.visit)(ptr, self.arg) }; + if r == 0 { + Ok(()) + } else { + Err(PyTraverseError(r)) + } } else { - Err(PyTraverseError(r)) + Ok(()) } }