@@ -32,7 +32,7 @@ const ATTACH_FORBIDDEN_DURING_TRAVERSE: isize = -1;
3232/// 2) PyGILState_Check always returns 1 if the sub-interpreter APIs have ever been called,
3333/// which could lead to incorrect conclusions that the thread is attached.
3434#[ inline( always) ]
35- fn thread_is_attached ( ) -> bool {
35+ pub ( crate ) fn thread_is_attached ( ) -> bool {
3636 ATTACH_COUNT . try_with ( |c| c. get ( ) > 0 ) . unwrap_or ( false )
3737}
3838
@@ -298,44 +298,28 @@ impl Drop for ForbidAttaching {
298298 }
299299}
300300
301- /// Increments the reference count of a Python object if the thread is attached. If
302- /// the thread is not attached, this function will panic.
303- ///
304- /// # Safety
305- /// The object must be an owned Python reference.
306- #[ cfg( feature = "py-clone" ) ]
307- #[ track_caller]
308- pub unsafe fn register_incref ( obj : NonNull < ffi:: PyObject > ) {
309- if thread_is_attached ( ) {
310- unsafe { ffi:: Py_INCREF ( obj. as_ptr ( ) ) }
311- } else {
312- panic ! ( "Cannot clone pointer into Python heap without the thread being attached." ) ;
313- }
314- }
315-
316301/// Registers a Python object pointer inside the release pool, to have its reference count decreased
317302/// the next time the thread is attached in pyo3.
318303///
319304/// If the thread is attached, the reference count will be decreased immediately instead of being queued
320305/// for later.
321306///
322307/// # Safety
323- /// The object must be an owned Python reference.
324- #[ track_caller]
308+ /// - The object must be an owned Python reference.
309+ /// - The reference must not be used after calling this function.
310+ #[ inline]
325311pub unsafe fn register_decref ( obj : NonNull < ffi:: PyObject > ) {
326- if thread_is_attached ( ) {
327- unsafe { ffi:: Py_DECREF ( obj. as_ptr ( ) ) }
328- } else {
329- #[ cfg( not( pyo3_disable_reference_pool) ) ]
312+ #[ cfg( not( pyo3_disable_reference_pool) ) ]
313+ {
330314 get_pool ( ) . register_decref ( obj) ;
331- # [ cfg ( all (
332- pyo3_disable_reference_pool ,
333- not ( pyo3_leak_on_drop_without_reference_pool )
334- ) ) ]
335- {
336- let _trap = PanicTrap :: new ( "Aborting the process to avoid panic-from-drop." ) ;
337- panic ! ( "Cannot drop pointer into Python heap without the thread being attached .") ;
338- }
315+ }
316+ # [ cfg ( all (
317+ pyo3_disable_reference_pool ,
318+ not ( pyo3_leak_on_drop_without_reference_pool )
319+ ) ) ]
320+ {
321+ let _trap = PanicTrap :: new ( "Aborting the process to avoid panic-from-drop .") ;
322+ panic ! ( "Cannot drop pointer into Python heap without the thread being attached." ) ;
339323 }
340324}
341325
0 commit comments