@@ -138,6 +138,8 @@ pub struct PyBitGeneratorGuard<'py> {
138138 lock : Bound < ' py , PyAny > ,
139139}
140140
141+ // SAFETY: we can’t have public APIs that access the Python objects,
142+ // only the `raw_bitgen` pointer.
141143unsafe impl Send for PyBitGeneratorGuard < ' _ > { }
142144
143145impl Drop for PyBitGeneratorGuard < ' _ > {
@@ -150,8 +152,10 @@ impl Drop for PyBitGeneratorGuard<'_> {
150152// SAFETY: We hold the `BitGenerator.lock`,
151153// so nothing apart from us is allowed to change its state.
152154impl PyBitGeneratorGuard < ' _ > {
153- /// Drop the lock, allowing access to.
154- pub fn try_drop ( self ) -> PyResult < ( ) > {
155+ /// Drop the lock manually before `Drop::drop` tries to do it (used for testing).
156+ /// SAFETY: Can’t be used inside of a
157+ #[ allow( dead_code) ]
158+ unsafe fn try_drop ( self ) -> PyResult < ( ) > {
155159 self . lock . call_method0 ( "release" ) ?;
156160 Ok ( ( ) )
157161 }
@@ -219,7 +223,7 @@ mod tests {
219223 py. allow_threads ( || {
220224 let _ = bitgen. next_raw ( ) ;
221225 } ) ;
222- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
226+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
223227 Ok ( ( ) )
224228 } )
225229 }
@@ -236,7 +240,7 @@ mod tests {
236240 assert ! ( bitgen. random_ratio( 1 , 1 ) ) ;
237241 assert ! ( !bitgen. random_ratio( 0 , 1 ) ) ;
238242 } ) ;
239- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
243+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
240244 Ok ( ( ) )
241245 } )
242246 }
@@ -247,7 +251,7 @@ mod tests {
247251 let generator = get_bit_generator ( py) ?;
248252 let bitgen = generator. lock ( ) ?;
249253 assert ! ( generator. lock( ) . is_err( ) ) ;
250- assert ! ( bitgen. try_drop( ) . is_ok( ) ) ;
254+ assert ! ( unsafe { bitgen. try_drop( ) } . is_ok( ) ) ;
251255 Ok ( ( ) )
252256 } )
253257 }
0 commit comments