diff --git a/newsfragments/3131.changed.md b/newsfragments/3131.changed.md new file mode 100644 index 00000000..53768a5b --- /dev/null +++ b/newsfragments/3131.changed.md @@ -0,0 +1 @@ +Extend the lifetime of the GIL token returned by `PyRef::py` and `PyRefMut::py` to match the underlying borrow. diff --git a/src/pycell.rs b/src/pycell.rs index 0be39ac6..dbd5fff9 100644 --- a/src/pycell.rs +++ b/src/pycell.rs @@ -583,8 +583,8 @@ pub struct PyRef<'p, T: PyClass> { impl<'p, T: PyClass> PyRef<'p, T> { /// Returns a `Python` token that is bound to the lifetime of the `PyRef`. - pub fn py(&self) -> Python<'_> { - unsafe { Python::assume_gil_acquired() } + pub fn py(&self) -> Python<'p> { + self.inner.py() } } @@ -706,8 +706,8 @@ pub struct PyRefMut<'p, T: PyClass> { impl<'p, T: PyClass> PyRefMut<'p, T> { /// Returns a `Python` token that is bound to the lifetime of the `PyRefMut`. - pub fn py(&self) -> Python<'_> { - unsafe { Python::assume_gil_acquired() } + pub fn py(&self) -> Python<'p> { + self.inner.py() } }