# HG changeset patch # User Yuya Nishihara # Date 1569053101 -32400 # Node ID 0df8312463ae0558bab306a074c8cfcb68ac5099 # Parent f8c114f20d2d4850a4ee8d6c01af555251cb449e rust-cpython: keep Python<'a> token in PyRefMut This just clarifies that the GIL is obtained while PyRefMut is dereferenced, so there's no need of extra acquire_gil() to drop the reference. diff -r f8c114f20d2d -r 0df8312463ae rust/hg-cpython/src/ref_sharing.rs --- a/rust/hg-cpython/src/ref_sharing.rs Sat Sep 21 17:15:50 2019 +0900 +++ b/rust/hg-cpython/src/ref_sharing.rs Sat Sep 21 17:05:01 2019 +0900 @@ -205,6 +205,7 @@ /// Holds a mutable reference to data shared between Python and Rust. pub struct PyRefMut<'a, T> { + py: Python<'a>, inner: RefMut<'a, T>, py_shared_state: &'a PySharedState, } @@ -213,11 +214,12 @@ // Must be constructed by PySharedState after checking its leak_count. // Otherwise, drop() would incorrectly update the state. fn new( - _py: Python<'a>, + py: Python<'a>, inner: RefMut<'a, T>, py_shared_state: &'a PySharedState, ) -> Self { Self { + py, inner, py_shared_state, } @@ -239,10 +241,8 @@ impl<'a, T> Drop for PyRefMut<'a, T> { fn drop(&mut self) { - let gil = Python::acquire_gil(); - let py = gil.python(); unsafe { - self.py_shared_state.decrease_leak_count(py, true); + self.py_shared_state.decrease_leak_count(self.py, true); } } }